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;
114 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
121 #define type_name_fn(res) \
122 const char * res##_str(int type) { \
124 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
125 if (res##_names[i].type == type) \
126 return res##_names[i].name; \
128 return "(invalid)"; \
131 struct type_name encoder_type_names[] = {
132 { DRM_MODE_ENCODER_NONE, "none" },
133 { DRM_MODE_ENCODER_DAC, "DAC" },
134 { DRM_MODE_ENCODER_TMDS, "TMDS" },
135 { DRM_MODE_ENCODER_LVDS, "LVDS" },
136 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
139 static type_name_fn(encoder_type)
141 struct type_name connector_status_names[] = {
142 { DRM_MODE_CONNECTED, "connected" },
143 { DRM_MODE_DISCONNECTED, "disconnected" },
144 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
147 static type_name_fn(connector_status)
149 struct type_name connector_type_names[] = {
150 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
151 { DRM_MODE_CONNECTOR_VGA, "VGA" },
152 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
153 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
154 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
155 { DRM_MODE_CONNECTOR_Composite, "composite" },
156 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
157 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
158 { DRM_MODE_CONNECTOR_Component, "component" },
159 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
160 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
161 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
162 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
163 { DRM_MODE_CONNECTOR_TV, "TV" },
164 { DRM_MODE_CONNECTOR_eDP, "eDP" },
167 static type_name_fn(connector_type)
169 #define bit_name_fn(res) \
170 const char * res##_str(int type) { \
172 const char *sep = ""; \
173 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
174 if (type & (1 << i)) { \
175 printf("%s%s", sep, res##_names[i]); \
182 static const char *mode_type_names[] = {
192 static bit_name_fn(mode_type)
194 static const char *mode_flag_names[] = {
211 static bit_name_fn(mode_flag)
213 static void dump_encoders(struct device *dev)
215 drmModeEncoder *encoder;
218 printf("Encoders:\n");
219 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
220 for (i = 0; i < dev->resources->res->count_encoders; i++) {
221 encoder = dev->resources->encoders[i].encoder;
225 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
228 encoder_type_str(encoder->encoder_type),
229 encoder->possible_crtcs,
230 encoder->possible_clones);
235 static void dump_mode(drmModeModeInfo *mode)
237 printf(" %s %d %d %d %d %d %d %d %d %d",
250 mode_flag_str(mode->flags);
252 mode_type_str(mode->type);
256 static void dump_blob(struct device *dev, uint32_t blob_id)
259 unsigned char *blob_data;
260 drmModePropertyBlobPtr blob;
262 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
266 blob_data = blob->data;
268 for (i = 0; i < blob->length; i++) {
271 printf("%.2hhx", blob_data[i]);
275 drmModeFreePropertyBlob(blob);
278 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
279 uint32_t prop_id, uint64_t value)
282 printf("\t%d", prop_id);
288 printf(" %s:\n", prop->name);
290 printf("\t\tflags:");
291 if (prop->flags & DRM_MODE_PROP_PENDING)
293 if (prop->flags & DRM_MODE_PROP_RANGE)
295 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
296 printf(" immutable");
297 if (prop->flags & DRM_MODE_PROP_ENUM)
299 if (prop->flags & DRM_MODE_PROP_BITMASK)
301 if (prop->flags & DRM_MODE_PROP_BLOB)
305 if (prop->flags & DRM_MODE_PROP_RANGE) {
306 printf("\t\tvalues:");
307 for (i = 0; i < prop->count_values; i++)
308 printf(" %"PRIu64, prop->values[i]);
312 if (prop->flags & DRM_MODE_PROP_ENUM) {
313 printf("\t\tenums:");
314 for (i = 0; i < prop->count_enums; i++)
315 printf(" %s=%llu", prop->enums[i].name,
316 prop->enums[i].value);
318 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
319 printf("\t\tvalues:");
320 for (i = 0; i < prop->count_enums; i++)
321 printf(" %s=0x%llx", prop->enums[i].name,
322 (1LL << prop->enums[i].value));
325 assert(prop->count_enums == 0);
328 if (prop->flags & DRM_MODE_PROP_BLOB) {
329 printf("\t\tblobs:\n");
330 for (i = 0; i < prop->count_blobs; i++)
331 dump_blob(dev, prop->blob_ids[i]);
334 assert(prop->count_blobs == 0);
337 printf("\t\tvalue:");
338 if (prop->flags & DRM_MODE_PROP_BLOB)
339 dump_blob(dev, value);
341 printf(" %"PRIu64"\n", value);
344 static void dump_connectors(struct device *dev)
348 printf("Connectors:\n");
349 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
350 for (i = 0; i < dev->resources->res->count_connectors; i++) {
351 struct connector *_connector = &dev->resources->connectors[i];
352 drmModeConnector *connector = _connector->connector;
356 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
357 connector->connector_id,
358 connector->encoder_id,
359 connector_status_str(connector->connection),
360 connector_type_str(connector->connector_type),
361 connector->mmWidth, connector->mmHeight,
362 connector->count_modes);
364 for (j = 0; j < connector->count_encoders; j++)
365 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
368 if (connector->count_modes) {
370 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
372 for (j = 0; j < connector->count_modes; j++)
373 dump_mode(&connector->modes[j]);
376 if (_connector->props) {
378 for (j = 0; j < (int)_connector->props->count_props; j++)
379 dump_prop(dev, _connector->props_info[j],
380 _connector->props->props[j],
381 _connector->props->prop_values[j]);
387 static void dump_crtcs(struct device *dev)
393 printf("id\tfb\tpos\tsize\n");
394 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
395 struct crtc *_crtc = &dev->resources->crtcs[i];
396 drmModeCrtc *crtc = _crtc->crtc;
400 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
404 crtc->width, crtc->height);
405 dump_mode(&crtc->mode);
409 for (j = 0; j < _crtc->props->count_props; j++)
410 dump_prop(dev, _crtc->props_info[j],
411 _crtc->props->props[j],
412 _crtc->props->prop_values[j]);
414 printf(" no properties found\n");
420 static void dump_framebuffers(struct device *dev)
425 printf("Frame buffers:\n");
426 printf("id\tsize\tpitch\n");
427 for (i = 0; i < dev->resources->res->count_fbs; i++) {
428 fb = dev->resources->fbs[i].fb;
432 printf("%u\t(%ux%u)\t%u\n",
434 fb->width, fb->height,
440 static void dump_planes(struct device *dev)
445 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
447 if (!dev->resources->plane_res)
450 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
451 struct plane *plane = &dev->resources->planes[i];
452 drmModePlane *ovr = plane->plane;
456 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
457 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
458 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
459 ovr->gamma_size, ovr->possible_crtcs);
461 if (!ovr->count_formats)
465 for (j = 0; j < ovr->count_formats; j++)
466 printf(" %4.4s", (char *)&ovr->formats[j]);
471 for (j = 0; j < plane->props->count_props; j++)
472 dump_prop(dev, plane->props_info[j],
473 plane->props->props[j],
474 plane->props->prop_values[j]);
476 printf(" no properties found\n");
484 static void free_resources(struct resources *res)
489 #define free_resource(_res, __res, type, Type) \
492 if (!(_res)->type##s) \
494 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
495 if (!(_res)->type##s[i].type) \
497 drmModeFree##Type((_res)->type##s[i].type); \
499 free((_res)->type##s); \
502 #define free_properties(_res, __res, type) \
505 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
506 drmModeFreeObjectProperties(res->type##s[i].props); \
507 free(res->type##s[i].props_info); \
512 free_properties(res, res, crtc);
514 free_resource(res, res, crtc, Crtc);
515 free_resource(res, res, encoder, Encoder);
516 free_resource(res, res, connector, Connector);
517 free_resource(res, res, fb, FB);
519 drmModeFreeResources(res->res);
522 if (res->plane_res) {
523 free_properties(res, plane_res, plane);
525 free_resource(res, plane_res, plane, Plane);
527 drmModeFreePlaneResources(res->plane_res);
533 static struct resources *get_resources(struct device *dev)
535 struct resources *res;
538 res = malloc(sizeof *res);
542 memset(res, 0, sizeof *res);
544 res->res = drmModeGetResources(dev->fd);
546 fprintf(stderr, "drmModeGetResources failed: %s\n",
551 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
552 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
553 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
554 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
556 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
559 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
560 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
561 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
562 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
564 #define get_resource(_res, __res, type, Type) \
567 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
568 (_res)->type##s[i].type = \
569 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
570 if (!(_res)->type##s[i].type) \
571 fprintf(stderr, "could not get %s %i: %s\n", \
572 #type, (_res)->__res->type##s[i], \
577 get_resource(res, res, crtc, Crtc);
578 get_resource(res, res, encoder, Encoder);
579 get_resource(res, res, connector, Connector);
580 get_resource(res, res, fb, FB);
582 #define get_properties(_res, __res, type, Type) \
585 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
586 struct type *obj = &res->type##s[i]; \
589 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
590 DRM_MODE_OBJECT_##Type); \
593 "could not get %s %i properties: %s\n", \
594 #type, obj->type->type##_id, \
598 obj->props_info = malloc(obj->props->count_props * \
599 sizeof *obj->props_info); \
600 if (!obj->props_info) \
602 for (j = 0; j < obj->props->count_props; ++j) \
603 obj->props_info[j] = \
604 drmModeGetProperty(dev->fd, obj->props->props[j]); \
608 get_properties(res, res, crtc, CRTC);
609 get_properties(res, res, connector, CONNECTOR);
611 for (i = 0; i < res->res->count_crtcs; ++i)
612 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
614 res->plane_res = drmModeGetPlaneResources(dev->fd);
615 if (!res->plane_res) {
616 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
621 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
625 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
627 get_resource(res, plane_res, plane, Plane);
628 get_properties(res, plane_res, plane, PLANE);
637 /* -----------------------------------------------------------------------------
638 * Connectors and planes
642 * Mode setting with the kernel interfaces is a bit of a chore.
643 * First you have to find the connector in question and make sure the
644 * requested mode is available.
645 * Then you need to find the encoder attached to that connector so you
646 * can bind it with a free crtc.
648 struct connector_arg {
654 drmModeModeInfo *mode;
656 unsigned int fb_id[2], current_fb_id;
657 struct timeval start;
663 uint32_t crtc_id; /* the id of CRTC to bind to */
668 char format_str[5]; /* need to leave room for terminating \0 */
672 static void connector_find_mode(struct device *dev, struct connector_arg *c)
674 drmModeConnector *connector;
675 drmModeEncoder *encoder;
678 /* First, find the connector & mode */
680 for (i = 0; i < dev->resources->res->count_connectors; i++) {
681 connector = dev->resources->connectors[i].connector;
685 if (!connector->count_modes)
688 if (connector->connector_id != c->id)
691 for (j = 0; j < connector->count_modes; j++) {
692 c->mode = &connector->modes[j];
693 if (!strcmp(c->mode->name, c->mode_str))
697 /* Found it, break out */
703 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
707 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
708 * locate a CRTC that can be attached to the connector.
710 if (c->crtc_id == (uint32_t)-1) {
711 for (i = 0; i < dev->resources->res->count_encoders; i++) {
712 encoder = dev->resources->encoders[i].encoder;
716 if (encoder->encoder_id == connector->encoder_id) {
717 c->crtc_id = encoder->crtc_id;
723 if (c->crtc_id == (uint32_t)-1)
726 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
727 struct crtc *crtc = &dev->resources->crtcs[i];
729 if (c->crtc_id == crtc->crtc->crtc_id) {
730 crtc->mode = c->mode;
737 /* -----------------------------------------------------------------------------
741 struct property_arg {
744 char name[DRM_PROP_NAME_LEN+1];
749 static void set_property(struct device *dev, struct property_arg *p)
751 drmModeObjectProperties *props;
752 drmModePropertyRes **props_info;
753 const char *obj_type;
760 #define find_object(_res, __res, type, Type) \
762 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
763 struct type *obj = &(_res)->type##s[i]; \
764 if (obj->type->type##_id != p->obj_id) \
766 p->obj_type = DRM_MODE_OBJECT_##Type; \
768 props = obj->props; \
769 props_info = obj->props_info; \
773 find_object(dev->resources, res, crtc, CRTC);
774 if (p->obj_type == 0)
775 find_object(dev->resources, res, connector, CONNECTOR);
776 if (p->obj_type == 0)
777 find_object(dev->resources, plane_res, plane, PLANE);
778 if (p->obj_type == 0) {
779 fprintf(stderr, "Object %i not found, can't set property\n",
785 fprintf(stderr, "%s %i has no properties\n",
786 obj_type, p->obj_id);
790 for (i = 0; i < (int)props->count_props; ++i) {
793 if (strcmp(props_info[i]->name, p->name) == 0)
797 if (i == (int)props->count_props) {
798 fprintf(stderr, "%s %i has no %s property\n",
799 obj_type, p->obj_id, p->name);
803 p->prop_id = props->props[i];
805 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
806 p->prop_id, p->value);
808 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
809 obj_type, p->obj_id, p->name, p->value, strerror(errno));
812 /* -------------------------------------------------------------------------- */
815 page_flip_handler(int fd, unsigned int frame,
816 unsigned int sec, unsigned int usec, void *data)
818 struct connector_arg *c;
819 unsigned int new_fb_id;
824 if (c->current_fb_id == c->fb_id[0])
825 new_fb_id = c->fb_id[1];
827 new_fb_id = c->fb_id[0];
829 drmModePageFlip(fd, c->crtc_id, new_fb_id,
830 DRM_MODE_PAGE_FLIP_EVENT, c);
831 c->current_fb_id = new_fb_id;
833 if (c->swap_count == 60) {
834 gettimeofday(&end, NULL);
835 t = end.tv_sec + end.tv_usec * 1e-6 -
836 (c->start.tv_sec + c->start.tv_usec * 1e-6);
837 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
843 static int set_plane(struct device *dev, struct plane_arg *p)
846 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
847 uint32_t plane_id = 0;
848 struct kms_bo *plane_bo;
849 uint32_t plane_flags = 0;
850 int crtc_x, crtc_y, crtc_w, crtc_h;
851 struct crtc *crtc = NULL;
855 /* Find an unused plane which can be connected to our CRTC. Find the
856 * CRTC index first, then iterate over available planes.
858 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
859 if (p->crtc_id == dev->resources->res->crtcs[i]) {
860 crtc = &dev->resources->crtcs[i];
867 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
871 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
872 ovr = dev->resources->planes[i].plane;
876 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
877 plane_id = ovr->plane_id;
881 fprintf(stderr, "no unused plane available for CRTC %u\n",
882 crtc->crtc->crtc_id);
886 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
887 p->w, p->h, p->format_str, plane_id);
889 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
890 pitches, offsets, PATTERN_TILES);
891 if (plane_bo == NULL)
894 /* just use single plane format for now.. */
895 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
896 handles, pitches, offsets, &p->fb_id, plane_flags)) {
897 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
901 if (!p->has_position) {
902 /* Default to the middle of the screen */
903 crtc_x = (crtc->mode->hdisplay - p->w) / 2;
904 crtc_y = (crtc->mode->vdisplay - p->h) / 2;
912 /* note src coords (last 4 args) are in Q16 format */
913 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
914 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
915 0, 0, p->w << 16, p->h << 16)) {
916 fprintf(stderr, "failed to enable plane: %s\n",
921 ovr->crtc_id = crtc->crtc->crtc_id;
926 static void set_mode(struct device *dev, struct connector_arg *c, unsigned int count)
928 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
935 dev->mode.height = 0;
937 for (i = 0; i < count; i++) {
938 connector_find_mode(dev, &c[i]);
939 if (c[i].mode == NULL)
941 dev->mode.width += c[i].mode->hdisplay;
942 if (dev->mode.height < c[i].mode->vdisplay)
943 dev->mode.height = c[i].mode->vdisplay;
946 bo = create_test_buffer(dev->kms, c->fourcc,
947 dev->mode.width, dev->mode.height,
948 handles, pitches, offsets, PATTERN_SMPTE);
952 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height, c->fourcc,
953 handles, pitches, offsets, &fb_id, 0);
955 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
956 dev->mode.width, dev->mode.height, strerror(errno));
961 for (i = 0; i < count; i++) {
962 if (c[i].mode == NULL)
965 printf("setting mode %s@%s on connector %d, crtc %d\n",
966 c[i].mode_str, c[i].format_str, c[i].id, c[i].crtc_id);
968 ret = drmModeSetCrtc(dev->fd, c[i].crtc_id, fb_id, x, 0,
969 &c[i].id, 1, c[i].mode);
971 /* XXX: Actually check if this is needed */
972 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
974 x += c[i].mode->hdisplay;
977 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
983 dev->mode.fb_id = fb_id;
986 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
990 /* set up planes/overlays */
991 for (i = 0; i < count; i++)
992 if (set_plane(dev, &p[i]))
996 static void test_page_flip(struct device *dev, struct connector_arg *c, unsigned int count)
998 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
999 unsigned int other_fb_id;
1000 struct kms_bo *other_bo;
1001 drmEventContext evctx;
1005 other_bo = create_test_buffer(dev->kms, c->fourcc,
1006 dev->mode.width, dev->mode.height,
1007 handles, pitches, offsets, PATTERN_PLAIN);
1008 if (other_bo == NULL)
1011 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height, c->fourcc,
1012 handles, pitches, offsets, &other_fb_id, 0);
1014 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1018 for (i = 0; i < count; i++) {
1019 if (c[i].mode == NULL)
1022 ret = drmModePageFlip(dev->fd, c[i].crtc_id, other_fb_id,
1023 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1025 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1028 gettimeofday(&c[i].start, NULL);
1029 c[i].swap_count = 0;
1030 c[i].fb_id[0] = dev->mode.fb_id;
1031 c[i].fb_id[1] = other_fb_id;
1032 c[i].current_fb_id = other_fb_id;
1035 memset(&evctx, 0, sizeof evctx);
1036 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1037 evctx.vblank_handler = NULL;
1038 evctx.page_flip_handler = page_flip_handler;
1042 struct pollfd pfd[2];
1045 pfd[0].events = POLLIN;
1047 pfd[1].events = POLLIN;
1049 if (poll(pfd, 2, -1) < 0) {
1050 fprintf(stderr, "poll error\n");
1057 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1063 FD_SET(dev->fd, &fds);
1064 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1067 fprintf(stderr, "select timed out or error (ret %d)\n",
1070 } else if (FD_ISSET(0, &fds)) {
1075 drmHandleEvent(dev->fd, &evctx);
1078 kms_bo_destroy(&other_bo);
1081 #define min(a, b) ((a) < (b) ? (a) : (b))
1083 static int parse_connector(struct connector_arg *c, const char *arg)
1089 c->crtc_id = (uint32_t)-1;
1090 strcpy(c->format_str, "XR24");
1092 c->id = strtoul(arg, &endp, 10);
1095 c->crtc_id = strtoul(arg, &endp, 10);
1102 p = strchrnul(arg, '@');
1103 len = min(sizeof c->mode_str - 1, (unsigned int)(p - arg));
1104 strncpy(c->mode_str, arg, len);
1105 c->mode_str[len] = '\0';
1108 strncpy(c->format_str, p + 1, 4);
1109 c->format_str[4] = '\0';
1112 c->fourcc = format_fourcc(c->format_str);
1113 if (c->fourcc == 0) {
1114 fprintf(stderr, "unknown format %s\n", c->format_str);
1121 static int parse_plane(struct plane_arg *plane, const char *p)
1125 memset(plane, 0, sizeof *plane);
1127 plane->crtc_id = strtoul(p, &end, 10);
1132 plane->w = strtoul(p, &end, 10);
1137 plane->h = strtoul(p, &end, 10);
1139 if (*end == '+' || *end == '-') {
1140 plane->x = strtol(end, &end, 10);
1141 if (*end != '+' && *end != '-')
1143 plane->y = strtol(end, &end, 10);
1145 plane->has_position = true;
1153 strcpy(plane->format_str, p);
1155 strcpy(plane->format_str, "XR24");
1158 plane->fourcc = format_fourcc(plane->format_str);
1159 if (plane->fourcc == 0) {
1160 fprintf(stderr, "unknown format %s\n", plane->format_str);
1167 static int parse_property(struct property_arg *p, const char *arg)
1169 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1173 p->name[DRM_PROP_NAME_LEN] = '\0';
1178 static void usage(char *name)
1180 fprintf(stderr, "usage: %s [-cdefMmPpsvw]\n", name);
1182 fprintf(stderr, "\n Query options:\n\n");
1183 fprintf(stderr, "\t-c\tlist connectors\n");
1184 fprintf(stderr, "\t-e\tlist encoders\n");
1185 fprintf(stderr, "\t-f\tlist framebuffers\n");
1186 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1188 fprintf(stderr, "\n Test options:\n\n");
1189 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][@<format>]\tset a plane\n");
1190 fprintf(stderr, "\t-s <connector_id>[@<crtc_id>]:<mode>[@<format>]\tset a mode\n");
1191 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1192 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1194 fprintf(stderr, "\n Generic options:\n\n");
1195 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1196 fprintf(stderr, "\t-M module\tuse the given driver\n");
1198 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1202 static int page_flipping_supported(void)
1204 /*FIXME: generic ioctl needed? */
1208 struct drm_i915_getparam gp;
1210 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1213 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1215 fprintf(stderr, "drm_i915_getparam: %m\n");
1223 static char optstr[] = "cdefM:P:ps:vw:";
1225 int main(int argc, char **argv)
1230 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1231 int drop_master = 0;
1233 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc" };
1234 char *module = NULL;
1236 int count = 0, plane_count = 0;
1237 unsigned int prop_count = 0;
1238 struct connector_arg *con_args = NULL;
1239 struct plane_arg *plane_args = NULL;
1240 struct property_arg *prop_args = NULL;
1241 unsigned int args = 0;
1244 memset(&dev, 0, sizeof dev);
1247 while ((c = getopt(argc, argv, optstr)) != -1) {
1265 /* Preserve the default behaviour of dumping all information. */
1269 plane_args = realloc(plane_args,
1270 (plane_count + 1) * sizeof *plane_args);
1271 if (plane_args == NULL) {
1272 fprintf(stderr, "memory allocation failed\n");
1276 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1286 con_args = realloc(con_args,
1287 (count + 1) * sizeof *con_args);
1288 if (con_args == NULL) {
1289 fprintf(stderr, "memory allocation failed\n");
1293 if (parse_connector(&con_args[count], optarg) < 0)
1302 prop_args = realloc(prop_args,
1303 (prop_count + 1) * sizeof *prop_args);
1304 if (prop_args == NULL) {
1305 fprintf(stderr, "memory allocation failed\n");
1309 if (parse_property(&prop_args[prop_count], optarg) < 0)
1321 encoders = connectors = crtcs = planes = framebuffers = 1;
1324 dev.fd = drmOpen(module, NULL);
1326 fprintf(stderr, "failed to open device '%s'.\n", module);
1330 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1331 printf("trying to open device '%s'...", modules[i]);
1332 dev.fd = drmOpen(modules[i], NULL);
1334 printf("failed.\n");
1336 printf("success.\n");
1342 fprintf(stderr, "no device found.\n");
1347 if (test_vsync && !page_flipping_supported()) {
1348 fprintf(stderr, "page flipping not supported by drm.\n");
1352 if (test_vsync && !count) {
1353 fprintf(stderr, "page flipping requires at least one -s option.\n");
1357 dev.resources = get_resources(&dev);
1358 if (!dev.resources) {
1363 #define dump_resource(dev, res) if (res) dump_##res(dev)
1365 dump_resource(&dev, encoders);
1366 dump_resource(&dev, connectors);
1367 dump_resource(&dev, crtcs);
1368 dump_resource(&dev, planes);
1369 dump_resource(&dev, framebuffers);
1371 for (i = 0; i < prop_count; ++i)
1372 set_property(&dev, &prop_args[i]);
1374 if (count || plane_count) {
1375 ret = kms_create(dev.fd, &dev.kms);
1377 fprintf(stderr, "failed to create kms driver: %s\n",
1383 set_mode(&dev, con_args, count);
1386 set_planes(&dev, plane_args, plane_count);
1389 test_page_flip(&dev, con_args, count);
1392 drmDropMaster(dev.fd);
1394 kms_bo_destroy(&dev.mode.bo);
1395 kms_destroy(&dev.kms);
1400 free_resources(dev.resources);