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.
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
59 #include "xf86drmMode.h"
60 #include "drm_fourcc.h"
62 #include "util/common.h"
63 #include "util/format.h"
65 #include "util/pattern.h"
72 drmModeObjectProperties *props;
73 drmModePropertyRes **props_info;
74 drmModeModeInfo *mode;
78 drmModeEncoder *encoder;
82 drmModeConnector *connector;
83 drmModeObjectProperties *props;
84 drmModePropertyRes **props_info;
94 drmModeObjectProperties *props;
95 drmModePropertyRes **props_info;
100 drmModePlaneRes *plane_res;
103 struct encoder *encoders;
104 struct connector *connectors;
106 struct plane *planes;
112 struct resources *resources;
120 struct bo *cursor_bo;
124 static inline int64_t U642I64(uint64_t val)
126 return (int64_t)*((int64_t *)&val);
129 #define bit_name_fn(res) \
130 const char * res##_str(int type) { \
132 const char *sep = ""; \
133 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
134 if (type & (1 << i)) { \
135 printf("%s%s", sep, res##_names[i]); \
142 static const char *mode_type_names[] = {
152 static bit_name_fn(mode_type)
154 static const char *mode_flag_names[] = {
171 static bit_name_fn(mode_flag)
173 static void dump_fourcc(uint32_t fourcc)
182 static void dump_encoders(struct device *dev)
184 drmModeEncoder *encoder;
187 printf("Encoders:\n");
188 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
189 for (i = 0; i < dev->resources->res->count_encoders; i++) {
190 encoder = dev->resources->encoders[i].encoder;
194 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
197 util_lookup_encoder_type_name(encoder->encoder_type),
198 encoder->possible_crtcs,
199 encoder->possible_clones);
204 static void dump_mode(drmModeModeInfo *mode)
206 printf(" %s %d %d %d %d %d %d %d %d %d %d",
220 mode_flag_str(mode->flags);
222 mode_type_str(mode->type);
226 static void dump_blob(struct device *dev, uint32_t blob_id)
229 unsigned char *blob_data;
230 drmModePropertyBlobPtr blob;
232 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
238 blob_data = blob->data;
240 for (i = 0; i < blob->length; i++) {
243 printf("%.2hhx", blob_data[i]);
247 drmModeFreePropertyBlob(blob);
250 static const char *modifier_to_string(uint64_t modifier)
253 case DRM_FORMAT_MOD_INVALID:
255 case DRM_FORMAT_MOD_LINEAR:
257 case I915_FORMAT_MOD_X_TILED:
259 case I915_FORMAT_MOD_Y_TILED:
261 case I915_FORMAT_MOD_Yf_TILED:
263 case I915_FORMAT_MOD_Y_TILED_CCS:
264 return "Y_TILED_CCS";
265 case I915_FORMAT_MOD_Yf_TILED_CCS:
266 return "Yf_TILED_CCS";
267 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
268 return "SAMSUNG_64_32_TILE";
269 case DRM_FORMAT_MOD_VIVANTE_TILED:
270 return "VIVANTE_TILED";
271 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
272 return "VIVANTE_SUPER_TILED";
273 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
274 return "VIVANTE_SPLIT_TILED";
275 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
276 return "VIVANTE_SPLIT_SUPER_TILED";
277 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
278 return "NVIDIA_TEGRA_TILED";
279 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
280 return "NVIDIA_16BX2_BLOCK(0)";
281 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
282 return "NVIDIA_16BX2_BLOCK(1)";
283 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
284 return "NVIDIA_16BX2_BLOCK(2)";
285 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
286 return "NVIDIA_16BX2_BLOCK(3)";
287 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
288 return "NVIDIA_16BX2_BLOCK(4)";
289 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
290 return "NVIDIA_16BX2_BLOCK(5)";
291 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
292 return "MOD_BROADCOM_VC4_T_TILED";
294 return "(UNKNOWN MODIFIER)";
298 static void dump_in_formats(struct device *dev, uint32_t blob_id)
301 drmModePropertyBlobPtr blob;
302 struct drm_format_modifier_blob *header;
304 struct drm_format_modifier *modifiers;
306 printf("\t\tin_formats blob decoded:\n");
307 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
314 formats = (uint32_t *) ((char *) header + header->formats_offset);
315 modifiers = (struct drm_format_modifier *)
316 ((char *) header + header->modifiers_offset);
318 for (i = 0; i < header->count_formats; i++) {
320 dump_fourcc(formats[i]);
322 for (j = 0; j < header->count_modifiers; j++) {
323 uint64_t mask = 1ULL << i;
324 if (modifiers[j].formats & mask)
325 printf(" %s", modifier_to_string(modifiers[j].modifier));
330 drmModeFreePropertyBlob(blob);
333 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
334 uint32_t prop_id, uint64_t value)
337 printf("\t%d", prop_id);
343 printf(" %s:\n", prop->name);
345 printf("\t\tflags:");
346 if (prop->flags & DRM_MODE_PROP_PENDING)
348 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
349 printf(" immutable");
350 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
351 printf(" signed range");
352 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
354 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
356 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
358 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
360 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
364 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
365 printf("\t\tvalues:");
366 for (i = 0; i < prop->count_values; i++)
367 printf(" %"PRId64, U642I64(prop->values[i]));
371 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
372 printf("\t\tvalues:");
373 for (i = 0; i < prop->count_values; i++)
374 printf(" %"PRIu64, prop->values[i]);
378 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
379 printf("\t\tenums:");
380 for (i = 0; i < prop->count_enums; i++)
381 printf(" %s=%llu", prop->enums[i].name,
382 prop->enums[i].value);
384 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
385 printf("\t\tvalues:");
386 for (i = 0; i < prop->count_enums; i++)
387 printf(" %s=0x%llx", prop->enums[i].name,
388 (1LL << prop->enums[i].value));
391 assert(prop->count_enums == 0);
394 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
395 printf("\t\tblobs:\n");
396 for (i = 0; i < prop->count_blobs; i++)
397 dump_blob(dev, prop->blob_ids[i]);
400 assert(prop->count_blobs == 0);
403 printf("\t\tvalue:");
404 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
405 dump_blob(dev, value);
406 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
407 printf(" %"PRId64"\n", value);
409 printf(" %"PRIu64"\n", value);
411 if (strcmp(prop->name, "IN_FORMATS") == 0)
412 dump_in_formats(dev, value);
415 static void dump_connectors(struct device *dev)
419 printf("Connectors:\n");
420 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
421 for (i = 0; i < dev->resources->res->count_connectors; i++) {
422 struct connector *_connector = &dev->resources->connectors[i];
423 drmModeConnector *connector = _connector->connector;
427 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
428 connector->connector_id,
429 connector->encoder_id,
430 util_lookup_connector_status_name(connector->connection),
432 connector->mmWidth, connector->mmHeight,
433 connector->count_modes);
435 for (j = 0; j < connector->count_encoders; j++)
436 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
439 if (connector->count_modes) {
441 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
443 for (j = 0; j < connector->count_modes; j++)
444 dump_mode(&connector->modes[j]);
447 if (_connector->props) {
449 for (j = 0; j < (int)_connector->props->count_props; j++)
450 dump_prop(dev, _connector->props_info[j],
451 _connector->props->props[j],
452 _connector->props->prop_values[j]);
458 static void dump_crtcs(struct device *dev)
464 printf("id\tfb\tpos\tsize\n");
465 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
466 struct crtc *_crtc = &dev->resources->crtcs[i];
467 drmModeCrtc *crtc = _crtc->crtc;
471 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
475 crtc->width, crtc->height);
476 dump_mode(&crtc->mode);
480 for (j = 0; j < _crtc->props->count_props; j++)
481 dump_prop(dev, _crtc->props_info[j],
482 _crtc->props->props[j],
483 _crtc->props->prop_values[j]);
485 printf(" no properties found\n");
491 static void dump_framebuffers(struct device *dev)
496 printf("Frame buffers:\n");
497 printf("id\tsize\tpitch\n");
498 for (i = 0; i < dev->resources->res->count_fbs; i++) {
499 fb = dev->resources->fbs[i].fb;
503 printf("%u\t(%ux%u)\t%u\n",
505 fb->width, fb->height,
511 static void dump_planes(struct device *dev)
516 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
518 if (!dev->resources->plane_res)
521 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
522 struct plane *plane = &dev->resources->planes[i];
523 drmModePlane *ovr = plane->plane;
527 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
528 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
529 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
530 ovr->gamma_size, ovr->possible_crtcs);
532 if (!ovr->count_formats)
536 for (j = 0; j < ovr->count_formats; j++)
537 dump_fourcc(ovr->formats[j]);
542 for (j = 0; j < plane->props->count_props; j++)
543 dump_prop(dev, plane->props_info[j],
544 plane->props->props[j],
545 plane->props->prop_values[j]);
547 printf(" no properties found\n");
555 static void free_resources(struct resources *res)
562 #define free_resource(_res, __res, type, Type) \
564 if (!(_res)->type##s) \
566 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
567 if (!(_res)->type##s[i].type) \
569 drmModeFree##Type((_res)->type##s[i].type); \
571 free((_res)->type##s); \
574 #define free_properties(_res, __res, type) \
576 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
577 drmModeFreeObjectProperties(res->type##s[i].props); \
578 free(res->type##s[i].props_info); \
583 free_properties(res, res, crtc);
585 free_resource(res, res, crtc, Crtc);
586 free_resource(res, res, encoder, Encoder);
588 for (i = 0; i < res->res->count_connectors; i++)
589 free(res->connectors[i].name);
591 free_resource(res, res, connector, Connector);
592 free_resource(res, res, fb, FB);
594 drmModeFreeResources(res->res);
597 if (res->plane_res) {
598 free_properties(res, plane_res, plane);
600 free_resource(res, plane_res, plane, Plane);
602 drmModeFreePlaneResources(res->plane_res);
608 static struct resources *get_resources(struct device *dev)
610 struct resources *res;
613 res = calloc(1, sizeof(*res));
617 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
619 res->res = drmModeGetResources(dev->fd);
621 fprintf(stderr, "drmModeGetResources failed: %s\n",
626 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
627 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
628 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
629 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
631 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
634 #define get_resource(_res, __res, type, Type) \
636 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
637 (_res)->type##s[i].type = \
638 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
639 if (!(_res)->type##s[i].type) \
640 fprintf(stderr, "could not get %s %i: %s\n", \
641 #type, (_res)->__res->type##s[i], \
646 get_resource(res, res, crtc, Crtc);
647 get_resource(res, res, encoder, Encoder);
648 get_resource(res, res, connector, Connector);
649 get_resource(res, res, fb, FB);
651 /* Set the name of all connectors based on the type name and the per-type ID. */
652 for (i = 0; i < res->res->count_connectors; i++) {
653 struct connector *connector = &res->connectors[i];
654 drmModeConnector *conn = connector->connector;
657 num = asprintf(&connector->name, "%s-%u",
658 util_lookup_connector_type_name(conn->connector_type),
659 conn->connector_type_id);
664 #define get_properties(_res, __res, type, Type) \
666 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
667 struct type *obj = &res->type##s[i]; \
670 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
671 DRM_MODE_OBJECT_##Type); \
674 "could not get %s %i properties: %s\n", \
675 #type, obj->type->type##_id, \
679 obj->props_info = calloc(obj->props->count_props, \
680 sizeof(*obj->props_info)); \
681 if (!obj->props_info) \
683 for (j = 0; j < obj->props->count_props; ++j) \
684 obj->props_info[j] = \
685 drmModeGetProperty(dev->fd, obj->props->props[j]); \
689 get_properties(res, res, crtc, CRTC);
690 get_properties(res, res, connector, CONNECTOR);
692 for (i = 0; i < res->res->count_crtcs; ++i)
693 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
695 res->plane_res = drmModeGetPlaneResources(dev->fd);
696 if (!res->plane_res) {
697 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
702 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
706 get_resource(res, plane_res, plane, Plane);
707 get_properties(res, plane_res, plane, PLANE);
716 static int get_crtc_index(struct device *dev, uint32_t id)
720 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
721 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
722 if (crtc && crtc->crtc_id == id)
729 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
731 struct connector *connector;
734 for (i = 0; i < dev->resources->res->count_connectors; i++) {
735 connector = &dev->resources->connectors[i];
737 if (strcmp(connector->name, name) == 0)
738 return connector->connector;
744 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
746 drmModeConnector *connector;
749 for (i = 0; i < dev->resources->res->count_connectors; i++) {
750 connector = dev->resources->connectors[i].connector;
751 if (connector && connector->connector_id == id)
758 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
760 drmModeEncoder *encoder;
763 for (i = 0; i < dev->resources->res->count_encoders; i++) {
764 encoder = dev->resources->encoders[i].encoder;
765 if (encoder && encoder->encoder_id == id)
772 /* -----------------------------------------------------------------------------
777 * Mode setting with the kernel interfaces is a bit of a chore.
778 * First you have to find the connector in question and make sure the
779 * requested mode is available.
780 * Then you need to find the encoder attached to that connector so you
781 * can bind it with a free crtc.
786 unsigned int num_cons;
790 unsigned int vrefresh;
792 drmModeModeInfo *mode;
794 unsigned int fb_id[2], current_fb_id;
795 struct timeval start;
801 uint32_t plane_id; /* the id of plane to use */
802 uint32_t crtc_id; /* the id of CRTC to bind to */
809 char format_str[5]; /* need to leave room for terminating \0 */
813 static drmModeModeInfo *
814 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
815 const unsigned int vrefresh)
817 drmModeConnector *connector;
818 drmModeModeInfo *mode;
821 connector = get_connector_by_id(dev, con_id);
822 if (!connector || !connector->count_modes)
825 for (i = 0; i < connector->count_modes; i++) {
826 mode = &connector->modes[i];
827 if (!strcmp(mode->name, mode_str)) {
828 /* If the vertical refresh frequency is not specified then return the
829 * first mode that match with the name. Else, return the mode that match
830 * the name and the specified vertical refresh frequency.
834 else if (mode->vrefresh == vrefresh)
842 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
844 uint32_t possible_crtcs = ~0;
845 uint32_t active_crtcs = 0;
846 unsigned int crtc_idx;
850 for (i = 0; i < pipe->num_cons; ++i) {
851 uint32_t crtcs_for_connector = 0;
852 drmModeConnector *connector;
853 drmModeEncoder *encoder;
856 connector = get_connector_by_id(dev, pipe->con_ids[i]);
860 for (j = 0; j < connector->count_encoders; ++j) {
861 encoder = get_encoder_by_id(dev, connector->encoders[j]);
865 crtcs_for_connector |= encoder->possible_crtcs;
867 idx = get_crtc_index(dev, encoder->crtc_id);
869 active_crtcs |= 1 << idx;
872 possible_crtcs &= crtcs_for_connector;
878 /* Return the first possible and active CRTC if one exists, or the first
879 * possible CRTC otherwise.
881 if (possible_crtcs & active_crtcs)
882 crtc_idx = ffs(possible_crtcs & active_crtcs);
884 crtc_idx = ffs(possible_crtcs);
886 return &dev->resources->crtcs[crtc_idx - 1];
889 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
891 drmModeModeInfo *mode = NULL;
896 for (i = 0; i < (int)pipe->num_cons; i++) {
897 mode = connector_find_mode(dev, pipe->con_ids[i],
898 pipe->mode_str, pipe->vrefresh);
901 "failed to find mode \"%s\" for connector %s\n",
902 pipe->mode_str, pipe->cons[i]);
907 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
908 * locate a CRTC that can be attached to all the connectors.
910 if (pipe->crtc_id != (uint32_t)-1) {
911 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
912 struct crtc *crtc = &dev->resources->crtcs[i];
914 if (pipe->crtc_id == crtc->crtc->crtc_id) {
920 pipe->crtc = pipe_find_crtc(dev, pipe);
924 fprintf(stderr, "failed to find CRTC for pipe\n");
929 pipe->crtc->mode = mode;
934 /* -----------------------------------------------------------------------------
938 struct property_arg {
941 char name[DRM_PROP_NAME_LEN+1];
946 static void set_property(struct device *dev, struct property_arg *p)
948 drmModeObjectProperties *props = NULL;
949 drmModePropertyRes **props_info = NULL;
950 const char *obj_type;
957 #define find_object(_res, __res, type, Type) \
959 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
960 struct type *obj = &(_res)->type##s[i]; \
961 if (obj->type->type##_id != p->obj_id) \
963 p->obj_type = DRM_MODE_OBJECT_##Type; \
965 props = obj->props; \
966 props_info = obj->props_info; \
970 find_object(dev->resources, res, crtc, CRTC);
971 if (p->obj_type == 0)
972 find_object(dev->resources, res, connector, CONNECTOR);
973 if (p->obj_type == 0)
974 find_object(dev->resources, plane_res, plane, PLANE);
975 if (p->obj_type == 0) {
976 fprintf(stderr, "Object %i not found, can't set property\n",
982 fprintf(stderr, "%s %i has no properties\n",
983 obj_type, p->obj_id);
987 for (i = 0; i < (int)props->count_props; ++i) {
990 if (strcmp(props_info[i]->name, p->name) == 0)
994 if (i == (int)props->count_props) {
995 fprintf(stderr, "%s %i has no %s property\n",
996 obj_type, p->obj_id, p->name);
1000 p->prop_id = props->props[i];
1002 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1003 p->prop_id, p->value);
1005 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1006 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1009 /* -------------------------------------------------------------------------- */
1012 page_flip_handler(int fd, unsigned int frame,
1013 unsigned int sec, unsigned int usec, void *data)
1015 struct pipe_arg *pipe;
1016 unsigned int new_fb_id;
1021 if (pipe->current_fb_id == pipe->fb_id[0])
1022 new_fb_id = pipe->fb_id[1];
1024 new_fb_id = pipe->fb_id[0];
1026 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
1027 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1028 pipe->current_fb_id = new_fb_id;
1030 if (pipe->swap_count == 60) {
1031 gettimeofday(&end, NULL);
1032 t = end.tv_sec + end.tv_usec * 1e-6 -
1033 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1034 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1035 pipe->swap_count = 0;
1040 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1044 for (i = 0; i < ovr->count_formats; ++i) {
1045 if (ovr->formats[i] == fmt)
1052 static int set_plane(struct device *dev, struct plane_arg *p)
1055 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1057 struct bo *plane_bo;
1058 uint32_t plane_flags = 0;
1059 int crtc_x, crtc_y, crtc_w, crtc_h;
1060 struct crtc *crtc = NULL;
1064 /* Find an unused plane which can be connected to our CRTC. Find the
1065 * CRTC index first, then iterate over available planes.
1067 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1068 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1069 crtc = &dev->resources->crtcs[i];
1076 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1080 plane_id = p->plane_id;
1082 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
1083 ovr = dev->resources->planes[i].plane;
1087 if (plane_id && plane_id != ovr->plane_id)
1090 if (!format_support(ovr, p->fourcc))
1093 if ((ovr->possible_crtcs & (1 << pipe)) &&
1094 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1095 plane_id = ovr->plane_id;
1100 if (i == dev->resources->plane_res->count_planes) {
1101 fprintf(stderr, "no unused plane available for CRTC %u\n",
1102 crtc->crtc->crtc_id);
1106 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1107 p->w, p->h, p->format_str, plane_id);
1109 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1110 pitches, offsets, UTIL_PATTERN_TILES);
1111 if (plane_bo == NULL)
1116 /* just use single plane format for now.. */
1117 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1118 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1119 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1123 crtc_w = p->w * p->scale;
1124 crtc_h = p->h * p->scale;
1125 if (!p->has_position) {
1126 /* Default to the middle of the screen */
1127 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1128 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1134 /* note src coords (last 4 args) are in Q16 format */
1135 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1136 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1137 0, 0, p->w << 16, p->h << 16)) {
1138 fprintf(stderr, "failed to enable plane: %s\n",
1143 ovr->crtc_id = crtc->crtc->crtc_id;
1148 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1152 for (i = 0; i < count; i++) {
1154 drmModeRmFB(dev->fd, p[i].fb_id);
1156 bo_destroy(p[i].bo);
1161 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1163 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1170 dev->mode.width = 0;
1171 dev->mode.height = 0;
1172 dev->mode.fb_id = 0;
1174 for (i = 0; i < count; i++) {
1175 struct pipe_arg *pipe = &pipes[i];
1177 ret = pipe_find_crtc_and_mode(dev, pipe);
1181 dev->mode.width += pipe->mode->hdisplay;
1182 if (dev->mode.height < pipe->mode->vdisplay)
1183 dev->mode.height = pipe->mode->vdisplay;
1186 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1187 dev->mode.height, handles, pitches, offsets,
1188 UTIL_PATTERN_SMPTE);
1194 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1195 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1197 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1198 dev->mode.width, dev->mode.height, strerror(errno));
1202 dev->mode.fb_id = fb_id;
1205 for (i = 0; i < count; i++) {
1206 struct pipe_arg *pipe = &pipes[i];
1208 if (pipe->mode == NULL)
1211 printf("setting mode %s-%dHz@%s on connectors ",
1212 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1213 for (j = 0; j < pipe->num_cons; ++j)
1214 printf("%s, ", pipe->cons[j]);
1215 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1217 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1218 x, 0, pipe->con_ids, pipe->num_cons,
1221 /* XXX: Actually check if this is needed */
1222 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1224 x += pipe->mode->hdisplay;
1227 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1233 static void clear_mode(struct device *dev)
1235 if (dev->mode.fb_id)
1236 drmModeRmFB(dev->fd, dev->mode.fb_id);
1238 bo_destroy(dev->mode.bo);
1241 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1245 /* set up planes/overlays */
1246 for (i = 0; i < count; i++)
1247 if (set_plane(dev, &p[i]))
1251 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1253 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1258 /* maybe make cursor width/height configurable some day */
1262 /* create cursor bo.. just using PATTERN_PLAIN as it has
1265 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1266 offsets, UTIL_PATTERN_PLAIN);
1270 dev->mode.cursor_bo = bo;
1272 for (i = 0; i < count; i++) {
1273 struct pipe_arg *pipe = &pipes[i];
1274 ret = cursor_init(dev->fd, handles[0],
1275 pipe->crtc->crtc->crtc_id,
1276 pipe->mode->hdisplay, pipe->mode->vdisplay,
1279 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1288 static void clear_cursors(struct device *dev)
1292 if (dev->mode.cursor_bo)
1293 bo_destroy(dev->mode.cursor_bo);
1296 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1298 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1299 unsigned int other_fb_id;
1300 struct bo *other_bo;
1301 drmEventContext evctx;
1305 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1306 dev->mode.height, handles, pitches, offsets,
1307 UTIL_PATTERN_PLAIN);
1308 if (other_bo == NULL)
1311 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1312 pipes[0].fourcc, handles, pitches, offsets,
1315 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1319 for (i = 0; i < count; i++) {
1320 struct pipe_arg *pipe = &pipes[i];
1322 if (pipe->mode == NULL)
1325 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1326 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1329 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1332 gettimeofday(&pipe->start, NULL);
1333 pipe->swap_count = 0;
1334 pipe->fb_id[0] = dev->mode.fb_id;
1335 pipe->fb_id[1] = other_fb_id;
1336 pipe->current_fb_id = other_fb_id;
1339 memset(&evctx, 0, sizeof evctx);
1340 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1341 evctx.vblank_handler = NULL;
1342 evctx.page_flip_handler = page_flip_handler;
1346 struct pollfd pfd[2];
1349 pfd[0].events = POLLIN;
1351 pfd[1].events = POLLIN;
1353 if (poll(pfd, 2, -1) < 0) {
1354 fprintf(stderr, "poll error\n");
1361 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1366 FD_SET(dev->fd, &fds);
1367 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1370 fprintf(stderr, "select timed out or error (ret %d)\n",
1373 } else if (FD_ISSET(0, &fds)) {
1378 drmHandleEvent(dev->fd, &evctx);
1382 drmModeRmFB(dev->fd, other_fb_id);
1384 bo_destroy(other_bo);
1387 #define min(a, b) ((a) < (b) ? (a) : (b))
1389 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1397 pipe->crtc_id = (uint32_t)-1;
1398 strcpy(pipe->format_str, "XR24");
1400 /* Count the number of connectors and allocate them. */
1402 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1407 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1408 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1409 if (pipe->con_ids == NULL || pipe->cons == NULL)
1412 /* Parse the connectors. */
1413 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1414 endp = strpbrk(p, ",@:");
1418 pipe->cons[i] = strndup(p, endp - p);
1424 if (i != pipe->num_cons - 1)
1427 /* Parse the remaining parameters. */
1430 pipe->crtc_id = strtoul(arg, &endp, 10);
1437 /* Search for the vertical refresh or the format. */
1438 p = strpbrk(arg, "-@");
1440 p = arg + strlen(arg);
1441 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1442 strncpy(pipe->mode_str, arg, len);
1443 pipe->mode_str[len] = '\0';
1446 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1451 strncpy(pipe->format_str, p + 1, 4);
1452 pipe->format_str[4] = '\0';
1455 pipe->fourcc = util_format_fourcc(pipe->format_str);
1456 if (pipe->fourcc == 0) {
1457 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1464 static int parse_plane(struct plane_arg *plane, const char *p)
1468 plane->plane_id = strtoul(p, &end, 10);
1473 plane->crtc_id = strtoul(p, &end, 10);
1478 plane->w = strtoul(p, &end, 10);
1483 plane->h = strtoul(p, &end, 10);
1485 if (*end == '+' || *end == '-') {
1486 plane->x = strtol(end, &end, 10);
1487 if (*end != '+' && *end != '-')
1489 plane->y = strtol(end, &end, 10);
1491 plane->has_position = true;
1496 plane->scale = strtod(p, &end);
1497 if (plane->scale <= 0.0)
1508 strcpy(plane->format_str, p);
1510 strcpy(plane->format_str, "XR24");
1513 plane->fourcc = util_format_fourcc(plane->format_str);
1514 if (plane->fourcc == 0) {
1515 fprintf(stderr, "unknown format %s\n", plane->format_str);
1522 static int parse_property(struct property_arg *p, const char *arg)
1524 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1528 p->name[DRM_PROP_NAME_LEN] = '\0';
1533 static void usage(char *name)
1535 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1537 fprintf(stderr, "\n Query options:\n\n");
1538 fprintf(stderr, "\t-c\tlist connectors\n");
1539 fprintf(stderr, "\t-e\tlist encoders\n");
1540 fprintf(stderr, "\t-f\tlist framebuffers\n");
1541 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1543 fprintf(stderr, "\n Test options:\n\n");
1544 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1545 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1546 fprintf(stderr, "\t-C\ttest hw cursor\n");
1547 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1548 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1550 fprintf(stderr, "\n Generic options:\n\n");
1551 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1552 fprintf(stderr, "\t-M module\tuse the given driver\n");
1553 fprintf(stderr, "\t-D device\tuse the given device\n");
1555 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1559 static int page_flipping_supported(void)
1561 /*FIXME: generic ioctl needed? */
1565 struct drm_i915_getparam gp;
1567 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1570 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1572 fprintf(stderr, "drm_i915_getparam: %m\n");
1580 static int cursor_supported(void)
1582 /*FIXME: generic ioctl needed? */
1586 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1588 drmModeConnector *connector;
1593 for (i = 0; i < pipe->num_cons; i++) {
1594 id = strtoul(pipe->cons[i], &endp, 10);
1595 if (endp == pipe->cons[i]) {
1596 connector = get_connector_by_name(dev, pipe->cons[i]);
1598 fprintf(stderr, "no connector named '%s'\n",
1603 id = connector->connector_id;
1606 pipe->con_ids[i] = id;
1612 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1614 int main(int argc, char **argv)
1619 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1620 int drop_master = 0;
1622 int test_cursor = 0;
1623 char *device = NULL;
1624 char *module = NULL;
1626 unsigned int count = 0, plane_count = 0;
1627 unsigned int prop_count = 0;
1628 struct pipe_arg *pipe_args = NULL;
1629 struct plane_arg *plane_args = NULL;
1630 struct property_arg *prop_args = NULL;
1631 unsigned int args = 0;
1634 memset(&dev, 0, sizeof dev);
1637 while ((c = getopt(argc, argv, optstr)) != -1) {
1659 /* Preserve the default behaviour of dumping all information. */
1663 plane_args = realloc(plane_args,
1664 (plane_count + 1) * sizeof *plane_args);
1665 if (plane_args == NULL) {
1666 fprintf(stderr, "memory allocation failed\n");
1669 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1671 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1681 pipe_args = realloc(pipe_args,
1682 (count + 1) * sizeof *pipe_args);
1683 if (pipe_args == NULL) {
1684 fprintf(stderr, "memory allocation failed\n");
1687 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1689 if (parse_connector(&pipe_args[count], optarg) < 0)
1701 prop_args = realloc(prop_args,
1702 (prop_count + 1) * sizeof *prop_args);
1703 if (prop_args == NULL) {
1704 fprintf(stderr, "memory allocation failed\n");
1707 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1709 if (parse_property(&prop_args[prop_count], optarg) < 0)
1721 encoders = connectors = crtcs = planes = framebuffers = 1;
1723 dev.fd = util_open(device, module);
1727 if (test_vsync && !page_flipping_supported()) {
1728 fprintf(stderr, "page flipping not supported by drm.\n");
1732 if (test_vsync && !count) {
1733 fprintf(stderr, "page flipping requires at least one -s option.\n");
1737 if (test_cursor && !cursor_supported()) {
1738 fprintf(stderr, "hw cursor not supported by drm.\n");
1742 dev.resources = get_resources(&dev);
1743 if (!dev.resources) {
1748 for (i = 0; i < count; i++) {
1749 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1750 free_resources(dev.resources);
1756 #define dump_resource(dev, res) if (res) dump_##res(dev)
1758 dump_resource(&dev, encoders);
1759 dump_resource(&dev, connectors);
1760 dump_resource(&dev, crtcs);
1761 dump_resource(&dev, planes);
1762 dump_resource(&dev, framebuffers);
1764 for (i = 0; i < prop_count; ++i)
1765 set_property(&dev, &prop_args[i]);
1767 if (count || plane_count) {
1770 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1771 if (ret || cap == 0) {
1772 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1777 set_mode(&dev, pipe_args, count);
1780 set_planes(&dev, plane_args, plane_count);
1783 set_cursors(&dev, pipe_args, count);
1786 test_page_flip(&dev, pipe_args, count);
1789 drmDropMaster(dev.fd);
1794 clear_cursors(&dev);
1797 clear_planes(&dev, plane_args, plane_count);
1803 free_resources(dev.resources);