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 #include "xf86drmMode.h"
59 #include "drm_fourcc.h"
67 drmModeObjectProperties *props;
68 drmModePropertyRes **props_info;
69 drmModeModeInfo *mode;
73 drmModeEncoder *encoder;
77 drmModeConnector *connector;
78 drmModeObjectProperties *props;
79 drmModePropertyRes **props_info;
88 drmModeObjectProperties *props;
89 drmModePropertyRes **props_info;
94 drmModePlaneRes *plane_res;
97 struct encoder *encoders;
98 struct connector *connectors;
100 struct plane *planes;
106 struct resources *resources;
107 struct kms_driver *kms;
118 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
125 #define type_name_fn(res) \
126 const char * res##_str(int type) { \
128 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
129 if (res##_names[i].type == type) \
130 return res##_names[i].name; \
132 return "(invalid)"; \
135 struct type_name encoder_type_names[] = {
136 { DRM_MODE_ENCODER_NONE, "none" },
137 { DRM_MODE_ENCODER_DAC, "DAC" },
138 { DRM_MODE_ENCODER_TMDS, "TMDS" },
139 { DRM_MODE_ENCODER_LVDS, "LVDS" },
140 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
143 static type_name_fn(encoder_type)
145 struct type_name connector_status_names[] = {
146 { DRM_MODE_CONNECTED, "connected" },
147 { DRM_MODE_DISCONNECTED, "disconnected" },
148 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
151 static type_name_fn(connector_status)
153 struct type_name connector_type_names[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
171 static type_name_fn(connector_type)
173 #define bit_name_fn(res) \
174 const char * res##_str(int type) { \
176 const char *sep = ""; \
177 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
178 if (type & (1 << i)) { \
179 printf("%s%s", sep, res##_names[i]); \
186 static const char *mode_type_names[] = {
196 static bit_name_fn(mode_type)
198 static const char *mode_flag_names[] = {
215 static bit_name_fn(mode_flag)
217 static void dump_encoders(struct device *dev)
219 drmModeEncoder *encoder;
222 printf("Encoders:\n");
223 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
224 for (i = 0; i < dev->resources->res->count_encoders; i++) {
225 encoder = dev->resources->encoders[i].encoder;
229 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
232 encoder_type_str(encoder->encoder_type),
233 encoder->possible_crtcs,
234 encoder->possible_clones);
239 static void dump_mode(drmModeModeInfo *mode)
241 printf(" %s %d %d %d %d %d %d %d %d %d",
254 mode_flag_str(mode->flags);
256 mode_type_str(mode->type);
260 static void dump_blob(struct device *dev, uint32_t blob_id)
263 unsigned char *blob_data;
264 drmModePropertyBlobPtr blob;
266 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
272 blob_data = blob->data;
274 for (i = 0; i < blob->length; i++) {
277 printf("%.2hhx", blob_data[i]);
281 drmModeFreePropertyBlob(blob);
284 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
285 uint32_t prop_id, uint64_t value)
288 printf("\t%d", prop_id);
294 printf(" %s:\n", prop->name);
296 printf("\t\tflags:");
297 if (prop->flags & DRM_MODE_PROP_PENDING)
299 if (prop->flags & DRM_MODE_PROP_RANGE)
301 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
302 printf(" immutable");
303 if (prop->flags & DRM_MODE_PROP_ENUM)
305 if (prop->flags & DRM_MODE_PROP_BITMASK)
307 if (prop->flags & DRM_MODE_PROP_BLOB)
311 if (prop->flags & DRM_MODE_PROP_RANGE) {
312 printf("\t\tvalues:");
313 for (i = 0; i < prop->count_values; i++)
314 printf(" %"PRIu64, prop->values[i]);
318 if (prop->flags & DRM_MODE_PROP_ENUM) {
319 printf("\t\tenums:");
320 for (i = 0; i < prop->count_enums; i++)
321 printf(" %s=%llu", prop->enums[i].name,
322 prop->enums[i].value);
324 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
325 printf("\t\tvalues:");
326 for (i = 0; i < prop->count_enums; i++)
327 printf(" %s=0x%llx", prop->enums[i].name,
328 (1LL << prop->enums[i].value));
331 assert(prop->count_enums == 0);
334 if (prop->flags & DRM_MODE_PROP_BLOB) {
335 printf("\t\tblobs:\n");
336 for (i = 0; i < prop->count_blobs; i++)
337 dump_blob(dev, prop->blob_ids[i]);
340 assert(prop->count_blobs == 0);
343 printf("\t\tvalue:");
344 if (prop->flags & DRM_MODE_PROP_BLOB)
345 dump_blob(dev, value);
347 printf(" %"PRIu64"\n", value);
350 static void dump_connectors(struct device *dev)
354 printf("Connectors:\n");
355 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
356 for (i = 0; i < dev->resources->res->count_connectors; i++) {
357 struct connector *_connector = &dev->resources->connectors[i];
358 drmModeConnector *connector = _connector->connector;
362 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
363 connector->connector_id,
364 connector->encoder_id,
365 connector_status_str(connector->connection),
366 connector_type_str(connector->connector_type),
367 connector->mmWidth, connector->mmHeight,
368 connector->count_modes);
370 for (j = 0; j < connector->count_encoders; j++)
371 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
374 if (connector->count_modes) {
376 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
378 for (j = 0; j < connector->count_modes; j++)
379 dump_mode(&connector->modes[j]);
382 if (_connector->props) {
384 for (j = 0; j < (int)_connector->props->count_props; j++)
385 dump_prop(dev, _connector->props_info[j],
386 _connector->props->props[j],
387 _connector->props->prop_values[j]);
393 static void dump_crtcs(struct device *dev)
399 printf("id\tfb\tpos\tsize\n");
400 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
401 struct crtc *_crtc = &dev->resources->crtcs[i];
402 drmModeCrtc *crtc = _crtc->crtc;
406 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
410 crtc->width, crtc->height);
411 dump_mode(&crtc->mode);
415 for (j = 0; j < _crtc->props->count_props; j++)
416 dump_prop(dev, _crtc->props_info[j],
417 _crtc->props->props[j],
418 _crtc->props->prop_values[j]);
420 printf(" no properties found\n");
426 static void dump_framebuffers(struct device *dev)
431 printf("Frame buffers:\n");
432 printf("id\tsize\tpitch\n");
433 for (i = 0; i < dev->resources->res->count_fbs; i++) {
434 fb = dev->resources->fbs[i].fb;
438 printf("%u\t(%ux%u)\t%u\n",
440 fb->width, fb->height,
446 static void dump_planes(struct device *dev)
451 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
453 if (!dev->resources->plane_res)
456 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
457 struct plane *plane = &dev->resources->planes[i];
458 drmModePlane *ovr = plane->plane;
462 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
463 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
464 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
465 ovr->gamma_size, ovr->possible_crtcs);
467 if (!ovr->count_formats)
471 for (j = 0; j < ovr->count_formats; j++)
472 printf(" %4.4s", (char *)&ovr->formats[j]);
477 for (j = 0; j < plane->props->count_props; j++)
478 dump_prop(dev, plane->props_info[j],
479 plane->props->props[j],
480 plane->props->prop_values[j]);
482 printf(" no properties found\n");
490 static void free_resources(struct resources *res)
495 #define free_resource(_res, __res, type, Type) \
498 if (!(_res)->type##s) \
500 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
501 if (!(_res)->type##s[i].type) \
503 drmModeFree##Type((_res)->type##s[i].type); \
505 free((_res)->type##s); \
508 #define free_properties(_res, __res, type) \
511 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
512 drmModeFreeObjectProperties(res->type##s[i].props); \
513 free(res->type##s[i].props_info); \
518 free_properties(res, res, crtc);
520 free_resource(res, res, crtc, Crtc);
521 free_resource(res, res, encoder, Encoder);
522 free_resource(res, res, connector, Connector);
523 free_resource(res, res, fb, FB);
525 drmModeFreeResources(res->res);
528 if (res->plane_res) {
529 free_properties(res, plane_res, plane);
531 free_resource(res, plane_res, plane, Plane);
533 drmModeFreePlaneResources(res->plane_res);
539 static struct resources *get_resources(struct device *dev)
541 struct resources *res;
544 res = malloc(sizeof *res);
548 memset(res, 0, sizeof *res);
550 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
552 res->res = drmModeGetResources(dev->fd);
554 fprintf(stderr, "drmModeGetResources failed: %s\n",
559 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
560 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
561 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
562 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
564 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
567 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
568 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
569 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
570 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
572 #define get_resource(_res, __res, type, Type) \
575 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
576 (_res)->type##s[i].type = \
577 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
578 if (!(_res)->type##s[i].type) \
579 fprintf(stderr, "could not get %s %i: %s\n", \
580 #type, (_res)->__res->type##s[i], \
585 get_resource(res, res, crtc, Crtc);
586 get_resource(res, res, encoder, Encoder);
587 get_resource(res, res, connector, Connector);
588 get_resource(res, res, fb, FB);
590 #define get_properties(_res, __res, type, Type) \
593 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
594 struct type *obj = &res->type##s[i]; \
597 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
598 DRM_MODE_OBJECT_##Type); \
601 "could not get %s %i properties: %s\n", \
602 #type, obj->type->type##_id, \
606 obj->props_info = malloc(obj->props->count_props * \
607 sizeof *obj->props_info); \
608 if (!obj->props_info) \
610 for (j = 0; j < obj->props->count_props; ++j) \
611 obj->props_info[j] = \
612 drmModeGetProperty(dev->fd, obj->props->props[j]); \
616 get_properties(res, res, crtc, CRTC);
617 get_properties(res, res, connector, CONNECTOR);
619 for (i = 0; i < res->res->count_crtcs; ++i)
620 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
622 res->plane_res = drmModeGetPlaneResources(dev->fd);
623 if (!res->plane_res) {
624 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
629 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
633 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
635 get_resource(res, plane_res, plane, Plane);
636 get_properties(res, plane_res, plane, PLANE);
645 static int get_crtc_index(struct device *dev, uint32_t id)
649 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
650 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
651 if (crtc && crtc->crtc_id == id)
658 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
660 drmModeConnector *connector;
663 for (i = 0; i < dev->resources->res->count_connectors; i++) {
664 connector = dev->resources->connectors[i].connector;
665 if (connector && connector->connector_id == id)
672 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
674 drmModeEncoder *encoder;
677 for (i = 0; i < dev->resources->res->count_encoders; i++) {
678 encoder = dev->resources->encoders[i].encoder;
679 if (encoder && encoder->encoder_id == id)
686 /* -----------------------------------------------------------------------------
691 * Mode setting with the kernel interfaces is a bit of a chore.
692 * First you have to find the connector in question and make sure the
693 * requested mode is available.
694 * Then you need to find the encoder attached to that connector so you
695 * can bind it with a free crtc.
699 unsigned int num_cons;
703 unsigned int vrefresh;
705 drmModeModeInfo *mode;
707 unsigned int fb_id[2], current_fb_id;
708 struct timeval start;
714 uint32_t crtc_id; /* the id of CRTC to bind to */
720 char format_str[5]; /* need to leave room for terminating \0 */
724 static drmModeModeInfo *
725 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
726 const unsigned int vrefresh)
728 drmModeConnector *connector;
729 drmModeModeInfo *mode;
732 connector = get_connector_by_id(dev, con_id);
733 if (!connector || !connector->count_modes)
736 for (i = 0; i < connector->count_modes; i++) {
737 mode = &connector->modes[i];
738 if (!strcmp(mode->name, mode_str)) {
739 /* If the vertical refresh frequency is not specified then return the
740 * first mode that match with the name. Else, return the mode that match
741 * the name and the specified vertical refresh frequency.
745 else if (mode->vrefresh == vrefresh)
753 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
755 uint32_t possible_crtcs = ~0;
756 uint32_t active_crtcs = 0;
757 unsigned int crtc_idx;
761 for (i = 0; i < pipe->num_cons; ++i) {
762 uint32_t crtcs_for_connector = 0;
763 drmModeConnector *connector;
764 drmModeEncoder *encoder;
767 connector = get_connector_by_id(dev, pipe->con_ids[i]);
771 for (j = 0; j < connector->count_encoders; ++j) {
772 encoder = get_encoder_by_id(dev, connector->encoders[j]);
776 crtcs_for_connector |= encoder->possible_crtcs;
778 idx = get_crtc_index(dev, encoder->crtc_id);
780 active_crtcs |= 1 << idx;
783 possible_crtcs &= crtcs_for_connector;
789 /* Return the first possible and active CRTC if one exists, or the first
790 * possible CRTC otherwise.
792 if (possible_crtcs & active_crtcs)
793 crtc_idx = ffs(possible_crtcs & active_crtcs);
795 crtc_idx = ffs(possible_crtcs);
797 return &dev->resources->crtcs[crtc_idx - 1];
800 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
802 drmModeModeInfo *mode = NULL;
807 for (i = 0; i < (int)pipe->num_cons; i++) {
808 mode = connector_find_mode(dev, pipe->con_ids[i],
809 pipe->mode_str, pipe->vrefresh);
812 "failed to find mode \"%s\" for connector %u\n",
813 pipe->mode_str, pipe->con_ids[i]);
818 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
819 * locate a CRTC that can be attached to all the connectors.
821 if (pipe->crtc_id != (uint32_t)-1) {
822 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
823 struct crtc *crtc = &dev->resources->crtcs[i];
825 if (pipe->crtc_id == crtc->crtc->crtc_id) {
831 pipe->crtc = pipe_find_crtc(dev, pipe);
835 fprintf(stderr, "failed to find CRTC for pipe\n");
840 pipe->crtc->mode = mode;
845 /* -----------------------------------------------------------------------------
849 struct property_arg {
852 char name[DRM_PROP_NAME_LEN+1];
857 static void set_property(struct device *dev, struct property_arg *p)
859 drmModeObjectProperties *props = NULL;
860 drmModePropertyRes **props_info = NULL;
861 const char *obj_type;
868 #define find_object(_res, __res, type, Type) \
870 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
871 struct type *obj = &(_res)->type##s[i]; \
872 if (obj->type->type##_id != p->obj_id) \
874 p->obj_type = DRM_MODE_OBJECT_##Type; \
876 props = obj->props; \
877 props_info = obj->props_info; \
881 find_object(dev->resources, res, crtc, CRTC);
882 if (p->obj_type == 0)
883 find_object(dev->resources, res, connector, CONNECTOR);
884 if (p->obj_type == 0)
885 find_object(dev->resources, plane_res, plane, PLANE);
886 if (p->obj_type == 0) {
887 fprintf(stderr, "Object %i not found, can't set property\n",
893 fprintf(stderr, "%s %i has no properties\n",
894 obj_type, p->obj_id);
898 for (i = 0; i < (int)props->count_props; ++i) {
901 if (strcmp(props_info[i]->name, p->name) == 0)
905 if (i == (int)props->count_props) {
906 fprintf(stderr, "%s %i has no %s property\n",
907 obj_type, p->obj_id, p->name);
911 p->prop_id = props->props[i];
913 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
914 p->prop_id, p->value);
916 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
917 obj_type, p->obj_id, p->name, p->value, strerror(errno));
920 /* -------------------------------------------------------------------------- */
923 page_flip_handler(int fd, unsigned int frame,
924 unsigned int sec, unsigned int usec, void *data)
926 struct pipe_arg *pipe;
927 unsigned int new_fb_id;
932 if (pipe->current_fb_id == pipe->fb_id[0])
933 new_fb_id = pipe->fb_id[1];
935 new_fb_id = pipe->fb_id[0];
937 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
938 DRM_MODE_PAGE_FLIP_EVENT, pipe);
939 pipe->current_fb_id = new_fb_id;
941 if (pipe->swap_count == 60) {
942 gettimeofday(&end, NULL);
943 t = end.tv_sec + end.tv_usec * 1e-6 -
944 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
945 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
946 pipe->swap_count = 0;
951 static int set_plane(struct device *dev, struct plane_arg *p)
954 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
955 uint32_t plane_id = 0;
956 struct kms_bo *plane_bo;
957 uint32_t plane_flags = 0;
958 int crtc_x, crtc_y, crtc_w, crtc_h;
959 struct crtc *crtc = NULL;
963 /* Find an unused plane which can be connected to our CRTC. Find the
964 * CRTC index first, then iterate over available planes.
966 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
967 if (p->crtc_id == dev->resources->res->crtcs[i]) {
968 crtc = &dev->resources->crtcs[i];
975 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
979 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
980 ovr = dev->resources->planes[i].plane;
984 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
985 plane_id = ovr->plane_id;
989 fprintf(stderr, "no unused plane available for CRTC %u\n",
990 crtc->crtc->crtc_id);
994 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
995 p->w, p->h, p->format_str, plane_id);
997 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
998 pitches, offsets, PATTERN_TILES);
999 if (plane_bo == NULL)
1002 /* just use single plane format for now.. */
1003 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1004 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1005 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1009 crtc_w = p->w * p->scale;
1010 crtc_h = p->h * p->scale;
1011 if (!p->has_position) {
1012 /* Default to the middle of the screen */
1013 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1014 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1020 /* note src coords (last 4 args) are in Q16 format */
1021 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1022 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1023 0, 0, p->w << 16, p->h << 16)) {
1024 fprintf(stderr, "failed to enable plane: %s\n",
1029 ovr->crtc_id = crtc->crtc->crtc_id;
1034 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1036 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1043 dev->mode.width = 0;
1044 dev->mode.height = 0;
1046 for (i = 0; i < count; i++) {
1047 struct pipe_arg *pipe = &pipes[i];
1049 ret = pipe_find_crtc_and_mode(dev, pipe);
1053 dev->mode.width += pipe->mode->hdisplay;
1054 if (dev->mode.height < pipe->mode->vdisplay)
1055 dev->mode.height = pipe->mode->vdisplay;
1058 bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1059 dev->mode.width, dev->mode.height,
1060 handles, pitches, offsets, PATTERN_SMPTE);
1064 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1065 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1067 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1068 dev->mode.width, dev->mode.height, strerror(errno));
1073 for (i = 0; i < count; i++) {
1074 struct pipe_arg *pipe = &pipes[i];
1076 if (pipe->mode == NULL)
1079 printf("setting mode %s-%dHz@%s on connectors ",
1080 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1081 for (j = 0; j < pipe->num_cons; ++j)
1082 printf("%u, ", pipe->con_ids[j]);
1083 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1085 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1086 x, 0, pipe->con_ids, pipe->num_cons,
1089 /* XXX: Actually check if this is needed */
1090 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1092 x += pipe->mode->hdisplay;
1095 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1101 dev->mode.fb_id = fb_id;
1104 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1108 /* set up planes/overlays */
1109 for (i = 0; i < count; i++)
1110 if (set_plane(dev, &p[i]))
1114 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1116 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1121 /* maybe make cursor width/height configurable some day */
1125 /* create cursor bo.. just using PATTERN_PLAIN as it has
1128 bo = create_test_buffer(dev->kms, DRM_FORMAT_ARGB8888,
1129 cw, ch, handles, pitches, offsets, PATTERN_PLAIN);
1133 for (i = 0; i < count; i++) {
1134 struct pipe_arg *pipe = &pipes[i];
1135 ret = cursor_init(dev->fd, handles[0],
1136 pipe->crtc->crtc->crtc_id,
1137 pipe->mode->hdisplay, pipe->mode->vdisplay,
1140 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1149 static void clear_cursors(struct device *dev)
1154 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1156 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1157 unsigned int other_fb_id;
1158 struct kms_bo *other_bo;
1159 drmEventContext evctx;
1163 other_bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1164 dev->mode.width, dev->mode.height,
1165 handles, pitches, offsets, PATTERN_PLAIN);
1166 if (other_bo == NULL)
1169 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1170 pipes[0].fourcc, handles, pitches, offsets,
1173 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1177 for (i = 0; i < count; i++) {
1178 struct pipe_arg *pipe = &pipes[i];
1180 if (pipe->mode == NULL)
1183 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1184 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1187 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1190 gettimeofday(&pipe->start, NULL);
1191 pipe->swap_count = 0;
1192 pipe->fb_id[0] = dev->mode.fb_id;
1193 pipe->fb_id[1] = other_fb_id;
1194 pipe->current_fb_id = other_fb_id;
1197 memset(&evctx, 0, sizeof evctx);
1198 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1199 evctx.vblank_handler = NULL;
1200 evctx.page_flip_handler = page_flip_handler;
1204 struct pollfd pfd[2];
1207 pfd[0].events = POLLIN;
1209 pfd[1].events = POLLIN;
1211 if (poll(pfd, 2, -1) < 0) {
1212 fprintf(stderr, "poll error\n");
1219 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1225 FD_SET(dev->fd, &fds);
1226 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1229 fprintf(stderr, "select timed out or error (ret %d)\n",
1232 } else if (FD_ISSET(0, &fds)) {
1237 drmHandleEvent(dev->fd, &evctx);
1240 kms_bo_destroy(&other_bo);
1243 #define min(a, b) ((a) < (b) ? (a) : (b))
1245 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1253 pipe->crtc_id = (uint32_t)-1;
1254 strcpy(pipe->format_str, "XR24");
1256 /* Count the number of connectors and allocate them. */
1258 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1263 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1264 if (pipe->con_ids == NULL)
1267 /* Parse the connectors. */
1268 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1269 pipe->con_ids[i] = strtoul(p, &endp, 10);
1274 if (i != pipe->num_cons - 1)
1277 /* Parse the remaining parameters. */
1280 pipe->crtc_id = strtoul(arg, &endp, 10);
1287 /* Search for the vertical refresh or the format. */
1288 p = strpbrk(arg, "-@");
1290 p = arg + strlen(arg);
1291 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1292 strncpy(pipe->mode_str, arg, len);
1293 pipe->mode_str[len] = '\0';
1296 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1301 strncpy(pipe->format_str, p + 1, 4);
1302 pipe->format_str[4] = '\0';
1305 pipe->fourcc = format_fourcc(pipe->format_str);
1306 if (pipe->fourcc == 0) {
1307 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1314 static int parse_plane(struct plane_arg *plane, const char *p)
1318 memset(plane, 0, sizeof *plane);
1320 plane->crtc_id = strtoul(p, &end, 10);
1325 plane->w = strtoul(p, &end, 10);
1330 plane->h = strtoul(p, &end, 10);
1332 if (*end == '+' || *end == '-') {
1333 plane->x = strtol(end, &end, 10);
1334 if (*end != '+' && *end != '-')
1336 plane->y = strtol(end, &end, 10);
1338 plane->has_position = true;
1343 plane->scale = strtod(p, &end);
1344 if (plane->scale <= 0.0)
1355 strcpy(plane->format_str, p);
1357 strcpy(plane->format_str, "XR24");
1360 plane->fourcc = format_fourcc(plane->format_str);
1361 if (plane->fourcc == 0) {
1362 fprintf(stderr, "unknown format %s\n", plane->format_str);
1369 static int parse_property(struct property_arg *p, const char *arg)
1371 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1375 p->name[DRM_PROP_NAME_LEN] = '\0';
1380 static void usage(char *name)
1382 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1384 fprintf(stderr, "\n Query options:\n\n");
1385 fprintf(stderr, "\t-c\tlist connectors\n");
1386 fprintf(stderr, "\t-e\tlist encoders\n");
1387 fprintf(stderr, "\t-f\tlist framebuffers\n");
1388 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1390 fprintf(stderr, "\n Test options:\n\n");
1391 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1392 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1393 fprintf(stderr, "\t-C\ttest hw cursor\n");
1394 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1395 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1397 fprintf(stderr, "\n Generic options:\n\n");
1398 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1399 fprintf(stderr, "\t-M module\tuse the given driver\n");
1400 fprintf(stderr, "\t-D device\tuse the given device\n");
1402 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1406 static int page_flipping_supported(void)
1408 /*FIXME: generic ioctl needed? */
1412 struct drm_i915_getparam gp;
1414 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1417 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1419 fprintf(stderr, "drm_i915_getparam: %m\n");
1427 static int cursor_supported(void)
1429 /*FIXME: generic ioctl needed? */
1433 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1435 int main(int argc, char **argv)
1440 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1441 int drop_master = 0;
1443 int test_cursor = 0;
1444 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm", "sti" };
1445 char *device = NULL;
1446 char *module = NULL;
1448 int count = 0, plane_count = 0;
1449 unsigned int prop_count = 0;
1450 struct pipe_arg *pipe_args = NULL;
1451 struct plane_arg *plane_args = NULL;
1452 struct property_arg *prop_args = NULL;
1453 unsigned int args = 0;
1456 memset(&dev, 0, sizeof dev);
1459 while ((c = getopt(argc, argv, optstr)) != -1) {
1481 /* Preserve the default behaviour of dumping all information. */
1485 plane_args = realloc(plane_args,
1486 (plane_count + 1) * sizeof *plane_args);
1487 if (plane_args == NULL) {
1488 fprintf(stderr, "memory allocation failed\n");
1492 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1502 pipe_args = realloc(pipe_args,
1503 (count + 1) * sizeof *pipe_args);
1504 if (pipe_args == NULL) {
1505 fprintf(stderr, "memory allocation failed\n");
1509 if (parse_connector(&pipe_args[count], optarg) < 0)
1521 prop_args = realloc(prop_args,
1522 (prop_count + 1) * sizeof *prop_args);
1523 if (prop_args == NULL) {
1524 fprintf(stderr, "memory allocation failed\n");
1528 if (parse_property(&prop_args[prop_count], optarg) < 0)
1540 encoders = connectors = crtcs = planes = framebuffers = 1;
1543 dev.fd = drmOpen(module, device);
1545 fprintf(stderr, "failed to open device '%s'.\n", module);
1549 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1550 printf("trying to open device '%s'...", modules[i]);
1551 dev.fd = drmOpen(modules[i], device);
1553 printf("failed.\n");
1555 printf("success.\n");
1561 fprintf(stderr, "no device found.\n");
1566 if (test_vsync && !page_flipping_supported()) {
1567 fprintf(stderr, "page flipping not supported by drm.\n");
1571 if (test_vsync && !count) {
1572 fprintf(stderr, "page flipping requires at least one -s option.\n");
1576 if (test_cursor && !cursor_supported()) {
1577 fprintf(stderr, "hw cursor not supported by drm.\n");
1581 dev.resources = get_resources(&dev);
1582 if (!dev.resources) {
1587 #define dump_resource(dev, res) if (res) dump_##res(dev)
1589 dump_resource(&dev, encoders);
1590 dump_resource(&dev, connectors);
1591 dump_resource(&dev, crtcs);
1592 dump_resource(&dev, planes);
1593 dump_resource(&dev, framebuffers);
1595 for (i = 0; i < prop_count; ++i)
1596 set_property(&dev, &prop_args[i]);
1598 if (count || plane_count) {
1599 ret = kms_create(dev.fd, &dev.kms);
1601 fprintf(stderr, "failed to create kms driver: %s\n",
1607 set_mode(&dev, pipe_args, count);
1610 set_planes(&dev, plane_args, plane_count);
1613 set_cursors(&dev, pipe_args, count);
1616 test_page_flip(&dev, pipe_args, count);
1619 drmDropMaster(dev.fd);
1624 clear_cursors(&dev);
1626 kms_bo_destroy(&dev.mode.bo);
1627 kms_destroy(&dev.kms);
1630 free_resources(dev.resources);