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 #define bit_name_fn(res) \
122 char * res##_str(int type) { \
124 const char *sep = ""; \
125 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
126 if (type & (1 << i)) { \
127 printf("%s%s", sep, res##_names[i]); \
133 static const char *mode_type_names[] = {
143 bit_name_fn(mode_type)
145 static const char *mode_flag_names[] = {
162 bit_name_fn(mode_flag)
164 void dump_encoders(void)
166 drmModeEncoder *encoder;
169 printf("Encoders:\n");
170 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
171 for (i = 0; i < resources->count_encoders; i++) {
172 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
175 fprintf(stderr, "could not get encoder %i: %s\n",
176 resources->encoders[i], strerror(errno));
179 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
182 encoder_type_str(encoder->encoder_type),
183 encoder->possible_crtcs,
184 encoder->possible_clones);
185 drmModeFreeEncoder(encoder);
190 void dump_mode(drmModeModeInfo *mode)
192 printf(" %s %d %d %d %d %d %d %d %d %d",
205 mode_flag_str(mode->flags);
207 mode_type_str(mode->type);
212 dump_blob(uint32_t blob_id)
215 unsigned char *blob_data;
216 drmModePropertyBlobPtr blob;
218 blob = drmModeGetPropertyBlob(fd, blob_id);
222 blob_data = blob->data;
224 for (i = 0; i < blob->length; i++) {
227 printf("%.2hhx", blob_data[i]);
231 drmModeFreePropertyBlob(blob);
235 dump_prop(uint32_t prop_id, uint64_t value)
238 drmModePropertyPtr prop;
240 prop = drmModeGetProperty(fd, prop_id);
242 printf("\t%d", prop_id);
248 printf(" %s:\n", prop->name);
250 printf("\t\tflags:");
251 if (prop->flags & DRM_MODE_PROP_PENDING)
253 if (prop->flags & DRM_MODE_PROP_RANGE)
255 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
256 printf(" immutable");
257 if (prop->flags & DRM_MODE_PROP_ENUM)
259 if (prop->flags & DRM_MODE_PROP_BITMASK)
261 if (prop->flags & DRM_MODE_PROP_BLOB)
265 if (prop->flags & DRM_MODE_PROP_RANGE) {
266 printf("\t\tvalues:");
267 for (i = 0; i < prop->count_values; i++)
268 printf(" %"PRIu64, prop->values[i]);
272 if (prop->flags & DRM_MODE_PROP_ENUM) {
273 printf("\t\tenums:");
274 for (i = 0; i < prop->count_enums; i++)
275 printf(" %s=%llu", prop->enums[i].name,
276 prop->enums[i].value);
278 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
279 printf("\t\tvalues:");
280 for (i = 0; i < prop->count_enums; i++)
281 printf(" %s=0x%llx", prop->enums[i].name,
282 (1LL << prop->enums[i].value));
285 assert(prop->count_enums == 0);
288 if (prop->flags & DRM_MODE_PROP_BLOB) {
289 printf("\t\tblobs:\n");
290 for (i = 0; i < prop->count_blobs; i++)
291 dump_blob(prop->blob_ids[i]);
294 assert(prop->count_blobs == 0);
297 printf("\t\tvalue:");
298 if (prop->flags & DRM_MODE_PROP_BLOB)
301 printf(" %"PRIu64"\n", value);
303 drmModeFreeProperty(prop);
306 void dump_connectors(void)
308 drmModeConnector *connector;
311 printf("Connectors:\n");
312 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
313 for (i = 0; i < resources->count_connectors; i++) {
314 connector = drmModeGetConnector(fd, resources->connectors[i]);
317 fprintf(stderr, "could not get connector %i: %s\n",
318 resources->connectors[i], strerror(errno));
322 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
323 connector->connector_id,
324 connector->encoder_id,
325 connector_status_str(connector->connection),
326 connector_type_str(connector->connector_type),
327 connector->mmWidth, connector->mmHeight,
328 connector->count_modes);
330 for (j = 0; j < connector->count_encoders; j++)
331 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
334 if (connector->count_modes) {
336 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
338 for (j = 0; j < connector->count_modes; j++)
339 dump_mode(&connector->modes[j]);
342 for (j = 0; j < connector->count_props; j++)
343 dump_prop(connector->props[j],
344 connector->prop_values[j]);
347 drmModeFreeConnector(connector);
352 void dump_crtcs(void)
355 drmModeObjectPropertiesPtr props;
360 printf("id\tfb\tpos\tsize\n");
361 for (i = 0; i < resources->count_crtcs; i++) {
362 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
365 fprintf(stderr, "could not get crtc %i: %s\n",
366 resources->crtcs[i], strerror(errno));
369 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
373 crtc->width, crtc->height);
374 dump_mode(&crtc->mode);
377 props = drmModeObjectGetProperties(fd, crtc->crtc_id,
378 DRM_MODE_OBJECT_CRTC);
380 for (j = 0; j < props->count_props; j++)
381 dump_prop(props->props[j],
382 props->prop_values[j]);
383 drmModeFreeObjectProperties(props);
385 printf("\tcould not get crtc properties: %s\n",
389 drmModeFreeCrtc(crtc);
394 void dump_framebuffers(void)
399 printf("Frame buffers:\n");
400 printf("id\tsize\tpitch\n");
401 for (i = 0; i < resources->count_fbs; i++) {
402 fb = drmModeGetFB(fd, resources->fbs[i]);
405 fprintf(stderr, "could not get fb %i: %s\n",
406 resources->fbs[i], strerror(errno));
409 printf("%u\t(%ux%u)\t%u\n",
411 fb->width, fb->height,
419 static void dump_planes(void)
421 drmModeObjectPropertiesPtr props;
422 drmModePlaneRes *plane_resources;
426 plane_resources = drmModeGetPlaneResources(fd);
427 if (!plane_resources) {
428 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
434 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
435 for (i = 0; i < plane_resources->count_planes; i++) {
436 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
438 fprintf(stderr, "drmModeGetPlane failed: %s\n",
443 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
444 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
445 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
448 if (!ovr->count_formats)
452 for (j = 0; j < ovr->count_formats; j++)
453 printf(" %4.4s", (char *)&ovr->formats[j]);
457 props = drmModeObjectGetProperties(fd, ovr->plane_id,
458 DRM_MODE_OBJECT_PLANE);
460 for (j = 0; j < props->count_props; j++)
461 dump_prop(props->props[j],
462 props->prop_values[j]);
463 drmModeFreeObjectProperties(props);
465 printf("\tcould not get plane properties: %s\n",
469 drmModeFreePlane(ovr);
473 drmModeFreePlaneResources(plane_resources);
478 * Mode setting with the kernel interfaces is a bit of a chore.
479 * First you have to find the connector in question and make sure the
480 * requested mode is available.
481 * Then you need to find the encoder attached to that connector so you
482 * can bind it with a free crtc.
487 drmModeModeInfo *mode;
488 drmModeEncoder *encoder;
491 unsigned int fb_id[2], current_fb_id;
492 struct timeval start;
498 uint32_t con_id; /* the id of connector to bind to */
501 char format_str[5]; /* need to leave room for terminating \0 */
505 connector_find_mode(struct connector *c)
507 drmModeConnector *connector;
510 /* First, find the connector & mode */
512 for (i = 0; i < resources->count_connectors; i++) {
513 connector = drmModeGetConnector(fd, resources->connectors[i]);
516 fprintf(stderr, "could not get connector %i: %s\n",
517 resources->connectors[i], strerror(errno));
518 drmModeFreeConnector(connector);
522 if (!connector->count_modes) {
523 drmModeFreeConnector(connector);
527 if (connector->connector_id != c->id) {
528 drmModeFreeConnector(connector);
532 for (j = 0; j < connector->count_modes; j++) {
533 c->mode = &connector->modes[j];
534 if (!strcmp(c->mode->name, c->mode_str))
538 /* Found it, break out */
542 drmModeFreeConnector(connector);
546 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
550 /* Now get the encoder */
551 for (i = 0; i < resources->count_encoders; i++) {
552 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
555 fprintf(stderr, "could not get encoder %i: %s\n",
556 resources->encoders[i], strerror(errno));
557 drmModeFreeEncoder(c->encoder);
561 if (c->encoder->encoder_id == connector->encoder_id)
564 drmModeFreeEncoder(c->encoder);
568 c->crtc = c->encoder->crtc_id;
570 /* and figure out which crtc index it is: */
571 for (i = 0; i < resources->count_crtcs; i++) {
572 if (c->crtc == resources->crtcs[i]) {
580 static struct kms_bo *
581 allocate_buffer(struct kms_driver *kms,
582 int width, int height, int *stride)
585 unsigned bo_attribs[] = {
588 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
589 KMS_TERMINATE_PROP_LIST
593 bo_attribs[1] = width;
594 bo_attribs[3] = height;
596 ret = kms_bo_create(kms, bo_attribs, &bo);
598 fprintf(stderr, "failed to alloc buffer: %s\n",
603 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
605 fprintf(stderr, "failed to retreive buffer stride: %s\n",
615 make_pwetty(void *data, int width, int height, int stride)
618 cairo_surface_t *surface;
622 surface = cairo_image_surface_create_for_data(data,
626 cr = cairo_create(surface);
627 cairo_surface_destroy(surface);
629 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
630 for (x = 0; x < width; x += 250)
631 for (y = 0; y < height; y += 250) {
634 cairo_move_to(cr, x, y - 20);
635 cairo_line_to(cr, x, y + 20);
636 cairo_move_to(cr, x - 20, y);
637 cairo_line_to(cr, x + 20, y);
638 cairo_new_sub_path(cr);
639 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
640 cairo_set_line_width(cr, 4);
641 cairo_set_source_rgb(cr, 0, 0, 0);
642 cairo_stroke_preserve(cr);
643 cairo_set_source_rgb(cr, 1, 1, 1);
644 cairo_set_line_width(cr, 2);
647 snprintf(buf, sizeof buf, "%d, %d", x, y);
648 cairo_move_to(cr, x + 20, y + 20);
649 cairo_text_path(cr, buf);
650 cairo_set_source_rgb(cr, 0, 0, 0);
651 cairo_stroke_preserve(cr);
652 cairo_set_source_rgb(cr, 1, 1, 1);
660 /* -----------------------------------------------------------------------------
664 /* swap these for big endian.. */
674 static const struct format_name format_names[] = {
675 { DRM_FORMAT_YUYV, "YUYV" },
676 { DRM_FORMAT_NV12, "NV12" },
677 { DRM_FORMAT_YVU420, "YV12" },
678 { DRM_FORMAT_XRGB1555, "XR15" },
679 { DRM_FORMAT_XRGB8888, "XR24" },
680 { DRM_FORMAT_ARGB1555, "AR15" },
683 unsigned int format_fourcc(const char *name)
686 for (i = 0; i < ARRAY_SIZE(format_names); i++) {
687 if (!strcmp(format_names[i].name, name))
688 return format_names[i].format;
694 fill420(unsigned char *y, unsigned char *u, unsigned char *v,
695 int cs /*chroma pixel stride */,
696 int n, int width, int height, int stride)
700 /* paint the buffer with colored tiles, in blocks of 2x2 */
701 for (j = 0; j < height; j+=2) {
702 unsigned char *y1p = y + j * stride;
703 unsigned char *y2p = y1p + stride;
704 unsigned char *up = u + (j/2) * stride * cs / 2;
705 unsigned char *vp = v + (j/2) * stride * cs / 2;
707 for (i = 0; i < width; i+=2) {
708 div_t d = div(n+i+j, width);
709 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
710 unsigned char *rgbp = (unsigned char *)&rgb;
711 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
713 *(y2p++) = *(y1p++) = y;
714 *(y2p++) = *(y1p++) = y;
716 *up = (rgbp[BLUE] - y) * 0.565 + 128;
717 *vp = (rgbp[RED] - y) * 0.713 + 128;
725 fill422(unsigned char *virtual, int n, int width, int height, int stride)
728 /* paint the buffer with colored tiles */
729 for (j = 0; j < height; j++) {
730 uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride);
731 for (i = 0; i < width; i++) {
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]);
738 *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
740 *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
746 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
749 /* paint the buffer with colored tiles */
750 for (j = 0; j < height; j++) {
751 uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride);
752 for (i = 0; i < width; i++) {
753 div_t d = div(n+i+j, width);
754 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
755 unsigned char *rgbp = (unsigned char *)&rgb;
758 (rgbp[RED] >> 3) << 10 |
759 (rgbp[GREEN] >> 3) << 5 |
766 fill8888(unsigned char *virtual, int width, int height, int stride)
769 /* paint the buffer with colored tiles */
770 for (j = 0; j < height; j++) {
771 uint32_t *ptr = (uint32_t*)((char*)virtual + j * stride);
772 for (i = 0; i < width; i++) {
773 div_t d = div(i, width);
775 0x00130502 * (d.quot >> 6) +
776 0x000a1120 * (d.rem >> 6);
780 make_pwetty(virtual, width, height, stride);
784 fill_grey(unsigned char *virtual, int width, int height, int stride)
786 memset(virtual, 0x77, stride * height);
789 static struct kms_bo *
790 create_test_buffer(struct kms_driver *kms, unsigned int format,
791 int width, int height, int handles[4],
792 int pitches[4], int offsets[4], int grey)
798 bo = allocate_buffer(kms, width, height, &pitches[0]);
802 ret = kms_bo_map(bo, &virtual);
804 fprintf(stderr, "failed to map buffer: %s\n",
810 /* just testing a limited # of formats to test single
811 * and multi-planar path.. would be nice to add more..
814 case DRM_FORMAT_YUYV:
815 pitches[0] = width * 2;
817 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
819 fill422(virtual, 0, width, height, pitches[0]);
822 case DRM_FORMAT_NV12:
825 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
827 offsets[1] = width * height;
828 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
830 fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
831 2, 0, width, height, pitches[0]);
834 case DRM_FORMAT_YVU420:
837 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
838 pitches[1] = width / 2;
839 offsets[1] = width * height;
840 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
841 pitches[2] = width / 2;
842 offsets[2] = offsets[1] + (width * height) / 4;
843 kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
845 fill420(virtual, virtual+offsets[1], virtual+offsets[2],
846 1, 0, width, height, pitches[0]);
849 case DRM_FORMAT_XRGB1555:
850 pitches[0] = width * 2;
852 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
854 fill1555(virtual, 0, width, height, pitches[0]);
857 case DRM_FORMAT_XRGB8888:
858 pitches[0] = width * 4;
860 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
863 fill_grey(virtual, width, height, pitches[0]);
865 fill8888(virtual, width, height, pitches[0]);
868 case DRM_FORMAT_ARGB1555:
869 pitches[0] = width * 2;
871 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
873 fill1555(virtual, 0, width, height, pitches[0]);
882 /* -------------------------------------------------------------------------- */
885 page_flip_handler(int fd, unsigned int frame,
886 unsigned int sec, unsigned int usec, void *data)
889 unsigned int new_fb_id;
894 if (c->current_fb_id == c->fb_id[0])
895 new_fb_id = c->fb_id[1];
897 new_fb_id = c->fb_id[0];
899 drmModePageFlip(fd, c->crtc, new_fb_id,
900 DRM_MODE_PAGE_FLIP_EVENT, c);
901 c->current_fb_id = new_fb_id;
903 if (c->swap_count == 60) {
904 gettimeofday(&end, NULL);
905 t = end.tv_sec + end.tv_usec * 1e-6 -
906 (c->start.tv_sec + c->start.tv_usec * 1e-6);
907 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
914 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
916 drmModePlaneRes *plane_resources;
918 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
919 uint32_t plane_id = 0;
920 struct kms_bo *plane_bo;
921 uint32_t plane_flags = 0, format;
922 int ret, crtc_x, crtc_y, crtc_w, crtc_h;
925 format = format_fourcc(p->format_str);
927 fprintf(stderr, "Unknown format: %s\n", p->format_str);
931 /* find an unused plane which can be connected to our crtc */
932 plane_resources = drmModeGetPlaneResources(fd);
933 if (!plane_resources) {
934 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
939 for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
940 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
942 fprintf(stderr, "drmModeGetPlane failed: %s\n",
947 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
948 plane_id = ovr->plane_id;
950 drmModeFreePlane(ovr);
953 fprintf(stderr, "testing %dx%d@%s overlay plane\n",
954 p->w, p->h, p->format_str);
957 fprintf(stderr, "failed to find plane!\n");
961 plane_bo = create_test_buffer(kms, format, p->w, p->h, handles,
962 pitches, offsets, 0);
963 if (plane_bo == NULL)
966 /* just use single plane format for now.. */
967 if (drmModeAddFB2(fd, p->w, p->h, format,
968 handles, pitches, offsets, &p->fb_id, plane_flags)) {
969 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
973 /* ok, boring.. but for now put in middle of screen: */
974 crtc_x = c->mode->hdisplay / 3;
975 crtc_y = c->mode->vdisplay / 3;
979 /* note src coords (last 4 args) are in Q16 format */
980 if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
981 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
982 0, 0, p->w << 16, p->h << 16)) {
983 fprintf(stderr, "failed to enable plane: %s\n",
992 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
995 struct kms_driver *kms;
996 struct kms_bo *bo, *other_bo;
997 unsigned int fb_id, other_fb_id;
998 int i, j, ret, width, height, x;
999 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1000 drmEventContext evctx;
1004 for (i = 0; i < count; i++) {
1005 connector_find_mode(&c[i]);
1006 if (c[i].mode == NULL)
1008 width += c[i].mode->hdisplay;
1009 if (height < c[i].mode->vdisplay)
1010 height = c[i].mode->vdisplay;
1013 ret = kms_create(fd, &kms);
1015 fprintf(stderr, "failed to create kms driver: %s\n",
1020 bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1021 pitches, offsets, 0);
1025 ret = drmModeAddFB(fd, width, height, 24, 32, pitches[0], handles[0], &fb_id);
1027 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1028 width, height, strerror(errno));
1033 for (i = 0; i < count; i++) {
1034 if (c[i].mode == NULL)
1037 printf("setting mode %s on connector %d, crtc %d\n",
1038 c[i].mode_str, c[i].id, c[i].crtc);
1040 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1041 &c[i].id, 1, c[i].mode);
1043 /* XXX: Actually check if this is needed */
1044 drmModeDirtyFB(fd, fb_id, NULL, 0);
1046 x += c[i].mode->hdisplay;
1049 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1053 /* if we have a plane/overlay to show, set that up now: */
1054 for (j = 0; j < plane_count; j++)
1055 if (p[j].con_id == c[i].id)
1056 if (set_plane(kms, &c[i], &p[j]))
1063 other_bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1064 pitches, offsets, 1);
1065 if (other_bo == NULL)
1068 ret = drmModeAddFB(fd, width, height, 32, 32, pitches[0], handles[0],
1071 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1075 for (i = 0; i < count; i++) {
1076 if (c[i].mode == NULL)
1079 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1080 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1082 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1085 gettimeofday(&c[i].start, NULL);
1086 c[i].swap_count = 0;
1087 c[i].fb_id[0] = fb_id;
1088 c[i].fb_id[1] = other_fb_id;
1089 c[i].current_fb_id = other_fb_id;
1092 memset(&evctx, 0, sizeof evctx);
1093 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1094 evctx.vblank_handler = NULL;
1095 evctx.page_flip_handler = page_flip_handler;
1099 struct pollfd pfd[2];
1102 pfd[0].events = POLLIN;
1104 pfd[1].events = POLLIN;
1106 if (poll(pfd, 2, -1) < 0) {
1107 fprintf(stderr, "poll error\n");
1114 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1121 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1124 fprintf(stderr, "select timed out or error (ret %d)\n",
1127 } else if (FD_ISSET(0, &fds)) {
1132 drmHandleEvent(fd, &evctx);
1135 kms_bo_destroy(&bo);
1136 kms_bo_destroy(&other_bo);
1140 extern char *optarg;
1141 extern int optind, opterr, optopt;
1142 static char optstr[] = "ecpmfs:P:v";
1144 void usage(char *name)
1146 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1147 fprintf(stderr, "\t-e\tlist encoders\n");
1148 fprintf(stderr, "\t-c\tlist connectors\n");
1149 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1150 fprintf(stderr, "\t-m\tlist modes\n");
1151 fprintf(stderr, "\t-f\tlist framebuffers\n");
1152 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1153 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1154 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1155 fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1156 fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1157 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1161 #define dump_resource(res) if (res) dump_##res()
1163 static int page_flipping_supported(void)
1165 /*FIXME: generic ioctl needed? */
1169 struct drm_i915_getparam gp;
1171 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1174 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1176 fprintf(stderr, "drm_i915_getparam: %m\n");
1184 int main(int argc, char **argv)
1187 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1189 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1191 int count = 0, plane_count = 0;
1192 struct connector con_args[2];
1193 struct plane plane_args[2] = {0};
1196 while ((c = getopt(argc, argv, optstr)) != -1) {
1218 con_args[count].crtc = -1;
1219 if (sscanf(optarg, "%d:%64s",
1220 &con_args[count].id,
1221 con_args[count].mode_str) != 2 &&
1222 sscanf(optarg, "%d@%d:%64s",
1223 &con_args[count].id,
1224 &con_args[count].crtc,
1225 con_args[count].mode_str) != 3)
1230 strcpy(plane_args[plane_count].format_str, "XR24");
1231 if (sscanf(optarg, "%d:%dx%d@%4s",
1232 &plane_args[plane_count].con_id,
1233 &plane_args[plane_count].w,
1234 &plane_args[plane_count].h,
1235 plane_args[plane_count].format_str) != 4 &&
1236 sscanf(optarg, "%d:%dx%d",
1237 &plane_args[plane_count].con_id,
1238 &plane_args[plane_count].w,
1239 &plane_args[plane_count].h) != 3)
1250 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1252 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1253 printf("trying to load module %s...", modules[i]);
1254 fd = drmOpen(modules[i], NULL);
1256 printf("failed.\n");
1258 printf("success.\n");
1263 if (test_vsync && !page_flipping_supported()) {
1264 fprintf(stderr, "page flipping not supported by drm.\n");
1268 if (i == ARRAY_SIZE(modules)) {
1269 fprintf(stderr, "failed to load any modules, aborting.\n");
1273 resources = drmModeGetResources(fd);
1275 fprintf(stderr, "drmModeGetResources failed: %s\n",
1281 dump_resource(encoders);
1282 dump_resource(connectors);
1283 dump_resource(crtcs);
1284 dump_resource(planes);
1285 dump_resource(framebuffers);
1288 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1292 drmModeFreeResources(resources);