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.
55 #include "xf86drmMode.h"
56 #include "drm_fourcc.h"
63 drmModeObjectProperties *props;
64 drmModePropertyRes **props_info;
65 drmModeModeInfo *mode;
69 drmModeEncoder *encoder;
73 drmModeConnector *connector;
74 drmModeObjectProperties *props;
75 drmModePropertyRes **props_info;
84 drmModeObjectProperties *props;
85 drmModePropertyRes **props_info;
90 drmModePlaneRes *plane_res;
93 struct encoder *encoders;
94 struct connector *connectors;
102 struct resources *resources;
103 struct kms_driver *kms;
106 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
113 #define type_name_fn(res) \
114 const char * res##_str(int type) { \
116 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
117 if (res##_names[i].type == type) \
118 return res##_names[i].name; \
120 return "(invalid)"; \
123 struct type_name encoder_type_names[] = {
124 { DRM_MODE_ENCODER_NONE, "none" },
125 { DRM_MODE_ENCODER_DAC, "DAC" },
126 { DRM_MODE_ENCODER_TMDS, "TMDS" },
127 { DRM_MODE_ENCODER_LVDS, "LVDS" },
128 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
131 static type_name_fn(encoder_type)
133 struct type_name connector_status_names[] = {
134 { DRM_MODE_CONNECTED, "connected" },
135 { DRM_MODE_DISCONNECTED, "disconnected" },
136 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
139 static type_name_fn(connector_status)
141 struct type_name connector_type_names[] = {
142 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
143 { DRM_MODE_CONNECTOR_VGA, "VGA" },
144 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
145 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
146 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
147 { DRM_MODE_CONNECTOR_Composite, "composite" },
148 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
149 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
150 { DRM_MODE_CONNECTOR_Component, "component" },
151 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
152 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
153 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
154 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
155 { DRM_MODE_CONNECTOR_TV, "TV" },
156 { DRM_MODE_CONNECTOR_eDP, "eDP" },
159 static type_name_fn(connector_type)
161 #define bit_name_fn(res) \
162 const char * res##_str(int type) { \
164 const char *sep = ""; \
165 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
166 if (type & (1 << i)) { \
167 printf("%s%s", sep, res##_names[i]); \
174 static const char *mode_type_names[] = {
184 static bit_name_fn(mode_type)
186 static const char *mode_flag_names[] = {
203 static bit_name_fn(mode_flag)
205 static void dump_encoders(struct device *dev)
207 drmModeEncoder *encoder;
210 printf("Encoders:\n");
211 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
212 for (i = 0; i < dev->resources->res->count_encoders; i++) {
213 encoder = dev->resources->encoders[i].encoder;
217 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
220 encoder_type_str(encoder->encoder_type),
221 encoder->possible_crtcs,
222 encoder->possible_clones);
227 static void dump_mode(drmModeModeInfo *mode)
229 printf(" %s %d %d %d %d %d %d %d %d %d",
242 mode_flag_str(mode->flags);
244 mode_type_str(mode->type);
248 static void dump_blob(struct device *dev, uint32_t blob_id)
251 unsigned char *blob_data;
252 drmModePropertyBlobPtr blob;
254 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
258 blob_data = blob->data;
260 for (i = 0; i < blob->length; i++) {
263 printf("%.2hhx", blob_data[i]);
267 drmModeFreePropertyBlob(blob);
270 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
271 uint32_t prop_id, uint64_t value)
274 printf("\t%d", prop_id);
280 printf(" %s:\n", prop->name);
282 printf("\t\tflags:");
283 if (prop->flags & DRM_MODE_PROP_PENDING)
285 if (prop->flags & DRM_MODE_PROP_RANGE)
287 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
288 printf(" immutable");
289 if (prop->flags & DRM_MODE_PROP_ENUM)
291 if (prop->flags & DRM_MODE_PROP_BITMASK)
293 if (prop->flags & DRM_MODE_PROP_BLOB)
297 if (prop->flags & DRM_MODE_PROP_RANGE) {
298 printf("\t\tvalues:");
299 for (i = 0; i < prop->count_values; i++)
300 printf(" %"PRIu64, prop->values[i]);
304 if (prop->flags & DRM_MODE_PROP_ENUM) {
305 printf("\t\tenums:");
306 for (i = 0; i < prop->count_enums; i++)
307 printf(" %s=%llu", prop->enums[i].name,
308 prop->enums[i].value);
310 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
311 printf("\t\tvalues:");
312 for (i = 0; i < prop->count_enums; i++)
313 printf(" %s=0x%llx", prop->enums[i].name,
314 (1LL << prop->enums[i].value));
317 assert(prop->count_enums == 0);
320 if (prop->flags & DRM_MODE_PROP_BLOB) {
321 printf("\t\tblobs:\n");
322 for (i = 0; i < prop->count_blobs; i++)
323 dump_blob(dev, prop->blob_ids[i]);
326 assert(prop->count_blobs == 0);
329 printf("\t\tvalue:");
330 if (prop->flags & DRM_MODE_PROP_BLOB)
331 dump_blob(dev, value);
333 printf(" %"PRIu64"\n", value);
336 static void dump_connectors(struct device *dev)
340 printf("Connectors:\n");
341 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
342 for (i = 0; i < dev->resources->res->count_connectors; i++) {
343 struct connector *_connector = &dev->resources->connectors[i];
344 drmModeConnector *connector = _connector->connector;
348 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
349 connector->connector_id,
350 connector->encoder_id,
351 connector_status_str(connector->connection),
352 connector_type_str(connector->connector_type),
353 connector->mmWidth, connector->mmHeight,
354 connector->count_modes);
356 for (j = 0; j < connector->count_encoders; j++)
357 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
360 if (connector->count_modes) {
362 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
364 for (j = 0; j < connector->count_modes; j++)
365 dump_mode(&connector->modes[j]);
368 if (_connector->props) {
370 for (j = 0; j < (int)_connector->props->count_props; j++)
371 dump_prop(dev, _connector->props_info[j],
372 _connector->props->props[j],
373 _connector->props->prop_values[j]);
379 static void dump_crtcs(struct device *dev)
385 printf("id\tfb\tpos\tsize\n");
386 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
387 struct crtc *_crtc = &dev->resources->crtcs[i];
388 drmModeCrtc *crtc = _crtc->crtc;
392 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
396 crtc->width, crtc->height);
397 dump_mode(&crtc->mode);
401 for (j = 0; j < _crtc->props->count_props; j++)
402 dump_prop(dev, _crtc->props_info[j],
403 _crtc->props->props[j],
404 _crtc->props->prop_values[j]);
406 printf(" no properties found\n");
412 static void dump_framebuffers(struct device *dev)
417 printf("Frame buffers:\n");
418 printf("id\tsize\tpitch\n");
419 for (i = 0; i < dev->resources->res->count_fbs; i++) {
420 fb = dev->resources->fbs[i].fb;
424 printf("%u\t(%ux%u)\t%u\n",
426 fb->width, fb->height,
432 static void dump_planes(struct device *dev)
437 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
439 if (!dev->resources->plane_res)
442 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
443 struct plane *plane = &dev->resources->planes[i];
444 drmModePlane *ovr = plane->plane;
448 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
449 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
450 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
451 ovr->gamma_size, ovr->possible_crtcs);
453 if (!ovr->count_formats)
457 for (j = 0; j < ovr->count_formats; j++)
458 printf(" %4.4s", (char *)&ovr->formats[j]);
463 for (j = 0; j < plane->props->count_props; j++)
464 dump_prop(dev, plane->props_info[j],
465 plane->props->props[j],
466 plane->props->prop_values[j]);
468 printf(" no properties found\n");
476 static void free_resources(struct resources *res)
481 #define free_resource(_res, __res, type, Type) \
484 if (!(_res)->type##s) \
486 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
487 if (!(_res)->type##s[i].type) \
489 drmModeFree##Type((_res)->type##s[i].type); \
491 free((_res)->type##s); \
494 #define free_properties(_res, __res, type) \
497 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
498 drmModeFreeObjectProperties(res->type##s[i].props); \
499 free(res->type##s[i].props_info); \
504 free_properties(res, res, crtc);
506 free_resource(res, res, crtc, Crtc);
507 free_resource(res, res, encoder, Encoder);
508 free_resource(res, res, connector, Connector);
509 free_resource(res, res, fb, FB);
511 drmModeFreeResources(res->res);
514 if (res->plane_res) {
515 free_properties(res, plane_res, plane);
517 free_resource(res, plane_res, plane, Plane);
519 drmModeFreePlaneResources(res->plane_res);
525 static struct resources *get_resources(struct device *dev)
527 struct resources *res;
530 res = malloc(sizeof *res);
534 memset(res, 0, sizeof *res);
536 res->res = drmModeGetResources(dev->fd);
538 fprintf(stderr, "drmModeGetResources failed: %s\n",
543 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
544 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
545 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
546 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
548 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
551 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
552 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
553 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
554 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
556 #define get_resource(_res, __res, type, Type) \
559 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
560 (_res)->type##s[i].type = \
561 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
562 if (!(_res)->type##s[i].type) \
563 fprintf(stderr, "could not get %s %i: %s\n", \
564 #type, (_res)->__res->type##s[i], \
569 get_resource(res, res, crtc, Crtc);
570 get_resource(res, res, encoder, Encoder);
571 get_resource(res, res, connector, Connector);
572 get_resource(res, res, fb, FB);
574 #define get_properties(_res, __res, type, Type) \
577 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
578 struct type *obj = &res->type##s[i]; \
581 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
582 DRM_MODE_OBJECT_##Type); \
585 "could not get %s %i properties: %s\n", \
586 #type, obj->type->type##_id, \
590 obj->props_info = malloc(obj->props->count_props * \
591 sizeof *obj->props_info); \
592 if (!obj->props_info) \
594 for (j = 0; j < obj->props->count_props; ++j) \
595 obj->props_info[j] = \
596 drmModeGetProperty(dev->fd, obj->props->props[j]); \
600 get_properties(res, res, crtc, CRTC);
601 get_properties(res, res, connector, CONNECTOR);
603 for (i = 0; i < res->res->count_crtcs; ++i)
604 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
606 res->plane_res = drmModeGetPlaneResources(dev->fd);
607 if (!res->plane_res) {
608 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
613 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
617 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
619 get_resource(res, plane_res, plane, Plane);
620 get_properties(res, plane_res, plane, PLANE);
629 /* -----------------------------------------------------------------------------
630 * Connectors and planes
634 * Mode setting with the kernel interfaces is a bit of a chore.
635 * First you have to find the connector in question and make sure the
636 * requested mode is available.
637 * Then you need to find the encoder attached to that connector so you
638 * can bind it with a free crtc.
640 struct connector_arg {
646 drmModeModeInfo *mode;
648 unsigned int fb_id[2], current_fb_id;
649 struct timeval start;
655 uint32_t crtc_id; /* the id of CRTC to bind to */
660 char format_str[5]; /* need to leave room for terminating \0 */
664 static void connector_find_mode(struct device *dev, struct connector_arg *c)
666 drmModeConnector *connector;
667 drmModeEncoder *encoder;
670 /* First, find the connector & mode */
672 for (i = 0; i < dev->resources->res->count_connectors; i++) {
673 connector = dev->resources->connectors[i].connector;
677 if (!connector->count_modes)
680 if (connector->connector_id != c->id)
683 for (j = 0; j < connector->count_modes; j++) {
684 c->mode = &connector->modes[j];
685 if (!strcmp(c->mode->name, c->mode_str))
689 /* Found it, break out */
695 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
699 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
700 * locate a CRTC that can be attached to the connector.
702 if (c->crtc_id == (uint32_t)-1) {
703 for (i = 0; i < dev->resources->res->count_encoders; i++) {
704 encoder = dev->resources->encoders[i].encoder;
708 if (encoder->encoder_id == connector->encoder_id) {
709 c->crtc_id = encoder->crtc_id;
715 if (c->crtc_id == (uint32_t)-1)
718 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
719 struct crtc *crtc = &dev->resources->crtcs[i];
721 if (c->crtc_id == crtc->crtc->crtc_id) {
722 crtc->mode = c->mode;
729 /* -----------------------------------------------------------------------------
733 struct property_arg {
736 char name[DRM_PROP_NAME_LEN+1];
741 static void set_property(struct device *dev, struct property_arg *p)
743 drmModeObjectProperties *props;
744 drmModePropertyRes **props_info;
745 const char *obj_type;
752 #define find_object(_res, __res, type, Type) \
754 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
755 struct type *obj = &(_res)->type##s[i]; \
756 if (obj->type->type##_id != p->obj_id) \
758 p->obj_type = DRM_MODE_OBJECT_##Type; \
760 props = obj->props; \
761 props_info = obj->props_info; \
765 find_object(dev->resources, res, crtc, CRTC);
766 if (p->obj_type == 0)
767 find_object(dev->resources, res, connector, CONNECTOR);
768 if (p->obj_type == 0)
769 find_object(dev->resources, plane_res, plane, PLANE);
770 if (p->obj_type == 0) {
771 fprintf(stderr, "Object %i not found, can't set property\n",
777 fprintf(stderr, "%s %i has no properties\n",
778 obj_type, p->obj_id);
782 for (i = 0; i < (int)props->count_props; ++i) {
785 if (strcmp(props_info[i]->name, p->name) == 0)
789 if (i == (int)props->count_props) {
790 fprintf(stderr, "%s %i has no %s property\n",
791 obj_type, p->obj_id, p->name);
795 p->prop_id = props->props[i];
797 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
798 p->prop_id, p->value);
800 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
801 obj_type, p->obj_id, p->name, p->value, strerror(errno));
804 /* -------------------------------------------------------------------------- */
807 page_flip_handler(int fd, unsigned int frame,
808 unsigned int sec, unsigned int usec, void *data)
810 struct connector_arg *c;
811 unsigned int new_fb_id;
816 if (c->current_fb_id == c->fb_id[0])
817 new_fb_id = c->fb_id[1];
819 new_fb_id = c->fb_id[0];
821 drmModePageFlip(fd, c->crtc_id, new_fb_id,
822 DRM_MODE_PAGE_FLIP_EVENT, c);
823 c->current_fb_id = new_fb_id;
825 if (c->swap_count == 60) {
826 gettimeofday(&end, NULL);
827 t = end.tv_sec + end.tv_usec * 1e-6 -
828 (c->start.tv_sec + c->start.tv_usec * 1e-6);
829 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
835 static int set_plane(struct device *dev, struct plane_arg *p)
838 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
839 uint32_t plane_id = 0;
840 struct kms_bo *plane_bo;
841 uint32_t plane_flags = 0;
842 int crtc_x, crtc_y, crtc_w, crtc_h;
847 /* Find an unused plane which can be connected to our CRTC. Find the
848 * CRTC index first, then iterate over available planes.
850 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
851 if (p->crtc_id == dev->resources->res->crtcs[i]) {
852 crtc = &dev->resources->crtcs[i];
859 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
863 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
864 ovr = dev->resources->planes[i].plane;
868 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
869 plane_id = ovr->plane_id;
873 fprintf(stderr, "no unused plane available for CRTC %u\n",
874 crtc->crtc->crtc_id);
878 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
879 p->w, p->h, p->format_str, plane_id);
881 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
882 pitches, offsets, PATTERN_TILES);
883 if (plane_bo == NULL)
886 /* just use single plane format for now.. */
887 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
888 handles, pitches, offsets, &p->fb_id, plane_flags)) {
889 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
893 if (!p->has_position) {
894 /* Default to the middle of the screen */
895 crtc_x = (crtc->mode->hdisplay - p->w) / 2;
896 crtc_y = (crtc->mode->vdisplay - p->h) / 2;
904 /* note src coords (last 4 args) are in Q16 format */
905 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
906 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
907 0, 0, p->w << 16, p->h << 16)) {
908 fprintf(stderr, "failed to enable plane: %s\n",
913 ovr->crtc_id = crtc->crtc->crtc_id;
918 static void set_mode(struct device *dev, struct connector_arg *c, int count,
919 struct plane_arg *p, int plane_count, int page_flip)
921 struct kms_bo *bo, *other_bo;
922 unsigned int fb_id, other_fb_id;
923 int i, j, ret, width, height, x;
924 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
925 drmEventContext evctx;
929 for (i = 0; i < count; i++) {
930 connector_find_mode(dev, &c[i]);
931 if (c[i].mode == NULL)
933 width += c[i].mode->hdisplay;
934 if (height < c[i].mode->vdisplay)
935 height = c[i].mode->vdisplay;
938 bo = create_test_buffer(dev->kms, c->fourcc, width, height, handles,
939 pitches, offsets, PATTERN_SMPTE);
943 ret = drmModeAddFB2(dev->fd, width, height, c->fourcc,
944 handles, pitches, offsets, &fb_id, 0);
946 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
947 width, height, strerror(errno));
952 for (i = 0; i < count; i++) {
953 if (c[i].mode == NULL)
956 printf("setting mode %s@%s on connector %d, crtc %d\n",
957 c[i].mode_str, c[i].format_str, c[i].id, c[i].crtc_id);
959 ret = drmModeSetCrtc(dev->fd, c[i].crtc_id, fb_id, x, 0,
960 &c[i].id, 1, c[i].mode);
962 /* XXX: Actually check if this is needed */
963 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
965 x += c[i].mode->hdisplay;
968 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
972 /* if we have a plane/overlay to show, set that up now: */
973 for (j = 0; j < plane_count; j++)
974 if (p[j].crtc_id == c[i].crtc_id)
975 if (set_plane(dev, &p[j]))
982 other_bo = create_test_buffer(dev->kms, c->fourcc, width, height, handles,
983 pitches, offsets, PATTERN_PLAIN);
984 if (other_bo == NULL)
987 ret = drmModeAddFB2(dev->fd, width, height, c->fourcc, handles, pitches, offsets,
990 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
994 for (i = 0; i < count; i++) {
995 if (c[i].mode == NULL)
998 ret = drmModePageFlip(dev->fd, c[i].crtc_id, other_fb_id,
999 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1001 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1004 gettimeofday(&c[i].start, NULL);
1005 c[i].swap_count = 0;
1006 c[i].fb_id[0] = fb_id;
1007 c[i].fb_id[1] = other_fb_id;
1008 c[i].current_fb_id = other_fb_id;
1011 memset(&evctx, 0, sizeof evctx);
1012 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1013 evctx.vblank_handler = NULL;
1014 evctx.page_flip_handler = page_flip_handler;
1018 struct pollfd pfd[2];
1021 pfd[0].events = POLLIN;
1023 pfd[1].events = POLLIN;
1025 if (poll(pfd, 2, -1) < 0) {
1026 fprintf(stderr, "poll error\n");
1033 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1039 FD_SET(dev->fd, &fds);
1040 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1043 fprintf(stderr, "select timed out or error (ret %d)\n",
1046 } else if (FD_ISSET(0, &fds)) {
1051 drmHandleEvent(dev->fd, &evctx);
1054 kms_bo_destroy(&bo);
1055 kms_bo_destroy(&other_bo);
1058 #define min(a, b) ((a) < (b) ? (a) : (b))
1060 static int parse_connector(struct connector_arg *c, const char *arg)
1066 c->crtc_id = (uint32_t)-1;
1067 strcpy(c->format_str, "XR24");
1069 c->id = strtoul(arg, &endp, 10);
1072 c->crtc_id = strtoul(arg, &endp, 10);
1079 p = strchrnul(arg, '@');
1080 len = min(sizeof c->mode_str - 1, (unsigned int)(p - arg));
1081 strncpy(c->mode_str, arg, len);
1082 c->mode_str[len] = '\0';
1085 strncpy(c->format_str, p + 1, 4);
1086 c->format_str[4] = '\0';
1089 c->fourcc = format_fourcc(c->format_str);
1090 if (c->fourcc == 0) {
1091 fprintf(stderr, "unknown format %s\n", c->format_str);
1098 static int parse_plane(struct plane_arg *plane, const char *p)
1102 memset(plane, 0, sizeof *plane);
1104 plane->crtc_id = strtoul(p, &end, 10);
1109 plane->w = strtoul(p, &end, 10);
1114 plane->h = strtoul(p, &end, 10);
1116 if (*end == '+' || *end == '-') {
1117 plane->x = strtol(end, &end, 10);
1118 if (*end != '+' && *end != '-')
1120 plane->y = strtol(end, &end, 10);
1122 plane->has_position = true;
1130 strcpy(plane->format_str, p);
1132 strcpy(plane->format_str, "XR24");
1135 plane->fourcc = format_fourcc(plane->format_str);
1136 if (plane->fourcc == 0) {
1137 fprintf(stderr, "unknown format %s\n", plane->format_str);
1144 static int parse_property(struct property_arg *p, const char *arg)
1146 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1150 p->name[DRM_PROP_NAME_LEN] = '\0';
1155 static void usage(char *name)
1157 fprintf(stderr, "usage: %s [-cdefMmPpsvw]\n", name);
1159 fprintf(stderr, "\n Query options:\n\n");
1160 fprintf(stderr, "\t-c\tlist connectors\n");
1161 fprintf(stderr, "\t-e\tlist encoders\n");
1162 fprintf(stderr, "\t-f\tlist framebuffers\n");
1163 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1165 fprintf(stderr, "\n Test options:\n\n");
1166 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][@<format>]\tset a plane\n");
1167 fprintf(stderr, "\t-s <connector_id>[@<crtc_id>]:<mode>[@<format>]\tset a mode\n");
1168 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1169 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1171 fprintf(stderr, "\n Generic options:\n\n");
1172 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1173 fprintf(stderr, "\t-M module\tuse the given driver\n");
1175 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1179 static int page_flipping_supported(void)
1181 /*FIXME: generic ioctl needed? */
1185 struct drm_i915_getparam gp;
1187 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1190 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1192 fprintf(stderr, "drm_i915_getparam: %m\n");
1200 static char optstr[] = "cdefM:P:ps:vw:";
1202 int main(int argc, char **argv)
1207 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1208 int drop_master = 0;
1210 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc" };
1211 char *module = NULL;
1213 int count = 0, plane_count = 0;
1214 unsigned int prop_count = 0;
1215 struct connector_arg *con_args = NULL;
1216 struct plane_arg *plane_args = NULL;
1217 struct property_arg *prop_args = NULL;
1218 unsigned int args = 0;
1222 while ((c = getopt(argc, argv, optstr)) != -1) {
1240 /* Preserve the default behaviour of dumping all information. */
1244 plane_args = realloc(plane_args,
1245 (plane_count + 1) * sizeof *plane_args);
1246 if (plane_args == NULL) {
1247 fprintf(stderr, "memory allocation failed\n");
1251 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1261 con_args = realloc(con_args,
1262 (count + 1) * sizeof *con_args);
1263 if (con_args == NULL) {
1264 fprintf(stderr, "memory allocation failed\n");
1268 if (parse_connector(&con_args[count], optarg) < 0)
1277 prop_args = realloc(prop_args,
1278 (prop_count + 1) * sizeof *prop_args);
1279 if (prop_args == NULL) {
1280 fprintf(stderr, "memory allocation failed\n");
1284 if (parse_property(&prop_args[prop_count], optarg) < 0)
1296 encoders = connectors = crtcs = planes = framebuffers = 1;
1299 dev.fd = drmOpen(module, NULL);
1301 fprintf(stderr, "failed to open device '%s'.\n", module);
1305 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1306 printf("trying to open device '%s'...", modules[i]);
1307 dev.fd = drmOpen(modules[i], NULL);
1309 printf("failed.\n");
1311 printf("success.\n");
1317 fprintf(stderr, "no device found.\n");
1322 if (test_vsync && !page_flipping_supported()) {
1323 fprintf(stderr, "page flipping not supported by drm.\n");
1327 dev.resources = get_resources(&dev);
1328 if (!dev.resources) {
1333 #define dump_resource(dev, res) if (res) dump_##res(dev)
1335 dump_resource(&dev, encoders);
1336 dump_resource(&dev, connectors);
1337 dump_resource(&dev, crtcs);
1338 dump_resource(&dev, planes);
1339 dump_resource(&dev, framebuffers);
1341 for (i = 0; i < prop_count; ++i)
1342 set_property(&dev, &prop_args[i]);
1345 ret = kms_create(dev.fd, &dev.kms);
1347 fprintf(stderr, "failed to create kms driver: %s\n",
1352 set_mode(&dev, con_args, count, plane_args, plane_count, test_vsync);
1354 drmDropMaster(dev.fd);
1356 kms_destroy(&dev.kms);
1360 free_resources(dev.resources);