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.
56 #include "xf86drmMode.h"
57 #include "drm_fourcc.h"
64 drmModeObjectProperties *props;
65 drmModePropertyRes **props_info;
66 drmModeModeInfo *mode;
70 drmModeEncoder *encoder;
74 drmModeConnector *connector;
75 drmModeObjectProperties *props;
76 drmModePropertyRes **props_info;
85 drmModeObjectProperties *props;
86 drmModePropertyRes **props_info;
91 drmModePlaneRes *plane_res;
94 struct encoder *encoders;
95 struct connector *connectors;
103 struct resources *resources;
104 struct kms_driver *kms;
115 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
122 #define type_name_fn(res) \
123 const char * res##_str(int type) { \
125 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
126 if (res##_names[i].type == type) \
127 return res##_names[i].name; \
129 return "(invalid)"; \
132 struct type_name encoder_type_names[] = {
133 { DRM_MODE_ENCODER_NONE, "none" },
134 { DRM_MODE_ENCODER_DAC, "DAC" },
135 { DRM_MODE_ENCODER_TMDS, "TMDS" },
136 { DRM_MODE_ENCODER_LVDS, "LVDS" },
137 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
140 static type_name_fn(encoder_type)
142 struct type_name connector_status_names[] = {
143 { DRM_MODE_CONNECTED, "connected" },
144 { DRM_MODE_DISCONNECTED, "disconnected" },
145 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
148 static type_name_fn(connector_status)
150 struct type_name connector_type_names[] = {
151 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
152 { DRM_MODE_CONNECTOR_VGA, "VGA" },
153 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
154 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
155 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
156 { DRM_MODE_CONNECTOR_Composite, "composite" },
157 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
158 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
159 { DRM_MODE_CONNECTOR_Component, "component" },
160 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
161 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
162 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
163 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
164 { DRM_MODE_CONNECTOR_TV, "TV" },
165 { DRM_MODE_CONNECTOR_eDP, "eDP" },
168 static type_name_fn(connector_type)
170 #define bit_name_fn(res) \
171 const char * res##_str(int type) { \
173 const char *sep = ""; \
174 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
175 if (type & (1 << i)) { \
176 printf("%s%s", sep, res##_names[i]); \
183 static const char *mode_type_names[] = {
193 static bit_name_fn(mode_type)
195 static const char *mode_flag_names[] = {
212 static bit_name_fn(mode_flag)
214 static void dump_encoders(struct device *dev)
216 drmModeEncoder *encoder;
219 printf("Encoders:\n");
220 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
221 for (i = 0; i < dev->resources->res->count_encoders; i++) {
222 encoder = dev->resources->encoders[i].encoder;
226 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
229 encoder_type_str(encoder->encoder_type),
230 encoder->possible_crtcs,
231 encoder->possible_clones);
236 static void dump_mode(drmModeModeInfo *mode)
238 printf(" %s %d %d %d %d %d %d %d %d %d",
251 mode_flag_str(mode->flags);
253 mode_type_str(mode->type);
257 static void dump_blob(struct device *dev, uint32_t blob_id)
260 unsigned char *blob_data;
261 drmModePropertyBlobPtr blob;
263 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
267 blob_data = blob->data;
269 for (i = 0; i < blob->length; i++) {
272 printf("%.2hhx", blob_data[i]);
276 drmModeFreePropertyBlob(blob);
279 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
280 uint32_t prop_id, uint64_t value)
283 printf("\t%d", prop_id);
289 printf(" %s:\n", prop->name);
291 printf("\t\tflags:");
292 if (prop->flags & DRM_MODE_PROP_PENDING)
294 if (prop->flags & DRM_MODE_PROP_RANGE)
296 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
297 printf(" immutable");
298 if (prop->flags & DRM_MODE_PROP_ENUM)
300 if (prop->flags & DRM_MODE_PROP_BITMASK)
302 if (prop->flags & DRM_MODE_PROP_BLOB)
306 if (prop->flags & DRM_MODE_PROP_RANGE) {
307 printf("\t\tvalues:");
308 for (i = 0; i < prop->count_values; i++)
309 printf(" %"PRIu64, prop->values[i]);
313 if (prop->flags & DRM_MODE_PROP_ENUM) {
314 printf("\t\tenums:");
315 for (i = 0; i < prop->count_enums; i++)
316 printf(" %s=%llu", prop->enums[i].name,
317 prop->enums[i].value);
319 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
320 printf("\t\tvalues:");
321 for (i = 0; i < prop->count_enums; i++)
322 printf(" %s=0x%llx", prop->enums[i].name,
323 (1LL << prop->enums[i].value));
326 assert(prop->count_enums == 0);
329 if (prop->flags & DRM_MODE_PROP_BLOB) {
330 printf("\t\tblobs:\n");
331 for (i = 0; i < prop->count_blobs; i++)
332 dump_blob(dev, prop->blob_ids[i]);
335 assert(prop->count_blobs == 0);
338 printf("\t\tvalue:");
339 if (prop->flags & DRM_MODE_PROP_BLOB)
340 dump_blob(dev, value);
342 printf(" %"PRIu64"\n", value);
345 static void dump_connectors(struct device *dev)
349 printf("Connectors:\n");
350 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
351 for (i = 0; i < dev->resources->res->count_connectors; i++) {
352 struct connector *_connector = &dev->resources->connectors[i];
353 drmModeConnector *connector = _connector->connector;
357 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
358 connector->connector_id,
359 connector->encoder_id,
360 connector_status_str(connector->connection),
361 connector_type_str(connector->connector_type),
362 connector->mmWidth, connector->mmHeight,
363 connector->count_modes);
365 for (j = 0; j < connector->count_encoders; j++)
366 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
369 if (connector->count_modes) {
371 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
373 for (j = 0; j < connector->count_modes; j++)
374 dump_mode(&connector->modes[j]);
377 if (_connector->props) {
379 for (j = 0; j < (int)_connector->props->count_props; j++)
380 dump_prop(dev, _connector->props_info[j],
381 _connector->props->props[j],
382 _connector->props->prop_values[j]);
388 static void dump_crtcs(struct device *dev)
394 printf("id\tfb\tpos\tsize\n");
395 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
396 struct crtc *_crtc = &dev->resources->crtcs[i];
397 drmModeCrtc *crtc = _crtc->crtc;
401 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
405 crtc->width, crtc->height);
406 dump_mode(&crtc->mode);
410 for (j = 0; j < _crtc->props->count_props; j++)
411 dump_prop(dev, _crtc->props_info[j],
412 _crtc->props->props[j],
413 _crtc->props->prop_values[j]);
415 printf(" no properties found\n");
421 static void dump_framebuffers(struct device *dev)
426 printf("Frame buffers:\n");
427 printf("id\tsize\tpitch\n");
428 for (i = 0; i < dev->resources->res->count_fbs; i++) {
429 fb = dev->resources->fbs[i].fb;
433 printf("%u\t(%ux%u)\t%u\n",
435 fb->width, fb->height,
441 static void dump_planes(struct device *dev)
446 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
448 if (!dev->resources->plane_res)
451 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
452 struct plane *plane = &dev->resources->planes[i];
453 drmModePlane *ovr = plane->plane;
457 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
458 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
459 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
460 ovr->gamma_size, ovr->possible_crtcs);
462 if (!ovr->count_formats)
466 for (j = 0; j < ovr->count_formats; j++)
467 printf(" %4.4s", (char *)&ovr->formats[j]);
472 for (j = 0; j < plane->props->count_props; j++)
473 dump_prop(dev, plane->props_info[j],
474 plane->props->props[j],
475 plane->props->prop_values[j]);
477 printf(" no properties found\n");
485 static void free_resources(struct resources *res)
490 #define free_resource(_res, __res, type, Type) \
493 if (!(_res)->type##s) \
495 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
496 if (!(_res)->type##s[i].type) \
498 drmModeFree##Type((_res)->type##s[i].type); \
500 free((_res)->type##s); \
503 #define free_properties(_res, __res, type) \
506 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
507 drmModeFreeObjectProperties(res->type##s[i].props); \
508 free(res->type##s[i].props_info); \
513 free_properties(res, res, crtc);
515 free_resource(res, res, crtc, Crtc);
516 free_resource(res, res, encoder, Encoder);
517 free_resource(res, res, connector, Connector);
518 free_resource(res, res, fb, FB);
520 drmModeFreeResources(res->res);
523 if (res->plane_res) {
524 free_properties(res, plane_res, plane);
526 free_resource(res, plane_res, plane, Plane);
528 drmModeFreePlaneResources(res->plane_res);
534 static struct resources *get_resources(struct device *dev)
536 struct resources *res;
539 res = malloc(sizeof *res);
543 memset(res, 0, sizeof *res);
545 res->res = drmModeGetResources(dev->fd);
547 fprintf(stderr, "drmModeGetResources failed: %s\n",
552 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
553 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
554 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
555 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
557 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
560 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
561 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
562 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
563 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
565 #define get_resource(_res, __res, type, Type) \
568 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
569 (_res)->type##s[i].type = \
570 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
571 if (!(_res)->type##s[i].type) \
572 fprintf(stderr, "could not get %s %i: %s\n", \
573 #type, (_res)->__res->type##s[i], \
578 get_resource(res, res, crtc, Crtc);
579 get_resource(res, res, encoder, Encoder);
580 get_resource(res, res, connector, Connector);
581 get_resource(res, res, fb, FB);
583 #define get_properties(_res, __res, type, Type) \
586 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
587 struct type *obj = &res->type##s[i]; \
590 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
591 DRM_MODE_OBJECT_##Type); \
594 "could not get %s %i properties: %s\n", \
595 #type, obj->type->type##_id, \
599 obj->props_info = malloc(obj->props->count_props * \
600 sizeof *obj->props_info); \
601 if (!obj->props_info) \
603 for (j = 0; j < obj->props->count_props; ++j) \
604 obj->props_info[j] = \
605 drmModeGetProperty(dev->fd, obj->props->props[j]); \
609 get_properties(res, res, crtc, CRTC);
610 get_properties(res, res, connector, CONNECTOR);
612 for (i = 0; i < res->res->count_crtcs; ++i)
613 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
615 res->plane_res = drmModeGetPlaneResources(dev->fd);
616 if (!res->plane_res) {
617 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
622 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
626 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
628 get_resource(res, plane_res, plane, Plane);
629 get_properties(res, plane_res, plane, PLANE);
638 static int get_crtc_index(struct device *dev, uint32_t id)
642 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
643 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
644 if (crtc && crtc->crtc_id == id)
651 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
653 drmModeConnector *connector;
656 for (i = 0; i < dev->resources->res->count_connectors; i++) {
657 connector = dev->resources->connectors[i].connector;
658 if (connector && connector->connector_id == id)
665 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
667 drmModeEncoder *encoder;
670 for (i = 0; i < dev->resources->res->count_encoders; i++) {
671 encoder = dev->resources->encoders[i].encoder;
672 if (encoder && encoder->encoder_id == id)
679 /* -----------------------------------------------------------------------------
684 * Mode setting with the kernel interfaces is a bit of a chore.
685 * First you have to find the connector in question and make sure the
686 * requested mode is available.
687 * Then you need to find the encoder attached to that connector so you
688 * can bind it with a free crtc.
692 unsigned int num_cons;
697 drmModeModeInfo *mode;
699 unsigned int fb_id[2], current_fb_id;
700 struct timeval start;
706 uint32_t crtc_id; /* the id of CRTC to bind to */
712 char format_str[5]; /* need to leave room for terminating \0 */
716 static drmModeModeInfo *
717 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str)
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))
736 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
738 uint32_t possible_crtcs = ~0;
739 uint32_t active_crtcs = 0;
740 unsigned int crtc_idx;
744 for (i = 0; i < pipe->num_cons; ++i) {
745 uint32_t crtcs_for_connector = 0;
746 drmModeConnector *connector;
747 drmModeEncoder *encoder;
750 connector = get_connector_by_id(dev, pipe->con_ids[i]);
754 for (j = 0; j < connector->count_encoders; ++j) {
755 encoder = get_encoder_by_id(dev, connector->encoders[j]);
759 crtcs_for_connector |= encoder->possible_crtcs;
761 idx = get_crtc_index(dev, encoder->crtc_id);
763 active_crtcs |= 1 << idx;
766 possible_crtcs &= crtcs_for_connector;
772 /* Return the first possible and active CRTC if one exists, or the first
773 * possible CRTC otherwise.
775 if (possible_crtcs & active_crtcs)
776 crtc_idx = ffs(possible_crtcs & active_crtcs);
778 crtc_idx = ffs(possible_crtcs);
780 return &dev->resources->crtcs[crtc_idx - 1];
783 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
785 drmModeModeInfo *mode = NULL;
790 for (i = 0; i < (int)pipe->num_cons; i++) {
791 mode = connector_find_mode(dev, pipe->con_ids[i],
795 "failed to find mode \"%s\" for connector %u\n",
796 pipe->mode_str, pipe->con_ids[i]);
801 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
802 * locate a CRTC that can be attached to all the connectors.
804 if (pipe->crtc_id != (uint32_t)-1) {
805 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
806 struct crtc *crtc = &dev->resources->crtcs[i];
808 if (pipe->crtc_id == crtc->crtc->crtc_id) {
814 pipe->crtc = pipe_find_crtc(dev, pipe);
818 fprintf(stderr, "failed to find CRTC for pipe\n");
823 pipe->crtc->mode = mode;
828 /* -----------------------------------------------------------------------------
832 struct property_arg {
835 char name[DRM_PROP_NAME_LEN+1];
840 static void set_property(struct device *dev, struct property_arg *p)
842 drmModeObjectProperties *props = NULL;
843 drmModePropertyRes **props_info = NULL;
844 const char *obj_type;
851 #define find_object(_res, __res, type, Type) \
853 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
854 struct type *obj = &(_res)->type##s[i]; \
855 if (obj->type->type##_id != p->obj_id) \
857 p->obj_type = DRM_MODE_OBJECT_##Type; \
859 props = obj->props; \
860 props_info = obj->props_info; \
864 find_object(dev->resources, res, crtc, CRTC);
865 if (p->obj_type == 0)
866 find_object(dev->resources, res, connector, CONNECTOR);
867 if (p->obj_type == 0)
868 find_object(dev->resources, plane_res, plane, PLANE);
869 if (p->obj_type == 0) {
870 fprintf(stderr, "Object %i not found, can't set property\n",
876 fprintf(stderr, "%s %i has no properties\n",
877 obj_type, p->obj_id);
881 for (i = 0; i < (int)props->count_props; ++i) {
884 if (strcmp(props_info[i]->name, p->name) == 0)
888 if (i == (int)props->count_props) {
889 fprintf(stderr, "%s %i has no %s property\n",
890 obj_type, p->obj_id, p->name);
894 p->prop_id = props->props[i];
896 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
897 p->prop_id, p->value);
899 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
900 obj_type, p->obj_id, p->name, p->value, strerror(errno));
903 /* -------------------------------------------------------------------------- */
906 page_flip_handler(int fd, unsigned int frame,
907 unsigned int sec, unsigned int usec, void *data)
909 struct pipe_arg *pipe;
910 unsigned int new_fb_id;
915 if (pipe->current_fb_id == pipe->fb_id[0])
916 new_fb_id = pipe->fb_id[1];
918 new_fb_id = pipe->fb_id[0];
920 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
921 DRM_MODE_PAGE_FLIP_EVENT, pipe);
922 pipe->current_fb_id = new_fb_id;
924 if (pipe->swap_count == 60) {
925 gettimeofday(&end, NULL);
926 t = end.tv_sec + end.tv_usec * 1e-6 -
927 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
928 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
929 pipe->swap_count = 0;
934 static int set_plane(struct device *dev, struct plane_arg *p)
937 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
938 uint32_t plane_id = 0;
939 struct kms_bo *plane_bo;
940 uint32_t plane_flags = 0;
941 int crtc_x, crtc_y, crtc_w, crtc_h;
942 struct crtc *crtc = NULL;
946 /* Find an unused plane which can be connected to our CRTC. Find the
947 * CRTC index first, then iterate over available planes.
949 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
950 if (p->crtc_id == dev->resources->res->crtcs[i]) {
951 crtc = &dev->resources->crtcs[i];
958 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
962 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
963 ovr = dev->resources->planes[i].plane;
967 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
968 plane_id = ovr->plane_id;
972 fprintf(stderr, "no unused plane available for CRTC %u\n",
973 crtc->crtc->crtc_id);
977 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
978 p->w, p->h, p->format_str, plane_id);
980 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
981 pitches, offsets, PATTERN_TILES);
982 if (plane_bo == NULL)
985 /* just use single plane format for now.. */
986 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
987 handles, pitches, offsets, &p->fb_id, plane_flags)) {
988 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
992 crtc_w = p->w * p->scale;
993 crtc_h = p->h * p->scale;
994 if (!p->has_position) {
995 /* Default to the middle of the screen */
996 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
997 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1003 /* note src coords (last 4 args) are in Q16 format */
1004 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1005 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1006 0, 0, p->w << 16, p->h << 16)) {
1007 fprintf(stderr, "failed to enable plane: %s\n",
1012 ovr->crtc_id = crtc->crtc->crtc_id;
1017 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1019 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1026 dev->mode.width = 0;
1027 dev->mode.height = 0;
1029 for (i = 0; i < count; i++) {
1030 struct pipe_arg *pipe = &pipes[i];
1032 ret = pipe_find_crtc_and_mode(dev, pipe);
1036 dev->mode.width += pipe->mode->hdisplay;
1037 if (dev->mode.height < pipe->mode->vdisplay)
1038 dev->mode.height = pipe->mode->vdisplay;
1041 bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1042 dev->mode.width, dev->mode.height,
1043 handles, pitches, offsets, PATTERN_SMPTE);
1047 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1048 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1050 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1051 dev->mode.width, dev->mode.height, strerror(errno));
1056 for (i = 0; i < count; i++) {
1057 struct pipe_arg *pipe = &pipes[i];
1059 if (pipe->mode == NULL)
1062 printf("setting mode %s@%s on connectors ",
1063 pipe->mode_str, pipe->format_str);
1064 for (j = 0; j < pipe->num_cons; ++j)
1065 printf("%u, ", pipe->con_ids[j]);
1066 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1068 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1069 x, 0, pipe->con_ids, pipe->num_cons,
1072 /* XXX: Actually check if this is needed */
1073 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1075 x += pipe->mode->hdisplay;
1078 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1084 dev->mode.fb_id = fb_id;
1087 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1091 /* set up planes/overlays */
1092 for (i = 0; i < count; i++)
1093 if (set_plane(dev, &p[i]))
1097 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1099 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1100 unsigned int other_fb_id;
1101 struct kms_bo *other_bo;
1102 drmEventContext evctx;
1106 other_bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1107 dev->mode.width, dev->mode.height,
1108 handles, pitches, offsets, PATTERN_PLAIN);
1109 if (other_bo == NULL)
1112 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1113 pipes[0].fourcc, handles, pitches, offsets,
1116 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1120 for (i = 0; i < count; i++) {
1121 struct pipe_arg *pipe = &pipes[i];
1123 if (pipe->mode == NULL)
1126 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1127 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1130 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1133 gettimeofday(&pipe->start, NULL);
1134 pipe->swap_count = 0;
1135 pipe->fb_id[0] = dev->mode.fb_id;
1136 pipe->fb_id[1] = other_fb_id;
1137 pipe->current_fb_id = other_fb_id;
1140 memset(&evctx, 0, sizeof evctx);
1141 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1142 evctx.vblank_handler = NULL;
1143 evctx.page_flip_handler = page_flip_handler;
1147 struct pollfd pfd[2];
1150 pfd[0].events = POLLIN;
1152 pfd[1].events = POLLIN;
1154 if (poll(pfd, 2, -1) < 0) {
1155 fprintf(stderr, "poll error\n");
1162 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1168 FD_SET(dev->fd, &fds);
1169 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1172 fprintf(stderr, "select timed out or error (ret %d)\n",
1175 } else if (FD_ISSET(0, &fds)) {
1180 drmHandleEvent(dev->fd, &evctx);
1183 kms_bo_destroy(&other_bo);
1186 #define min(a, b) ((a) < (b) ? (a) : (b))
1188 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1195 pipe->crtc_id = (uint32_t)-1;
1196 strcpy(pipe->format_str, "XR24");
1198 /* Count the number of connectors and allocate them. */
1200 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1205 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1206 if (pipe->con_ids == NULL)
1209 /* Parse the connectors. */
1210 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1211 pipe->con_ids[i] = strtoul(p, &endp, 10);
1216 if (i != pipe->num_cons - 1)
1219 /* Parse the remaining parameters. */
1222 pipe->crtc_id = strtoul(arg, &endp, 10);
1229 p = strchrnul(arg, '@');
1230 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1231 strncpy(pipe->mode_str, arg, len);
1232 pipe->mode_str[len] = '\0';
1235 strncpy(pipe->format_str, p + 1, 4);
1236 pipe->format_str[4] = '\0';
1239 pipe->fourcc = format_fourcc(pipe->format_str);
1240 if (pipe->fourcc == 0) {
1241 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1248 static int parse_plane(struct plane_arg *plane, const char *p)
1252 memset(plane, 0, sizeof *plane);
1254 plane->crtc_id = strtoul(p, &end, 10);
1259 plane->w = strtoul(p, &end, 10);
1264 plane->h = strtoul(p, &end, 10);
1266 if (*end == '+' || *end == '-') {
1267 plane->x = strtol(end, &end, 10);
1268 if (*end != '+' && *end != '-')
1270 plane->y = strtol(end, &end, 10);
1272 plane->has_position = true;
1277 plane->scale = strtod(p, &end);
1278 if (plane->scale <= 0.0)
1289 strcpy(plane->format_str, p);
1291 strcpy(plane->format_str, "XR24");
1294 plane->fourcc = format_fourcc(plane->format_str);
1295 if (plane->fourcc == 0) {
1296 fprintf(stderr, "unknown format %s\n", plane->format_str);
1303 static int parse_property(struct property_arg *p, const char *arg)
1305 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1309 p->name[DRM_PROP_NAME_LEN] = '\0';
1314 static void usage(char *name)
1316 fprintf(stderr, "usage: %s [-cDdefMPpsvw]\n", name);
1318 fprintf(stderr, "\n Query options:\n\n");
1319 fprintf(stderr, "\t-c\tlist connectors\n");
1320 fprintf(stderr, "\t-e\tlist encoders\n");
1321 fprintf(stderr, "\t-f\tlist framebuffers\n");
1322 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1324 fprintf(stderr, "\n Test options:\n\n");
1325 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1326 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[@<format>]\tset a mode\n");
1327 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1328 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1330 fprintf(stderr, "\n Generic options:\n\n");
1331 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1332 fprintf(stderr, "\t-M module\tuse the given driver\n");
1333 fprintf(stderr, "\t-D device\tuse the given device\n");
1335 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1339 static int page_flipping_supported(void)
1341 /*FIXME: generic ioctl needed? */
1345 struct drm_i915_getparam gp;
1347 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1350 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1352 fprintf(stderr, "drm_i915_getparam: %m\n");
1360 static char optstr[] = "cdD:efM:P:ps:vw:";
1362 int main(int argc, char **argv)
1367 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1368 int drop_master = 0;
1370 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm" };
1371 char *device = NULL;
1372 char *module = NULL;
1374 int count = 0, plane_count = 0;
1375 unsigned int prop_count = 0;
1376 struct pipe_arg *pipe_args = NULL;
1377 struct plane_arg *plane_args = NULL;
1378 struct property_arg *prop_args = NULL;
1379 unsigned int args = 0;
1382 memset(&dev, 0, sizeof dev);
1385 while ((c = getopt(argc, argv, optstr)) != -1) {
1407 /* Preserve the default behaviour of dumping all information. */
1411 plane_args = realloc(plane_args,
1412 (plane_count + 1) * sizeof *plane_args);
1413 if (plane_args == NULL) {
1414 fprintf(stderr, "memory allocation failed\n");
1418 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1428 pipe_args = realloc(pipe_args,
1429 (count + 1) * sizeof *pipe_args);
1430 if (pipe_args == NULL) {
1431 fprintf(stderr, "memory allocation failed\n");
1435 if (parse_connector(&pipe_args[count], optarg) < 0)
1444 prop_args = realloc(prop_args,
1445 (prop_count + 1) * sizeof *prop_args);
1446 if (prop_args == NULL) {
1447 fprintf(stderr, "memory allocation failed\n");
1451 if (parse_property(&prop_args[prop_count], optarg) < 0)
1463 encoders = connectors = crtcs = planes = framebuffers = 1;
1466 dev.fd = drmOpen(module, device);
1468 fprintf(stderr, "failed to open device '%s'.\n", module);
1472 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1473 printf("trying to open device '%s'...", modules[i]);
1474 dev.fd = drmOpen(modules[i], device);
1476 printf("failed.\n");
1478 printf("success.\n");
1484 fprintf(stderr, "no device found.\n");
1489 if (test_vsync && !page_flipping_supported()) {
1490 fprintf(stderr, "page flipping not supported by drm.\n");
1494 if (test_vsync && !count) {
1495 fprintf(stderr, "page flipping requires at least one -s option.\n");
1499 dev.resources = get_resources(&dev);
1500 if (!dev.resources) {
1505 #define dump_resource(dev, res) if (res) dump_##res(dev)
1507 dump_resource(&dev, encoders);
1508 dump_resource(&dev, connectors);
1509 dump_resource(&dev, crtcs);
1510 dump_resource(&dev, planes);
1511 dump_resource(&dev, framebuffers);
1513 for (i = 0; i < prop_count; ++i)
1514 set_property(&dev, &prop_args[i]);
1516 if (count || plane_count) {
1517 ret = kms_create(dev.fd, &dev.kms);
1519 fprintf(stderr, "failed to create kms driver: %s\n",
1525 set_mode(&dev, pipe_args, count);
1528 set_planes(&dev, plane_args, plane_count);
1531 test_page_flip(&dev, pipe_args, count);
1534 drmDropMaster(dev.fd);
1536 kms_bo_destroy(&dev.mode.bo);
1537 kms_destroy(&dev.kms);
1542 free_resources(dev.resources);