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.
58 #include "xf86drmMode.h"
59 #include "drm_fourcc.h"
66 drmModeObjectProperties *props;
67 drmModePropertyRes **props_info;
68 drmModeModeInfo *mode;
72 drmModeEncoder *encoder;
76 drmModeConnector *connector;
77 drmModeObjectProperties *props;
78 drmModePropertyRes **props_info;
87 drmModeObjectProperties *props;
88 drmModePropertyRes **props_info;
93 drmModePlaneRes *plane_res;
96 struct encoder *encoders;
97 struct connector *connectors;
105 struct resources *resources;
116 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
117 static inline int64_t U642I64(uint64_t val)
119 return (int64_t)*((int64_t *)&val);
127 #define type_name_fn(res) \
128 const char * res##_str(int type) { \
130 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
131 if (res##_names[i].type == type) \
132 return res##_names[i].name; \
134 return "(invalid)"; \
137 struct type_name encoder_type_names[] = {
138 { DRM_MODE_ENCODER_NONE, "none" },
139 { DRM_MODE_ENCODER_DAC, "DAC" },
140 { DRM_MODE_ENCODER_TMDS, "TMDS" },
141 { DRM_MODE_ENCODER_LVDS, "LVDS" },
142 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
145 static type_name_fn(encoder_type)
147 struct type_name connector_status_names[] = {
148 { DRM_MODE_CONNECTED, "connected" },
149 { DRM_MODE_DISCONNECTED, "disconnected" },
150 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
153 static type_name_fn(connector_status)
155 struct type_name connector_type_names[] = {
156 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
157 { DRM_MODE_CONNECTOR_VGA, "VGA" },
158 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
159 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
160 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
161 { DRM_MODE_CONNECTOR_Composite, "composite" },
162 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
163 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
164 { DRM_MODE_CONNECTOR_Component, "component" },
165 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
166 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
167 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
168 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
169 { DRM_MODE_CONNECTOR_TV, "TV" },
170 { DRM_MODE_CONNECTOR_eDP, "eDP" },
173 static type_name_fn(connector_type)
175 #define bit_name_fn(res) \
176 const char * res##_str(int type) { \
178 const char *sep = ""; \
179 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
180 if (type & (1 << i)) { \
181 printf("%s%s", sep, res##_names[i]); \
188 static const char *mode_type_names[] = {
198 static bit_name_fn(mode_type)
200 static const char *mode_flag_names[] = {
217 static bit_name_fn(mode_flag)
219 static void dump_encoders(struct device *dev)
221 drmModeEncoder *encoder;
224 printf("Encoders:\n");
225 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
226 for (i = 0; i < dev->resources->res->count_encoders; i++) {
227 encoder = dev->resources->encoders[i].encoder;
231 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
234 encoder_type_str(encoder->encoder_type),
235 encoder->possible_crtcs,
236 encoder->possible_clones);
241 static void dump_mode(drmModeModeInfo *mode)
243 printf(" %s %d %d %d %d %d %d %d %d %d",
256 mode_flag_str(mode->flags);
258 mode_type_str(mode->type);
262 static void dump_blob(struct device *dev, uint32_t blob_id)
265 unsigned char *blob_data;
266 drmModePropertyBlobPtr blob;
268 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
274 blob_data = blob->data;
276 for (i = 0; i < blob->length; i++) {
279 printf("%.2hhx", blob_data[i]);
283 drmModeFreePropertyBlob(blob);
286 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
287 uint32_t prop_id, uint64_t value)
290 printf("\t%d", prop_id);
296 printf(" %s:\n", prop->name);
298 printf("\t\tflags:");
299 if (prop->flags & DRM_MODE_PROP_PENDING)
301 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
302 printf(" immutable");
303 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
304 printf(" signed range");
305 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
307 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
309 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
311 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
313 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
317 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
318 printf("\t\tvalues:");
319 for (i = 0; i < prop->count_values; i++)
320 printf(" %"PRId64, U642I64(prop->values[i]));
324 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
325 printf("\t\tvalues:");
326 for (i = 0; i < prop->count_values; i++)
327 printf(" %"PRIu64, prop->values[i]);
331 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
332 printf("\t\tenums:");
333 for (i = 0; i < prop->count_enums; i++)
334 printf(" %s=%llu", prop->enums[i].name,
335 prop->enums[i].value);
337 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
338 printf("\t\tvalues:");
339 for (i = 0; i < prop->count_enums; i++)
340 printf(" %s=0x%llx", prop->enums[i].name,
341 (1LL << prop->enums[i].value));
344 assert(prop->count_enums == 0);
347 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
348 printf("\t\tblobs:\n");
349 for (i = 0; i < prop->count_blobs; i++)
350 dump_blob(dev, prop->blob_ids[i]);
353 assert(prop->count_blobs == 0);
356 printf("\t\tvalue:");
357 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
358 dump_blob(dev, value);
360 printf(" %"PRIu64"\n", value);
363 static void dump_connectors(struct device *dev)
367 printf("Connectors:\n");
368 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
369 for (i = 0; i < dev->resources->res->count_connectors; i++) {
370 struct connector *_connector = &dev->resources->connectors[i];
371 drmModeConnector *connector = _connector->connector;
375 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
376 connector->connector_id,
377 connector->encoder_id,
378 connector_status_str(connector->connection),
379 connector_type_str(connector->connector_type),
380 connector->mmWidth, connector->mmHeight,
381 connector->count_modes);
383 for (j = 0; j < connector->count_encoders; j++)
384 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
387 if (connector->count_modes) {
389 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
391 for (j = 0; j < connector->count_modes; j++)
392 dump_mode(&connector->modes[j]);
395 if (_connector->props) {
397 for (j = 0; j < (int)_connector->props->count_props; j++)
398 dump_prop(dev, _connector->props_info[j],
399 _connector->props->props[j],
400 _connector->props->prop_values[j]);
406 static void dump_crtcs(struct device *dev)
412 printf("id\tfb\tpos\tsize\n");
413 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
414 struct crtc *_crtc = &dev->resources->crtcs[i];
415 drmModeCrtc *crtc = _crtc->crtc;
419 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
423 crtc->width, crtc->height);
424 dump_mode(&crtc->mode);
428 for (j = 0; j < _crtc->props->count_props; j++)
429 dump_prop(dev, _crtc->props_info[j],
430 _crtc->props->props[j],
431 _crtc->props->prop_values[j]);
433 printf(" no properties found\n");
439 static void dump_framebuffers(struct device *dev)
444 printf("Frame buffers:\n");
445 printf("id\tsize\tpitch\n");
446 for (i = 0; i < dev->resources->res->count_fbs; i++) {
447 fb = dev->resources->fbs[i].fb;
451 printf("%u\t(%ux%u)\t%u\n",
453 fb->width, fb->height,
459 static void dump_planes(struct device *dev)
464 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
466 if (!dev->resources->plane_res)
469 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
470 struct plane *plane = &dev->resources->planes[i];
471 drmModePlane *ovr = plane->plane;
475 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
476 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
477 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
478 ovr->gamma_size, ovr->possible_crtcs);
480 if (!ovr->count_formats)
484 for (j = 0; j < ovr->count_formats; j++)
485 printf(" %4.4s", (char *)&ovr->formats[j]);
490 for (j = 0; j < plane->props->count_props; j++)
491 dump_prop(dev, plane->props_info[j],
492 plane->props->props[j],
493 plane->props->prop_values[j]);
495 printf(" no properties found\n");
503 static void free_resources(struct resources *res)
508 #define free_resource(_res, __res, type, Type) \
511 if (!(_res)->type##s) \
513 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
514 if (!(_res)->type##s[i].type) \
516 drmModeFree##Type((_res)->type##s[i].type); \
518 free((_res)->type##s); \
521 #define free_properties(_res, __res, type) \
524 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
525 drmModeFreeObjectProperties(res->type##s[i].props); \
526 free(res->type##s[i].props_info); \
531 free_properties(res, res, crtc);
533 free_resource(res, res, crtc, Crtc);
534 free_resource(res, res, encoder, Encoder);
535 free_resource(res, res, connector, Connector);
536 free_resource(res, res, fb, FB);
538 drmModeFreeResources(res->res);
541 if (res->plane_res) {
542 free_properties(res, plane_res, plane);
544 free_resource(res, plane_res, plane, Plane);
546 drmModeFreePlaneResources(res->plane_res);
552 static struct resources *get_resources(struct device *dev)
554 struct resources *res;
557 res = malloc(sizeof *res);
561 memset(res, 0, sizeof *res);
563 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
565 res->res = drmModeGetResources(dev->fd);
567 fprintf(stderr, "drmModeGetResources failed: %s\n",
572 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
573 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
574 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
575 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
577 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
580 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
581 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
582 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
583 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
585 #define get_resource(_res, __res, type, Type) \
588 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
589 (_res)->type##s[i].type = \
590 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
591 if (!(_res)->type##s[i].type) \
592 fprintf(stderr, "could not get %s %i: %s\n", \
593 #type, (_res)->__res->type##s[i], \
598 get_resource(res, res, crtc, Crtc);
599 get_resource(res, res, encoder, Encoder);
600 get_resource(res, res, connector, Connector);
601 get_resource(res, res, fb, FB);
603 #define get_properties(_res, __res, type, Type) \
606 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
607 struct type *obj = &res->type##s[i]; \
610 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
611 DRM_MODE_OBJECT_##Type); \
614 "could not get %s %i properties: %s\n", \
615 #type, obj->type->type##_id, \
619 obj->props_info = malloc(obj->props->count_props * \
620 sizeof *obj->props_info); \
621 if (!obj->props_info) \
623 for (j = 0; j < obj->props->count_props; ++j) \
624 obj->props_info[j] = \
625 drmModeGetProperty(dev->fd, obj->props->props[j]); \
629 get_properties(res, res, crtc, CRTC);
630 get_properties(res, res, connector, CONNECTOR);
632 for (i = 0; i < res->res->count_crtcs; ++i)
633 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
635 res->plane_res = drmModeGetPlaneResources(dev->fd);
636 if (!res->plane_res) {
637 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
642 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
646 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
648 get_resource(res, plane_res, plane, Plane);
649 get_properties(res, plane_res, plane, PLANE);
658 static int get_crtc_index(struct device *dev, uint32_t id)
662 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
663 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
664 if (crtc && crtc->crtc_id == id)
671 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
673 drmModeConnector *connector;
676 for (i = 0; i < dev->resources->res->count_connectors; i++) {
677 connector = dev->resources->connectors[i].connector;
678 if (connector && connector->connector_id == id)
685 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
687 drmModeEncoder *encoder;
690 for (i = 0; i < dev->resources->res->count_encoders; i++) {
691 encoder = dev->resources->encoders[i].encoder;
692 if (encoder && encoder->encoder_id == id)
699 /* -----------------------------------------------------------------------------
704 * Mode setting with the kernel interfaces is a bit of a chore.
705 * First you have to find the connector in question and make sure the
706 * requested mode is available.
707 * Then you need to find the encoder attached to that connector so you
708 * can bind it with a free crtc.
712 unsigned int num_cons;
716 unsigned int vrefresh;
718 drmModeModeInfo *mode;
720 unsigned int fb_id[2], current_fb_id;
721 struct timeval start;
727 uint32_t crtc_id; /* the id of CRTC to bind to */
733 char format_str[5]; /* need to leave room for terminating \0 */
737 static drmModeModeInfo *
738 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
739 const unsigned int vrefresh)
741 drmModeConnector *connector;
742 drmModeModeInfo *mode;
745 connector = get_connector_by_id(dev, con_id);
746 if (!connector || !connector->count_modes)
749 for (i = 0; i < connector->count_modes; i++) {
750 mode = &connector->modes[i];
751 if (!strcmp(mode->name, mode_str)) {
752 /* If the vertical refresh frequency is not specified then return the
753 * first mode that match with the name. Else, return the mode that match
754 * the name and the specified vertical refresh frequency.
758 else if (mode->vrefresh == vrefresh)
766 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
768 uint32_t possible_crtcs = ~0;
769 uint32_t active_crtcs = 0;
770 unsigned int crtc_idx;
774 for (i = 0; i < pipe->num_cons; ++i) {
775 uint32_t crtcs_for_connector = 0;
776 drmModeConnector *connector;
777 drmModeEncoder *encoder;
780 connector = get_connector_by_id(dev, pipe->con_ids[i]);
784 for (j = 0; j < connector->count_encoders; ++j) {
785 encoder = get_encoder_by_id(dev, connector->encoders[j]);
789 crtcs_for_connector |= encoder->possible_crtcs;
791 idx = get_crtc_index(dev, encoder->crtc_id);
793 active_crtcs |= 1 << idx;
796 possible_crtcs &= crtcs_for_connector;
802 /* Return the first possible and active CRTC if one exists, or the first
803 * possible CRTC otherwise.
805 if (possible_crtcs & active_crtcs)
806 crtc_idx = ffs(possible_crtcs & active_crtcs);
808 crtc_idx = ffs(possible_crtcs);
810 return &dev->resources->crtcs[crtc_idx - 1];
813 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
815 drmModeModeInfo *mode = NULL;
820 for (i = 0; i < (int)pipe->num_cons; i++) {
821 mode = connector_find_mode(dev, pipe->con_ids[i],
822 pipe->mode_str, pipe->vrefresh);
825 "failed to find mode \"%s\" for connector %u\n",
826 pipe->mode_str, pipe->con_ids[i]);
831 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
832 * locate a CRTC that can be attached to all the connectors.
834 if (pipe->crtc_id != (uint32_t)-1) {
835 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
836 struct crtc *crtc = &dev->resources->crtcs[i];
838 if (pipe->crtc_id == crtc->crtc->crtc_id) {
844 pipe->crtc = pipe_find_crtc(dev, pipe);
848 fprintf(stderr, "failed to find CRTC for pipe\n");
853 pipe->crtc->mode = mode;
858 /* -----------------------------------------------------------------------------
862 struct property_arg {
865 char name[DRM_PROP_NAME_LEN+1];
870 static void set_property(struct device *dev, struct property_arg *p)
872 drmModeObjectProperties *props = NULL;
873 drmModePropertyRes **props_info = NULL;
874 const char *obj_type;
881 #define find_object(_res, __res, type, Type) \
883 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
884 struct type *obj = &(_res)->type##s[i]; \
885 if (obj->type->type##_id != p->obj_id) \
887 p->obj_type = DRM_MODE_OBJECT_##Type; \
889 props = obj->props; \
890 props_info = obj->props_info; \
894 find_object(dev->resources, res, crtc, CRTC);
895 if (p->obj_type == 0)
896 find_object(dev->resources, res, connector, CONNECTOR);
897 if (p->obj_type == 0)
898 find_object(dev->resources, plane_res, plane, PLANE);
899 if (p->obj_type == 0) {
900 fprintf(stderr, "Object %i not found, can't set property\n",
906 fprintf(stderr, "%s %i has no properties\n",
907 obj_type, p->obj_id);
911 for (i = 0; i < (int)props->count_props; ++i) {
914 if (strcmp(props_info[i]->name, p->name) == 0)
918 if (i == (int)props->count_props) {
919 fprintf(stderr, "%s %i has no %s property\n",
920 obj_type, p->obj_id, p->name);
924 p->prop_id = props->props[i];
926 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
927 p->prop_id, p->value);
929 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
930 obj_type, p->obj_id, p->name, p->value, strerror(errno));
933 /* -------------------------------------------------------------------------- */
936 page_flip_handler(int fd, unsigned int frame,
937 unsigned int sec, unsigned int usec, void *data)
939 struct pipe_arg *pipe;
940 unsigned int new_fb_id;
945 if (pipe->current_fb_id == pipe->fb_id[0])
946 new_fb_id = pipe->fb_id[1];
948 new_fb_id = pipe->fb_id[0];
950 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
951 DRM_MODE_PAGE_FLIP_EVENT, pipe);
952 pipe->current_fb_id = new_fb_id;
954 if (pipe->swap_count == 60) {
955 gettimeofday(&end, NULL);
956 t = end.tv_sec + end.tv_usec * 1e-6 -
957 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
958 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
959 pipe->swap_count = 0;
964 static int set_plane(struct device *dev, struct plane_arg *p)
967 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
968 uint32_t plane_id = 0;
970 uint32_t plane_flags = 0;
971 int crtc_x, crtc_y, crtc_w, crtc_h;
972 struct crtc *crtc = NULL;
976 /* Find an unused plane which can be connected to our CRTC. Find the
977 * CRTC index first, then iterate over available planes.
979 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
980 if (p->crtc_id == dev->resources->res->crtcs[i]) {
981 crtc = &dev->resources->crtcs[i];
988 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
992 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
993 ovr = dev->resources->planes[i].plane;
997 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
998 plane_id = ovr->plane_id;
1002 fprintf(stderr, "no unused plane available for CRTC %u\n",
1003 crtc->crtc->crtc_id);
1007 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1008 p->w, p->h, p->format_str, plane_id);
1010 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1011 pitches, offsets, PATTERN_TILES);
1012 if (plane_bo == NULL)
1015 /* just use single plane format for now.. */
1016 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1017 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1018 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1022 crtc_w = p->w * p->scale;
1023 crtc_h = p->h * p->scale;
1024 if (!p->has_position) {
1025 /* Default to the middle of the screen */
1026 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1027 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1033 /* note src coords (last 4 args) are in Q16 format */
1034 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1035 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1036 0, 0, p->w << 16, p->h << 16)) {
1037 fprintf(stderr, "failed to enable plane: %s\n",
1042 ovr->crtc_id = crtc->crtc->crtc_id;
1047 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1049 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1056 dev->mode.width = 0;
1057 dev->mode.height = 0;
1059 for (i = 0; i < count; i++) {
1060 struct pipe_arg *pipe = &pipes[i];
1062 ret = pipe_find_crtc_and_mode(dev, pipe);
1066 dev->mode.width += pipe->mode->hdisplay;
1067 if (dev->mode.height < pipe->mode->vdisplay)
1068 dev->mode.height = pipe->mode->vdisplay;
1071 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1072 handles, pitches, offsets, PATTERN_SMPTE);
1076 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1077 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1079 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1080 dev->mode.width, dev->mode.height, strerror(errno));
1085 for (i = 0; i < count; i++) {
1086 struct pipe_arg *pipe = &pipes[i];
1088 if (pipe->mode == NULL)
1091 printf("setting mode %s-%dHz@%s on connectors ",
1092 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1093 for (j = 0; j < pipe->num_cons; ++j)
1094 printf("%u, ", pipe->con_ids[j]);
1095 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1097 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1098 x, 0, pipe->con_ids, pipe->num_cons,
1101 /* XXX: Actually check if this is needed */
1102 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1104 x += pipe->mode->hdisplay;
1107 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1113 dev->mode.fb_id = fb_id;
1116 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1120 /* set up planes/overlays */
1121 for (i = 0; i < count; i++)
1122 if (set_plane(dev, &p[i]))
1126 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1128 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1133 /* maybe make cursor width/height configurable some day */
1137 /* create cursor bo.. just using PATTERN_PLAIN as it has
1140 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1141 offsets, PATTERN_PLAIN);
1145 for (i = 0; i < count; i++) {
1146 struct pipe_arg *pipe = &pipes[i];
1147 ret = cursor_init(dev->fd, handles[0],
1148 pipe->crtc->crtc->crtc_id,
1149 pipe->mode->hdisplay, pipe->mode->vdisplay,
1152 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1161 static void clear_cursors(struct device *dev)
1166 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1168 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1169 unsigned int other_fb_id;
1170 struct bo *other_bo;
1171 drmEventContext evctx;
1175 other_bo = bo_create(dev->fd, pipes[0].fourcc,
1176 dev->mode.width, dev->mode.height,
1177 handles, pitches, offsets, PATTERN_PLAIN);
1178 if (other_bo == NULL)
1181 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1182 pipes[0].fourcc, handles, pitches, offsets,
1185 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1189 for (i = 0; i < count; i++) {
1190 struct pipe_arg *pipe = &pipes[i];
1192 if (pipe->mode == NULL)
1195 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1196 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1199 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1202 gettimeofday(&pipe->start, NULL);
1203 pipe->swap_count = 0;
1204 pipe->fb_id[0] = dev->mode.fb_id;
1205 pipe->fb_id[1] = other_fb_id;
1206 pipe->current_fb_id = other_fb_id;
1209 memset(&evctx, 0, sizeof evctx);
1210 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1211 evctx.vblank_handler = NULL;
1212 evctx.page_flip_handler = page_flip_handler;
1216 struct pollfd pfd[2];
1219 pfd[0].events = POLLIN;
1221 pfd[1].events = POLLIN;
1223 if (poll(pfd, 2, -1) < 0) {
1224 fprintf(stderr, "poll error\n");
1231 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1237 FD_SET(dev->fd, &fds);
1238 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1241 fprintf(stderr, "select timed out or error (ret %d)\n",
1244 } else if (FD_ISSET(0, &fds)) {
1249 drmHandleEvent(dev->fd, &evctx);
1252 bo_destroy(other_bo);
1255 #define min(a, b) ((a) < (b) ? (a) : (b))
1257 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1265 pipe->crtc_id = (uint32_t)-1;
1266 strcpy(pipe->format_str, "XR24");
1268 /* Count the number of connectors and allocate them. */
1270 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1275 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1276 if (pipe->con_ids == NULL)
1279 /* Parse the connectors. */
1280 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1281 pipe->con_ids[i] = strtoul(p, &endp, 10);
1286 if (i != pipe->num_cons - 1)
1289 /* Parse the remaining parameters. */
1292 pipe->crtc_id = strtoul(arg, &endp, 10);
1299 /* Search for the vertical refresh or the format. */
1300 p = strpbrk(arg, "-@");
1302 p = arg + strlen(arg);
1303 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1304 strncpy(pipe->mode_str, arg, len);
1305 pipe->mode_str[len] = '\0';
1308 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1313 strncpy(pipe->format_str, p + 1, 4);
1314 pipe->format_str[4] = '\0';
1317 pipe->fourcc = format_fourcc(pipe->format_str);
1318 if (pipe->fourcc == 0) {
1319 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1326 static int parse_plane(struct plane_arg *plane, const char *p)
1330 memset(plane, 0, sizeof *plane);
1332 plane->crtc_id = strtoul(p, &end, 10);
1337 plane->w = strtoul(p, &end, 10);
1342 plane->h = strtoul(p, &end, 10);
1344 if (*end == '+' || *end == '-') {
1345 plane->x = strtol(end, &end, 10);
1346 if (*end != '+' && *end != '-')
1348 plane->y = strtol(end, &end, 10);
1350 plane->has_position = true;
1355 plane->scale = strtod(p, &end);
1356 if (plane->scale <= 0.0)
1367 strcpy(plane->format_str, p);
1369 strcpy(plane->format_str, "XR24");
1372 plane->fourcc = format_fourcc(plane->format_str);
1373 if (plane->fourcc == 0) {
1374 fprintf(stderr, "unknown format %s\n", plane->format_str);
1381 static int parse_property(struct property_arg *p, const char *arg)
1383 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1387 p->name[DRM_PROP_NAME_LEN] = '\0';
1392 static void usage(char *name)
1394 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1396 fprintf(stderr, "\n Query options:\n\n");
1397 fprintf(stderr, "\t-c\tlist connectors\n");
1398 fprintf(stderr, "\t-e\tlist encoders\n");
1399 fprintf(stderr, "\t-f\tlist framebuffers\n");
1400 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1402 fprintf(stderr, "\n Test options:\n\n");
1403 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1404 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1405 fprintf(stderr, "\t-C\ttest hw cursor\n");
1406 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1407 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1409 fprintf(stderr, "\n Generic options:\n\n");
1410 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1411 fprintf(stderr, "\t-M module\tuse the given driver\n");
1412 fprintf(stderr, "\t-D device\tuse the given device\n");
1414 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1418 static int page_flipping_supported(void)
1420 /*FIXME: generic ioctl needed? */
1424 struct drm_i915_getparam gp;
1426 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1429 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1431 fprintf(stderr, "drm_i915_getparam: %m\n");
1439 static int cursor_supported(void)
1441 /*FIXME: generic ioctl needed? */
1445 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1447 int main(int argc, char **argv)
1452 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1453 int drop_master = 0;
1455 int test_cursor = 0;
1456 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm", "sti", "tegra", "imx-drm", "rockchip" };
1457 char *device = NULL;
1458 char *module = NULL;
1460 int count = 0, plane_count = 0;
1461 unsigned int prop_count = 0;
1462 struct pipe_arg *pipe_args = NULL;
1463 struct plane_arg *plane_args = NULL;
1464 struct property_arg *prop_args = NULL;
1465 unsigned int args = 0;
1468 memset(&dev, 0, sizeof dev);
1471 while ((c = getopt(argc, argv, optstr)) != -1) {
1493 /* Preserve the default behaviour of dumping all information. */
1497 plane_args = realloc(plane_args,
1498 (plane_count + 1) * sizeof *plane_args);
1499 if (plane_args == NULL) {
1500 fprintf(stderr, "memory allocation failed\n");
1504 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1514 pipe_args = realloc(pipe_args,
1515 (count + 1) * sizeof *pipe_args);
1516 if (pipe_args == NULL) {
1517 fprintf(stderr, "memory allocation failed\n");
1521 if (parse_connector(&pipe_args[count], optarg) < 0)
1533 prop_args = realloc(prop_args,
1534 (prop_count + 1) * sizeof *prop_args);
1535 if (prop_args == NULL) {
1536 fprintf(stderr, "memory allocation failed\n");
1540 if (parse_property(&prop_args[prop_count], optarg) < 0)
1552 encoders = connectors = crtcs = planes = framebuffers = 1;
1555 dev.fd = drmOpen(module, device);
1557 fprintf(stderr, "failed to open device '%s'.\n", module);
1561 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1562 printf("trying to open device '%s'...", modules[i]);
1563 dev.fd = drmOpen(modules[i], device);
1565 printf("failed.\n");
1567 printf("success.\n");
1573 fprintf(stderr, "no device found.\n");
1578 if (test_vsync && !page_flipping_supported()) {
1579 fprintf(stderr, "page flipping not supported by drm.\n");
1583 if (test_vsync && !count) {
1584 fprintf(stderr, "page flipping requires at least one -s option.\n");
1588 if (test_cursor && !cursor_supported()) {
1589 fprintf(stderr, "hw cursor not supported by drm.\n");
1593 dev.resources = get_resources(&dev);
1594 if (!dev.resources) {
1599 #define dump_resource(dev, res) if (res) dump_##res(dev)
1601 dump_resource(&dev, encoders);
1602 dump_resource(&dev, connectors);
1603 dump_resource(&dev, crtcs);
1604 dump_resource(&dev, planes);
1605 dump_resource(&dev, framebuffers);
1607 for (i = 0; i < prop_count; ++i)
1608 set_property(&dev, &prop_args[i]);
1610 if (count || plane_count) {
1613 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1614 if (ret || cap == 0) {
1615 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1620 set_mode(&dev, pipe_args, count);
1623 set_planes(&dev, plane_args, plane_count);
1626 set_cursors(&dev, pipe_args, count);
1629 test_page_flip(&dev, pipe_args, count);
1632 drmDropMaster(dev.fd);
1637 clear_cursors(&dev);
1639 bo_destroy(dev.mode.bo);
1642 free_resources(dev.resources);