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.
54 #include "xf86drmMode.h"
55 #include "drm_fourcc.h"
63 drmModeRes *resources;
66 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
73 #define type_name_fn(res) \
74 char * res##_str(int type) { \
76 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
77 if (res##_names[i].type == type) \
78 return res##_names[i].name; \
83 struct type_name encoder_type_names[] = {
84 { DRM_MODE_ENCODER_NONE, "none" },
85 { DRM_MODE_ENCODER_DAC, "DAC" },
86 { DRM_MODE_ENCODER_TMDS, "TMDS" },
87 { DRM_MODE_ENCODER_LVDS, "LVDS" },
88 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
91 type_name_fn(encoder_type)
93 struct type_name connector_status_names[] = {
94 { DRM_MODE_CONNECTED, "connected" },
95 { DRM_MODE_DISCONNECTED, "disconnected" },
96 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
99 type_name_fn(connector_status)
101 struct type_name connector_type_names[] = {
102 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
103 { DRM_MODE_CONNECTOR_VGA, "VGA" },
104 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
105 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
106 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
107 { DRM_MODE_CONNECTOR_Composite, "composite" },
108 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
109 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
110 { DRM_MODE_CONNECTOR_Component, "component" },
111 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
112 { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
113 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
114 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
115 { DRM_MODE_CONNECTOR_TV, "TV" },
116 { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
119 type_name_fn(connector_type)
121 void dump_encoders(void)
123 drmModeEncoder *encoder;
126 printf("Encoders:\n");
127 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
128 for (i = 0; i < resources->count_encoders; i++) {
129 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
132 fprintf(stderr, "could not get encoder %i: %s\n",
133 resources->encoders[i], strerror(errno));
136 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
139 encoder_type_str(encoder->encoder_type),
140 encoder->possible_crtcs,
141 encoder->possible_clones);
142 drmModeFreeEncoder(encoder);
147 void dump_mode(drmModeModeInfo *mode)
149 printf("\t%s %d %d %d %d %d %d %d %d %d\n",
163 dump_blob(uint32_t blob_id)
166 unsigned char *blob_data;
167 drmModePropertyBlobPtr blob;
169 blob = drmModeGetPropertyBlob(fd, blob_id);
173 blob_data = blob->data;
175 for (i = 0; i < blob->length; i++) {
178 printf("%.2hhx", blob_data[i]);
182 drmModeFreePropertyBlob(blob);
186 dump_prop(uint32_t prop_id, uint64_t value)
189 drmModePropertyPtr prop;
191 prop = drmModeGetProperty(fd, prop_id);
193 printf("\t%d", prop_id);
199 printf(" %s:\n", prop->name);
201 printf("\t\tflags:");
202 if (prop->flags & DRM_MODE_PROP_PENDING)
204 if (prop->flags & DRM_MODE_PROP_RANGE)
206 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
207 printf(" immutable");
208 if (prop->flags & DRM_MODE_PROP_ENUM)
210 if (prop->flags & DRM_MODE_PROP_BITMASK)
212 if (prop->flags & DRM_MODE_PROP_BLOB)
216 if (prop->flags & DRM_MODE_PROP_RANGE) {
217 printf("\t\tvalues:");
218 for (i = 0; i < prop->count_values; i++)
219 printf(" %"PRIu64, prop->values[i]);
223 if (prop->flags & DRM_MODE_PROP_ENUM) {
224 printf("\t\tenums:");
225 for (i = 0; i < prop->count_enums; i++)
226 printf(" %s=%llu", prop->enums[i].name,
227 prop->enums[i].value);
229 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
230 printf("\t\tvalues:");
231 for (i = 0; i < prop->count_enums; i++)
232 printf(" %s=0x%llx", prop->enums[i].name,
233 (1LL << prop->enums[i].value));
236 assert(prop->count_enums == 0);
239 if (prop->flags & DRM_MODE_PROP_BLOB) {
240 printf("\t\tblobs:\n");
241 for (i = 0; i < prop->count_blobs; i++)
242 dump_blob(prop->blob_ids[i]);
245 assert(prop->count_blobs == 0);
248 printf("\t\tvalue:");
249 if (prop->flags & DRM_MODE_PROP_BLOB)
252 printf(" %"PRIu64"\n", value);
254 drmModeFreeProperty(prop);
257 void dump_connectors(void)
259 drmModeConnector *connector;
262 printf("Connectors:\n");
263 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
264 for (i = 0; i < resources->count_connectors; i++) {
265 connector = drmModeGetConnector(fd, resources->connectors[i]);
268 fprintf(stderr, "could not get connector %i: %s\n",
269 resources->connectors[i], strerror(errno));
273 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
274 connector->connector_id,
275 connector->encoder_id,
276 connector_status_str(connector->connection),
277 connector_type_str(connector->connector_type),
278 connector->mmWidth, connector->mmHeight,
279 connector->count_modes);
281 for (j = 0; j < connector->count_encoders; j++)
282 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
285 if (connector->count_modes) {
287 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
289 for (j = 0; j < connector->count_modes; j++)
290 dump_mode(&connector->modes[j]);
293 for (j = 0; j < connector->count_props; j++)
294 dump_prop(connector->props[j],
295 connector->prop_values[j]);
298 drmModeFreeConnector(connector);
303 void dump_crtcs(void)
306 drmModeObjectPropertiesPtr props;
311 printf("id\tfb\tpos\tsize\n");
312 for (i = 0; i < resources->count_crtcs; i++) {
313 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
316 fprintf(stderr, "could not get crtc %i: %s\n",
317 resources->crtcs[i], strerror(errno));
320 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
324 crtc->width, crtc->height);
325 dump_mode(&crtc->mode);
328 props = drmModeObjectGetProperties(fd, crtc->crtc_id,
329 DRM_MODE_OBJECT_CRTC);
331 for (j = 0; j < props->count_props; j++)
332 dump_prop(props->props[j],
333 props->prop_values[j]);
334 drmModeFreeObjectProperties(props);
336 printf("\tcould not get crtc properties: %s\n",
340 drmModeFreeCrtc(crtc);
345 void dump_framebuffers(void)
350 printf("Frame buffers:\n");
351 printf("id\tsize\tpitch\n");
352 for (i = 0; i < resources->count_fbs; i++) {
353 fb = drmModeGetFB(fd, resources->fbs[i]);
356 fprintf(stderr, "could not get fb %i: %s\n",
357 resources->fbs[i], strerror(errno));
360 printf("%u\t(%ux%u)\t%u\n",
362 fb->width, fb->height,
370 static void dump_planes(void)
372 drmModeObjectPropertiesPtr props;
373 drmModePlaneRes *plane_resources;
377 plane_resources = drmModeGetPlaneResources(fd);
378 if (!plane_resources) {
379 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
385 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
386 for (i = 0; i < plane_resources->count_planes; i++) {
387 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
389 fprintf(stderr, "drmModeGetPlane failed: %s\n",
394 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
395 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
396 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
399 if (!ovr->count_formats)
403 for (j = 0; j < ovr->count_formats; j++)
404 printf(" %4.4s", (char *)&ovr->formats[j]);
408 props = drmModeObjectGetProperties(fd, ovr->plane_id,
409 DRM_MODE_OBJECT_PLANE);
411 for (j = 0; j < props->count_props; j++)
412 dump_prop(props->props[j],
413 props->prop_values[j]);
414 drmModeFreeObjectProperties(props);
416 printf("\tcould not get plane properties: %s\n",
420 drmModeFreePlane(ovr);
424 drmModeFreePlaneResources(plane_resources);
429 * Mode setting with the kernel interfaces is a bit of a chore.
430 * First you have to find the connector in question and make sure the
431 * requested mode is available.
432 * Then you need to find the encoder attached to that connector so you
433 * can bind it with a free crtc.
438 drmModeModeInfo *mode;
439 drmModeEncoder *encoder;
442 unsigned int fb_id[2], current_fb_id;
443 struct timeval start;
449 uint32_t con_id; /* the id of connector to bind to */
452 char format_str[5]; /* need to leave room for terminating \0 */
456 connector_find_mode(struct connector *c)
458 drmModeConnector *connector;
461 /* First, find the connector & mode */
463 for (i = 0; i < resources->count_connectors; i++) {
464 connector = drmModeGetConnector(fd, resources->connectors[i]);
467 fprintf(stderr, "could not get connector %i: %s\n",
468 resources->connectors[i], strerror(errno));
469 drmModeFreeConnector(connector);
473 if (!connector->count_modes) {
474 drmModeFreeConnector(connector);
478 if (connector->connector_id != c->id) {
479 drmModeFreeConnector(connector);
483 for (j = 0; j < connector->count_modes; j++) {
484 c->mode = &connector->modes[j];
485 if (!strcmp(c->mode->name, c->mode_str))
489 /* Found it, break out */
493 drmModeFreeConnector(connector);
497 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
501 /* Now get the encoder */
502 for (i = 0; i < resources->count_encoders; i++) {
503 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
506 fprintf(stderr, "could not get encoder %i: %s\n",
507 resources->encoders[i], strerror(errno));
508 drmModeFreeEncoder(c->encoder);
512 if (c->encoder->encoder_id == connector->encoder_id)
515 drmModeFreeEncoder(c->encoder);
519 c->crtc = c->encoder->crtc_id;
521 /* and figure out which crtc index it is: */
522 for (i = 0; i < resources->count_crtcs; i++) {
523 if (c->crtc == resources->crtcs[i]) {
531 static struct kms_bo *
532 allocate_buffer(struct kms_driver *kms,
533 int width, int height, int *stride)
536 unsigned bo_attribs[] = {
539 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
540 KMS_TERMINATE_PROP_LIST
544 bo_attribs[1] = width;
545 bo_attribs[3] = height;
547 ret = kms_bo_create(kms, bo_attribs, &bo);
549 fprintf(stderr, "failed to alloc buffer: %s\n",
554 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
556 fprintf(stderr, "failed to retreive buffer stride: %s\n",
566 make_pwetty(void *data, int width, int height, int stride)
569 cairo_surface_t *surface;
573 surface = cairo_image_surface_create_for_data(data,
577 cr = cairo_create(surface);
578 cairo_surface_destroy(surface);
580 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
581 for (x = 0; x < width; x += 250)
582 for (y = 0; y < height; y += 250) {
585 cairo_move_to(cr, x, y - 20);
586 cairo_line_to(cr, x, y + 20);
587 cairo_move_to(cr, x - 20, y);
588 cairo_line_to(cr, x + 20, y);
589 cairo_new_sub_path(cr);
590 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
591 cairo_set_line_width(cr, 4);
592 cairo_set_source_rgb(cr, 0, 0, 0);
593 cairo_stroke_preserve(cr);
594 cairo_set_source_rgb(cr, 1, 1, 1);
595 cairo_set_line_width(cr, 2);
598 snprintf(buf, sizeof buf, "%d, %d", x, y);
599 cairo_move_to(cr, x + 20, y + 20);
600 cairo_text_path(cr, buf);
601 cairo_set_source_rgb(cr, 0, 0, 0);
602 cairo_stroke_preserve(cr);
603 cairo_set_source_rgb(cr, 1, 1, 1);
612 create_test_buffer(struct kms_driver *kms,
613 int width, int height, int *stride_out,
614 struct kms_bo **bo_out)
617 int ret, i, j, stride;
620 bo = allocate_buffer(kms, width, height, &stride);
624 ret = kms_bo_map(bo, &virtual);
626 fprintf(stderr, "failed to map buffer: %s\n",
632 /* paint the buffer with colored tiles */
633 for (j = 0; j < height; j++) {
634 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
635 for (i = 0; i < width; i++) {
636 div_t d = div(i, width);
638 0x00130502 * (d.quot >> 6) +
639 0x000a1120 * (d.rem >> 6);
643 make_pwetty(virtual, width, height, stride);
648 *stride_out = stride;
653 create_grey_buffer(struct kms_driver *kms,
654 int width, int height, int *stride_out,
655 struct kms_bo **bo_out)
658 int size, ret, stride;
661 bo = allocate_buffer(kms, width, height, &stride);
665 ret = kms_bo_map(bo, &virtual);
667 fprintf(stderr, "failed to map buffer: %s\n",
673 size = stride * height;
674 memset(virtual, 0x77, size);
678 *stride_out = stride;
684 page_flip_handler(int fd, unsigned int frame,
685 unsigned int sec, unsigned int usec, void *data)
688 unsigned int new_fb_id;
693 if (c->current_fb_id == c->fb_id[0])
694 new_fb_id = c->fb_id[1];
696 new_fb_id = c->fb_id[0];
698 drmModePageFlip(fd, c->crtc, new_fb_id,
699 DRM_MODE_PAGE_FLIP_EVENT, c);
700 c->current_fb_id = new_fb_id;
702 if (c->swap_count == 60) {
703 gettimeofday(&end, NULL);
704 t = end.tv_sec + end.tv_usec * 1e-6 -
705 (c->start.tv_sec + c->start.tv_usec * 1e-6);
706 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
712 /* swap these for big endian.. */
718 fill420(unsigned char *y, unsigned char *u, unsigned char *v,
719 int cs /*chroma pixel stride */,
720 int n, int width, int height, int stride)
724 /* paint the buffer with colored tiles, in blocks of 2x2 */
725 for (j = 0; j < height; j+=2) {
726 unsigned char *y1p = y + j * stride;
727 unsigned char *y2p = y1p + stride;
728 unsigned char *up = u + (j/2) * stride * cs / 2;
729 unsigned char *vp = v + (j/2) * stride * cs / 2;
731 for (i = 0; i < width; i+=2) {
732 div_t d = div(n+i+j, width);
733 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
734 unsigned char *rgbp = (unsigned char *)&rgb;
735 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
737 *(y2p++) = *(y1p++) = y;
738 *(y2p++) = *(y1p++) = y;
740 *up = (rgbp[BLUE] - y) * 0.565 + 128;
741 *vp = (rgbp[RED] - y) * 0.713 + 128;
749 fill422(unsigned char *virtual, int n, int width, int height, int stride)
752 /* paint the buffer with colored tiles */
753 for (j = 0; j < height; j++) {
754 uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride);
755 for (i = 0; i < width; i++) {
756 div_t d = div(n+i+j, width);
757 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
758 unsigned char *rgbp = (unsigned char *)&rgb;
759 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
762 *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
764 *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
770 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
773 /* paint the buffer with colored tiles */
774 for (j = 0; j < height; j++) {
775 uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride);
776 for (i = 0; i < width; i++) {
777 div_t d = div(n+i+j, width);
778 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
779 unsigned char *rgbp = (unsigned char *)&rgb;
782 (rgbp[RED] >> 3) << 10 |
783 (rgbp[GREEN] >> 3) << 5 |
790 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
792 drmModePlaneRes *plane_resources;
794 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
795 uint32_t plane_id = 0;
796 struct kms_bo *plane_bo;
797 uint32_t plane_flags = 0, format;
798 int ret, crtc_x, crtc_y, crtc_w, crtc_h;
801 /* find an unused plane which can be connected to our crtc */
802 plane_resources = drmModeGetPlaneResources(fd);
803 if (!plane_resources) {
804 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
809 for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
810 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
812 fprintf(stderr, "drmModeGetPlane failed: %s\n",
817 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
818 plane_id = ovr->plane_id;
820 drmModeFreePlane(ovr);
823 fprintf(stderr, "testing %dx%d@%s overlay plane\n",
824 p->w, p->h, p->format_str);
827 fprintf(stderr, "failed to find plane!\n");
831 if (!strcmp(p->format_str, "XR24")) {
832 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
834 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
835 format = DRM_FORMAT_XRGB8888;
839 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
840 * YUV formats, we don't use all of it.. since 4bytes/pixel is
841 * worst case, so live with it for now and just don't use all
844 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
848 ret = kms_bo_map(plane_bo, &virtual);
850 fprintf(stderr, "failed to map buffer: %s\n",
852 kms_bo_destroy(&plane_bo);
856 /* just testing a limited # of formats to test single
857 * and multi-planar path.. would be nice to add more..
859 if (!strcmp(p->format_str, "YUYV")) {
860 pitches[0] = p->w * 2;
862 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
864 fill422(virtual, 0, p->w, p->h, pitches[0]);
866 format = DRM_FORMAT_YUYV;
867 } else if (!strcmp(p->format_str, "NV12")) {
870 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
872 offsets[1] = p->w * p->h;
873 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
875 fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
876 2, 0, p->w, p->h, pitches[0]);
878 format = DRM_FORMAT_NV12;
879 } else if (!strcmp(p->format_str, "YV12")) {
882 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
883 pitches[1] = p->w / 2;
884 offsets[1] = p->w * p->h;
885 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
886 pitches[2] = p->w / 2;
887 offsets[2] = offsets[1] + (p->w * p->h) / 4;
888 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[2]);
890 fill420(virtual, virtual+offsets[1], virtual+offsets[2],
891 1, 0, p->w, p->h, pitches[0]);
893 format = DRM_FORMAT_YVU420;
894 } else if (!strcmp(p->format_str, "XR15")) {
895 pitches[0] = p->w * 2;
897 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
899 fill1555(virtual, 0, p->w, p->h, pitches[0]);
901 format = DRM_FORMAT_XRGB1555;
902 } else if (!strcmp(p->format_str, "AR15")) {
903 pitches[0] = p->w * 2;
905 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
907 fill1555(virtual, 0, p->w, p->h, pitches[0]);
909 format = DRM_FORMAT_ARGB1555;
911 fprintf(stderr, "Unknown format: %s\n", p->format_str);
915 kms_bo_unmap(plane_bo);
918 /* just use single plane format for now.. */
919 if (drmModeAddFB2(fd, p->w, p->h, format,
920 handles, pitches, offsets, &p->fb_id, plane_flags)) {
921 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
925 /* ok, boring.. but for now put in middle of screen: */
926 crtc_x = c->mode->hdisplay / 3;
927 crtc_y = c->mode->vdisplay / 3;
931 /* note src coords (last 4 args) are in Q16 format */
932 if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
933 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
934 0, 0, p->w << 16, p->h << 16)) {
935 fprintf(stderr, "failed to enable plane: %s\n",
944 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
947 struct kms_driver *kms;
948 struct kms_bo *bo, *other_bo;
949 unsigned int fb_id, other_fb_id;
950 int i, j, ret, width, height, x, stride;
952 drmEventContext evctx;
956 for (i = 0; i < count; i++) {
957 connector_find_mode(&c[i]);
958 if (c[i].mode == NULL)
960 width += c[i].mode->hdisplay;
961 if (height < c[i].mode->vdisplay)
962 height = c[i].mode->vdisplay;
965 ret = kms_create(fd, &kms);
967 fprintf(stderr, "failed to create kms driver: %s\n",
972 if (create_test_buffer(kms, width, height, &stride, &bo))
975 kms_bo_get_prop(bo, KMS_HANDLE, &handle);
976 ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
978 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
979 width, height, strerror(errno));
984 for (i = 0; i < count; i++) {
985 if (c[i].mode == NULL)
988 printf("setting mode %s on connector %d, crtc %d\n",
989 c[i].mode_str, c[i].id, c[i].crtc);
991 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
992 &c[i].id, 1, c[i].mode);
994 /* XXX: Actually check if this is needed */
995 drmModeDirtyFB(fd, fb_id, NULL, 0);
997 x += c[i].mode->hdisplay;
1000 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1004 /* if we have a plane/overlay to show, set that up now: */
1005 for (j = 0; j < plane_count; j++)
1006 if (p[j].con_id == c[i].id)
1007 if (set_plane(kms, &c[i], &p[j]))
1014 if (create_grey_buffer(kms, width, height, &stride, &other_bo))
1017 kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
1018 ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
1021 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1025 for (i = 0; i < count; i++) {
1026 if (c[i].mode == NULL)
1029 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1030 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1032 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1035 gettimeofday(&c[i].start, NULL);
1036 c[i].swap_count = 0;
1037 c[i].fb_id[0] = fb_id;
1038 c[i].fb_id[1] = other_fb_id;
1039 c[i].current_fb_id = other_fb_id;
1042 memset(&evctx, 0, sizeof evctx);
1043 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1044 evctx.vblank_handler = NULL;
1045 evctx.page_flip_handler = page_flip_handler;
1049 struct pollfd pfd[2];
1052 pfd[0].events = POLLIN;
1054 pfd[1].events = POLLIN;
1056 if (poll(pfd, 2, -1) < 0) {
1057 fprintf(stderr, "poll error\n");
1064 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1071 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1074 fprintf(stderr, "select timed out or error (ret %d)\n",
1077 } else if (FD_ISSET(0, &fds)) {
1082 drmHandleEvent(fd, &evctx);
1085 kms_bo_destroy(&bo);
1086 kms_bo_destroy(&other_bo);
1090 extern char *optarg;
1091 extern int optind, opterr, optopt;
1092 static char optstr[] = "ecpmfs:P:v";
1094 void usage(char *name)
1096 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1097 fprintf(stderr, "\t-e\tlist encoders\n");
1098 fprintf(stderr, "\t-c\tlist connectors\n");
1099 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1100 fprintf(stderr, "\t-m\tlist modes\n");
1101 fprintf(stderr, "\t-f\tlist framebuffers\n");
1102 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1103 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1104 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1105 fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1106 fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1107 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1111 #define dump_resource(res) if (res) dump_##res()
1113 static int page_flipping_supported(void)
1115 /*FIXME: generic ioctl needed? */
1119 struct drm_i915_getparam gp;
1121 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1124 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1126 fprintf(stderr, "drm_i915_getparam: %m\n");
1134 int main(int argc, char **argv)
1137 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1139 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1141 int count = 0, plane_count = 0;
1142 struct connector con_args[2];
1143 struct plane plane_args[2] = {0};
1146 while ((c = getopt(argc, argv, optstr)) != -1) {
1168 con_args[count].crtc = -1;
1169 if (sscanf(optarg, "%d:%64s",
1170 &con_args[count].id,
1171 con_args[count].mode_str) != 2 &&
1172 sscanf(optarg, "%d@%d:%64s",
1173 &con_args[count].id,
1174 &con_args[count].crtc,
1175 con_args[count].mode_str) != 3)
1180 strcpy(plane_args[plane_count].format_str, "XR24");
1181 if (sscanf(optarg, "%d:%dx%d@%4s",
1182 &plane_args[plane_count].con_id,
1183 &plane_args[plane_count].w,
1184 &plane_args[plane_count].h,
1185 plane_args[plane_count].format_str) != 4 &&
1186 sscanf(optarg, "%d:%dx%d",
1187 &plane_args[plane_count].con_id,
1188 &plane_args[plane_count].w,
1189 &plane_args[plane_count].h) != 3)
1200 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1202 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1203 printf("trying to load module %s...", modules[i]);
1204 fd = drmOpen(modules[i], NULL);
1206 printf("failed.\n");
1208 printf("success.\n");
1213 if (test_vsync && !page_flipping_supported()) {
1214 fprintf(stderr, "page flipping not supported by drm.\n");
1218 if (i == ARRAY_SIZE(modules)) {
1219 fprintf(stderr, "failed to load any modules, aborting.\n");
1223 resources = drmModeGetResources(fd);
1225 fprintf(stderr, "drmModeGetResources failed: %s\n",
1231 dump_resource(encoders);
1232 dump_resource(connectors);
1233 dump_resource(crtcs);
1234 dump_resource(planes);
1235 dump_resource(framebuffers);
1238 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1242 drmModeFreeResources(resources);