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",
214 mode_flag_str(mode->flags);
216 mode_type_str(mode->type);
220 static void dump_blob(struct device *dev, uint32_t blob_id)
223 unsigned char *blob_data;
224 drmModePropertyBlobPtr blob;
226 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
232 blob_data = blob->data;
234 for (i = 0; i < blob->length; i++) {
237 printf("%.2hhx", blob_data[i]);
241 drmModeFreePropertyBlob(blob);
244 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
245 uint32_t prop_id, uint64_t value)
248 printf("\t%d", prop_id);
254 printf(" %s:\n", prop->name);
256 printf("\t\tflags:");
257 if (prop->flags & DRM_MODE_PROP_PENDING)
259 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
260 printf(" immutable");
261 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
262 printf(" signed range");
263 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
265 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
267 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
269 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
271 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
275 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
276 printf("\t\tvalues:");
277 for (i = 0; i < prop->count_values; i++)
278 printf(" %"PRId64, U642I64(prop->values[i]));
282 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
283 printf("\t\tvalues:");
284 for (i = 0; i < prop->count_values; i++)
285 printf(" %"PRIu64, prop->values[i]);
289 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
290 printf("\t\tenums:");
291 for (i = 0; i < prop->count_enums; i++)
292 printf(" %s=%llu", prop->enums[i].name,
293 prop->enums[i].value);
295 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
296 printf("\t\tvalues:");
297 for (i = 0; i < prop->count_enums; i++)
298 printf(" %s=0x%llx", prop->enums[i].name,
299 (1LL << prop->enums[i].value));
302 assert(prop->count_enums == 0);
305 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
306 printf("\t\tblobs:\n");
307 for (i = 0; i < prop->count_blobs; i++)
308 dump_blob(dev, prop->blob_ids[i]);
311 assert(prop->count_blobs == 0);
314 printf("\t\tvalue:");
315 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
316 dump_blob(dev, value);
318 printf(" %"PRIu64"\n", value);
321 static void dump_connectors(struct device *dev)
325 printf("Connectors:\n");
326 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
327 for (i = 0; i < dev->resources->res->count_connectors; i++) {
328 struct connector *_connector = &dev->resources->connectors[i];
329 drmModeConnector *connector = _connector->connector;
333 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
334 connector->connector_id,
335 connector->encoder_id,
336 util_lookup_connector_status_name(connector->connection),
338 connector->mmWidth, connector->mmHeight,
339 connector->count_modes);
341 for (j = 0; j < connector->count_encoders; j++)
342 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
345 if (connector->count_modes) {
347 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
349 for (j = 0; j < connector->count_modes; j++)
350 dump_mode(&connector->modes[j]);
353 if (_connector->props) {
355 for (j = 0; j < (int)_connector->props->count_props; j++)
356 dump_prop(dev, _connector->props_info[j],
357 _connector->props->props[j],
358 _connector->props->prop_values[j]);
364 static void dump_crtcs(struct device *dev)
370 printf("id\tfb\tpos\tsize\n");
371 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
372 struct crtc *_crtc = &dev->resources->crtcs[i];
373 drmModeCrtc *crtc = _crtc->crtc;
377 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
381 crtc->width, crtc->height);
382 dump_mode(&crtc->mode);
386 for (j = 0; j < _crtc->props->count_props; j++)
387 dump_prop(dev, _crtc->props_info[j],
388 _crtc->props->props[j],
389 _crtc->props->prop_values[j]);
391 printf(" no properties found\n");
397 static void dump_framebuffers(struct device *dev)
402 printf("Frame buffers:\n");
403 printf("id\tsize\tpitch\n");
404 for (i = 0; i < dev->resources->res->count_fbs; i++) {
405 fb = dev->resources->fbs[i].fb;
409 printf("%u\t(%ux%u)\t%u\n",
411 fb->width, fb->height,
417 static void dump_planes(struct device *dev)
422 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
424 if (!dev->resources->plane_res)
427 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
428 struct plane *plane = &dev->resources->planes[i];
429 drmModePlane *ovr = plane->plane;
433 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
434 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
435 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
436 ovr->gamma_size, ovr->possible_crtcs);
438 if (!ovr->count_formats)
442 for (j = 0; j < ovr->count_formats; j++)
443 printf(" %4.4s", (char *)&ovr->formats[j]);
448 for (j = 0; j < plane->props->count_props; j++)
449 dump_prop(dev, plane->props_info[j],
450 plane->props->props[j],
451 plane->props->prop_values[j]);
453 printf(" no properties found\n");
461 static void free_resources(struct resources *res)
468 #define free_resource(_res, __res, type, Type) \
470 if (!(_res)->type##s) \
472 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
473 if (!(_res)->type##s[i].type) \
475 drmModeFree##Type((_res)->type##s[i].type); \
477 free((_res)->type##s); \
480 #define free_properties(_res, __res, type) \
482 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
483 drmModeFreeObjectProperties(res->type##s[i].props); \
484 free(res->type##s[i].props_info); \
489 free_properties(res, res, crtc);
491 free_resource(res, res, crtc, Crtc);
492 free_resource(res, res, encoder, Encoder);
494 for (i = 0; i < res->res->count_connectors; i++)
495 free(res->connectors[i].name);
497 free_resource(res, res, connector, Connector);
498 free_resource(res, res, fb, FB);
500 drmModeFreeResources(res->res);
503 if (res->plane_res) {
504 free_properties(res, plane_res, plane);
506 free_resource(res, plane_res, plane, Plane);
508 drmModeFreePlaneResources(res->plane_res);
514 static struct resources *get_resources(struct device *dev)
516 struct resources *res;
519 res = calloc(1, sizeof(*res));
523 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
525 res->res = drmModeGetResources(dev->fd);
527 fprintf(stderr, "drmModeGetResources failed: %s\n",
532 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
533 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
534 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
535 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
537 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
540 #define get_resource(_res, __res, type, Type) \
542 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
543 (_res)->type##s[i].type = \
544 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
545 if (!(_res)->type##s[i].type) \
546 fprintf(stderr, "could not get %s %i: %s\n", \
547 #type, (_res)->__res->type##s[i], \
552 get_resource(res, res, crtc, Crtc);
553 get_resource(res, res, encoder, Encoder);
554 get_resource(res, res, connector, Connector);
555 get_resource(res, res, fb, FB);
557 /* Set the name of all connectors based on the type name and the per-type ID. */
558 for (i = 0; i < res->res->count_connectors; i++) {
559 struct connector *connector = &res->connectors[i];
560 drmModeConnector *conn = connector->connector;
562 asprintf(&connector->name, "%s-%u",
563 util_lookup_connector_type_name(conn->connector_type),
564 conn->connector_type_id);
567 #define get_properties(_res, __res, type, Type) \
569 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
570 struct type *obj = &res->type##s[i]; \
573 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
574 DRM_MODE_OBJECT_##Type); \
577 "could not get %s %i properties: %s\n", \
578 #type, obj->type->type##_id, \
582 obj->props_info = calloc(obj->props->count_props, \
583 sizeof(*obj->props_info)); \
584 if (!obj->props_info) \
586 for (j = 0; j < obj->props->count_props; ++j) \
587 obj->props_info[j] = \
588 drmModeGetProperty(dev->fd, obj->props->props[j]); \
592 get_properties(res, res, crtc, CRTC);
593 get_properties(res, res, connector, CONNECTOR);
595 for (i = 0; i < res->res->count_crtcs; ++i)
596 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
598 res->plane_res = drmModeGetPlaneResources(dev->fd);
599 if (!res->plane_res) {
600 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
605 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
609 get_resource(res, plane_res, plane, Plane);
610 get_properties(res, plane_res, plane, PLANE);
619 static int get_crtc_index(struct device *dev, uint32_t id)
623 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
624 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
625 if (crtc && crtc->crtc_id == id)
632 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
634 struct connector *connector;
637 for (i = 0; i < dev->resources->res->count_connectors; i++) {
638 connector = &dev->resources->connectors[i];
640 if (strcmp(connector->name, name) == 0)
641 return connector->connector;
647 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
649 drmModeConnector *connector;
652 for (i = 0; i < dev->resources->res->count_connectors; i++) {
653 connector = dev->resources->connectors[i].connector;
654 if (connector && connector->connector_id == id)
661 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
663 drmModeEncoder *encoder;
666 for (i = 0; i < dev->resources->res->count_encoders; i++) {
667 encoder = dev->resources->encoders[i].encoder;
668 if (encoder && encoder->encoder_id == id)
675 /* -----------------------------------------------------------------------------
680 * Mode setting with the kernel interfaces is a bit of a chore.
681 * First you have to find the connector in question and make sure the
682 * requested mode is available.
683 * Then you need to find the encoder attached to that connector so you
684 * can bind it with a free crtc.
689 unsigned int num_cons;
693 unsigned int vrefresh;
695 drmModeModeInfo *mode;
697 unsigned int fb_id[2], current_fb_id;
698 struct timeval start;
704 uint32_t crtc_id; /* the id of CRTC to bind to */
711 char format_str[5]; /* need to leave room for terminating \0 */
715 static drmModeModeInfo *
716 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
717 const unsigned int vrefresh)
719 drmModeConnector *connector;
720 drmModeModeInfo *mode;
723 connector = get_connector_by_id(dev, con_id);
724 if (!connector || !connector->count_modes)
727 for (i = 0; i < connector->count_modes; i++) {
728 mode = &connector->modes[i];
729 if (!strcmp(mode->name, mode_str)) {
730 /* If the vertical refresh frequency is not specified then return the
731 * first mode that match with the name. Else, return the mode that match
732 * the name and the specified vertical refresh frequency.
736 else if (mode->vrefresh == vrefresh)
744 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
746 uint32_t possible_crtcs = ~0;
747 uint32_t active_crtcs = 0;
748 unsigned int crtc_idx;
752 for (i = 0; i < pipe->num_cons; ++i) {
753 uint32_t crtcs_for_connector = 0;
754 drmModeConnector *connector;
755 drmModeEncoder *encoder;
758 connector = get_connector_by_id(dev, pipe->con_ids[i]);
762 for (j = 0; j < connector->count_encoders; ++j) {
763 encoder = get_encoder_by_id(dev, connector->encoders[j]);
767 crtcs_for_connector |= encoder->possible_crtcs;
769 idx = get_crtc_index(dev, encoder->crtc_id);
771 active_crtcs |= 1 << idx;
774 possible_crtcs &= crtcs_for_connector;
780 /* Return the first possible and active CRTC if one exists, or the first
781 * possible CRTC otherwise.
783 if (possible_crtcs & active_crtcs)
784 crtc_idx = ffs(possible_crtcs & active_crtcs);
786 crtc_idx = ffs(possible_crtcs);
788 return &dev->resources->crtcs[crtc_idx - 1];
791 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
793 drmModeModeInfo *mode = NULL;
798 for (i = 0; i < (int)pipe->num_cons; i++) {
799 mode = connector_find_mode(dev, pipe->con_ids[i],
800 pipe->mode_str, pipe->vrefresh);
803 "failed to find mode \"%s\" for connector %s\n",
804 pipe->mode_str, pipe->cons[i]);
809 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
810 * locate a CRTC that can be attached to all the connectors.
812 if (pipe->crtc_id != (uint32_t)-1) {
813 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
814 struct crtc *crtc = &dev->resources->crtcs[i];
816 if (pipe->crtc_id == crtc->crtc->crtc_id) {
822 pipe->crtc = pipe_find_crtc(dev, pipe);
826 fprintf(stderr, "failed to find CRTC for pipe\n");
831 pipe->crtc->mode = mode;
836 /* -----------------------------------------------------------------------------
840 struct property_arg {
843 char name[DRM_PROP_NAME_LEN+1];
848 static void set_property(struct device *dev, struct property_arg *p)
850 drmModeObjectProperties *props = NULL;
851 drmModePropertyRes **props_info = NULL;
852 const char *obj_type;
859 #define find_object(_res, __res, type, Type) \
861 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
862 struct type *obj = &(_res)->type##s[i]; \
863 if (obj->type->type##_id != p->obj_id) \
865 p->obj_type = DRM_MODE_OBJECT_##Type; \
867 props = obj->props; \
868 props_info = obj->props_info; \
872 find_object(dev->resources, res, crtc, CRTC);
873 if (p->obj_type == 0)
874 find_object(dev->resources, res, connector, CONNECTOR);
875 if (p->obj_type == 0)
876 find_object(dev->resources, plane_res, plane, PLANE);
877 if (p->obj_type == 0) {
878 fprintf(stderr, "Object %i not found, can't set property\n",
884 fprintf(stderr, "%s %i has no properties\n",
885 obj_type, p->obj_id);
889 for (i = 0; i < (int)props->count_props; ++i) {
892 if (strcmp(props_info[i]->name, p->name) == 0)
896 if (i == (int)props->count_props) {
897 fprintf(stderr, "%s %i has no %s property\n",
898 obj_type, p->obj_id, p->name);
902 p->prop_id = props->props[i];
904 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
905 p->prop_id, p->value);
907 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
908 obj_type, p->obj_id, p->name, p->value, strerror(errno));
911 /* -------------------------------------------------------------------------- */
914 page_flip_handler(int fd, unsigned int frame,
915 unsigned int sec, unsigned int usec, void *data)
917 struct pipe_arg *pipe;
918 unsigned int new_fb_id;
923 if (pipe->current_fb_id == pipe->fb_id[0])
924 new_fb_id = pipe->fb_id[1];
926 new_fb_id = pipe->fb_id[0];
928 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
929 DRM_MODE_PAGE_FLIP_EVENT, pipe);
930 pipe->current_fb_id = new_fb_id;
932 if (pipe->swap_count == 60) {
933 gettimeofday(&end, NULL);
934 t = end.tv_sec + end.tv_usec * 1e-6 -
935 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
936 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
937 pipe->swap_count = 0;
942 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
946 for (i = 0; i < ovr->count_formats; ++i) {
947 if (ovr->formats[i] == fmt)
954 static int set_plane(struct device *dev, struct plane_arg *p)
957 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
958 uint32_t plane_id = 0;
960 uint32_t plane_flags = 0;
961 int crtc_x, crtc_y, crtc_w, crtc_h;
962 struct crtc *crtc = NULL;
966 /* Find an unused plane which can be connected to our CRTC. Find the
967 * CRTC index first, then iterate over available planes.
969 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
970 if (p->crtc_id == dev->resources->res->crtcs[i]) {
971 crtc = &dev->resources->crtcs[i];
978 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
982 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
983 ovr = dev->resources->planes[i].plane;
984 if (!ovr || !format_support(ovr, p->fourcc))
987 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
988 plane_id = ovr->plane_id;
992 fprintf(stderr, "no unused plane available for CRTC %u\n",
993 crtc->crtc->crtc_id);
997 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
998 p->w, p->h, p->format_str, plane_id);
1000 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1001 pitches, offsets, UTIL_PATTERN_TILES);
1002 if (plane_bo == NULL)
1007 /* just use single plane format for now.. */
1008 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1009 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1010 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1014 crtc_w = p->w * p->scale;
1015 crtc_h = p->h * p->scale;
1016 if (!p->has_position) {
1017 /* Default to the middle of the screen */
1018 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1019 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1025 /* note src coords (last 4 args) are in Q16 format */
1026 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1027 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1028 0, 0, p->w << 16, p->h << 16)) {
1029 fprintf(stderr, "failed to enable plane: %s\n",
1034 ovr->crtc_id = crtc->crtc->crtc_id;
1039 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1043 for (i = 0; i < count; i++) {
1045 drmModeRmFB(dev->fd, p[i].fb_id);
1047 bo_destroy(p[i].bo);
1052 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1054 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1061 dev->mode.width = 0;
1062 dev->mode.height = 0;
1063 dev->mode.fb_id = 0;
1065 for (i = 0; i < count; i++) {
1066 struct pipe_arg *pipe = &pipes[i];
1068 ret = pipe_find_crtc_and_mode(dev, pipe);
1072 dev->mode.width += pipe->mode->hdisplay;
1073 if (dev->mode.height < pipe->mode->vdisplay)
1074 dev->mode.height = pipe->mode->vdisplay;
1077 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1078 dev->mode.height, handles, pitches, offsets,
1079 UTIL_PATTERN_SMPTE);
1085 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1086 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1088 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1089 dev->mode.width, dev->mode.height, strerror(errno));
1093 dev->mode.fb_id = fb_id;
1096 for (i = 0; i < count; i++) {
1097 struct pipe_arg *pipe = &pipes[i];
1099 if (pipe->mode == NULL)
1102 printf("setting mode %s-%dHz@%s on connectors ",
1103 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1104 for (j = 0; j < pipe->num_cons; ++j)
1105 printf("%s, ", pipe->cons[j]);
1106 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1108 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1109 x, 0, pipe->con_ids, pipe->num_cons,
1112 /* XXX: Actually check if this is needed */
1113 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1115 x += pipe->mode->hdisplay;
1118 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1124 static void clear_mode(struct device *dev)
1126 if (dev->mode.fb_id)
1127 drmModeRmFB(dev->fd, dev->mode.fb_id);
1129 bo_destroy(dev->mode.bo);
1132 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1136 /* set up planes/overlays */
1137 for (i = 0; i < count; i++)
1138 if (set_plane(dev, &p[i]))
1142 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1144 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1149 /* maybe make cursor width/height configurable some day */
1153 /* create cursor bo.. just using PATTERN_PLAIN as it has
1156 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1157 offsets, UTIL_PATTERN_PLAIN);
1161 dev->mode.cursor_bo = bo;
1163 for (i = 0; i < count; i++) {
1164 struct pipe_arg *pipe = &pipes[i];
1165 ret = cursor_init(dev->fd, handles[0],
1166 pipe->crtc->crtc->crtc_id,
1167 pipe->mode->hdisplay, pipe->mode->vdisplay,
1170 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1179 static void clear_cursors(struct device *dev)
1183 if (dev->mode.cursor_bo)
1184 bo_destroy(dev->mode.cursor_bo);
1187 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1189 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1190 unsigned int other_fb_id;
1191 struct bo *other_bo;
1192 drmEventContext evctx;
1196 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1197 dev->mode.height, handles, pitches, offsets,
1198 UTIL_PATTERN_PLAIN);
1199 if (other_bo == NULL)
1202 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1203 pipes[0].fourcc, handles, pitches, offsets,
1206 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1210 for (i = 0; i < count; i++) {
1211 struct pipe_arg *pipe = &pipes[i];
1213 if (pipe->mode == NULL)
1216 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1217 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1220 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1223 gettimeofday(&pipe->start, NULL);
1224 pipe->swap_count = 0;
1225 pipe->fb_id[0] = dev->mode.fb_id;
1226 pipe->fb_id[1] = other_fb_id;
1227 pipe->current_fb_id = other_fb_id;
1230 memset(&evctx, 0, sizeof evctx);
1231 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1232 evctx.vblank_handler = NULL;
1233 evctx.page_flip_handler = page_flip_handler;
1237 struct pollfd pfd[2];
1240 pfd[0].events = POLLIN;
1242 pfd[1].events = POLLIN;
1244 if (poll(pfd, 2, -1) < 0) {
1245 fprintf(stderr, "poll error\n");
1252 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1257 FD_SET(dev->fd, &fds);
1258 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1261 fprintf(stderr, "select timed out or error (ret %d)\n",
1264 } else if (FD_ISSET(0, &fds)) {
1269 drmHandleEvent(dev->fd, &evctx);
1273 drmModeRmFB(dev->fd, other_fb_id);
1275 bo_destroy(other_bo);
1278 #define min(a, b) ((a) < (b) ? (a) : (b))
1280 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1288 pipe->crtc_id = (uint32_t)-1;
1289 strcpy(pipe->format_str, "XR24");
1291 /* Count the number of connectors and allocate them. */
1293 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1298 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1299 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1300 if (pipe->con_ids == NULL || pipe->cons == NULL)
1303 /* Parse the connectors. */
1304 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1305 endp = strpbrk(p, ",@:");
1309 pipe->cons[i] = strndup(p, endp - p);
1315 if (i != pipe->num_cons - 1)
1318 /* Parse the remaining parameters. */
1321 pipe->crtc_id = strtoul(arg, &endp, 10);
1328 /* Search for the vertical refresh or the format. */
1329 p = strpbrk(arg, "-@");
1331 p = arg + strlen(arg);
1332 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1333 strncpy(pipe->mode_str, arg, len);
1334 pipe->mode_str[len] = '\0';
1337 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1342 strncpy(pipe->format_str, p + 1, 4);
1343 pipe->format_str[4] = '\0';
1346 pipe->fourcc = util_format_fourcc(pipe->format_str);
1347 if (pipe->fourcc == 0) {
1348 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1355 static int parse_plane(struct plane_arg *plane, const char *p)
1359 plane->crtc_id = strtoul(p, &end, 10);
1364 plane->w = strtoul(p, &end, 10);
1369 plane->h = strtoul(p, &end, 10);
1371 if (*end == '+' || *end == '-') {
1372 plane->x = strtol(end, &end, 10);
1373 if (*end != '+' && *end != '-')
1375 plane->y = strtol(end, &end, 10);
1377 plane->has_position = true;
1382 plane->scale = strtod(p, &end);
1383 if (plane->scale <= 0.0)
1394 strcpy(plane->format_str, p);
1396 strcpy(plane->format_str, "XR24");
1399 plane->fourcc = util_format_fourcc(plane->format_str);
1400 if (plane->fourcc == 0) {
1401 fprintf(stderr, "unknown format %s\n", plane->format_str);
1408 static int parse_property(struct property_arg *p, const char *arg)
1410 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1414 p->name[DRM_PROP_NAME_LEN] = '\0';
1419 static void usage(char *name)
1421 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1423 fprintf(stderr, "\n Query options:\n\n");
1424 fprintf(stderr, "\t-c\tlist connectors\n");
1425 fprintf(stderr, "\t-e\tlist encoders\n");
1426 fprintf(stderr, "\t-f\tlist framebuffers\n");
1427 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1429 fprintf(stderr, "\n Test options:\n\n");
1430 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1431 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1432 fprintf(stderr, "\t-C\ttest hw cursor\n");
1433 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1434 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1436 fprintf(stderr, "\n Generic options:\n\n");
1437 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1438 fprintf(stderr, "\t-M module\tuse the given driver\n");
1439 fprintf(stderr, "\t-D device\tuse the given device\n");
1441 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1445 static int page_flipping_supported(void)
1447 /*FIXME: generic ioctl needed? */
1451 struct drm_i915_getparam gp;
1453 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1456 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1458 fprintf(stderr, "drm_i915_getparam: %m\n");
1466 static int cursor_supported(void)
1468 /*FIXME: generic ioctl needed? */
1472 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1474 drmModeConnector *connector;
1479 for (i = 0; i < pipe->num_cons; i++) {
1480 id = strtoul(pipe->cons[i], &endp, 10);
1481 if (endp == pipe->cons[i]) {
1482 connector = get_connector_by_name(dev, pipe->cons[i]);
1484 fprintf(stderr, "no connector named '%s'\n",
1489 id = connector->connector_id;
1492 pipe->con_ids[i] = id;
1498 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1500 int main(int argc, char **argv)
1505 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1506 int drop_master = 0;
1508 int test_cursor = 0;
1509 char *device = NULL;
1510 char *module = NULL;
1512 unsigned int count = 0, plane_count = 0;
1513 unsigned int prop_count = 0;
1514 struct pipe_arg *pipe_args = NULL;
1515 struct plane_arg *plane_args = NULL;
1516 struct property_arg *prop_args = NULL;
1517 unsigned int args = 0;
1520 memset(&dev, 0, sizeof dev);
1523 while ((c = getopt(argc, argv, optstr)) != -1) {
1545 /* Preserve the default behaviour of dumping all information. */
1549 plane_args = realloc(plane_args,
1550 (plane_count + 1) * sizeof *plane_args);
1551 if (plane_args == NULL) {
1552 fprintf(stderr, "memory allocation failed\n");
1555 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1557 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1567 pipe_args = realloc(pipe_args,
1568 (count + 1) * sizeof *pipe_args);
1569 if (pipe_args == NULL) {
1570 fprintf(stderr, "memory allocation failed\n");
1573 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1575 if (parse_connector(&pipe_args[count], optarg) < 0)
1587 prop_args = realloc(prop_args,
1588 (prop_count + 1) * sizeof *prop_args);
1589 if (prop_args == NULL) {
1590 fprintf(stderr, "memory allocation failed\n");
1593 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1595 if (parse_property(&prop_args[prop_count], optarg) < 0)
1607 encoders = connectors = crtcs = planes = framebuffers = 1;
1609 dev.fd = util_open(device, module);
1613 if (test_vsync && !page_flipping_supported()) {
1614 fprintf(stderr, "page flipping not supported by drm.\n");
1618 if (test_vsync && !count) {
1619 fprintf(stderr, "page flipping requires at least one -s option.\n");
1623 if (test_cursor && !cursor_supported()) {
1624 fprintf(stderr, "hw cursor not supported by drm.\n");
1628 dev.resources = get_resources(&dev);
1629 if (!dev.resources) {
1634 for (i = 0; i < count; i++) {
1635 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1636 free_resources(dev.resources);
1642 #define dump_resource(dev, res) if (res) dump_##res(dev)
1644 dump_resource(&dev, encoders);
1645 dump_resource(&dev, connectors);
1646 dump_resource(&dev, crtcs);
1647 dump_resource(&dev, planes);
1648 dump_resource(&dev, framebuffers);
1650 for (i = 0; i < prop_count; ++i)
1651 set_property(&dev, &prop_args[i]);
1653 if (count || plane_count) {
1656 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1657 if (ret || cap == 0) {
1658 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1663 set_mode(&dev, pipe_args, count);
1666 set_planes(&dev, plane_args, plane_count);
1669 set_cursors(&dev, pipe_args, count);
1672 test_page_flip(&dev, pipe_args, count);
1675 drmDropMaster(dev.fd);
1680 clear_cursors(&dev);
1683 clear_planes(&dev, plane_args, plane_count);
1689 free_resources(dev.resources);