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_encoders(struct device *dev)
179 drmModeEncoder *encoder;
182 printf("Encoders:\n");
183 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
184 for (i = 0; i < dev->resources->res->count_encoders; i++) {
185 encoder = dev->resources->encoders[i].encoder;
189 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
192 util_lookup_encoder_type_name(encoder->encoder_type),
193 encoder->possible_crtcs,
194 encoder->possible_clones);
199 static void dump_mode(drmModeModeInfo *mode)
201 printf(" %s %d %d %d %d %d %d %d %d %d %d",
215 mode_flag_str(mode->flags);
217 mode_type_str(mode->type);
221 static void dump_blob(struct device *dev, uint32_t blob_id)
224 unsigned char *blob_data;
225 drmModePropertyBlobPtr blob;
227 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
233 blob_data = blob->data;
235 for (i = 0; i < blob->length; i++) {
238 printf("%.2hhx", blob_data[i]);
242 drmModeFreePropertyBlob(blob);
245 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
246 uint32_t prop_id, uint64_t value)
249 printf("\t%d", prop_id);
255 printf(" %s:\n", prop->name);
257 printf("\t\tflags:");
258 if (prop->flags & DRM_MODE_PROP_PENDING)
260 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
261 printf(" immutable");
262 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
263 printf(" signed range");
264 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
266 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
268 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
270 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
272 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
276 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
277 printf("\t\tvalues:");
278 for (i = 0; i < prop->count_values; i++)
279 printf(" %"PRId64, U642I64(prop->values[i]));
283 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
284 printf("\t\tvalues:");
285 for (i = 0; i < prop->count_values; i++)
286 printf(" %"PRIu64, prop->values[i]);
290 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
291 printf("\t\tenums:");
292 for (i = 0; i < prop->count_enums; i++)
293 printf(" %s=%llu", prop->enums[i].name,
294 prop->enums[i].value);
296 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
297 printf("\t\tvalues:");
298 for (i = 0; i < prop->count_enums; i++)
299 printf(" %s=0x%llx", prop->enums[i].name,
300 (1LL << prop->enums[i].value));
303 assert(prop->count_enums == 0);
306 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
307 printf("\t\tblobs:\n");
308 for (i = 0; i < prop->count_blobs; i++)
309 dump_blob(dev, prop->blob_ids[i]);
312 assert(prop->count_blobs == 0);
315 printf("\t\tvalue:");
316 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
317 dump_blob(dev, value);
318 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
319 printf(" %"PRId64"\n", value);
321 printf(" %"PRIu64"\n", value);
324 static void dump_connectors(struct device *dev)
328 printf("Connectors:\n");
329 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
330 for (i = 0; i < dev->resources->res->count_connectors; i++) {
331 struct connector *_connector = &dev->resources->connectors[i];
332 drmModeConnector *connector = _connector->connector;
336 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
337 connector->connector_id,
338 connector->encoder_id,
339 util_lookup_connector_status_name(connector->connection),
341 connector->mmWidth, connector->mmHeight,
342 connector->count_modes);
344 for (j = 0; j < connector->count_encoders; j++)
345 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
348 if (connector->count_modes) {
350 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
352 for (j = 0; j < connector->count_modes; j++)
353 dump_mode(&connector->modes[j]);
356 if (_connector->props) {
358 for (j = 0; j < (int)_connector->props->count_props; j++)
359 dump_prop(dev, _connector->props_info[j],
360 _connector->props->props[j],
361 _connector->props->prop_values[j]);
367 static void dump_crtcs(struct device *dev)
373 printf("id\tfb\tpos\tsize\n");
374 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
375 struct crtc *_crtc = &dev->resources->crtcs[i];
376 drmModeCrtc *crtc = _crtc->crtc;
380 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
384 crtc->width, crtc->height);
385 dump_mode(&crtc->mode);
389 for (j = 0; j < _crtc->props->count_props; j++)
390 dump_prop(dev, _crtc->props_info[j],
391 _crtc->props->props[j],
392 _crtc->props->prop_values[j]);
394 printf(" no properties found\n");
400 static void dump_framebuffers(struct device *dev)
405 printf("Frame buffers:\n");
406 printf("id\tsize\tpitch\n");
407 for (i = 0; i < dev->resources->res->count_fbs; i++) {
408 fb = dev->resources->fbs[i].fb;
412 printf("%u\t(%ux%u)\t%u\n",
414 fb->width, fb->height,
420 static void dump_planes(struct device *dev)
425 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
427 if (!dev->resources->plane_res)
430 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
431 struct plane *plane = &dev->resources->planes[i];
432 drmModePlane *ovr = plane->plane;
436 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
437 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
438 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
439 ovr->gamma_size, ovr->possible_crtcs);
441 if (!ovr->count_formats)
445 for (j = 0; j < ovr->count_formats; j++)
446 printf(" %4.4s", (char *)&ovr->formats[j]);
451 for (j = 0; j < plane->props->count_props; j++)
452 dump_prop(dev, plane->props_info[j],
453 plane->props->props[j],
454 plane->props->prop_values[j]);
456 printf(" no properties found\n");
464 static void free_resources(struct resources *res)
471 #define free_resource(_res, __res, type, Type) \
473 if (!(_res)->type##s) \
475 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
476 if (!(_res)->type##s[i].type) \
478 drmModeFree##Type((_res)->type##s[i].type); \
480 free((_res)->type##s); \
483 #define free_properties(_res, __res, type) \
485 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
486 drmModeFreeObjectProperties(res->type##s[i].props); \
487 free(res->type##s[i].props_info); \
492 free_properties(res, res, crtc);
494 free_resource(res, res, crtc, Crtc);
495 free_resource(res, res, encoder, Encoder);
497 for (i = 0; i < res->res->count_connectors; i++)
498 free(res->connectors[i].name);
500 free_resource(res, res, connector, Connector);
501 free_resource(res, res, fb, FB);
503 drmModeFreeResources(res->res);
506 if (res->plane_res) {
507 free_properties(res, plane_res, plane);
509 free_resource(res, plane_res, plane, Plane);
511 drmModeFreePlaneResources(res->plane_res);
517 static struct resources *get_resources(struct device *dev)
519 struct resources *res;
522 res = calloc(1, sizeof(*res));
526 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
528 res->res = drmModeGetResources(dev->fd);
530 fprintf(stderr, "drmModeGetResources failed: %s\n",
535 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
536 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
537 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
538 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
540 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
543 #define get_resource(_res, __res, type, Type) \
545 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
546 (_res)->type##s[i].type = \
547 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
548 if (!(_res)->type##s[i].type) \
549 fprintf(stderr, "could not get %s %i: %s\n", \
550 #type, (_res)->__res->type##s[i], \
555 get_resource(res, res, crtc, Crtc);
556 get_resource(res, res, encoder, Encoder);
557 get_resource(res, res, connector, Connector);
558 get_resource(res, res, fb, FB);
560 /* Set the name of all connectors based on the type name and the per-type ID. */
561 for (i = 0; i < res->res->count_connectors; i++) {
562 struct connector *connector = &res->connectors[i];
563 drmModeConnector *conn = connector->connector;
565 asprintf(&connector->name, "%s-%u",
566 util_lookup_connector_type_name(conn->connector_type),
567 conn->connector_type_id);
570 #define get_properties(_res, __res, type, Type) \
572 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
573 struct type *obj = &res->type##s[i]; \
576 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
577 DRM_MODE_OBJECT_##Type); \
580 "could not get %s %i properties: %s\n", \
581 #type, obj->type->type##_id, \
585 obj->props_info = calloc(obj->props->count_props, \
586 sizeof(*obj->props_info)); \
587 if (!obj->props_info) \
589 for (j = 0; j < obj->props->count_props; ++j) \
590 obj->props_info[j] = \
591 drmModeGetProperty(dev->fd, obj->props->props[j]); \
595 get_properties(res, res, crtc, CRTC);
596 get_properties(res, res, connector, CONNECTOR);
598 for (i = 0; i < res->res->count_crtcs; ++i)
599 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
601 res->plane_res = drmModeGetPlaneResources(dev->fd);
602 if (!res->plane_res) {
603 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
608 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
612 get_resource(res, plane_res, plane, Plane);
613 get_properties(res, plane_res, plane, PLANE);
622 static int get_crtc_index(struct device *dev, uint32_t id)
626 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
627 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
628 if (crtc && crtc->crtc_id == id)
635 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
637 struct connector *connector;
640 for (i = 0; i < dev->resources->res->count_connectors; i++) {
641 connector = &dev->resources->connectors[i];
643 if (strcmp(connector->name, name) == 0)
644 return connector->connector;
650 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
652 drmModeConnector *connector;
655 for (i = 0; i < dev->resources->res->count_connectors; i++) {
656 connector = dev->resources->connectors[i].connector;
657 if (connector && connector->connector_id == id)
664 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
666 drmModeEncoder *encoder;
669 for (i = 0; i < dev->resources->res->count_encoders; i++) {
670 encoder = dev->resources->encoders[i].encoder;
671 if (encoder && encoder->encoder_id == id)
678 /* -----------------------------------------------------------------------------
683 * Mode setting with the kernel interfaces is a bit of a chore.
684 * First you have to find the connector in question and make sure the
685 * requested mode is available.
686 * Then you need to find the encoder attached to that connector so you
687 * can bind it with a free crtc.
692 unsigned int num_cons;
696 unsigned int vrefresh;
698 drmModeModeInfo *mode;
700 unsigned int fb_id[2], current_fb_id;
701 struct timeval start;
707 uint32_t plane_id; /* the id of plane to use */
708 uint32_t crtc_id; /* the id of CRTC to bind to */
715 char format_str[5]; /* need to leave room for terminating \0 */
719 static drmModeModeInfo *
720 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
721 const unsigned int vrefresh)
723 drmModeConnector *connector;
724 drmModeModeInfo *mode;
727 connector = get_connector_by_id(dev, con_id);
728 if (!connector || !connector->count_modes)
731 for (i = 0; i < connector->count_modes; i++) {
732 mode = &connector->modes[i];
733 if (!strcmp(mode->name, mode_str)) {
734 /* If the vertical refresh frequency is not specified then return the
735 * first mode that match with the name. Else, return the mode that match
736 * the name and the specified vertical refresh frequency.
740 else if (mode->vrefresh == vrefresh)
748 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
750 uint32_t possible_crtcs = ~0;
751 uint32_t active_crtcs = 0;
752 unsigned int crtc_idx;
756 for (i = 0; i < pipe->num_cons; ++i) {
757 uint32_t crtcs_for_connector = 0;
758 drmModeConnector *connector;
759 drmModeEncoder *encoder;
762 connector = get_connector_by_id(dev, pipe->con_ids[i]);
766 for (j = 0; j < connector->count_encoders; ++j) {
767 encoder = get_encoder_by_id(dev, connector->encoders[j]);
771 crtcs_for_connector |= encoder->possible_crtcs;
773 idx = get_crtc_index(dev, encoder->crtc_id);
775 active_crtcs |= 1 << idx;
778 possible_crtcs &= crtcs_for_connector;
784 /* Return the first possible and active CRTC if one exists, or the first
785 * possible CRTC otherwise.
787 if (possible_crtcs & active_crtcs)
788 crtc_idx = ffs(possible_crtcs & active_crtcs);
790 crtc_idx = ffs(possible_crtcs);
792 return &dev->resources->crtcs[crtc_idx - 1];
795 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
797 drmModeModeInfo *mode = NULL;
802 for (i = 0; i < (int)pipe->num_cons; i++) {
803 mode = connector_find_mode(dev, pipe->con_ids[i],
804 pipe->mode_str, pipe->vrefresh);
807 "failed to find mode \"%s\" for connector %s\n",
808 pipe->mode_str, pipe->cons[i]);
813 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
814 * locate a CRTC that can be attached to all the connectors.
816 if (pipe->crtc_id != (uint32_t)-1) {
817 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
818 struct crtc *crtc = &dev->resources->crtcs[i];
820 if (pipe->crtc_id == crtc->crtc->crtc_id) {
826 pipe->crtc = pipe_find_crtc(dev, pipe);
830 fprintf(stderr, "failed to find CRTC for pipe\n");
835 pipe->crtc->mode = mode;
840 /* -----------------------------------------------------------------------------
844 struct property_arg {
847 char name[DRM_PROP_NAME_LEN+1];
852 static void set_property(struct device *dev, struct property_arg *p)
854 drmModeObjectProperties *props = NULL;
855 drmModePropertyRes **props_info = NULL;
856 const char *obj_type;
863 #define find_object(_res, __res, type, Type) \
865 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
866 struct type *obj = &(_res)->type##s[i]; \
867 if (obj->type->type##_id != p->obj_id) \
869 p->obj_type = DRM_MODE_OBJECT_##Type; \
871 props = obj->props; \
872 props_info = obj->props_info; \
876 find_object(dev->resources, res, crtc, CRTC);
877 if (p->obj_type == 0)
878 find_object(dev->resources, res, connector, CONNECTOR);
879 if (p->obj_type == 0)
880 find_object(dev->resources, plane_res, plane, PLANE);
881 if (p->obj_type == 0) {
882 fprintf(stderr, "Object %i not found, can't set property\n",
888 fprintf(stderr, "%s %i has no properties\n",
889 obj_type, p->obj_id);
893 for (i = 0; i < (int)props->count_props; ++i) {
896 if (strcmp(props_info[i]->name, p->name) == 0)
900 if (i == (int)props->count_props) {
901 fprintf(stderr, "%s %i has no %s property\n",
902 obj_type, p->obj_id, p->name);
906 p->prop_id = props->props[i];
908 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
909 p->prop_id, p->value);
911 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
912 obj_type, p->obj_id, p->name, p->value, strerror(errno));
915 /* -------------------------------------------------------------------------- */
918 page_flip_handler(int fd, unsigned int frame,
919 unsigned int sec, unsigned int usec, void *data)
921 struct pipe_arg *pipe;
922 unsigned int new_fb_id;
927 if (pipe->current_fb_id == pipe->fb_id[0])
928 new_fb_id = pipe->fb_id[1];
930 new_fb_id = pipe->fb_id[0];
932 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
933 DRM_MODE_PAGE_FLIP_EVENT, pipe);
934 pipe->current_fb_id = new_fb_id;
936 if (pipe->swap_count == 60) {
937 gettimeofday(&end, NULL);
938 t = end.tv_sec + end.tv_usec * 1e-6 -
939 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
940 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
941 pipe->swap_count = 0;
946 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
950 for (i = 0; i < ovr->count_formats; ++i) {
951 if (ovr->formats[i] == fmt)
958 static int set_plane(struct device *dev, struct plane_arg *p)
961 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
964 uint32_t plane_flags = 0;
965 int crtc_x, crtc_y, crtc_w, crtc_h;
966 struct crtc *crtc = NULL;
970 /* Find an unused plane which can be connected to our CRTC. Find the
971 * CRTC index first, then iterate over available planes.
973 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
974 if (p->crtc_id == dev->resources->res->crtcs[i]) {
975 crtc = &dev->resources->crtcs[i];
982 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
986 plane_id = p->plane_id;
988 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
989 ovr = dev->resources->planes[i].plane;
993 if (plane_id && plane_id != ovr->plane_id)
996 if (!format_support(ovr, p->fourcc))
999 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id) {
1000 plane_id = ovr->plane_id;
1005 if (i == dev->resources->plane_res->count_planes) {
1006 fprintf(stderr, "no unused plane available for CRTC %u\n",
1007 crtc->crtc->crtc_id);
1011 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1012 p->w, p->h, p->format_str, plane_id);
1014 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1015 pitches, offsets, UTIL_PATTERN_TILES);
1016 if (plane_bo == NULL)
1021 /* just use single plane format for now.. */
1022 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1023 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1024 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1028 crtc_w = p->w * p->scale;
1029 crtc_h = p->h * p->scale;
1030 if (!p->has_position) {
1031 /* Default to the middle of the screen */
1032 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1033 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1039 /* note src coords (last 4 args) are in Q16 format */
1040 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1041 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1042 0, 0, p->w << 16, p->h << 16)) {
1043 fprintf(stderr, "failed to enable plane: %s\n",
1048 ovr->crtc_id = crtc->crtc->crtc_id;
1053 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1057 for (i = 0; i < count; i++) {
1059 drmModeRmFB(dev->fd, p[i].fb_id);
1061 bo_destroy(p[i].bo);
1066 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1068 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1075 dev->mode.width = 0;
1076 dev->mode.height = 0;
1077 dev->mode.fb_id = 0;
1079 for (i = 0; i < count; i++) {
1080 struct pipe_arg *pipe = &pipes[i];
1082 ret = pipe_find_crtc_and_mode(dev, pipe);
1086 dev->mode.width += pipe->mode->hdisplay;
1087 if (dev->mode.height < pipe->mode->vdisplay)
1088 dev->mode.height = pipe->mode->vdisplay;
1091 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1092 dev->mode.height, handles, pitches, offsets,
1093 UTIL_PATTERN_SMPTE);
1099 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1100 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1102 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1103 dev->mode.width, dev->mode.height, strerror(errno));
1107 dev->mode.fb_id = fb_id;
1110 for (i = 0; i < count; i++) {
1111 struct pipe_arg *pipe = &pipes[i];
1113 if (pipe->mode == NULL)
1116 printf("setting mode %s-%dHz@%s on connectors ",
1117 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1118 for (j = 0; j < pipe->num_cons; ++j)
1119 printf("%s, ", pipe->cons[j]);
1120 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1122 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1123 x, 0, pipe->con_ids, pipe->num_cons,
1126 /* XXX: Actually check if this is needed */
1127 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1129 x += pipe->mode->hdisplay;
1132 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1138 static void clear_mode(struct device *dev)
1140 if (dev->mode.fb_id)
1141 drmModeRmFB(dev->fd, dev->mode.fb_id);
1143 bo_destroy(dev->mode.bo);
1146 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1150 /* set up planes/overlays */
1151 for (i = 0; i < count; i++)
1152 if (set_plane(dev, &p[i]))
1156 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1158 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1163 /* maybe make cursor width/height configurable some day */
1167 /* create cursor bo.. just using PATTERN_PLAIN as it has
1170 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1171 offsets, UTIL_PATTERN_PLAIN);
1175 dev->mode.cursor_bo = bo;
1177 for (i = 0; i < count; i++) {
1178 struct pipe_arg *pipe = &pipes[i];
1179 ret = cursor_init(dev->fd, handles[0],
1180 pipe->crtc->crtc->crtc_id,
1181 pipe->mode->hdisplay, pipe->mode->vdisplay,
1184 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1193 static void clear_cursors(struct device *dev)
1197 if (dev->mode.cursor_bo)
1198 bo_destroy(dev->mode.cursor_bo);
1201 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1203 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1204 unsigned int other_fb_id;
1205 struct bo *other_bo;
1206 drmEventContext evctx;
1210 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1211 dev->mode.height, handles, pitches, offsets,
1212 UTIL_PATTERN_PLAIN);
1213 if (other_bo == NULL)
1216 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1217 pipes[0].fourcc, handles, pitches, offsets,
1220 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1224 for (i = 0; i < count; i++) {
1225 struct pipe_arg *pipe = &pipes[i];
1227 if (pipe->mode == NULL)
1230 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1231 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1234 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1237 gettimeofday(&pipe->start, NULL);
1238 pipe->swap_count = 0;
1239 pipe->fb_id[0] = dev->mode.fb_id;
1240 pipe->fb_id[1] = other_fb_id;
1241 pipe->current_fb_id = other_fb_id;
1244 memset(&evctx, 0, sizeof evctx);
1245 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1246 evctx.vblank_handler = NULL;
1247 evctx.page_flip_handler = page_flip_handler;
1251 struct pollfd pfd[2];
1254 pfd[0].events = POLLIN;
1256 pfd[1].events = POLLIN;
1258 if (poll(pfd, 2, -1) < 0) {
1259 fprintf(stderr, "poll error\n");
1266 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1271 FD_SET(dev->fd, &fds);
1272 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1275 fprintf(stderr, "select timed out or error (ret %d)\n",
1278 } else if (FD_ISSET(0, &fds)) {
1283 drmHandleEvent(dev->fd, &evctx);
1287 drmModeRmFB(dev->fd, other_fb_id);
1289 bo_destroy(other_bo);
1292 #define min(a, b) ((a) < (b) ? (a) : (b))
1294 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1302 pipe->crtc_id = (uint32_t)-1;
1303 strcpy(pipe->format_str, "XR24");
1305 /* Count the number of connectors and allocate them. */
1307 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1312 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1313 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1314 if (pipe->con_ids == NULL || pipe->cons == NULL)
1317 /* Parse the connectors. */
1318 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1319 endp = strpbrk(p, ",@:");
1323 pipe->cons[i] = strndup(p, endp - p);
1329 if (i != pipe->num_cons - 1)
1332 /* Parse the remaining parameters. */
1335 pipe->crtc_id = strtoul(arg, &endp, 10);
1342 /* Search for the vertical refresh or the format. */
1343 p = strpbrk(arg, "-@");
1345 p = arg + strlen(arg);
1346 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1347 strncpy(pipe->mode_str, arg, len);
1348 pipe->mode_str[len] = '\0';
1351 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1356 strncpy(pipe->format_str, p + 1, 4);
1357 pipe->format_str[4] = '\0';
1360 pipe->fourcc = util_format_fourcc(pipe->format_str);
1361 if (pipe->fourcc == 0) {
1362 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1369 static int parse_plane(struct plane_arg *plane, const char *p)
1373 plane->plane_id = strtoul(p, &end, 10);
1378 plane->crtc_id = strtoul(p, &end, 10);
1383 plane->w = strtoul(p, &end, 10);
1388 plane->h = strtoul(p, &end, 10);
1390 if (*end == '+' || *end == '-') {
1391 plane->x = strtol(end, &end, 10);
1392 if (*end != '+' && *end != '-')
1394 plane->y = strtol(end, &end, 10);
1396 plane->has_position = true;
1401 plane->scale = strtod(p, &end);
1402 if (plane->scale <= 0.0)
1413 strcpy(plane->format_str, p);
1415 strcpy(plane->format_str, "XR24");
1418 plane->fourcc = util_format_fourcc(plane->format_str);
1419 if (plane->fourcc == 0) {
1420 fprintf(stderr, "unknown format %s\n", plane->format_str);
1427 static int parse_property(struct property_arg *p, const char *arg)
1429 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1433 p->name[DRM_PROP_NAME_LEN] = '\0';
1438 static void usage(char *name)
1440 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1442 fprintf(stderr, "\n Query options:\n\n");
1443 fprintf(stderr, "\t-c\tlist connectors\n");
1444 fprintf(stderr, "\t-e\tlist encoders\n");
1445 fprintf(stderr, "\t-f\tlist framebuffers\n");
1446 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1448 fprintf(stderr, "\n Test options:\n\n");
1449 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1450 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1451 fprintf(stderr, "\t-C\ttest hw cursor\n");
1452 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1453 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1455 fprintf(stderr, "\n Generic options:\n\n");
1456 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1457 fprintf(stderr, "\t-M module\tuse the given driver\n");
1458 fprintf(stderr, "\t-D device\tuse the given device\n");
1460 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1464 static int page_flipping_supported(void)
1466 /*FIXME: generic ioctl needed? */
1470 struct drm_i915_getparam gp;
1472 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1475 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1477 fprintf(stderr, "drm_i915_getparam: %m\n");
1485 static int cursor_supported(void)
1487 /*FIXME: generic ioctl needed? */
1491 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1493 drmModeConnector *connector;
1498 for (i = 0; i < pipe->num_cons; i++) {
1499 id = strtoul(pipe->cons[i], &endp, 10);
1500 if (endp == pipe->cons[i]) {
1501 connector = get_connector_by_name(dev, pipe->cons[i]);
1503 fprintf(stderr, "no connector named '%s'\n",
1508 id = connector->connector_id;
1511 pipe->con_ids[i] = id;
1517 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1519 int main(int argc, char **argv)
1524 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1525 int drop_master = 0;
1527 int test_cursor = 0;
1528 char *device = NULL;
1529 char *module = NULL;
1531 unsigned int count = 0, plane_count = 0;
1532 unsigned int prop_count = 0;
1533 struct pipe_arg *pipe_args = NULL;
1534 struct plane_arg *plane_args = NULL;
1535 struct property_arg *prop_args = NULL;
1536 unsigned int args = 0;
1539 memset(&dev, 0, sizeof dev);
1542 while ((c = getopt(argc, argv, optstr)) != -1) {
1564 /* Preserve the default behaviour of dumping all information. */
1568 plane_args = realloc(plane_args,
1569 (plane_count + 1) * sizeof *plane_args);
1570 if (plane_args == NULL) {
1571 fprintf(stderr, "memory allocation failed\n");
1574 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1576 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1586 pipe_args = realloc(pipe_args,
1587 (count + 1) * sizeof *pipe_args);
1588 if (pipe_args == NULL) {
1589 fprintf(stderr, "memory allocation failed\n");
1592 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1594 if (parse_connector(&pipe_args[count], optarg) < 0)
1606 prop_args = realloc(prop_args,
1607 (prop_count + 1) * sizeof *prop_args);
1608 if (prop_args == NULL) {
1609 fprintf(stderr, "memory allocation failed\n");
1612 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1614 if (parse_property(&prop_args[prop_count], optarg) < 0)
1626 encoders = connectors = crtcs = planes = framebuffers = 1;
1628 dev.fd = util_open(device, module);
1632 if (test_vsync && !page_flipping_supported()) {
1633 fprintf(stderr, "page flipping not supported by drm.\n");
1637 if (test_vsync && !count) {
1638 fprintf(stderr, "page flipping requires at least one -s option.\n");
1642 if (test_cursor && !cursor_supported()) {
1643 fprintf(stderr, "hw cursor not supported by drm.\n");
1647 dev.resources = get_resources(&dev);
1648 if (!dev.resources) {
1653 for (i = 0; i < count; i++) {
1654 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1655 free_resources(dev.resources);
1661 #define dump_resource(dev, res) if (res) dump_##res(dev)
1663 dump_resource(&dev, encoders);
1664 dump_resource(&dev, connectors);
1665 dump_resource(&dev, crtcs);
1666 dump_resource(&dev, planes);
1667 dump_resource(&dev, framebuffers);
1669 for (i = 0; i < prop_count; ++i)
1670 set_property(&dev, &prop_args[i]);
1672 if (count || plane_count) {
1675 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1676 if (ret || cap == 0) {
1677 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1682 set_mode(&dev, pipe_args, count);
1685 set_planes(&dev, plane_args, plane_count);
1688 set_cursors(&dev, pipe_args, count);
1691 test_page_flip(&dev, pipe_args, count);
1694 drmDropMaster(dev.fd);
1699 clear_cursors(&dev);
1702 clear_planes(&dev, plane_args, plane_count);
1708 free_resources(dev.resources);