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 "drm_fourcc.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" },
114 { DRM_MODE_CONNECTOR_TV, "TV" },
115 { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
118 type_name_fn(connector_type)
120 void dump_encoders(void)
122 drmModeEncoder *encoder;
125 printf("Encoders:\n");
126 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
127 for (i = 0; i < resources->count_encoders; i++) {
128 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
131 fprintf(stderr, "could not get encoder %i: %s\n",
132 resources->encoders[i], strerror(errno));
135 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
138 encoder_type_str(encoder->encoder_type),
139 encoder->possible_crtcs,
140 encoder->possible_clones);
141 drmModeFreeEncoder(encoder);
146 void dump_mode(drmModeModeInfo *mode)
148 printf(" %s %d %d %d %d %d %d %d %d %d\n",
162 dump_props(drmModeConnector *connector)
164 drmModePropertyPtr props;
167 for (i = 0; i < connector->count_props; i++) {
168 props = drmModeGetProperty(fd, connector->props[i]);
169 printf("\t%s, flags %d\n", props->name, props->flags);
170 drmModeFreeProperty(props);
174 void dump_connectors(void)
176 drmModeConnector *connector;
179 printf("Connectors:\n");
180 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
181 for (i = 0; i < resources->count_connectors; i++) {
182 connector = drmModeGetConnector(fd, resources->connectors[i]);
185 fprintf(stderr, "could not get connector %i: %s\n",
186 resources->connectors[i], strerror(errno));
190 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
191 connector->connector_id,
192 connector->encoder_id,
193 connector_status_str(connector->connection),
194 connector_type_str(connector->connector_type),
195 connector->mmWidth, connector->mmHeight,
196 connector->count_modes);
198 for (j = 0; j < connector->count_encoders; j++)
199 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
202 if (!connector->count_modes)
206 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
208 for (j = 0; j < connector->count_modes; j++)
209 dump_mode(&connector->modes[j]);
212 dump_props(connector);
214 drmModeFreeConnector(connector);
219 void dump_crtcs(void)
225 printf("id\tfb\tpos\tsize\n");
226 for (i = 0; i < resources->count_crtcs; i++) {
227 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
230 fprintf(stderr, "could not get crtc %i: %s\n",
231 resources->crtcs[i], strerror(errno));
234 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
238 crtc->width, crtc->height);
239 dump_mode(&crtc->mode);
241 drmModeFreeCrtc(crtc);
246 void dump_framebuffers(void)
251 printf("Frame buffers:\n");
252 printf("id\tsize\tpitch\n");
253 for (i = 0; i < resources->count_fbs; i++) {
254 fb = drmModeGetFB(fd, resources->fbs[i]);
257 fprintf(stderr, "could not get fb %i: %s\n",
258 resources->fbs[i], strerror(errno));
261 printf("%u\t(%ux%u)\t%u\n",
263 fb->width, fb->height,
271 static void dump_planes(void)
273 drmModePlaneRes *plane_resources;
277 plane_resources = drmModeGetPlaneResources(fd);
278 if (!plane_resources) {
279 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
285 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
286 for (i = 0; i < plane_resources->count_planes; i++) {
287 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
289 fprintf(stderr, "drmModeGetPlane failed: %s\n",
294 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
295 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
296 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
299 if (!ovr->count_formats)
303 for (j = 0; j < ovr->count_formats; j++)
304 printf(" %4.4s", (char *)&ovr->formats[j]);
307 drmModeFreePlane(ovr);
315 * Mode setting with the kernel interfaces is a bit of a chore.
316 * First you have to find the connector in question and make sure the
317 * requested mode is available.
318 * Then you need to find the encoder attached to that connector so you
319 * can bind it with a free crtc.
324 drmModeModeInfo *mode;
325 drmModeEncoder *encoder;
328 unsigned int fb_id[2], current_fb_id;
329 struct timeval start;
335 uint32_t con_id; /* the id of connector to bind to */
341 connector_find_mode(struct connector *c)
343 drmModeConnector *connector;
346 /* First, find the connector & mode */
348 for (i = 0; i < resources->count_connectors; i++) {
349 connector = drmModeGetConnector(fd, resources->connectors[i]);
352 fprintf(stderr, "could not get connector %i: %s\n",
353 resources->connectors[i], strerror(errno));
354 drmModeFreeConnector(connector);
358 if (!connector->count_modes) {
359 drmModeFreeConnector(connector);
363 if (connector->connector_id != c->id) {
364 drmModeFreeConnector(connector);
368 for (j = 0; j < connector->count_modes; j++) {
369 c->mode = &connector->modes[j];
370 if (!strcmp(c->mode->name, c->mode_str))
374 /* Found it, break out */
378 drmModeFreeConnector(connector);
382 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
386 /* Now get the encoder */
387 for (i = 0; i < resources->count_encoders; i++) {
388 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
391 fprintf(stderr, "could not get encoder %i: %s\n",
392 resources->encoders[i], strerror(errno));
393 drmModeFreeEncoder(c->encoder);
397 if (c->encoder->encoder_id == connector->encoder_id)
400 drmModeFreeEncoder(c->encoder);
404 c->crtc = c->encoder->crtc_id;
406 /* and figure out which crtc index it is: */
407 for (i = 0; i < resources->count_crtcs; i++) {
408 if (c->crtc == resources->crtcs[i]) {
416 static struct kms_bo *
417 allocate_buffer(struct kms_driver *kms,
418 int width, int height, int *stride)
421 unsigned bo_attribs[] = {
424 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
425 KMS_TERMINATE_PROP_LIST
429 bo_attribs[1] = width;
430 bo_attribs[3] = height;
432 ret = kms_bo_create(kms, bo_attribs, &bo);
434 fprintf(stderr, "failed to alloc buffer: %s\n",
439 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
441 fprintf(stderr, "failed to retreive buffer stride: %s\n",
451 make_pwetty(void *data, int width, int height, int stride)
454 cairo_surface_t *surface;
458 surface = cairo_image_surface_create_for_data(data,
462 cr = cairo_create(surface);
463 cairo_surface_destroy(surface);
465 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
466 for (x = 0; x < width; x += 250)
467 for (y = 0; y < height; y += 250) {
470 cairo_move_to(cr, x, y - 20);
471 cairo_line_to(cr, x, y + 20);
472 cairo_move_to(cr, x - 20, y);
473 cairo_line_to(cr, x + 20, y);
474 cairo_new_sub_path(cr);
475 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
476 cairo_set_line_width(cr, 4);
477 cairo_set_source_rgb(cr, 0, 0, 0);
478 cairo_stroke_preserve(cr);
479 cairo_set_source_rgb(cr, 1, 1, 1);
480 cairo_set_line_width(cr, 2);
483 snprintf(buf, sizeof buf, "%d, %d", x, y);
484 cairo_move_to(cr, x + 20, y + 20);
485 cairo_text_path(cr, buf);
486 cairo_set_source_rgb(cr, 0, 0, 0);
487 cairo_stroke_preserve(cr);
488 cairo_set_source_rgb(cr, 1, 1, 1);
497 create_test_buffer(struct kms_driver *kms,
498 int width, int height, int *stride_out,
499 struct kms_bo **bo_out)
502 int ret, i, j, stride;
505 bo = allocate_buffer(kms, width, height, &stride);
509 ret = kms_bo_map(bo, &virtual);
511 fprintf(stderr, "failed to map buffer: %s\n",
517 /* paint the buffer with colored tiles */
518 for (j = 0; j < height; j++) {
519 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
520 for (i = 0; i < width; i++) {
521 div_t d = div(i, width);
523 0x00130502 * (d.quot >> 6) +
524 0x000a1120 * (d.rem >> 6);
528 make_pwetty(virtual, width, height, stride);
533 *stride_out = stride;
538 create_grey_buffer(struct kms_driver *kms,
539 int width, int height, int *stride_out,
540 struct kms_bo **bo_out)
543 int size, ret, stride;
546 bo = allocate_buffer(kms, width, height, &stride);
550 ret = kms_bo_map(bo, &virtual);
552 fprintf(stderr, "failed to map buffer: %s\n",
558 size = stride * height;
559 memset(virtual, 0x77, size);
563 *stride_out = stride;
569 page_flip_handler(int fd, unsigned int frame,
570 unsigned int sec, unsigned int usec, void *data)
573 unsigned int new_fb_id;
578 if (c->current_fb_id == c->fb_id[0])
579 new_fb_id = c->fb_id[1];
581 new_fb_id = c->fb_id[0];
583 drmModePageFlip(fd, c->crtc, new_fb_id,
584 DRM_MODE_PAGE_FLIP_EVENT, c);
585 c->current_fb_id = new_fb_id;
587 if (c->swap_count == 60) {
588 gettimeofday(&end, NULL);
589 t = end.tv_sec + end.tv_usec * 1e-6 -
590 (c->start.tv_sec + c->start.tv_usec * 1e-6);
591 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
598 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
600 drmModePlaneRes *plane_resources;
602 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
603 uint32_t plane_id = 0;
604 struct kms_bo *plane_bo;
605 uint32_t plane_flags = 0;
606 int i, crtc_x, crtc_y, crtc_w, crtc_h;
608 /* find an unused plane which can be connected to our crtc */
609 plane_resources = drmModeGetPlaneResources(fd);
610 if (!plane_resources) {
611 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
616 for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
617 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
619 fprintf(stderr, "drmModeGetPlane failed: %s\n",
624 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
625 plane_id = ovr->plane_id;
627 drmModeFreePlane(ovr);
631 fprintf(stderr, "failed to find plane!\n");
635 /* TODO.. would be nice to test YUV overlays.. */
636 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
639 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
641 /* just use single plane format for now.. */
642 if (drmModeAddFB2(fd, p->w, p->h, DRM_FORMAT_XRGB8888,
643 handles, pitches, offsets, &p->fb_id, plane_flags)) {
644 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
648 /* ok, boring.. but for now put in middle of screen: */
649 crtc_x = c->mode->hdisplay / 3;
650 crtc_y = c->mode->vdisplay / 3;
654 /* note src coords (last 4 args) are in Q16 format */
655 if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
656 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
657 0, 0, p->w << 16, p->h << 16)) {
658 fprintf(stderr, "failed to enable plane: %s\n",
667 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
670 struct kms_driver *kms;
671 struct kms_bo *bo, *other_bo;
672 unsigned int fb_id, other_fb_id;
673 int i, j, ret, width, height, x, stride;
675 drmEventContext evctx;
679 for (i = 0; i < count; i++) {
680 connector_find_mode(&c[i]);
681 if (c[i].mode == NULL)
683 width += c[i].mode->hdisplay;
684 if (height < c[i].mode->vdisplay)
685 height = c[i].mode->vdisplay;
688 ret = kms_create(fd, &kms);
690 fprintf(stderr, "failed to create kms driver: %s\n",
695 if (create_test_buffer(kms, width, height, &stride, &bo))
698 kms_bo_get_prop(bo, KMS_HANDLE, &handle);
699 ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
701 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
702 width, height, strerror(errno));
707 for (i = 0; i < count; i++) {
708 if (c[i].mode == NULL)
711 printf("setting mode %s on connector %d, crtc %d\n",
712 c[i].mode_str, c[i].id, c[i].crtc);
714 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
715 &c[i].id, 1, c[i].mode);
717 /* XXX: Actually check if this is needed */
718 drmModeDirtyFB(fd, fb_id, NULL, 0);
720 x += c[i].mode->hdisplay;
723 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
727 /* if we have a plane/overlay to show, set that up now: */
728 for (j = 0; j < plane_count; j++)
729 if (p[j].con_id == c[i].id)
730 if (set_plane(kms, &c[i], &p[j]))
737 if (create_grey_buffer(kms, width, height, &stride, &other_bo))
740 kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
741 ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
744 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
748 for (i = 0; i < count; i++) {
749 if (c[i].mode == NULL)
752 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
753 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
755 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
758 gettimeofday(&c[i].start, NULL);
760 c[i].fb_id[0] = fb_id;
761 c[i].fb_id[1] = other_fb_id;
762 c[i].current_fb_id = other_fb_id;
765 memset(&evctx, 0, sizeof evctx);
766 evctx.version = DRM_EVENT_CONTEXT_VERSION;
767 evctx.vblank_handler = NULL;
768 evctx.page_flip_handler = page_flip_handler;
772 struct pollfd pfd[2];
775 pfd[0].events = POLLIN;
777 pfd[1].events = POLLIN;
779 if (poll(pfd, 2, -1) < 0) {
780 fprintf(stderr, "poll error\n");
787 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
794 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
797 fprintf(stderr, "select timed out or error (ret %d)\n",
800 } else if (FD_ISSET(0, &fds)) {
805 drmHandleEvent(fd, &evctx);
809 kms_bo_destroy(&other_bo);
814 extern int optind, opterr, optopt;
815 static char optstr[] = "ecpmfs:P:v";
817 void usage(char *name)
819 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
820 fprintf(stderr, "\t-e\tlist encoders\n");
821 fprintf(stderr, "\t-c\tlist connectors\n");
822 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
823 fprintf(stderr, "\t-m\tlist modes\n");
824 fprintf(stderr, "\t-f\tlist framebuffers\n");
825 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
826 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
827 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
828 fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
829 fprintf(stderr, "\n\tDefault is to dump all info.\n");
833 #define dump_resource(res) if (res) dump_##res()
835 static int page_flipping_supported(int fd)
837 /*FIXME: generic ioctl needed? */
841 struct drm_i915_getparam gp;
843 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
846 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
848 fprintf(stderr, "drm_i915_getparam: %m\n");
856 int main(int argc, char **argv)
859 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
861 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm" };
862 char *modeset = NULL;
863 int i, count = 0, plane_count = 0;
864 struct connector con_args[2];
865 struct plane plane_args[2];
868 while ((c = getopt(argc, argv, optstr)) != -1) {
890 modeset = strdup(optarg);
891 con_args[count].crtc = -1;
892 if (sscanf(optarg, "%d:%64s",
894 con_args[count].mode_str) != 2 &&
895 sscanf(optarg, "%d@%d:%64s",
897 &con_args[count].crtc,
898 con_args[count].mode_str) != 3)
903 if (sscanf(optarg, "%d:%dx%d",
904 &plane_args[plane_count].con_id,
905 &plane_args[plane_count].w,
906 &plane_args[plane_count].h) != 3)
917 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
919 for (i = 0; i < ARRAY_SIZE(modules); i++) {
920 printf("trying to load module %s...", modules[i]);
921 fd = drmOpen(modules[i], NULL);
925 printf("success.\n");
930 if (test_vsync && !page_flipping_supported(fd)) {
931 fprintf(stderr, "page flipping not supported by drm.\n");
935 if (i == ARRAY_SIZE(modules)) {
936 fprintf(stderr, "failed to load any modules, aborting.\n");
940 resources = drmModeGetResources(fd);
942 fprintf(stderr, "drmModeGetResources failed: %s\n",
948 dump_resource(encoders);
949 dump_resource(connectors);
950 dump_resource(crtcs);
951 dump_resource(planes);
952 dump_resource(framebuffers);
955 set_mode(con_args, count, plane_args, plane_count, test_vsync);
959 drmModeFreeResources(resources);