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.
52 #include "xf86drmMode.h"
53 #include "intel_bufmgr.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" },
115 type_name_fn(connector_type)
117 void dump_encoders(void)
119 drmModeEncoder *encoder;
122 printf("Encoders:\n");
123 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
124 for (i = 0; i < resources->count_encoders; i++) {
125 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
128 fprintf(stderr, "could not get encoder %i: %s\n",
129 resources->encoders[i], strerror(errno));
132 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
135 encoder_type_str(encoder->encoder_type),
136 encoder->possible_crtcs,
137 encoder->possible_clones);
138 drmModeFreeEncoder(encoder);
143 void dump_mode(drmModeModeInfo *mode)
145 printf(" %s %.02f %d %d %d %d %d %d %d %d\n",
147 (float)mode->vrefresh / 1000,
159 dump_props(drmModeConnector *connector)
161 drmModePropertyPtr props;
164 for (i = 0; i < connector->count_props; i++) {
165 props = drmModeGetProperty(fd, connector->props[i]);
166 printf("\t%s, flags %d\n", props->name, props->flags);
167 drmModeFreeProperty(props);
171 void dump_connectors(void)
173 drmModeConnector *connector;
176 printf("Connectors:\n");
177 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
178 for (i = 0; i < resources->count_connectors; i++) {
179 connector = drmModeGetConnector(fd, resources->connectors[i]);
182 fprintf(stderr, "could not get connector %i: %s\n",
183 resources->connectors[i], strerror(errno));
187 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
188 connector->connector_id,
189 connector->encoder_id,
190 connector_status_str(connector->connection),
191 connector_type_str(connector->connector_type),
192 connector->mmWidth, connector->mmHeight,
193 connector->count_modes);
195 for (j = 0; j < connector->count_encoders; j++)
196 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
199 if (!connector->count_modes)
203 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
205 for (j = 0; j < connector->count_modes; j++)
206 dump_mode(&connector->modes[j]);
208 drmModeFreeConnector(connector);
211 dump_props(connector);
216 void dump_crtcs(void)
222 printf("id\tfb\tpos\tsize\n");
223 for (i = 0; i < resources->count_crtcs; i++) {
224 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
227 fprintf(stderr, "could not get crtc %i: %s\n",
228 resources->crtcs[i], strerror(errno));
231 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
235 crtc->width, crtc->height);
236 dump_mode(&crtc->mode);
238 drmModeFreeCrtc(crtc);
243 void dump_framebuffers(void)
248 printf("Frame buffers:\n");
249 printf("id\tsize\tpitch\n");
250 for (i = 0; i < resources->count_fbs; i++) {
251 fb = drmModeGetFB(fd, resources->fbs[i]);
254 fprintf(stderr, "could not get fb %i: %s\n",
255 resources->fbs[i], strerror(errno));
258 printf("%d\t(%dx%d)\t%d\n",
260 fb->width, fb->height);
268 * Mode setting with the kernel interfaces is a bit of a chore.
269 * First you have to find the connector in question and make sure the
270 * requested mode is available.
271 * Then you need to find the encoder attached to that connector so you
272 * can bind it with a free crtc.
277 drmModeModeInfo *mode;
278 drmModeEncoder *encoder;
280 unsigned int fb_id[2], current_fb_id;
281 struct timeval start;
287 connector_find_mode(struct connector *c)
289 drmModeConnector *connector;
290 int i, j, size, ret, width, height;
292 /* First, find the connector & mode */
294 for (i = 0; i < resources->count_connectors; i++) {
295 connector = drmModeGetConnector(fd, resources->connectors[i]);
298 fprintf(stderr, "could not get connector %i: %s\n",
299 resources->connectors[i], strerror(errno));
300 drmModeFreeConnector(connector);
304 if (!connector->count_modes) {
305 drmModeFreeConnector(connector);
309 if (connector->connector_id != c->id) {
310 drmModeFreeConnector(connector);
314 for (j = 0; j < connector->count_modes; j++) {
315 c->mode = &connector->modes[j];
316 if (!strcmp(c->mode->name, c->mode_str))
320 /* Found it, break out */
324 drmModeFreeConnector(connector);
328 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
332 /* Now get the encoder */
333 for (i = 0; i < resources->count_encoders; i++) {
334 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
337 fprintf(stderr, "could not get encoder %i: %s\n",
338 resources->encoders[i], strerror(errno));
339 drmModeFreeEncoder(c->encoder);
343 if (c->encoder->encoder_id == connector->encoder_id)
346 drmModeFreeEncoder(c->encoder);
350 c->crtc = c->encoder->crtc_id;
356 create_test_buffer(drm_intel_bufmgr *bufmgr,
357 int width, int height, int *stride_out, drm_intel_bo **bo_out)
360 unsigned int *fb_ptr;
361 int size, ret, i, stride;
363 cairo_surface_t *surface;
368 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
369 stride = cairo_image_surface_get_stride(surface);
370 size = stride * height;
371 fb_ptr = (unsigned int *) cairo_image_surface_get_data(surface);
373 /* paint the buffer with colored tiles */
374 for (i = 0; i < width * height; i++) {
376 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
379 cr = cairo_create(surface);
380 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
381 for (x = 0; x < width; x += 250)
382 for (y = 0; y < height; y += 250) {
383 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
384 cairo_move_to(cr, x, y - 20);
385 cairo_line_to(cr, x, y + 20);
386 cairo_move_to(cr, x - 20, y);
387 cairo_line_to(cr, x + 20, y);
388 cairo_new_sub_path(cr);
389 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
390 cairo_set_line_width(cr, 4);
391 cairo_set_source_rgb(cr, 0, 0, 0);
392 cairo_stroke_preserve(cr);
393 cairo_set_source_rgb(cr, 1, 1, 1);
394 cairo_set_line_width(cr, 2);
396 snprintf(buf, sizeof buf, "%d, %d", x, y);
397 cairo_move_to(cr, x + 20, y + 20);
398 cairo_text_path(cr, buf);
399 cairo_set_source_rgb(cr, 0, 0, 0);
400 cairo_stroke_preserve(cr);
401 cairo_set_source_rgb(cr, 1, 1, 1);
407 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
409 fprintf(stderr, "failed to alloc buffer: %s\n",
414 drm_intel_bo_subdata(bo, 0, size, fb_ptr);
416 cairo_surface_destroy(surface);
419 *stride_out = stride;
427 create_test_buffer(drm_intel_bufmgr *bufmgr,
428 int width, int height, int *stride_out, drm_intel_bo **bo_out)
431 unsigned int *fb_ptr;
432 int size, ret, i, stride;
435 /* Mode size at 32 bpp */
437 size = stride * height;
439 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
441 fprintf(stderr, "failed to alloc buffer: %s\n",
446 ret = drm_intel_gem_bo_map_gtt(bo);
448 fprintf(stderr, "failed to GTT map buffer: %s\n",
453 fb_ptr = bo->virtual;
455 /* paint the buffer with colored tiles */
456 for (i = 0; i < width * height; i++) {
458 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
460 drm_intel_gem_bo_unmap_gtt(bo);
463 *stride_out = stride;
471 create_grey_buffer(drm_intel_bufmgr *bufmgr,
472 int width, int height, int *stride_out, drm_intel_bo **bo_out)
475 unsigned int *fb_ptr;
476 int size, ret, i, 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;
517 if (c->current_fb_id == c->fb_id[0])
518 new_fb_id = c->fb_id[1];
520 new_fb_id = c->fb_id[0];
522 drmModePageFlip(fd, c->crtc, new_fb_id,
523 DRM_MODE_PAGE_FLIP_EVENT, c);
524 c->current_fb_id = new_fb_id;
526 if (c->swap_count == 60) {
527 gettimeofday(&end, NULL);
528 t = end.tv_sec + end.tv_usec * 1e-6 -
529 (c->start.tv_sec + c->start.tv_usec * 1e-6);
530 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
537 set_mode(struct connector *c, int count, int page_flip)
539 drmModeConnector *connector;
540 drmModeEncoder *encoder = NULL;
541 struct drm_mode_modeinfo *mode = NULL;
542 drm_intel_bufmgr *bufmgr;
543 drm_intel_bo *bo, *other_bo;
544 unsigned int fb_id, other_fb_id;
545 int i, j, ret, width, height, x, stride;
546 drmEventContext evctx;
550 for (i = 0; i < count; i++) {
551 connector_find_mode(&c[i]);
552 if (c[i].mode == NULL)
554 width += c[i].mode->hdisplay;
555 if (height < c[i].mode->vdisplay)
556 height = c[i].mode->vdisplay;
559 bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
561 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
565 if (create_test_buffer(bufmgr, width, height, &stride, &bo))
568 ret = drmModeAddFB(fd, width, height, 32, 32, stride, bo->handle,
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(bufmgr, width, height, &stride, &other_bo))
599 ret = drmModeAddFB(fd, width, height, 32, 32, stride, other_bo->handle,
602 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
606 for (i = 0; i < count; i++) {
607 if (c[i].mode == NULL)
610 drmModePageFlip(fd, c[i].crtc, other_fb_id,
611 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
612 gettimeofday(&c[i].start, NULL);
614 c[i].fb_id[0] = fb_id;
615 c[i].fb_id[1] = other_fb_id;
616 c[i].current_fb_id = fb_id;
619 memset(&evctx, 0, sizeof evctx);
620 evctx.version = DRM_EVENT_CONTEXT_VERSION;
621 evctx.vblank_handler = NULL;
622 evctx.page_flip_handler = page_flip_handler;
625 struct pollfd pfd[2];
628 pfd[0].events = POLLIN;
630 pfd[1].events = POLLIN;
632 if (poll(pfd, 2, -1) < 0) {
633 fprintf(stderr, "poll error\n");
640 drmHandleEvent(fd, &evctx);
645 extern int optind, opterr, optopt;
646 static char optstr[] = "ecpmfs:v";
648 void usage(char *name)
650 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
651 fprintf(stderr, "\t-e\tlist encoders\n");
652 fprintf(stderr, "\t-c\tlist connectors\n");
653 fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
654 fprintf(stderr, "\t-m\tlist modes\n");
655 fprintf(stderr, "\t-f\tlist framebuffers\n");
656 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
657 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
658 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
659 fprintf(stderr, "\n\tDefault is to dump all info.\n");
663 #define dump_resource(res) if (res) dump_##res()
665 static int page_flipping_supported(int fd)
668 struct drm_i915_getparam gp;
670 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
673 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
675 fprintf(stderr, "drm_i915_getparam: %m\n");
682 int main(int argc, char **argv)
685 int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
687 char *modules[] = { "i915", "radeon" };
688 char *modeset = NULL, *mode, *connector;
689 int i, connector_id, count = 0;
690 struct connector con_args[2];
693 while ((c = getopt(argc, argv, optstr)) != -1) {
714 modeset = strdup(optarg);
715 con_args[count].crtc = -1;
716 if (sscanf(optarg, "%d:%64s",
718 &con_args[count].mode_str) != 2 &&
719 sscanf(optarg, "%d@%d:%64s",
721 &con_args[count].crtc,
722 &con_args[count].mode_str) != 3)
733 encoders = connectors = crtcs = modes = framebuffers = 1;
735 for (i = 0; i < ARRAY_SIZE(modules); i++) {
736 printf("trying to load module %s...", modules[i]);
737 fd = drmOpen(modules[i], NULL);
741 printf("success.\n");
746 if (test_vsync && !page_flipping_supported(fd)) {
747 fprintf(stderr, "page flipping not supported by drm.\n");
751 if (i == ARRAY_SIZE(modules)) {
752 fprintf(stderr, "failed to load any modules, aborting.\n");
756 resources = drmModeGetResources(fd);
758 fprintf(stderr, "drmModeGetResources failed: %s\n",
764 dump_resource(encoders);
765 dump_resource(connectors);
766 dump_resource(crtcs);
767 dump_resource(framebuffers);
770 set_mode(con_args, count, test_vsync);
774 drmModeFreeResources(resources);