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 */
338 char format_str[5]; /* need to leave room for terminating \0 */
342 connector_find_mode(struct connector *c)
344 drmModeConnector *connector;
347 /* First, find the connector & mode */
349 for (i = 0; i < resources->count_connectors; i++) {
350 connector = drmModeGetConnector(fd, resources->connectors[i]);
353 fprintf(stderr, "could not get connector %i: %s\n",
354 resources->connectors[i], strerror(errno));
355 drmModeFreeConnector(connector);
359 if (!connector->count_modes) {
360 drmModeFreeConnector(connector);
364 if (connector->connector_id != c->id) {
365 drmModeFreeConnector(connector);
369 for (j = 0; j < connector->count_modes; j++) {
370 c->mode = &connector->modes[j];
371 if (!strcmp(c->mode->name, c->mode_str))
375 /* Found it, break out */
379 drmModeFreeConnector(connector);
383 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
387 /* Now get the encoder */
388 for (i = 0; i < resources->count_encoders; i++) {
389 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
392 fprintf(stderr, "could not get encoder %i: %s\n",
393 resources->encoders[i], strerror(errno));
394 drmModeFreeEncoder(c->encoder);
398 if (c->encoder->encoder_id == connector->encoder_id)
401 drmModeFreeEncoder(c->encoder);
405 c->crtc = c->encoder->crtc_id;
407 /* and figure out which crtc index it is: */
408 for (i = 0; i < resources->count_crtcs; i++) {
409 if (c->crtc == resources->crtcs[i]) {
417 static struct kms_bo *
418 allocate_buffer(struct kms_driver *kms,
419 int width, int height, int *stride)
422 unsigned bo_attribs[] = {
425 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
426 KMS_TERMINATE_PROP_LIST
430 bo_attribs[1] = width;
431 bo_attribs[3] = height;
433 ret = kms_bo_create(kms, bo_attribs, &bo);
435 fprintf(stderr, "failed to alloc buffer: %s\n",
440 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
442 fprintf(stderr, "failed to retreive buffer stride: %s\n",
452 make_pwetty(void *data, int width, int height, int stride)
455 cairo_surface_t *surface;
459 surface = cairo_image_surface_create_for_data(data,
463 cr = cairo_create(surface);
464 cairo_surface_destroy(surface);
466 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
467 for (x = 0; x < width; x += 250)
468 for (y = 0; y < height; y += 250) {
471 cairo_move_to(cr, x, y - 20);
472 cairo_line_to(cr, x, y + 20);
473 cairo_move_to(cr, x - 20, y);
474 cairo_line_to(cr, x + 20, y);
475 cairo_new_sub_path(cr);
476 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
477 cairo_set_line_width(cr, 4);
478 cairo_set_source_rgb(cr, 0, 0, 0);
479 cairo_stroke_preserve(cr);
480 cairo_set_source_rgb(cr, 1, 1, 1);
481 cairo_set_line_width(cr, 2);
484 snprintf(buf, sizeof buf, "%d, %d", x, y);
485 cairo_move_to(cr, x + 20, y + 20);
486 cairo_text_path(cr, buf);
487 cairo_set_source_rgb(cr, 0, 0, 0);
488 cairo_stroke_preserve(cr);
489 cairo_set_source_rgb(cr, 1, 1, 1);
498 create_test_buffer(struct kms_driver *kms,
499 int width, int height, int *stride_out,
500 struct kms_bo **bo_out)
503 int ret, i, j, stride;
506 bo = allocate_buffer(kms, width, height, &stride);
510 ret = kms_bo_map(bo, &virtual);
512 fprintf(stderr, "failed to map buffer: %s\n",
518 /* paint the buffer with colored tiles */
519 for (j = 0; j < height; j++) {
520 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
521 for (i = 0; i < width; i++) {
522 div_t d = div(i, width);
524 0x00130502 * (d.quot >> 6) +
525 0x000a1120 * (d.rem >> 6);
529 make_pwetty(virtual, width, height, stride);
534 *stride_out = stride;
539 create_grey_buffer(struct kms_driver *kms,
540 int width, int height, int *stride_out,
541 struct kms_bo **bo_out)
544 int size, ret, stride;
547 bo = allocate_buffer(kms, width, height, &stride);
551 ret = kms_bo_map(bo, &virtual);
553 fprintf(stderr, "failed to map buffer: %s\n",
559 size = stride * height;
560 memset(virtual, 0x77, size);
564 *stride_out = stride;
570 page_flip_handler(int fd, unsigned int frame,
571 unsigned int sec, unsigned int usec, void *data)
574 unsigned int new_fb_id;
579 if (c->current_fb_id == c->fb_id[0])
580 new_fb_id = c->fb_id[1];
582 new_fb_id = c->fb_id[0];
584 drmModePageFlip(fd, c->crtc, new_fb_id,
585 DRM_MODE_PAGE_FLIP_EVENT, c);
586 c->current_fb_id = new_fb_id;
588 if (c->swap_count == 60) {
589 gettimeofday(&end, NULL);
590 t = end.tv_sec + end.tv_usec * 1e-6 -
591 (c->start.tv_sec + c->start.tv_usec * 1e-6);
592 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
598 /* swap these for big endian.. */
604 fill420(unsigned char *y, unsigned char *u, unsigned char *v,
605 int cs /*chroma pixel stride */,
606 int n, int width, int height, int stride)
610 /* paint the buffer with colored tiles, in blocks of 2x2 */
611 for (j = 0; j < height; j+=2) {
612 unsigned char *y1p = y + j * stride;
613 unsigned char *y2p = y1p + stride;
614 unsigned char *up = u + (j/2) * stride * cs / 2;
615 unsigned char *vp = v + (j/2) * stride * cs / 2;
617 for (i = 0; i < width; i+=2) {
618 div_t d = div(n+i+j, width);
619 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
620 unsigned char *rgbp = (unsigned char *)&rgb;
621 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
623 *(y2p++) = *(y1p++) = y;
624 *(y2p++) = *(y1p++) = y;
626 *up = (rgbp[BLUE] - y) * 0.565 + 128;
627 *vp = (rgbp[RED] - y) * 0.713 + 128;
635 fill422(unsigned char *virtual, int n, int width, int height, int stride)
638 /* paint the buffer with colored tiles */
639 for (j = 0; j < height; j++) {
640 uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride);
641 for (i = 0; i < width; i++) {
642 div_t d = div(n+i+j, width);
643 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
644 unsigned char *rgbp = (unsigned char *)&rgb;
645 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
648 *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
650 *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
656 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
659 /* paint the buffer with colored tiles */
660 for (j = 0; j < height; j++) {
661 uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride);
662 for (i = 0; i < width; i++) {
663 div_t d = div(n+i+j, width);
664 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
665 unsigned char *rgbp = (unsigned char *)&rgb;
668 (rgbp[RED] >> 3) << 10 |
669 (rgbp[GREEN] >> 3) << 5 |
676 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
678 drmModePlaneRes *plane_resources;
680 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
681 uint32_t plane_id = 0;
682 struct kms_bo *plane_bo;
683 uint32_t plane_flags = 0, format;
684 int i, ret, crtc_x, crtc_y, crtc_w, crtc_h;
686 /* find an unused plane which can be connected to our crtc */
687 plane_resources = drmModeGetPlaneResources(fd);
688 if (!plane_resources) {
689 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
694 for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
695 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
697 fprintf(stderr, "drmModeGetPlane failed: %s\n",
702 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
703 plane_id = ovr->plane_id;
705 drmModeFreePlane(ovr);
708 fprintf(stderr, "testing %dx%d@%s overlay plane\n",
709 p->w, p->h, p->format_str);
712 fprintf(stderr, "failed to find plane!\n");
716 if (!strcmp(p->format_str, "XR24")) {
717 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
719 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
720 format = DRM_FORMAT_XRGB8888;
724 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
725 * YUV formats, we don't use all of it.. since 4bytes/pixel is
726 * worst case, so live with it for now and just don't use all
729 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
733 ret = kms_bo_map(plane_bo, &virtual);
735 fprintf(stderr, "failed to map buffer: %s\n",
737 kms_bo_destroy(&plane_bo);
741 /* just testing a limited # of formats to test single
742 * and multi-planar path.. would be nice to add more..
744 if (!strcmp(p->format_str, "YUYV")) {
745 pitches[0] = p->w * 2;
747 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
749 fill422(virtual, 0, p->w, p->h, pitches[0]);
751 format = DRM_FORMAT_YUYV;
752 } else if (!strcmp(p->format_str, "NV12")) {
755 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
757 offsets[1] = p->w * p->h;
758 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
760 fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
761 2, 0, p->w, p->h, pitches[0]);
763 format = DRM_FORMAT_NV12;
764 } else if (!strcmp(p->format_str, "YV12")) {
767 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
768 pitches[1] = p->w / 2;
769 offsets[1] = p->w * p->h;
770 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
771 pitches[2] = p->w / 2;
772 offsets[2] = offsets[1] + (p->w * p->h) / 4;
773 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[2]);
775 fill420(virtual, virtual+offsets[1], virtual+offsets[2],
776 1, 0, p->w, p->h, pitches[0]);
778 format = DRM_FORMAT_YVU420;
779 } else if (!strcmp(p->format_str, "XR15")) {
780 pitches[0] = p->w * 2;
782 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
784 fill1555(virtual, 0, p->w, p->h, pitches[0]);
786 format = DRM_FORMAT_XRGB1555;
787 } else if (!strcmp(p->format_str, "AR15")) {
788 pitches[0] = p->w * 2;
790 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
792 fill1555(virtual, 0, p->w, p->h, pitches[0]);
794 format = DRM_FORMAT_ARGB1555;
796 fprintf(stderr, "Unknown format: %s\n", p->format_str);
800 kms_bo_unmap(plane_bo);
803 /* just use single plane format for now.. */
804 if (drmModeAddFB2(fd, p->w, p->h, format,
805 handles, pitches, offsets, &p->fb_id, plane_flags)) {
806 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
810 /* ok, boring.. but for now put in middle of screen: */
811 crtc_x = c->mode->hdisplay / 3;
812 crtc_y = c->mode->vdisplay / 3;
816 /* note src coords (last 4 args) are in Q16 format */
817 if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
818 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
819 0, 0, p->w << 16, p->h << 16)) {
820 fprintf(stderr, "failed to enable plane: %s\n",
829 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
832 struct kms_driver *kms;
833 struct kms_bo *bo, *other_bo;
834 unsigned int fb_id, other_fb_id;
835 int i, j, ret, width, height, x, stride;
837 drmEventContext evctx;
841 for (i = 0; i < count; i++) {
842 connector_find_mode(&c[i]);
843 if (c[i].mode == NULL)
845 width += c[i].mode->hdisplay;
846 if (height < c[i].mode->vdisplay)
847 height = c[i].mode->vdisplay;
850 ret = kms_create(fd, &kms);
852 fprintf(stderr, "failed to create kms driver: %s\n",
857 if (create_test_buffer(kms, width, height, &stride, &bo))
860 kms_bo_get_prop(bo, KMS_HANDLE, &handle);
861 ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
863 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
864 width, height, strerror(errno));
869 for (i = 0; i < count; i++) {
870 if (c[i].mode == NULL)
873 printf("setting mode %s on connector %d, crtc %d\n",
874 c[i].mode_str, c[i].id, c[i].crtc);
876 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
877 &c[i].id, 1, c[i].mode);
879 /* XXX: Actually check if this is needed */
880 drmModeDirtyFB(fd, fb_id, NULL, 0);
882 x += c[i].mode->hdisplay;
885 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
889 /* if we have a plane/overlay to show, set that up now: */
890 for (j = 0; j < plane_count; j++)
891 if (p[j].con_id == c[i].id)
892 if (set_plane(kms, &c[i], &p[j]))
899 if (create_grey_buffer(kms, width, height, &stride, &other_bo))
902 kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
903 ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
906 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
910 for (i = 0; i < count; i++) {
911 if (c[i].mode == NULL)
914 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
915 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
917 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
920 gettimeofday(&c[i].start, NULL);
922 c[i].fb_id[0] = fb_id;
923 c[i].fb_id[1] = other_fb_id;
924 c[i].current_fb_id = other_fb_id;
927 memset(&evctx, 0, sizeof evctx);
928 evctx.version = DRM_EVENT_CONTEXT_VERSION;
929 evctx.vblank_handler = NULL;
930 evctx.page_flip_handler = page_flip_handler;
934 struct pollfd pfd[2];
937 pfd[0].events = POLLIN;
939 pfd[1].events = POLLIN;
941 if (poll(pfd, 2, -1) < 0) {
942 fprintf(stderr, "poll error\n");
949 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
956 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
959 fprintf(stderr, "select timed out or error (ret %d)\n",
962 } else if (FD_ISSET(0, &fds)) {
967 drmHandleEvent(fd, &evctx);
971 kms_bo_destroy(&other_bo);
976 extern int optind, opterr, optopt;
977 static char optstr[] = "ecpmfs:P:v";
979 void usage(char *name)
981 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
982 fprintf(stderr, "\t-e\tlist encoders\n");
983 fprintf(stderr, "\t-c\tlist connectors\n");
984 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
985 fprintf(stderr, "\t-m\tlist modes\n");
986 fprintf(stderr, "\t-f\tlist framebuffers\n");
987 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
988 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
989 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
990 fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
991 fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
992 fprintf(stderr, "\n\tDefault is to dump all info.\n");
996 #define dump_resource(res) if (res) dump_##res()
998 static int page_flipping_supported(int fd)
1000 /*FIXME: generic ioctl needed? */
1004 struct drm_i915_getparam gp;
1006 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1009 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1011 fprintf(stderr, "drm_i915_getparam: %m\n");
1019 int main(int argc, char **argv)
1022 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1024 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm" };
1025 char *modeset = NULL;
1026 int i, count = 0, plane_count = 0;
1027 struct connector con_args[2];
1028 struct plane plane_args[2] = {0};
1031 while ((c = getopt(argc, argv, optstr)) != -1) {
1053 modeset = strdup(optarg);
1054 con_args[count].crtc = -1;
1055 if (sscanf(optarg, "%d:%64s",
1056 &con_args[count].id,
1057 con_args[count].mode_str) != 2 &&
1058 sscanf(optarg, "%d@%d:%64s",
1059 &con_args[count].id,
1060 &con_args[count].crtc,
1061 con_args[count].mode_str) != 3)
1066 strcpy(plane_args[plane_count].format_str, "XR24");
1067 if (sscanf(optarg, "%d:%dx%d@%4s",
1068 &plane_args[plane_count].con_id,
1069 &plane_args[plane_count].w,
1070 &plane_args[plane_count].h,
1071 plane_args[plane_count].format_str) != 4 &&
1072 sscanf(optarg, "%d:%dx%d",
1073 &plane_args[plane_count].con_id,
1074 &plane_args[plane_count].w,
1075 &plane_args[plane_count].h) != 3)
1086 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1088 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1089 printf("trying to load module %s...", modules[i]);
1090 fd = drmOpen(modules[i], NULL);
1092 printf("failed.\n");
1094 printf("success.\n");
1099 if (test_vsync && !page_flipping_supported(fd)) {
1100 fprintf(stderr, "page flipping not supported by drm.\n");
1104 if (i == ARRAY_SIZE(modules)) {
1105 fprintf(stderr, "failed to load any modules, aborting.\n");
1109 resources = drmModeGetResources(fd);
1111 fprintf(stderr, "drmModeGetResources failed: %s\n",
1117 dump_resource(encoders);
1118 dump_resource(connectors);
1119 dump_resource(crtcs);
1120 dump_resource(planes);
1121 dump_resource(framebuffers);
1124 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1128 drmModeFreeResources(resources);