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.
60 #include "xf86drmMode.h"
61 #include "drm_fourcc.h"
63 #include "util/common.h"
64 #include "util/format.h"
66 #include "util/pattern.h"
73 drmModeObjectProperties *props;
74 drmModePropertyRes **props_info;
75 drmModeModeInfo *mode;
79 drmModeEncoder *encoder;
83 drmModeConnector *connector;
84 drmModeObjectProperties *props;
85 drmModePropertyRes **props_info;
95 drmModeObjectProperties *props;
96 drmModePropertyRes **props_info;
101 drmModePlaneRes *plane_res;
104 struct encoder *encoders;
105 struct connector *connectors;
107 struct plane *planes;
113 struct resources *resources;
121 struct bo *cursor_bo;
125 static inline int64_t U642I64(uint64_t val)
127 return (int64_t)*((int64_t *)&val);
130 #define bit_name_fn(res) \
131 const char * res##_str(int type) { \
133 const char *sep = ""; \
134 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
135 if (type & (1 << i)) { \
136 printf("%s%s", sep, res##_names[i]); \
143 static const char *mode_type_names[] = {
153 static bit_name_fn(mode_type)
155 static const char *mode_flag_names[] = {
172 static bit_name_fn(mode_flag)
174 static void dump_encoders(struct device *dev)
176 drmModeEncoder *encoder;
179 printf("Encoders:\n");
180 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
181 for (i = 0; i < dev->resources->res->count_encoders; i++) {
182 encoder = dev->resources->encoders[i].encoder;
186 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
189 util_lookup_encoder_type_name(encoder->encoder_type),
190 encoder->possible_crtcs,
191 encoder->possible_clones);
196 static void dump_mode(drmModeModeInfo *mode)
198 printf(" %s %d %d %d %d %d %d %d %d %d",
211 mode_flag_str(mode->flags);
213 mode_type_str(mode->type);
217 static void dump_blob(struct device *dev, uint32_t blob_id)
220 unsigned char *blob_data;
221 drmModePropertyBlobPtr blob;
223 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
229 blob_data = blob->data;
231 for (i = 0; i < blob->length; i++) {
234 printf("%.2hhx", blob_data[i]);
238 drmModeFreePropertyBlob(blob);
241 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
242 uint32_t prop_id, uint64_t value)
245 printf("\t%d", prop_id);
251 printf(" %s:\n", prop->name);
253 printf("\t\tflags:");
254 if (prop->flags & DRM_MODE_PROP_PENDING)
256 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
257 printf(" immutable");
258 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
259 printf(" signed range");
260 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
262 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
264 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
266 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
268 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
272 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
273 printf("\t\tvalues:");
274 for (i = 0; i < prop->count_values; i++)
275 printf(" %"PRId64, U642I64(prop->values[i]));
279 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
280 printf("\t\tvalues:");
281 for (i = 0; i < prop->count_values; i++)
282 printf(" %"PRIu64, prop->values[i]);
286 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
287 printf("\t\tenums:");
288 for (i = 0; i < prop->count_enums; i++)
289 printf(" %s=%llu", prop->enums[i].name,
290 prop->enums[i].value);
292 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
293 printf("\t\tvalues:");
294 for (i = 0; i < prop->count_enums; i++)
295 printf(" %s=0x%llx", prop->enums[i].name,
296 (1LL << prop->enums[i].value));
299 assert(prop->count_enums == 0);
302 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
303 printf("\t\tblobs:\n");
304 for (i = 0; i < prop->count_blobs; i++)
305 dump_blob(dev, prop->blob_ids[i]);
308 assert(prop->count_blobs == 0);
311 printf("\t\tvalue:");
312 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
313 dump_blob(dev, value);
315 printf(" %"PRIu64"\n", value);
318 static void dump_connectors(struct device *dev)
322 printf("Connectors:\n");
323 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
324 for (i = 0; i < dev->resources->res->count_connectors; i++) {
325 struct connector *_connector = &dev->resources->connectors[i];
326 drmModeConnector *connector = _connector->connector;
330 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
331 connector->connector_id,
332 connector->encoder_id,
333 util_lookup_connector_status_name(connector->connection),
335 connector->mmWidth, connector->mmHeight,
336 connector->count_modes);
338 for (j = 0; j < connector->count_encoders; j++)
339 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
342 if (connector->count_modes) {
344 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
346 for (j = 0; j < connector->count_modes; j++)
347 dump_mode(&connector->modes[j]);
350 if (_connector->props) {
352 for (j = 0; j < (int)_connector->props->count_props; j++)
353 dump_prop(dev, _connector->props_info[j],
354 _connector->props->props[j],
355 _connector->props->prop_values[j]);
361 static void dump_crtcs(struct device *dev)
367 printf("id\tfb\tpos\tsize\n");
368 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
369 struct crtc *_crtc = &dev->resources->crtcs[i];
370 drmModeCrtc *crtc = _crtc->crtc;
374 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
378 crtc->width, crtc->height);
379 dump_mode(&crtc->mode);
383 for (j = 0; j < _crtc->props->count_props; j++)
384 dump_prop(dev, _crtc->props_info[j],
385 _crtc->props->props[j],
386 _crtc->props->prop_values[j]);
388 printf(" no properties found\n");
394 static void dump_framebuffers(struct device *dev)
399 printf("Frame buffers:\n");
400 printf("id\tsize\tpitch\n");
401 for (i = 0; i < dev->resources->res->count_fbs; i++) {
402 fb = dev->resources->fbs[i].fb;
406 printf("%u\t(%ux%u)\t%u\n",
408 fb->width, fb->height,
414 static void dump_planes(struct device *dev)
419 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
421 if (!dev->resources->plane_res)
424 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
425 struct plane *plane = &dev->resources->planes[i];
426 drmModePlane *ovr = plane->plane;
430 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
431 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
432 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
433 ovr->gamma_size, ovr->possible_crtcs);
435 if (!ovr->count_formats)
439 for (j = 0; j < ovr->count_formats; j++)
440 printf(" %4.4s", (char *)&ovr->formats[j]);
445 for (j = 0; j < plane->props->count_props; j++)
446 dump_prop(dev, plane->props_info[j],
447 plane->props->props[j],
448 plane->props->prop_values[j]);
450 printf(" no properties found\n");
458 static void free_resources(struct resources *res)
465 #define free_resource(_res, __res, type, Type) \
467 if (!(_res)->type##s) \
469 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
470 if (!(_res)->type##s[i].type) \
472 drmModeFree##Type((_res)->type##s[i].type); \
474 free((_res)->type##s); \
477 #define free_properties(_res, __res, type) \
479 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
480 drmModeFreeObjectProperties(res->type##s[i].props); \
481 free(res->type##s[i].props_info); \
486 free_properties(res, res, crtc);
488 free_resource(res, res, crtc, Crtc);
489 free_resource(res, res, encoder, Encoder);
491 for (i = 0; i < res->res->count_connectors; i++)
492 free(res->connectors[i].name);
494 free_resource(res, res, connector, Connector);
495 free_resource(res, res, fb, FB);
497 drmModeFreeResources(res->res);
500 if (res->plane_res) {
501 free_properties(res, plane_res, plane);
503 free_resource(res, plane_res, plane, Plane);
505 drmModeFreePlaneResources(res->plane_res);
511 static struct resources *get_resources(struct device *dev)
513 struct resources *res;
516 res = calloc(1, sizeof(*res));
520 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
522 res->res = drmModeGetResources(dev->fd);
524 fprintf(stderr, "drmModeGetResources failed: %s\n",
529 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
530 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
531 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
532 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
534 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
537 #define get_resource(_res, __res, type, Type) \
539 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
540 (_res)->type##s[i].type = \
541 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
542 if (!(_res)->type##s[i].type) \
543 fprintf(stderr, "could not get %s %i: %s\n", \
544 #type, (_res)->__res->type##s[i], \
549 get_resource(res, res, crtc, Crtc);
550 get_resource(res, res, encoder, Encoder);
551 get_resource(res, res, connector, Connector);
552 get_resource(res, res, fb, FB);
554 /* Set the name of all connectors based on the type name and the per-type ID. */
555 for (i = 0; i < res->res->count_connectors; i++) {
556 struct connector *connector = &res->connectors[i];
557 drmModeConnector *conn = connector->connector;
559 asprintf(&connector->name, "%s-%u",
560 util_lookup_connector_type_name(conn->connector_type),
561 conn->connector_type_id);
564 #define get_properties(_res, __res, type, Type) \
566 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
567 struct type *obj = &res->type##s[i]; \
570 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
571 DRM_MODE_OBJECT_##Type); \
574 "could not get %s %i properties: %s\n", \
575 #type, obj->type->type##_id, \
579 obj->props_info = calloc(obj->props->count_props, \
580 sizeof(*obj->props_info)); \
581 if (!obj->props_info) \
583 for (j = 0; j < obj->props->count_props; ++j) \
584 obj->props_info[j] = \
585 drmModeGetProperty(dev->fd, obj->props->props[j]); \
589 get_properties(res, res, crtc, CRTC);
590 get_properties(res, res, connector, CONNECTOR);
592 for (i = 0; i < res->res->count_crtcs; ++i)
593 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
595 res->plane_res = drmModeGetPlaneResources(dev->fd);
596 if (!res->plane_res) {
597 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
602 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
606 get_resource(res, plane_res, plane, Plane);
607 get_properties(res, plane_res, plane, PLANE);
616 static int get_crtc_index(struct device *dev, uint32_t id)
620 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
621 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
622 if (crtc && crtc->crtc_id == id)
629 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
631 struct connector *connector;
634 for (i = 0; i < dev->resources->res->count_connectors; i++) {
635 connector = &dev->resources->connectors[i];
637 if (strcmp(connector->name, name) == 0)
638 return connector->connector;
644 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
646 drmModeConnector *connector;
649 for (i = 0; i < dev->resources->res->count_connectors; i++) {
650 connector = dev->resources->connectors[i].connector;
651 if (connector && connector->connector_id == id)
658 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
660 drmModeEncoder *encoder;
663 for (i = 0; i < dev->resources->res->count_encoders; i++) {
664 encoder = dev->resources->encoders[i].encoder;
665 if (encoder && encoder->encoder_id == id)
672 /* -----------------------------------------------------------------------------
677 * Mode setting with the kernel interfaces is a bit of a chore.
678 * First you have to find the connector in question and make sure the
679 * requested mode is available.
680 * Then you need to find the encoder attached to that connector so you
681 * can bind it with a free crtc.
686 unsigned int num_cons;
690 unsigned int vrefresh;
692 drmModeModeInfo *mode;
694 unsigned int fb_id[2], current_fb_id;
695 struct timeval start;
701 uint32_t crtc_id; /* the id of CRTC to bind to */
708 char format_str[5]; /* need to leave room for terminating \0 */
712 static drmModeModeInfo *
713 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
714 const unsigned int vrefresh)
716 drmModeConnector *connector;
717 drmModeModeInfo *mode;
720 connector = get_connector_by_id(dev, con_id);
721 if (!connector || !connector->count_modes)
724 for (i = 0; i < connector->count_modes; i++) {
725 mode = &connector->modes[i];
726 if (!strcmp(mode->name, mode_str)) {
727 /* If the vertical refresh frequency is not specified then return the
728 * first mode that match with the name. Else, return the mode that match
729 * the name and the specified vertical refresh frequency.
733 else if (mode->vrefresh == vrefresh)
741 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
743 uint32_t possible_crtcs = ~0;
744 uint32_t active_crtcs = 0;
745 unsigned int crtc_idx;
749 for (i = 0; i < pipe->num_cons; ++i) {
750 uint32_t crtcs_for_connector = 0;
751 drmModeConnector *connector;
752 drmModeEncoder *encoder;
755 connector = get_connector_by_id(dev, pipe->con_ids[i]);
759 for (j = 0; j < connector->count_encoders; ++j) {
760 encoder = get_encoder_by_id(dev, connector->encoders[j]);
764 crtcs_for_connector |= encoder->possible_crtcs;
766 idx = get_crtc_index(dev, encoder->crtc_id);
768 active_crtcs |= 1 << idx;
771 possible_crtcs &= crtcs_for_connector;
777 /* Return the first possible and active CRTC if one exists, or the first
778 * possible CRTC otherwise.
780 if (possible_crtcs & active_crtcs)
781 crtc_idx = ffs(possible_crtcs & active_crtcs);
783 crtc_idx = ffs(possible_crtcs);
785 return &dev->resources->crtcs[crtc_idx - 1];
788 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
790 drmModeModeInfo *mode = NULL;
795 for (i = 0; i < (int)pipe->num_cons; i++) {
796 mode = connector_find_mode(dev, pipe->con_ids[i],
797 pipe->mode_str, pipe->vrefresh);
800 "failed to find mode \"%s\" for connector %s\n",
801 pipe->mode_str, pipe->cons[i]);
806 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
807 * locate a CRTC that can be attached to all the connectors.
809 if (pipe->crtc_id != (uint32_t)-1) {
810 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
811 struct crtc *crtc = &dev->resources->crtcs[i];
813 if (pipe->crtc_id == crtc->crtc->crtc_id) {
819 pipe->crtc = pipe_find_crtc(dev, pipe);
823 fprintf(stderr, "failed to find CRTC for pipe\n");
828 pipe->crtc->mode = mode;
833 /* -----------------------------------------------------------------------------
837 struct property_arg {
840 char name[DRM_PROP_NAME_LEN+1];
845 static void set_property(struct device *dev, struct property_arg *p)
847 drmModeObjectProperties *props = NULL;
848 drmModePropertyRes **props_info = NULL;
849 const char *obj_type;
856 #define find_object(_res, __res, type, Type) \
858 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
859 struct type *obj = &(_res)->type##s[i]; \
860 if (obj->type->type##_id != p->obj_id) \
862 p->obj_type = DRM_MODE_OBJECT_##Type; \
864 props = obj->props; \
865 props_info = obj->props_info; \
869 find_object(dev->resources, res, crtc, CRTC);
870 if (p->obj_type == 0)
871 find_object(dev->resources, res, connector, CONNECTOR);
872 if (p->obj_type == 0)
873 find_object(dev->resources, plane_res, plane, PLANE);
874 if (p->obj_type == 0) {
875 fprintf(stderr, "Object %i not found, can't set property\n",
881 fprintf(stderr, "%s %i has no properties\n",
882 obj_type, p->obj_id);
886 for (i = 0; i < (int)props->count_props; ++i) {
889 if (strcmp(props_info[i]->name, p->name) == 0)
893 if (i == (int)props->count_props) {
894 fprintf(stderr, "%s %i has no %s property\n",
895 obj_type, p->obj_id, p->name);
899 p->prop_id = props->props[i];
901 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
902 p->prop_id, p->value);
904 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
905 obj_type, p->obj_id, p->name, p->value, strerror(errno));
908 /* -------------------------------------------------------------------------- */
911 page_flip_handler(int fd, unsigned int frame,
912 unsigned int sec, unsigned int usec, void *data)
914 struct pipe_arg *pipe;
915 unsigned int new_fb_id;
920 if (pipe->current_fb_id == pipe->fb_id[0])
921 new_fb_id = pipe->fb_id[1];
923 new_fb_id = pipe->fb_id[0];
925 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
926 DRM_MODE_PAGE_FLIP_EVENT, pipe);
927 pipe->current_fb_id = new_fb_id;
929 if (pipe->swap_count == 60) {
930 gettimeofday(&end, NULL);
931 t = end.tv_sec + end.tv_usec * 1e-6 -
932 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
933 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
934 pipe->swap_count = 0;
939 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
943 for (i = 0; i < ovr->count_formats; ++i) {
944 if (ovr->formats[i] == fmt)
951 static int set_plane(struct device *dev, struct plane_arg *p)
954 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
955 uint32_t plane_id = 0;
957 uint32_t plane_flags = 0;
958 int crtc_x, crtc_y, crtc_w, crtc_h;
959 struct crtc *crtc = NULL;
963 /* Find an unused plane which can be connected to our CRTC. Find the
964 * CRTC index first, then iterate over available planes.
966 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
967 if (p->crtc_id == dev->resources->res->crtcs[i]) {
968 crtc = &dev->resources->crtcs[i];
975 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
979 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
980 ovr = dev->resources->planes[i].plane;
981 if (!ovr || !format_support(ovr, p->fourcc))
984 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
985 plane_id = ovr->plane_id;
989 fprintf(stderr, "no unused plane available for CRTC %u\n",
990 crtc->crtc->crtc_id);
994 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
995 p->w, p->h, p->format_str, plane_id);
997 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
998 pitches, offsets, UTIL_PATTERN_TILES);
999 if (plane_bo == NULL)
1004 /* just use single plane format for now.. */
1005 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1006 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1007 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1011 crtc_w = p->w * p->scale;
1012 crtc_h = p->h * p->scale;
1013 if (!p->has_position) {
1014 /* Default to the middle of the screen */
1015 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1016 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1022 /* note src coords (last 4 args) are in Q16 format */
1023 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1024 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1025 0, 0, p->w << 16, p->h << 16)) {
1026 fprintf(stderr, "failed to enable plane: %s\n",
1031 ovr->crtc_id = crtc->crtc->crtc_id;
1036 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1040 for (i = 0; i < count; i++) {
1042 drmModeRmFB(dev->fd, p[i].fb_id);
1044 bo_destroy(p[i].bo);
1049 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1051 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1058 dev->mode.width = 0;
1059 dev->mode.height = 0;
1060 dev->mode.fb_id = 0;
1062 for (i = 0; i < count; i++) {
1063 struct pipe_arg *pipe = &pipes[i];
1065 ret = pipe_find_crtc_and_mode(dev, pipe);
1069 dev->mode.width += pipe->mode->hdisplay;
1070 if (dev->mode.height < pipe->mode->vdisplay)
1071 dev->mode.height = pipe->mode->vdisplay;
1074 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1075 dev->mode.height, handles, pitches, offsets,
1076 UTIL_PATTERN_SMPTE);
1082 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1083 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1085 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1086 dev->mode.width, dev->mode.height, strerror(errno));
1090 dev->mode.fb_id = fb_id;
1093 for (i = 0; i < count; i++) {
1094 struct pipe_arg *pipe = &pipes[i];
1096 if (pipe->mode == NULL)
1099 printf("setting mode %s-%dHz@%s on connectors ",
1100 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1101 for (j = 0; j < pipe->num_cons; ++j)
1102 printf("%s, ", pipe->cons[j]);
1103 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1105 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1106 x, 0, pipe->con_ids, pipe->num_cons,
1109 /* XXX: Actually check if this is needed */
1110 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1112 x += pipe->mode->hdisplay;
1115 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1121 static void clear_mode(struct device *dev)
1123 if (dev->mode.fb_id)
1124 drmModeRmFB(dev->fd, dev->mode.fb_id);
1126 bo_destroy(dev->mode.bo);
1129 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1133 /* set up planes/overlays */
1134 for (i = 0; i < count; i++)
1135 if (set_plane(dev, &p[i]))
1139 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1141 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1146 /* maybe make cursor width/height configurable some day */
1150 /* create cursor bo.. just using PATTERN_PLAIN as it has
1153 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1154 offsets, UTIL_PATTERN_PLAIN);
1158 dev->mode.cursor_bo = bo;
1160 for (i = 0; i < count; i++) {
1161 struct pipe_arg *pipe = &pipes[i];
1162 ret = cursor_init(dev->fd, handles[0],
1163 pipe->crtc->crtc->crtc_id,
1164 pipe->mode->hdisplay, pipe->mode->vdisplay,
1167 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1176 static void clear_cursors(struct device *dev)
1180 if (dev->mode.cursor_bo)
1181 bo_destroy(dev->mode.cursor_bo);
1184 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1186 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1187 unsigned int other_fb_id;
1188 struct bo *other_bo;
1189 drmEventContext evctx;
1193 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1194 dev->mode.height, handles, pitches, offsets,
1195 UTIL_PATTERN_PLAIN);
1196 if (other_bo == NULL)
1199 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1200 pipes[0].fourcc, handles, pitches, offsets,
1203 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1207 for (i = 0; i < count; i++) {
1208 struct pipe_arg *pipe = &pipes[i];
1210 if (pipe->mode == NULL)
1213 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1214 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1217 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1220 gettimeofday(&pipe->start, NULL);
1221 pipe->swap_count = 0;
1222 pipe->fb_id[0] = dev->mode.fb_id;
1223 pipe->fb_id[1] = other_fb_id;
1224 pipe->current_fb_id = other_fb_id;
1227 memset(&evctx, 0, sizeof evctx);
1228 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1229 evctx.vblank_handler = NULL;
1230 evctx.page_flip_handler = page_flip_handler;
1234 struct pollfd pfd[2];
1237 pfd[0].events = POLLIN;
1239 pfd[1].events = POLLIN;
1241 if (poll(pfd, 2, -1) < 0) {
1242 fprintf(stderr, "poll error\n");
1249 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1254 FD_SET(dev->fd, &fds);
1255 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1258 fprintf(stderr, "select timed out or error (ret %d)\n",
1261 } else if (FD_ISSET(0, &fds)) {
1266 drmHandleEvent(dev->fd, &evctx);
1270 drmModeRmFB(dev->fd, other_fb_id);
1272 bo_destroy(other_bo);
1275 #define min(a, b) ((a) < (b) ? (a) : (b))
1277 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1285 pipe->crtc_id = (uint32_t)-1;
1286 strcpy(pipe->format_str, "XR24");
1288 /* Count the number of connectors and allocate them. */
1290 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1295 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1296 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1297 if (pipe->con_ids == NULL || pipe->cons == NULL)
1300 /* Parse the connectors. */
1301 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1302 endp = strpbrk(p, ",@:");
1306 pipe->cons[i] = strndup(p, endp - p);
1312 if (i != pipe->num_cons - 1)
1315 /* Parse the remaining parameters. */
1318 pipe->crtc_id = strtoul(arg, &endp, 10);
1325 /* Search for the vertical refresh or the format. */
1326 p = strpbrk(arg, "-@");
1328 p = arg + strlen(arg);
1329 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1330 strncpy(pipe->mode_str, arg, len);
1331 pipe->mode_str[len] = '\0';
1334 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1339 strncpy(pipe->format_str, p + 1, 4);
1340 pipe->format_str[4] = '\0';
1343 pipe->fourcc = util_format_fourcc(pipe->format_str);
1344 if (pipe->fourcc == 0) {
1345 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1352 static int parse_plane(struct plane_arg *plane, const char *p)
1356 plane->crtc_id = strtoul(p, &end, 10);
1361 plane->w = strtoul(p, &end, 10);
1366 plane->h = strtoul(p, &end, 10);
1368 if (*end == '+' || *end == '-') {
1369 plane->x = strtol(end, &end, 10);
1370 if (*end != '+' && *end != '-')
1372 plane->y = strtol(end, &end, 10);
1374 plane->has_position = true;
1379 plane->scale = strtod(p, &end);
1380 if (plane->scale <= 0.0)
1391 strcpy(plane->format_str, p);
1393 strcpy(plane->format_str, "XR24");
1396 plane->fourcc = util_format_fourcc(plane->format_str);
1397 if (plane->fourcc == 0) {
1398 fprintf(stderr, "unknown format %s\n", plane->format_str);
1405 static int parse_property(struct property_arg *p, const char *arg)
1407 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1411 p->name[DRM_PROP_NAME_LEN] = '\0';
1416 static void usage(char *name)
1418 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1420 fprintf(stderr, "\n Query options:\n\n");
1421 fprintf(stderr, "\t-c\tlist connectors\n");
1422 fprintf(stderr, "\t-e\tlist encoders\n");
1423 fprintf(stderr, "\t-f\tlist framebuffers\n");
1424 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1426 fprintf(stderr, "\n Test options:\n\n");
1427 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1428 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1429 fprintf(stderr, "\t-C\ttest hw cursor\n");
1430 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1431 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1433 fprintf(stderr, "\n Generic options:\n\n");
1434 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1435 fprintf(stderr, "\t-M module\tuse the given driver\n");
1436 fprintf(stderr, "\t-D device\tuse the given device\n");
1438 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1442 static int page_flipping_supported(void)
1444 /*FIXME: generic ioctl needed? */
1448 struct drm_i915_getparam gp;
1450 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1453 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1455 fprintf(stderr, "drm_i915_getparam: %m\n");
1463 static int cursor_supported(void)
1465 /*FIXME: generic ioctl needed? */
1469 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1471 drmModeConnector *connector;
1476 for (i = 0; i < pipe->num_cons; i++) {
1477 id = strtoul(pipe->cons[i], &endp, 10);
1478 if (endp == pipe->cons[i]) {
1479 connector = get_connector_by_name(dev, pipe->cons[i]);
1481 fprintf(stderr, "no connector named '%s'\n",
1486 id = connector->connector_id;
1489 pipe->con_ids[i] = id;
1495 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1497 int main(int argc, char **argv)
1502 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1503 int drop_master = 0;
1505 int test_cursor = 0;
1506 char *device = NULL;
1507 char *module = NULL;
1509 unsigned int count = 0, plane_count = 0;
1510 unsigned int prop_count = 0;
1511 struct pipe_arg *pipe_args = NULL;
1512 struct plane_arg *plane_args = NULL;
1513 struct property_arg *prop_args = NULL;
1514 unsigned int args = 0;
1517 memset(&dev, 0, sizeof dev);
1520 while ((c = getopt(argc, argv, optstr)) != -1) {
1542 /* Preserve the default behaviour of dumping all information. */
1546 plane_args = realloc(plane_args,
1547 (plane_count + 1) * sizeof *plane_args);
1548 if (plane_args == NULL) {
1549 fprintf(stderr, "memory allocation failed\n");
1552 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1554 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1564 pipe_args = realloc(pipe_args,
1565 (count + 1) * sizeof *pipe_args);
1566 if (pipe_args == NULL) {
1567 fprintf(stderr, "memory allocation failed\n");
1570 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1572 if (parse_connector(&pipe_args[count], optarg) < 0)
1584 prop_args = realloc(prop_args,
1585 (prop_count + 1) * sizeof *prop_args);
1586 if (prop_args == NULL) {
1587 fprintf(stderr, "memory allocation failed\n");
1590 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1592 if (parse_property(&prop_args[prop_count], optarg) < 0)
1604 encoders = connectors = crtcs = planes = framebuffers = 1;
1606 dev.fd = util_open(device, module);
1610 if (test_vsync && !page_flipping_supported()) {
1611 fprintf(stderr, "page flipping not supported by drm.\n");
1615 if (test_vsync && !count) {
1616 fprintf(stderr, "page flipping requires at least one -s option.\n");
1620 if (test_cursor && !cursor_supported()) {
1621 fprintf(stderr, "hw cursor not supported by drm.\n");
1625 dev.resources = get_resources(&dev);
1626 if (!dev.resources) {
1631 for (i = 0; i < count; i++) {
1632 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1633 free_resources(dev.resources);
1639 #define dump_resource(dev, res) if (res) dump_##res(dev)
1641 dump_resource(&dev, encoders);
1642 dump_resource(&dev, connectors);
1643 dump_resource(&dev, crtcs);
1644 dump_resource(&dev, planes);
1645 dump_resource(&dev, framebuffers);
1647 for (i = 0; i < prop_count; ++i)
1648 set_property(&dev, &prop_args[i]);
1650 if (count || plane_count) {
1653 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1654 if (ret || cap == 0) {
1655 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1660 set_mode(&dev, pipe_args, count);
1663 set_planes(&dev, plane_args, plane_count);
1666 set_cursors(&dev, pipe_args, count);
1669 test_page_flip(&dev, pipe_args, count);
1672 drmDropMaster(dev.fd);
1677 clear_cursors(&dev);
1680 clear_planes(&dev, plane_args, plane_count);
1686 free_resources(dev.resources);