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"
67 drmModeObjectProperties *props;
68 drmModePropertyRes **props_info;
69 drmModeModeInfo *mode;
73 drmModeEncoder *encoder;
77 drmModeConnector *connector;
78 drmModeObjectProperties *props;
79 drmModePropertyRes **props_info;
88 drmModeObjectProperties *props;
89 drmModePropertyRes **props_info;
94 drmModePlaneRes *plane_res;
97 struct encoder *encoders;
98 struct connector *connectors;
100 struct plane *planes;
106 struct resources *resources;
107 struct kms_driver *kms;
118 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
125 #define type_name_fn(res) \
126 const char * res##_str(int type) { \
128 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
129 if (res##_names[i].type == type) \
130 return res##_names[i].name; \
132 return "(invalid)"; \
135 struct type_name encoder_type_names[] = {
136 { DRM_MODE_ENCODER_NONE, "none" },
137 { DRM_MODE_ENCODER_DAC, "DAC" },
138 { DRM_MODE_ENCODER_TMDS, "TMDS" },
139 { DRM_MODE_ENCODER_LVDS, "LVDS" },
140 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
143 static type_name_fn(encoder_type)
145 struct type_name connector_status_names[] = {
146 { DRM_MODE_CONNECTED, "connected" },
147 { DRM_MODE_DISCONNECTED, "disconnected" },
148 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
151 static type_name_fn(connector_status)
153 struct type_name connector_type_names[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
171 static type_name_fn(connector_type)
173 #define bit_name_fn(res) \
174 const char * res##_str(int type) { \
176 const char *sep = ""; \
177 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
178 if (type & (1 << i)) { \
179 printf("%s%s", sep, res##_names[i]); \
186 static const char *mode_type_names[] = {
196 static bit_name_fn(mode_type)
198 static const char *mode_flag_names[] = {
215 static bit_name_fn(mode_flag)
217 static void dump_encoders(struct device *dev)
219 drmModeEncoder *encoder;
222 printf("Encoders:\n");
223 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
224 for (i = 0; i < dev->resources->res->count_encoders; i++) {
225 encoder = dev->resources->encoders[i].encoder;
229 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
232 encoder_type_str(encoder->encoder_type),
233 encoder->possible_crtcs,
234 encoder->possible_clones);
239 static void dump_mode(drmModeModeInfo *mode)
241 printf(" %s %d %d %d %d %d %d %d %d %d",
254 mode_flag_str(mode->flags);
256 mode_type_str(mode->type);
260 static void dump_blob(struct device *dev, uint32_t blob_id)
263 unsigned char *blob_data;
264 drmModePropertyBlobPtr blob;
266 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
270 blob_data = blob->data;
272 for (i = 0; i < blob->length; i++) {
275 printf("%.2hhx", blob_data[i]);
279 drmModeFreePropertyBlob(blob);
282 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
283 uint32_t prop_id, uint64_t value)
286 printf("\t%d", prop_id);
292 printf(" %s:\n", prop->name);
294 printf("\t\tflags:");
295 if (prop->flags & DRM_MODE_PROP_PENDING)
297 if (prop->flags & DRM_MODE_PROP_RANGE)
299 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
300 printf(" immutable");
301 if (prop->flags & DRM_MODE_PROP_ENUM)
303 if (prop->flags & DRM_MODE_PROP_BITMASK)
305 if (prop->flags & DRM_MODE_PROP_BLOB)
309 if (prop->flags & DRM_MODE_PROP_RANGE) {
310 printf("\t\tvalues:");
311 for (i = 0; i < prop->count_values; i++)
312 printf(" %"PRIu64, prop->values[i]);
316 if (prop->flags & DRM_MODE_PROP_ENUM) {
317 printf("\t\tenums:");
318 for (i = 0; i < prop->count_enums; i++)
319 printf(" %s=%llu", prop->enums[i].name,
320 prop->enums[i].value);
322 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
323 printf("\t\tvalues:");
324 for (i = 0; i < prop->count_enums; i++)
325 printf(" %s=0x%llx", prop->enums[i].name,
326 (1LL << prop->enums[i].value));
329 assert(prop->count_enums == 0);
332 if (prop->flags & DRM_MODE_PROP_BLOB) {
333 printf("\t\tblobs:\n");
334 for (i = 0; i < prop->count_blobs; i++)
335 dump_blob(dev, prop->blob_ids[i]);
338 assert(prop->count_blobs == 0);
341 printf("\t\tvalue:");
342 if (prop->flags & DRM_MODE_PROP_BLOB)
343 dump_blob(dev, value);
345 printf(" %"PRIu64"\n", value);
348 static void dump_connectors(struct device *dev)
352 printf("Connectors:\n");
353 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
354 for (i = 0; i < dev->resources->res->count_connectors; i++) {
355 struct connector *_connector = &dev->resources->connectors[i];
356 drmModeConnector *connector = _connector->connector;
360 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
361 connector->connector_id,
362 connector->encoder_id,
363 connector_status_str(connector->connection),
364 connector_type_str(connector->connector_type),
365 connector->mmWidth, connector->mmHeight,
366 connector->count_modes);
368 for (j = 0; j < connector->count_encoders; j++)
369 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
372 if (connector->count_modes) {
374 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
376 for (j = 0; j < connector->count_modes; j++)
377 dump_mode(&connector->modes[j]);
380 if (_connector->props) {
382 for (j = 0; j < (int)_connector->props->count_props; j++)
383 dump_prop(dev, _connector->props_info[j],
384 _connector->props->props[j],
385 _connector->props->prop_values[j]);
391 static void dump_crtcs(struct device *dev)
397 printf("id\tfb\tpos\tsize\n");
398 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
399 struct crtc *_crtc = &dev->resources->crtcs[i];
400 drmModeCrtc *crtc = _crtc->crtc;
404 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
408 crtc->width, crtc->height);
409 dump_mode(&crtc->mode);
413 for (j = 0; j < _crtc->props->count_props; j++)
414 dump_prop(dev, _crtc->props_info[j],
415 _crtc->props->props[j],
416 _crtc->props->prop_values[j]);
418 printf(" no properties found\n");
424 static void dump_framebuffers(struct device *dev)
429 printf("Frame buffers:\n");
430 printf("id\tsize\tpitch\n");
431 for (i = 0; i < dev->resources->res->count_fbs; i++) {
432 fb = dev->resources->fbs[i].fb;
436 printf("%u\t(%ux%u)\t%u\n",
438 fb->width, fb->height,
444 static void dump_planes(struct device *dev)
449 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
451 if (!dev->resources->plane_res)
454 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
455 struct plane *plane = &dev->resources->planes[i];
456 drmModePlane *ovr = plane->plane;
460 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
461 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
462 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
463 ovr->gamma_size, ovr->possible_crtcs);
465 if (!ovr->count_formats)
469 for (j = 0; j < ovr->count_formats; j++)
470 printf(" %4.4s", (char *)&ovr->formats[j]);
475 for (j = 0; j < plane->props->count_props; j++)
476 dump_prop(dev, plane->props_info[j],
477 plane->props->props[j],
478 plane->props->prop_values[j]);
480 printf(" no properties found\n");
488 static void free_resources(struct resources *res)
493 #define free_resource(_res, __res, type, Type) \
496 if (!(_res)->type##s) \
498 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
499 if (!(_res)->type##s[i].type) \
501 drmModeFree##Type((_res)->type##s[i].type); \
503 free((_res)->type##s); \
506 #define free_properties(_res, __res, type) \
509 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
510 drmModeFreeObjectProperties(res->type##s[i].props); \
511 free(res->type##s[i].props_info); \
516 free_properties(res, res, crtc);
518 free_resource(res, res, crtc, Crtc);
519 free_resource(res, res, encoder, Encoder);
520 free_resource(res, res, connector, Connector);
521 free_resource(res, res, fb, FB);
523 drmModeFreeResources(res->res);
526 if (res->plane_res) {
527 free_properties(res, plane_res, plane);
529 free_resource(res, plane_res, plane, Plane);
531 drmModeFreePlaneResources(res->plane_res);
537 static struct resources *get_resources(struct device *dev)
539 struct resources *res;
542 res = malloc(sizeof *res);
546 memset(res, 0, sizeof *res);
548 res->res = drmModeGetResources(dev->fd);
550 fprintf(stderr, "drmModeGetResources failed: %s\n",
555 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
556 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
557 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
558 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
560 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
563 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
564 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
565 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
566 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
568 #define get_resource(_res, __res, type, Type) \
571 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
572 (_res)->type##s[i].type = \
573 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
574 if (!(_res)->type##s[i].type) \
575 fprintf(stderr, "could not get %s %i: %s\n", \
576 #type, (_res)->__res->type##s[i], \
581 get_resource(res, res, crtc, Crtc);
582 get_resource(res, res, encoder, Encoder);
583 get_resource(res, res, connector, Connector);
584 get_resource(res, res, fb, FB);
586 #define get_properties(_res, __res, type, Type) \
589 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
590 struct type *obj = &res->type##s[i]; \
593 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
594 DRM_MODE_OBJECT_##Type); \
597 "could not get %s %i properties: %s\n", \
598 #type, obj->type->type##_id, \
602 obj->props_info = malloc(obj->props->count_props * \
603 sizeof *obj->props_info); \
604 if (!obj->props_info) \
606 for (j = 0; j < obj->props->count_props; ++j) \
607 obj->props_info[j] = \
608 drmModeGetProperty(dev->fd, obj->props->props[j]); \
612 get_properties(res, res, crtc, CRTC);
613 get_properties(res, res, connector, CONNECTOR);
615 for (i = 0; i < res->res->count_crtcs; ++i)
616 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
618 res->plane_res = drmModeGetPlaneResources(dev->fd);
619 if (!res->plane_res) {
620 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
625 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
629 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
631 get_resource(res, plane_res, plane, Plane);
632 get_properties(res, plane_res, plane, PLANE);
641 static int get_crtc_index(struct device *dev, uint32_t id)
645 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
646 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
647 if (crtc && crtc->crtc_id == id)
654 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
656 drmModeConnector *connector;
659 for (i = 0; i < dev->resources->res->count_connectors; i++) {
660 connector = dev->resources->connectors[i].connector;
661 if (connector && connector->connector_id == id)
668 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
670 drmModeEncoder *encoder;
673 for (i = 0; i < dev->resources->res->count_encoders; i++) {
674 encoder = dev->resources->encoders[i].encoder;
675 if (encoder && encoder->encoder_id == id)
682 /* -----------------------------------------------------------------------------
687 * Mode setting with the kernel interfaces is a bit of a chore.
688 * First you have to find the connector in question and make sure the
689 * requested mode is available.
690 * Then you need to find the encoder attached to that connector so you
691 * can bind it with a free crtc.
695 unsigned int num_cons;
699 unsigned int vrefresh;
701 drmModeModeInfo *mode;
703 unsigned int fb_id[2], current_fb_id;
704 struct timeval start;
710 uint32_t crtc_id; /* the id of CRTC to bind to */
716 char format_str[5]; /* need to leave room for terminating \0 */
720 static drmModeModeInfo *
721 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
722 const unsigned int vrefresh)
724 drmModeConnector *connector;
725 drmModeModeInfo *mode;
728 connector = get_connector_by_id(dev, con_id);
729 if (!connector || !connector->count_modes)
732 for (i = 0; i < connector->count_modes; i++) {
733 mode = &connector->modes[i];
734 if (!strcmp(mode->name, mode_str)) {
735 /* If the vertical refresh frequency is not specified then return the
736 * first mode that match with the name. Else, return the mode that match
737 * the name and the specified vertical refresh frequency.
741 else if (mode->vrefresh == vrefresh)
749 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
751 uint32_t possible_crtcs = ~0;
752 uint32_t active_crtcs = 0;
753 unsigned int crtc_idx;
757 for (i = 0; i < pipe->num_cons; ++i) {
758 uint32_t crtcs_for_connector = 0;
759 drmModeConnector *connector;
760 drmModeEncoder *encoder;
763 connector = get_connector_by_id(dev, pipe->con_ids[i]);
767 for (j = 0; j < connector->count_encoders; ++j) {
768 encoder = get_encoder_by_id(dev, connector->encoders[j]);
772 crtcs_for_connector |= encoder->possible_crtcs;
774 idx = get_crtc_index(dev, encoder->crtc_id);
776 active_crtcs |= 1 << idx;
779 possible_crtcs &= crtcs_for_connector;
785 /* Return the first possible and active CRTC if one exists, or the first
786 * possible CRTC otherwise.
788 if (possible_crtcs & active_crtcs)
789 crtc_idx = ffs(possible_crtcs & active_crtcs);
791 crtc_idx = ffs(possible_crtcs);
793 return &dev->resources->crtcs[crtc_idx - 1];
796 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
798 drmModeModeInfo *mode = NULL;
803 for (i = 0; i < (int)pipe->num_cons; i++) {
804 mode = connector_find_mode(dev, pipe->con_ids[i],
805 pipe->mode_str, pipe->vrefresh);
808 "failed to find mode \"%s\" for connector %u\n",
809 pipe->mode_str, pipe->con_ids[i]);
814 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
815 * locate a CRTC that can be attached to all the connectors.
817 if (pipe->crtc_id != (uint32_t)-1) {
818 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
819 struct crtc *crtc = &dev->resources->crtcs[i];
821 if (pipe->crtc_id == crtc->crtc->crtc_id) {
827 pipe->crtc = pipe_find_crtc(dev, pipe);
831 fprintf(stderr, "failed to find CRTC for pipe\n");
836 pipe->crtc->mode = mode;
841 /* -----------------------------------------------------------------------------
845 struct property_arg {
848 char name[DRM_PROP_NAME_LEN+1];
853 static void set_property(struct device *dev, struct property_arg *p)
855 drmModeObjectProperties *props = NULL;
856 drmModePropertyRes **props_info = NULL;
857 const char *obj_type;
864 #define find_object(_res, __res, type, Type) \
866 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
867 struct type *obj = &(_res)->type##s[i]; \
868 if (obj->type->type##_id != p->obj_id) \
870 p->obj_type = DRM_MODE_OBJECT_##Type; \
872 props = obj->props; \
873 props_info = obj->props_info; \
877 find_object(dev->resources, res, crtc, CRTC);
878 if (p->obj_type == 0)
879 find_object(dev->resources, res, connector, CONNECTOR);
880 if (p->obj_type == 0)
881 find_object(dev->resources, plane_res, plane, PLANE);
882 if (p->obj_type == 0) {
883 fprintf(stderr, "Object %i not found, can't set property\n",
889 fprintf(stderr, "%s %i has no properties\n",
890 obj_type, p->obj_id);
894 for (i = 0; i < (int)props->count_props; ++i) {
897 if (strcmp(props_info[i]->name, p->name) == 0)
901 if (i == (int)props->count_props) {
902 fprintf(stderr, "%s %i has no %s property\n",
903 obj_type, p->obj_id, p->name);
907 p->prop_id = props->props[i];
909 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
910 p->prop_id, p->value);
912 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
913 obj_type, p->obj_id, p->name, p->value, strerror(errno));
916 /* -------------------------------------------------------------------------- */
919 page_flip_handler(int fd, unsigned int frame,
920 unsigned int sec, unsigned int usec, void *data)
922 struct pipe_arg *pipe;
923 unsigned int new_fb_id;
928 if (pipe->current_fb_id == pipe->fb_id[0])
929 new_fb_id = pipe->fb_id[1];
931 new_fb_id = pipe->fb_id[0];
933 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
934 DRM_MODE_PAGE_FLIP_EVENT, pipe);
935 pipe->current_fb_id = new_fb_id;
937 if (pipe->swap_count == 60) {
938 gettimeofday(&end, NULL);
939 t = end.tv_sec + end.tv_usec * 1e-6 -
940 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
941 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
942 pipe->swap_count = 0;
947 static int set_plane(struct device *dev, struct plane_arg *p)
950 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
951 uint32_t plane_id = 0;
952 struct kms_bo *plane_bo;
953 uint32_t plane_flags = 0;
954 int crtc_x, crtc_y, crtc_w, crtc_h;
955 struct crtc *crtc = NULL;
959 /* Find an unused plane which can be connected to our CRTC. Find the
960 * CRTC index first, then iterate over available planes.
962 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
963 if (p->crtc_id == dev->resources->res->crtcs[i]) {
964 crtc = &dev->resources->crtcs[i];
971 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
975 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
976 ovr = dev->resources->planes[i].plane;
980 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
981 plane_id = ovr->plane_id;
985 fprintf(stderr, "no unused plane available for CRTC %u\n",
986 crtc->crtc->crtc_id);
990 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
991 p->w, p->h, p->format_str, plane_id);
993 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
994 pitches, offsets, PATTERN_TILES);
995 if (plane_bo == NULL)
998 /* just use single plane format for now.. */
999 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1000 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1001 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1005 crtc_w = p->w * p->scale;
1006 crtc_h = p->h * p->scale;
1007 if (!p->has_position) {
1008 /* Default to the middle of the screen */
1009 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1010 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1016 /* note src coords (last 4 args) are in Q16 format */
1017 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1018 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1019 0, 0, p->w << 16, p->h << 16)) {
1020 fprintf(stderr, "failed to enable plane: %s\n",
1025 ovr->crtc_id = crtc->crtc->crtc_id;
1030 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1032 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1039 dev->mode.width = 0;
1040 dev->mode.height = 0;
1042 for (i = 0; i < count; i++) {
1043 struct pipe_arg *pipe = &pipes[i];
1045 ret = pipe_find_crtc_and_mode(dev, pipe);
1049 dev->mode.width += pipe->mode->hdisplay;
1050 if (dev->mode.height < pipe->mode->vdisplay)
1051 dev->mode.height = pipe->mode->vdisplay;
1054 bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1055 dev->mode.width, dev->mode.height,
1056 handles, pitches, offsets, PATTERN_SMPTE);
1060 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1061 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1063 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1064 dev->mode.width, dev->mode.height, strerror(errno));
1069 for (i = 0; i < count; i++) {
1070 struct pipe_arg *pipe = &pipes[i];
1072 if (pipe->mode == NULL)
1075 printf("setting mode %s-%dHz@%s on connectors ",
1076 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1077 for (j = 0; j < pipe->num_cons; ++j)
1078 printf("%u, ", pipe->con_ids[j]);
1079 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1081 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1082 x, 0, pipe->con_ids, pipe->num_cons,
1085 /* XXX: Actually check if this is needed */
1086 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1088 x += pipe->mode->hdisplay;
1091 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1097 dev->mode.fb_id = fb_id;
1100 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1104 /* set up planes/overlays */
1105 for (i = 0; i < count; i++)
1106 if (set_plane(dev, &p[i]))
1110 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1112 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1117 /* maybe make cursor width/height configurable some day */
1121 /* create cursor bo.. just using PATTERN_PLAIN as it has
1124 bo = create_test_buffer(dev->kms, DRM_FORMAT_ARGB8888,
1125 cw, ch, handles, pitches, offsets, PATTERN_PLAIN);
1129 for (i = 0; i < count; i++) {
1130 struct pipe_arg *pipe = &pipes[i];
1131 ret = cursor_init(dev->fd, handles[0],
1132 pipe->crtc->crtc->crtc_id,
1133 pipe->mode->hdisplay, pipe->mode->vdisplay,
1136 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1145 static void clear_cursors(struct device *dev)
1150 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1152 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1153 unsigned int other_fb_id;
1154 struct kms_bo *other_bo;
1155 drmEventContext evctx;
1159 other_bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1160 dev->mode.width, dev->mode.height,
1161 handles, pitches, offsets, PATTERN_PLAIN);
1162 if (other_bo == NULL)
1165 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1166 pipes[0].fourcc, handles, pitches, offsets,
1169 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1173 for (i = 0; i < count; i++) {
1174 struct pipe_arg *pipe = &pipes[i];
1176 if (pipe->mode == NULL)
1179 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1180 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1183 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1186 gettimeofday(&pipe->start, NULL);
1187 pipe->swap_count = 0;
1188 pipe->fb_id[0] = dev->mode.fb_id;
1189 pipe->fb_id[1] = other_fb_id;
1190 pipe->current_fb_id = other_fb_id;
1193 memset(&evctx, 0, sizeof evctx);
1194 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1195 evctx.vblank_handler = NULL;
1196 evctx.page_flip_handler = page_flip_handler;
1200 struct pollfd pfd[2];
1203 pfd[0].events = POLLIN;
1205 pfd[1].events = POLLIN;
1207 if (poll(pfd, 2, -1) < 0) {
1208 fprintf(stderr, "poll error\n");
1215 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1221 FD_SET(dev->fd, &fds);
1222 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1225 fprintf(stderr, "select timed out or error (ret %d)\n",
1228 } else if (FD_ISSET(0, &fds)) {
1233 drmHandleEvent(dev->fd, &evctx);
1236 kms_bo_destroy(&other_bo);
1239 #define min(a, b) ((a) < (b) ? (a) : (b))
1241 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1249 pipe->crtc_id = (uint32_t)-1;
1250 strcpy(pipe->format_str, "XR24");
1252 /* Count the number of connectors and allocate them. */
1254 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1259 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1260 if (pipe->con_ids == NULL)
1263 /* Parse the connectors. */
1264 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1265 pipe->con_ids[i] = strtoul(p, &endp, 10);
1270 if (i != pipe->num_cons - 1)
1273 /* Parse the remaining parameters. */
1276 pipe->crtc_id = strtoul(arg, &endp, 10);
1283 /* Search for the vertical refresh or the format. */
1284 p = strpbrk(arg, "-@");
1286 p = arg + strlen(arg);
1287 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1288 strncpy(pipe->mode_str, arg, len);
1289 pipe->mode_str[len] = '\0';
1292 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1297 strncpy(pipe->format_str, p + 1, 4);
1298 pipe->format_str[4] = '\0';
1301 pipe->fourcc = format_fourcc(pipe->format_str);
1302 if (pipe->fourcc == 0) {
1303 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1310 static int parse_plane(struct plane_arg *plane, const char *p)
1314 memset(plane, 0, sizeof *plane);
1316 plane->crtc_id = strtoul(p, &end, 10);
1321 plane->w = strtoul(p, &end, 10);
1326 plane->h = strtoul(p, &end, 10);
1328 if (*end == '+' || *end == '-') {
1329 plane->x = strtol(end, &end, 10);
1330 if (*end != '+' && *end != '-')
1332 plane->y = strtol(end, &end, 10);
1334 plane->has_position = true;
1339 plane->scale = strtod(p, &end);
1340 if (plane->scale <= 0.0)
1351 strcpy(plane->format_str, p);
1353 strcpy(plane->format_str, "XR24");
1356 plane->fourcc = format_fourcc(plane->format_str);
1357 if (plane->fourcc == 0) {
1358 fprintf(stderr, "unknown format %s\n", plane->format_str);
1365 static int parse_property(struct property_arg *p, const char *arg)
1367 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1371 p->name[DRM_PROP_NAME_LEN] = '\0';
1376 static void usage(char *name)
1378 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1380 fprintf(stderr, "\n Query options:\n\n");
1381 fprintf(stderr, "\t-c\tlist connectors\n");
1382 fprintf(stderr, "\t-e\tlist encoders\n");
1383 fprintf(stderr, "\t-f\tlist framebuffers\n");
1384 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1386 fprintf(stderr, "\n Test options:\n\n");
1387 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1388 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1389 fprintf(stderr, "\t-C\ttest hw cursor\n");
1390 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1391 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1393 fprintf(stderr, "\n Generic options:\n\n");
1394 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1395 fprintf(stderr, "\t-M module\tuse the given driver\n");
1396 fprintf(stderr, "\t-D device\tuse the given device\n");
1398 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1402 static int page_flipping_supported(void)
1404 /*FIXME: generic ioctl needed? */
1408 struct drm_i915_getparam gp;
1410 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1413 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1415 fprintf(stderr, "drm_i915_getparam: %m\n");
1423 static int cursor_supported(void)
1425 /*FIXME: generic ioctl needed? */
1429 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1431 int main(int argc, char **argv)
1436 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1437 int drop_master = 0;
1439 int test_cursor = 0;
1440 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm" };
1441 char *device = NULL;
1442 char *module = NULL;
1444 int count = 0, plane_count = 0;
1445 unsigned int prop_count = 0;
1446 struct pipe_arg *pipe_args = NULL;
1447 struct plane_arg *plane_args = NULL;
1448 struct property_arg *prop_args = NULL;
1449 unsigned int args = 0;
1452 memset(&dev, 0, sizeof dev);
1455 while ((c = getopt(argc, argv, optstr)) != -1) {
1477 /* Preserve the default behaviour of dumping all information. */
1481 plane_args = realloc(plane_args,
1482 (plane_count + 1) * sizeof *plane_args);
1483 if (plane_args == NULL) {
1484 fprintf(stderr, "memory allocation failed\n");
1488 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1498 pipe_args = realloc(pipe_args,
1499 (count + 1) * sizeof *pipe_args);
1500 if (pipe_args == NULL) {
1501 fprintf(stderr, "memory allocation failed\n");
1505 if (parse_connector(&pipe_args[count], optarg) < 0)
1517 prop_args = realloc(prop_args,
1518 (prop_count + 1) * sizeof *prop_args);
1519 if (prop_args == NULL) {
1520 fprintf(stderr, "memory allocation failed\n");
1524 if (parse_property(&prop_args[prop_count], optarg) < 0)
1536 encoders = connectors = crtcs = planes = framebuffers = 1;
1539 dev.fd = drmOpen(module, device);
1541 fprintf(stderr, "failed to open device '%s'.\n", module);
1545 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1546 printf("trying to open device '%s'...", modules[i]);
1547 dev.fd = drmOpen(modules[i], device);
1549 printf("failed.\n");
1551 printf("success.\n");
1557 fprintf(stderr, "no device found.\n");
1562 if (test_vsync && !page_flipping_supported()) {
1563 fprintf(stderr, "page flipping not supported by drm.\n");
1567 if (test_vsync && !count) {
1568 fprintf(stderr, "page flipping requires at least one -s option.\n");
1572 if (test_cursor && !cursor_supported()) {
1573 fprintf(stderr, "hw cursor not supported by drm.\n");
1577 dev.resources = get_resources(&dev);
1578 if (!dev.resources) {
1583 #define dump_resource(dev, res) if (res) dump_##res(dev)
1585 dump_resource(&dev, encoders);
1586 dump_resource(&dev, connectors);
1587 dump_resource(&dev, crtcs);
1588 dump_resource(&dev, planes);
1589 dump_resource(&dev, framebuffers);
1591 for (i = 0; i < prop_count; ++i)
1592 set_property(&dev, &prop_args[i]);
1594 if (count || plane_count) {
1595 ret = kms_create(dev.fd, &dev.kms);
1597 fprintf(stderr, "failed to create kms driver: %s\n",
1603 set_mode(&dev, pipe_args, count);
1606 set_planes(&dev, plane_args, plane_count);
1609 set_cursors(&dev, pipe_args, count);
1612 test_page_flip(&dev, pipe_args, count);
1615 drmDropMaster(dev.fd);
1620 clear_cursors(&dev);
1622 kms_bo_destroy(&dev.mode.bo);
1623 kms_destroy(&dev.kms);
1626 free_resources(dev.resources);