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 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
63 #include "xf86drmMode.h"
64 #include "drm_fourcc.h"
66 #include "util/common.h"
67 #include "util/format.h"
69 #include "util/pattern.h"
76 drmModeObjectProperties *props;
77 drmModePropertyRes **props_info;
78 drmModeModeInfo *mode;
82 drmModeEncoder *encoder;
86 drmModeConnector *connector;
87 drmModeObjectProperties *props;
88 drmModePropertyRes **props_info;
98 drmModeObjectProperties *props;
99 drmModePropertyRes **props_info;
104 drmModePlaneRes *plane_res;
107 struct encoder *encoders;
108 struct connector *connectors;
110 struct plane *planes;
116 struct resources *resources;
124 struct bo *cursor_bo;
128 static inline int64_t U642I64(uint64_t val)
130 return (int64_t)*((int64_t *)&val);
133 #define bit_name_fn(res) \
134 const char * res##_str(int type) { \
136 const char *sep = ""; \
137 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
138 if (type & (1 << i)) { \
139 printf("%s%s", sep, res##_names[i]); \
146 static const char *mode_type_names[] = {
156 static bit_name_fn(mode_type)
158 static const char *mode_flag_names[] = {
175 static bit_name_fn(mode_flag)
177 static void dump_fourcc(uint32_t fourcc)
186 static void dump_encoders(struct device *dev)
188 drmModeEncoder *encoder;
191 printf("Encoders:\n");
192 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
193 for (i = 0; i < dev->resources->res->count_encoders; i++) {
194 encoder = dev->resources->encoders[i].encoder;
198 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
201 util_lookup_encoder_type_name(encoder->encoder_type),
202 encoder->possible_crtcs,
203 encoder->possible_clones);
208 static void dump_mode(drmModeModeInfo *mode)
210 printf(" %s %d %d %d %d %d %d %d %d %d %d",
224 mode_flag_str(mode->flags);
226 mode_type_str(mode->type);
230 static void dump_blob(struct device *dev, uint32_t blob_id)
233 unsigned char *blob_data;
234 drmModePropertyBlobPtr blob;
236 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
242 blob_data = blob->data;
244 for (i = 0; i < blob->length; i++) {
247 printf("%.2hhx", blob_data[i]);
251 drmModeFreePropertyBlob(blob);
254 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
255 uint32_t prop_id, uint64_t value)
258 printf("\t%d", prop_id);
264 printf(" %s:\n", prop->name);
266 printf("\t\tflags:");
267 if (prop->flags & DRM_MODE_PROP_PENDING)
269 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
270 printf(" immutable");
271 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
272 printf(" signed range");
273 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
275 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
277 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
279 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
281 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
285 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
286 printf("\t\tvalues:");
287 for (i = 0; i < prop->count_values; i++)
288 printf(" %"PRId64, U642I64(prop->values[i]));
292 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
293 printf("\t\tvalues:");
294 for (i = 0; i < prop->count_values; i++)
295 printf(" %"PRIu64, prop->values[i]);
299 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
300 printf("\t\tenums:");
301 for (i = 0; i < prop->count_enums; i++)
302 printf(" %s=%llu", prop->enums[i].name,
303 prop->enums[i].value);
305 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
306 printf("\t\tvalues:");
307 for (i = 0; i < prop->count_enums; i++)
308 printf(" %s=0x%llx", prop->enums[i].name,
309 (1LL << prop->enums[i].value));
312 assert(prop->count_enums == 0);
315 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
316 printf("\t\tblobs:\n");
317 for (i = 0; i < prop->count_blobs; i++)
318 dump_blob(dev, prop->blob_ids[i]);
321 assert(prop->count_blobs == 0);
324 printf("\t\tvalue:");
325 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
326 dump_blob(dev, value);
327 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
328 printf(" %"PRId64"\n", value);
330 printf(" %"PRIu64"\n", value);
333 static void dump_connectors(struct device *dev)
337 printf("Connectors:\n");
338 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
339 for (i = 0; i < dev->resources->res->count_connectors; i++) {
340 struct connector *_connector = &dev->resources->connectors[i];
341 drmModeConnector *connector = _connector->connector;
345 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
346 connector->connector_id,
347 connector->encoder_id,
348 util_lookup_connector_status_name(connector->connection),
350 connector->mmWidth, connector->mmHeight,
351 connector->count_modes);
353 for (j = 0; j < connector->count_encoders; j++)
354 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
357 if (connector->count_modes) {
359 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
361 for (j = 0; j < connector->count_modes; j++)
362 dump_mode(&connector->modes[j]);
365 if (_connector->props) {
367 for (j = 0; j < (int)_connector->props->count_props; j++)
368 dump_prop(dev, _connector->props_info[j],
369 _connector->props->props[j],
370 _connector->props->prop_values[j]);
376 static void dump_crtcs(struct device *dev)
382 printf("id\tfb\tpos\tsize\n");
383 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
384 struct crtc *_crtc = &dev->resources->crtcs[i];
385 drmModeCrtc *crtc = _crtc->crtc;
389 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
393 crtc->width, crtc->height);
394 dump_mode(&crtc->mode);
398 for (j = 0; j < _crtc->props->count_props; j++)
399 dump_prop(dev, _crtc->props_info[j],
400 _crtc->props->props[j],
401 _crtc->props->prop_values[j]);
403 printf(" no properties found\n");
409 static void dump_framebuffers(struct device *dev)
414 printf("Frame buffers:\n");
415 printf("id\tsize\tpitch\n");
416 for (i = 0; i < dev->resources->res->count_fbs; i++) {
417 fb = dev->resources->fbs[i].fb;
421 printf("%u\t(%ux%u)\t%u\n",
423 fb->width, fb->height,
429 static void dump_planes(struct device *dev)
434 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
436 if (!dev->resources->plane_res)
439 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
440 struct plane *plane = &dev->resources->planes[i];
441 drmModePlane *ovr = plane->plane;
445 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
446 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
447 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
448 ovr->gamma_size, ovr->possible_crtcs);
450 if (!ovr->count_formats)
454 for (j = 0; j < ovr->count_formats; j++)
455 dump_fourcc(ovr->formats[j]);
460 for (j = 0; j < plane->props->count_props; j++)
461 dump_prop(dev, plane->props_info[j],
462 plane->props->props[j],
463 plane->props->prop_values[j]);
465 printf(" no properties found\n");
473 static void free_resources(struct resources *res)
480 #define free_resource(_res, __res, type, Type) \
482 if (!(_res)->type##s) \
484 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
485 if (!(_res)->type##s[i].type) \
487 drmModeFree##Type((_res)->type##s[i].type); \
489 free((_res)->type##s); \
492 #define free_properties(_res, __res, type) \
494 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
495 drmModeFreeObjectProperties(res->type##s[i].props); \
496 free(res->type##s[i].props_info); \
501 free_properties(res, res, crtc);
503 free_resource(res, res, crtc, Crtc);
504 free_resource(res, res, encoder, Encoder);
506 for (i = 0; i < res->res->count_connectors; i++)
507 free(res->connectors[i].name);
509 free_resource(res, res, connector, Connector);
510 free_resource(res, res, fb, FB);
512 drmModeFreeResources(res->res);
515 if (res->plane_res) {
516 free_properties(res, plane_res, plane);
518 free_resource(res, plane_res, plane, Plane);
520 drmModeFreePlaneResources(res->plane_res);
526 static struct resources *get_resources(struct device *dev)
528 struct resources *res;
531 res = calloc(1, sizeof(*res));
535 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
537 res->res = drmModeGetResources(dev->fd);
539 fprintf(stderr, "drmModeGetResources failed: %s\n",
544 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
545 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
546 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
547 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
549 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
552 #define get_resource(_res, __res, type, Type) \
554 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
555 (_res)->type##s[i].type = \
556 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
557 if (!(_res)->type##s[i].type) \
558 fprintf(stderr, "could not get %s %i: %s\n", \
559 #type, (_res)->__res->type##s[i], \
564 get_resource(res, res, crtc, Crtc);
565 get_resource(res, res, encoder, Encoder);
566 get_resource(res, res, connector, Connector);
567 get_resource(res, res, fb, FB);
569 /* Set the name of all connectors based on the type name and the per-type ID. */
570 for (i = 0; i < res->res->count_connectors; i++) {
571 struct connector *connector = &res->connectors[i];
572 drmModeConnector *conn = connector->connector;
574 asprintf(&connector->name, "%s-%u",
575 util_lookup_connector_type_name(conn->connector_type),
576 conn->connector_type_id);
579 #define get_properties(_res, __res, type, Type) \
581 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
582 struct type *obj = &res->type##s[i]; \
585 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
586 DRM_MODE_OBJECT_##Type); \
589 "could not get %s %i properties: %s\n", \
590 #type, obj->type->type##_id, \
594 obj->props_info = calloc(obj->props->count_props, \
595 sizeof(*obj->props_info)); \
596 if (!obj->props_info) \
598 for (j = 0; j < obj->props->count_props; ++j) \
599 obj->props_info[j] = \
600 drmModeGetProperty(dev->fd, obj->props->props[j]); \
604 get_properties(res, res, crtc, CRTC);
605 get_properties(res, res, connector, CONNECTOR);
607 for (i = 0; i < res->res->count_crtcs; ++i)
608 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
610 res->plane_res = drmModeGetPlaneResources(dev->fd);
611 if (!res->plane_res) {
612 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
617 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
621 get_resource(res, plane_res, plane, Plane);
622 get_properties(res, plane_res, plane, PLANE);
631 static int get_crtc_index(struct device *dev, uint32_t id)
635 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
636 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
637 if (crtc && crtc->crtc_id == id)
644 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
646 struct connector *connector;
649 for (i = 0; i < dev->resources->res->count_connectors; i++) {
650 connector = &dev->resources->connectors[i];
652 if (strcmp(connector->name, name) == 0)
653 return connector->connector;
659 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
661 drmModeConnector *connector;
664 for (i = 0; i < dev->resources->res->count_connectors; i++) {
665 connector = dev->resources->connectors[i].connector;
666 if (connector && connector->connector_id == id)
673 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
675 drmModeEncoder *encoder;
678 for (i = 0; i < dev->resources->res->count_encoders; i++) {
679 encoder = dev->resources->encoders[i].encoder;
680 if (encoder && encoder->encoder_id == id)
687 /* -----------------------------------------------------------------------------
692 * Mode setting with the kernel interfaces is a bit of a chore.
693 * First you have to find the connector in question and make sure the
694 * requested mode is available.
695 * Then you need to find the encoder attached to that connector so you
696 * can bind it with a free crtc.
701 unsigned int num_cons;
705 unsigned int vrefresh;
707 drmModeModeInfo *mode;
709 unsigned int fb_id[2], current_fb_id;
710 struct timeval start;
716 uint32_t plane_id; /* the id of plane to use */
717 uint32_t crtc_id; /* the id of CRTC to bind to */
724 char format_str[5]; /* need to leave room for terminating \0 */
728 static drmModeModeInfo *
729 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
730 const unsigned int vrefresh)
732 drmModeConnector *connector;
733 drmModeModeInfo *mode;
736 connector = get_connector_by_id(dev, con_id);
737 if (!connector || !connector->count_modes)
740 for (i = 0; i < connector->count_modes; i++) {
741 mode = &connector->modes[i];
742 if (!strcmp(mode->name, mode_str)) {
743 /* If the vertical refresh frequency is not specified then return the
744 * first mode that match with the name. Else, return the mode that match
745 * the name and the specified vertical refresh frequency.
749 else if (mode->vrefresh == vrefresh)
757 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
759 uint32_t possible_crtcs = ~0;
760 uint32_t active_crtcs = 0;
761 unsigned int crtc_idx;
765 for (i = 0; i < pipe->num_cons; ++i) {
766 uint32_t crtcs_for_connector = 0;
767 drmModeConnector *connector;
768 drmModeEncoder *encoder;
771 connector = get_connector_by_id(dev, pipe->con_ids[i]);
775 for (j = 0; j < connector->count_encoders; ++j) {
776 encoder = get_encoder_by_id(dev, connector->encoders[j]);
780 crtcs_for_connector |= encoder->possible_crtcs;
782 idx = get_crtc_index(dev, encoder->crtc_id);
784 active_crtcs |= 1 << idx;
787 possible_crtcs &= crtcs_for_connector;
793 /* Return the first possible and active CRTC if one exists, or the first
794 * possible CRTC otherwise.
796 if (possible_crtcs & active_crtcs)
797 crtc_idx = ffs(possible_crtcs & active_crtcs);
799 crtc_idx = ffs(possible_crtcs);
801 return &dev->resources->crtcs[crtc_idx - 1];
804 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
806 drmModeModeInfo *mode = NULL;
811 for (i = 0; i < (int)pipe->num_cons; i++) {
812 mode = connector_find_mode(dev, pipe->con_ids[i],
813 pipe->mode_str, pipe->vrefresh);
816 "failed to find mode \"%s\" for connector %s\n",
817 pipe->mode_str, pipe->cons[i]);
822 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
823 * locate a CRTC that can be attached to all the connectors.
825 if (pipe->crtc_id != (uint32_t)-1) {
826 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
827 struct crtc *crtc = &dev->resources->crtcs[i];
829 if (pipe->crtc_id == crtc->crtc->crtc_id) {
835 pipe->crtc = pipe_find_crtc(dev, pipe);
839 fprintf(stderr, "failed to find CRTC for pipe\n");
844 pipe->crtc->mode = mode;
849 /* -----------------------------------------------------------------------------
853 struct property_arg {
856 char name[DRM_PROP_NAME_LEN+1];
861 static void set_property(struct device *dev, struct property_arg *p)
863 drmModeObjectProperties *props = NULL;
864 drmModePropertyRes **props_info = NULL;
865 const char *obj_type;
872 #define find_object(_res, __res, type, Type) \
874 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
875 struct type *obj = &(_res)->type##s[i]; \
876 if (obj->type->type##_id != p->obj_id) \
878 p->obj_type = DRM_MODE_OBJECT_##Type; \
880 props = obj->props; \
881 props_info = obj->props_info; \
885 find_object(dev->resources, res, crtc, CRTC);
886 if (p->obj_type == 0)
887 find_object(dev->resources, res, connector, CONNECTOR);
888 if (p->obj_type == 0)
889 find_object(dev->resources, plane_res, plane, PLANE);
890 if (p->obj_type == 0) {
891 fprintf(stderr, "Object %i not found, can't set property\n",
897 fprintf(stderr, "%s %i has no properties\n",
898 obj_type, p->obj_id);
902 for (i = 0; i < (int)props->count_props; ++i) {
905 if (strcmp(props_info[i]->name, p->name) == 0)
909 if (i == (int)props->count_props) {
910 fprintf(stderr, "%s %i has no %s property\n",
911 obj_type, p->obj_id, p->name);
915 p->prop_id = props->props[i];
917 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
918 p->prop_id, p->value);
920 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
921 obj_type, p->obj_id, p->name, p->value, strerror(errno));
924 /* -------------------------------------------------------------------------- */
927 page_flip_handler(int fd, unsigned int frame,
928 unsigned int sec, unsigned int usec, void *data)
930 struct pipe_arg *pipe;
931 unsigned int new_fb_id;
936 if (pipe->current_fb_id == pipe->fb_id[0])
937 new_fb_id = pipe->fb_id[1];
939 new_fb_id = pipe->fb_id[0];
941 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
942 DRM_MODE_PAGE_FLIP_EVENT, pipe);
943 pipe->current_fb_id = new_fb_id;
945 if (pipe->swap_count == 60) {
946 gettimeofday(&end, NULL);
947 t = end.tv_sec + end.tv_usec * 1e-6 -
948 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
949 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
950 pipe->swap_count = 0;
955 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
959 for (i = 0; i < ovr->count_formats; ++i) {
960 if (ovr->formats[i] == fmt)
967 static int set_plane(struct device *dev, struct plane_arg *p)
970 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
973 uint32_t plane_flags = 0;
974 int crtc_x, crtc_y, crtc_w, crtc_h;
975 struct crtc *crtc = NULL;
979 /* Find an unused plane which can be connected to our CRTC. Find the
980 * CRTC index first, then iterate over available planes.
982 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
983 if (p->crtc_id == dev->resources->res->crtcs[i]) {
984 crtc = &dev->resources->crtcs[i];
991 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
995 plane_id = p->plane_id;
997 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
998 ovr = dev->resources->planes[i].plane;
1002 if (plane_id && plane_id != ovr->plane_id)
1005 if (!format_support(ovr, p->fourcc))
1008 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id) {
1009 plane_id = ovr->plane_id;
1014 if (i == dev->resources->plane_res->count_planes) {
1015 fprintf(stderr, "no unused plane available for CRTC %u\n",
1016 crtc->crtc->crtc_id);
1020 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1021 p->w, p->h, p->format_str, plane_id);
1023 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1024 pitches, offsets, UTIL_PATTERN_TILES);
1025 if (plane_bo == NULL)
1030 /* just use single plane format for now.. */
1031 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1032 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1033 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1037 crtc_w = p->w * p->scale;
1038 crtc_h = p->h * p->scale;
1039 if (!p->has_position) {
1040 /* Default to the middle of the screen */
1041 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1042 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1048 /* note src coords (last 4 args) are in Q16 format */
1049 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1050 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1051 0, 0, p->w << 16, p->h << 16)) {
1052 fprintf(stderr, "failed to enable plane: %s\n",
1057 ovr->crtc_id = crtc->crtc->crtc_id;
1062 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1066 for (i = 0; i < count; i++) {
1068 drmModeRmFB(dev->fd, p[i].fb_id);
1070 bo_destroy(p[i].bo);
1075 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1077 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1084 dev->mode.width = 0;
1085 dev->mode.height = 0;
1086 dev->mode.fb_id = 0;
1088 for (i = 0; i < count; i++) {
1089 struct pipe_arg *pipe = &pipes[i];
1091 ret = pipe_find_crtc_and_mode(dev, pipe);
1095 dev->mode.width += pipe->mode->hdisplay;
1096 if (dev->mode.height < pipe->mode->vdisplay)
1097 dev->mode.height = pipe->mode->vdisplay;
1100 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1101 dev->mode.height, handles, pitches, offsets,
1102 UTIL_PATTERN_SMPTE);
1108 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1109 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1111 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1112 dev->mode.width, dev->mode.height, strerror(errno));
1116 dev->mode.fb_id = fb_id;
1119 for (i = 0; i < count; i++) {
1120 struct pipe_arg *pipe = &pipes[i];
1122 if (pipe->mode == NULL)
1125 printf("setting mode %s-%dHz@%s on connectors ",
1126 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1127 for (j = 0; j < pipe->num_cons; ++j)
1128 printf("%s, ", pipe->cons[j]);
1129 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1131 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1132 x, 0, pipe->con_ids, pipe->num_cons,
1135 /* XXX: Actually check if this is needed */
1136 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1138 x += pipe->mode->hdisplay;
1141 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1147 static void clear_mode(struct device *dev)
1149 if (dev->mode.fb_id)
1150 drmModeRmFB(dev->fd, dev->mode.fb_id);
1152 bo_destroy(dev->mode.bo);
1155 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1159 /* set up planes/overlays */
1160 for (i = 0; i < count; i++)
1161 if (set_plane(dev, &p[i]))
1165 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1167 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1172 /* maybe make cursor width/height configurable some day */
1176 /* create cursor bo.. just using PATTERN_PLAIN as it has
1179 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1180 offsets, UTIL_PATTERN_PLAIN);
1184 dev->mode.cursor_bo = bo;
1186 for (i = 0; i < count; i++) {
1187 struct pipe_arg *pipe = &pipes[i];
1188 ret = cursor_init(dev->fd, handles[0],
1189 pipe->crtc->crtc->crtc_id,
1190 pipe->mode->hdisplay, pipe->mode->vdisplay,
1193 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1202 static void clear_cursors(struct device *dev)
1206 if (dev->mode.cursor_bo)
1207 bo_destroy(dev->mode.cursor_bo);
1210 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1212 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1213 unsigned int other_fb_id;
1214 struct bo *other_bo;
1215 drmEventContext evctx;
1219 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1220 dev->mode.height, handles, pitches, offsets,
1221 UTIL_PATTERN_PLAIN);
1222 if (other_bo == NULL)
1225 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1226 pipes[0].fourcc, handles, pitches, offsets,
1229 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1233 for (i = 0; i < count; i++) {
1234 struct pipe_arg *pipe = &pipes[i];
1236 if (pipe->mode == NULL)
1239 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1240 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1243 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1246 gettimeofday(&pipe->start, NULL);
1247 pipe->swap_count = 0;
1248 pipe->fb_id[0] = dev->mode.fb_id;
1249 pipe->fb_id[1] = other_fb_id;
1250 pipe->current_fb_id = other_fb_id;
1253 memset(&evctx, 0, sizeof evctx);
1254 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1255 evctx.vblank_handler = NULL;
1256 evctx.page_flip_handler = page_flip_handler;
1260 struct pollfd pfd[2];
1263 pfd[0].events = POLLIN;
1265 pfd[1].events = POLLIN;
1267 if (poll(pfd, 2, -1) < 0) {
1268 fprintf(stderr, "poll error\n");
1275 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1280 FD_SET(dev->fd, &fds);
1281 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1284 fprintf(stderr, "select timed out or error (ret %d)\n",
1287 } else if (FD_ISSET(0, &fds)) {
1292 drmHandleEvent(dev->fd, &evctx);
1296 drmModeRmFB(dev->fd, other_fb_id);
1298 bo_destroy(other_bo);
1301 #define min(a, b) ((a) < (b) ? (a) : (b))
1303 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1311 pipe->crtc_id = (uint32_t)-1;
1312 strcpy(pipe->format_str, "XR24");
1314 /* Count the number of connectors and allocate them. */
1316 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1321 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1322 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1323 if (pipe->con_ids == NULL || pipe->cons == NULL)
1326 /* Parse the connectors. */
1327 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1328 endp = strpbrk(p, ",@:");
1332 pipe->cons[i] = strndup(p, endp - p);
1338 if (i != pipe->num_cons - 1)
1341 /* Parse the remaining parameters. */
1344 pipe->crtc_id = strtoul(arg, &endp, 10);
1351 /* Search for the vertical refresh or the format. */
1352 p = strpbrk(arg, "-@");
1354 p = arg + strlen(arg);
1355 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1356 strncpy(pipe->mode_str, arg, len);
1357 pipe->mode_str[len] = '\0';
1360 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1365 strncpy(pipe->format_str, p + 1, 4);
1366 pipe->format_str[4] = '\0';
1369 pipe->fourcc = util_format_fourcc(pipe->format_str);
1370 if (pipe->fourcc == 0) {
1371 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1378 static int parse_plane(struct plane_arg *plane, const char *p)
1382 plane->plane_id = strtoul(p, &end, 10);
1387 plane->crtc_id = strtoul(p, &end, 10);
1392 plane->w = strtoul(p, &end, 10);
1397 plane->h = strtoul(p, &end, 10);
1399 if (*end == '+' || *end == '-') {
1400 plane->x = strtol(end, &end, 10);
1401 if (*end != '+' && *end != '-')
1403 plane->y = strtol(end, &end, 10);
1405 plane->has_position = true;
1410 plane->scale = strtod(p, &end);
1411 if (plane->scale <= 0.0)
1422 strcpy(plane->format_str, p);
1424 strcpy(plane->format_str, "XR24");
1427 plane->fourcc = util_format_fourcc(plane->format_str);
1428 if (plane->fourcc == 0) {
1429 fprintf(stderr, "unknown format %s\n", plane->format_str);
1436 static int parse_property(struct property_arg *p, const char *arg)
1438 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1442 p->name[DRM_PROP_NAME_LEN] = '\0';
1447 static void usage(char *name)
1449 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1451 fprintf(stderr, "\n Query options:\n\n");
1452 fprintf(stderr, "\t-c\tlist connectors\n");
1453 fprintf(stderr, "\t-e\tlist encoders\n");
1454 fprintf(stderr, "\t-f\tlist framebuffers\n");
1455 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1457 fprintf(stderr, "\n Test options:\n\n");
1458 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1459 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1460 fprintf(stderr, "\t-C\ttest hw cursor\n");
1461 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1462 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1464 fprintf(stderr, "\n Generic options:\n\n");
1465 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1466 fprintf(stderr, "\t-M module\tuse the given driver\n");
1467 fprintf(stderr, "\t-D device\tuse the given device\n");
1469 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1473 static int page_flipping_supported(void)
1475 /*FIXME: generic ioctl needed? */
1479 struct drm_i915_getparam gp;
1481 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1484 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1486 fprintf(stderr, "drm_i915_getparam: %m\n");
1494 static int cursor_supported(void)
1496 /*FIXME: generic ioctl needed? */
1500 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1502 drmModeConnector *connector;
1507 for (i = 0; i < pipe->num_cons; i++) {
1508 id = strtoul(pipe->cons[i], &endp, 10);
1509 if (endp == pipe->cons[i]) {
1510 connector = get_connector_by_name(dev, pipe->cons[i]);
1512 fprintf(stderr, "no connector named '%s'\n",
1517 id = connector->connector_id;
1520 pipe->con_ids[i] = id;
1526 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1528 int main(int argc, char **argv)
1533 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1534 int drop_master = 0;
1536 int test_cursor = 0;
1537 char *device = NULL;
1538 char *module = NULL;
1540 unsigned int count = 0, plane_count = 0;
1541 unsigned int prop_count = 0;
1542 struct pipe_arg *pipe_args = NULL;
1543 struct plane_arg *plane_args = NULL;
1544 struct property_arg *prop_args = NULL;
1545 unsigned int args = 0;
1548 memset(&dev, 0, sizeof dev);
1551 while ((c = getopt(argc, argv, optstr)) != -1) {
1573 /* Preserve the default behaviour of dumping all information. */
1577 plane_args = realloc(plane_args,
1578 (plane_count + 1) * sizeof *plane_args);
1579 if (plane_args == NULL) {
1580 fprintf(stderr, "memory allocation failed\n");
1583 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1585 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1595 pipe_args = realloc(pipe_args,
1596 (count + 1) * sizeof *pipe_args);
1597 if (pipe_args == NULL) {
1598 fprintf(stderr, "memory allocation failed\n");
1601 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1603 if (parse_connector(&pipe_args[count], optarg) < 0)
1615 prop_args = realloc(prop_args,
1616 (prop_count + 1) * sizeof *prop_args);
1617 if (prop_args == NULL) {
1618 fprintf(stderr, "memory allocation failed\n");
1621 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1623 if (parse_property(&prop_args[prop_count], optarg) < 0)
1635 encoders = connectors = crtcs = planes = framebuffers = 1;
1637 dev.fd = util_open(device, module);
1641 if (test_vsync && !page_flipping_supported()) {
1642 fprintf(stderr, "page flipping not supported by drm.\n");
1646 if (test_vsync && !count) {
1647 fprintf(stderr, "page flipping requires at least one -s option.\n");
1651 if (test_cursor && !cursor_supported()) {
1652 fprintf(stderr, "hw cursor not supported by drm.\n");
1656 dev.resources = get_resources(&dev);
1657 if (!dev.resources) {
1662 for (i = 0; i < count; i++) {
1663 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1664 free_resources(dev.resources);
1670 #define dump_resource(dev, res) if (res) dump_##res(dev)
1672 dump_resource(&dev, encoders);
1673 dump_resource(&dev, connectors);
1674 dump_resource(&dev, crtcs);
1675 dump_resource(&dev, planes);
1676 dump_resource(&dev, framebuffers);
1678 for (i = 0; i < prop_count; ++i)
1679 set_property(&dev, &prop_args[i]);
1681 if (count || plane_count) {
1684 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1685 if (ret || cap == 0) {
1686 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1691 set_mode(&dev, pipe_args, count);
1694 set_planes(&dev, plane_args, plane_count);
1697 set_cursors(&dev, pipe_args, count);
1700 test_page_flip(&dev, pipe_args, count);
1703 drmDropMaster(dev.fd);
1708 clear_cursors(&dev);
1711 clear_planes(&dev, plane_args, plane_count);
1717 free_resources(dev.resources);