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"
65 drmModeObjectProperties *props;
66 drmModePropertyRes **props_info;
67 drmModeModeInfo *mode;
71 drmModeEncoder *encoder;
75 drmModeConnector *connector;
76 drmModeObjectProperties *props;
77 drmModePropertyRes **props_info;
86 drmModeObjectProperties *props;
87 drmModePropertyRes **props_info;
92 drmModePlaneRes *plane_res;
95 struct encoder *encoders;
96 struct connector *connectors;
104 struct resources *resources;
105 struct kms_driver *kms;
116 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
123 #define type_name_fn(res) \
124 const char * res##_str(int type) { \
126 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
127 if (res##_names[i].type == type) \
128 return res##_names[i].name; \
130 return "(invalid)"; \
133 struct type_name encoder_type_names[] = {
134 { DRM_MODE_ENCODER_NONE, "none" },
135 { DRM_MODE_ENCODER_DAC, "DAC" },
136 { DRM_MODE_ENCODER_TMDS, "TMDS" },
137 { DRM_MODE_ENCODER_LVDS, "LVDS" },
138 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
141 static type_name_fn(encoder_type)
143 struct type_name connector_status_names[] = {
144 { DRM_MODE_CONNECTED, "connected" },
145 { DRM_MODE_DISCONNECTED, "disconnected" },
146 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
149 static type_name_fn(connector_status)
151 struct type_name connector_type_names[] = {
152 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
153 { DRM_MODE_CONNECTOR_VGA, "VGA" },
154 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
155 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
156 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
157 { DRM_MODE_CONNECTOR_Composite, "composite" },
158 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
159 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
160 { DRM_MODE_CONNECTOR_Component, "component" },
161 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
162 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
163 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
164 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
165 { DRM_MODE_CONNECTOR_TV, "TV" },
166 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 static type_name_fn(connector_type)
171 #define bit_name_fn(res) \
172 const char * res##_str(int type) { \
174 const char *sep = ""; \
175 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
176 if (type & (1 << i)) { \
177 printf("%s%s", sep, res##_names[i]); \
184 static const char *mode_type_names[] = {
194 static bit_name_fn(mode_type)
196 static const char *mode_flag_names[] = {
213 static bit_name_fn(mode_flag)
215 static void dump_encoders(struct device *dev)
217 drmModeEncoder *encoder;
220 printf("Encoders:\n");
221 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
222 for (i = 0; i < dev->resources->res->count_encoders; i++) {
223 encoder = dev->resources->encoders[i].encoder;
227 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
230 encoder_type_str(encoder->encoder_type),
231 encoder->possible_crtcs,
232 encoder->possible_clones);
237 static void dump_mode(drmModeModeInfo *mode)
239 printf(" %s %d %d %d %d %d %d %d %d %d",
252 mode_flag_str(mode->flags);
254 mode_type_str(mode->type);
258 static void dump_blob(struct device *dev, uint32_t blob_id)
261 unsigned char *blob_data;
262 drmModePropertyBlobPtr blob;
264 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
268 blob_data = blob->data;
270 for (i = 0; i < blob->length; i++) {
273 printf("%.2hhx", blob_data[i]);
277 drmModeFreePropertyBlob(blob);
280 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
281 uint32_t prop_id, uint64_t value)
284 printf("\t%d", prop_id);
290 printf(" %s:\n", prop->name);
292 printf("\t\tflags:");
293 if (prop->flags & DRM_MODE_PROP_PENDING)
295 if (prop->flags & DRM_MODE_PROP_RANGE)
297 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
298 printf(" immutable");
299 if (prop->flags & DRM_MODE_PROP_ENUM)
301 if (prop->flags & DRM_MODE_PROP_BITMASK)
303 if (prop->flags & DRM_MODE_PROP_BLOB)
307 if (prop->flags & DRM_MODE_PROP_RANGE) {
308 printf("\t\tvalues:");
309 for (i = 0; i < prop->count_values; i++)
310 printf(" %"PRIu64, prop->values[i]);
314 if (prop->flags & DRM_MODE_PROP_ENUM) {
315 printf("\t\tenums:");
316 for (i = 0; i < prop->count_enums; i++)
317 printf(" %s=%llu", prop->enums[i].name,
318 prop->enums[i].value);
320 } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
321 printf("\t\tvalues:");
322 for (i = 0; i < prop->count_enums; i++)
323 printf(" %s=0x%llx", prop->enums[i].name,
324 (1LL << prop->enums[i].value));
327 assert(prop->count_enums == 0);
330 if (prop->flags & DRM_MODE_PROP_BLOB) {
331 printf("\t\tblobs:\n");
332 for (i = 0; i < prop->count_blobs; i++)
333 dump_blob(dev, prop->blob_ids[i]);
336 assert(prop->count_blobs == 0);
339 printf("\t\tvalue:");
340 if (prop->flags & DRM_MODE_PROP_BLOB)
341 dump_blob(dev, value);
343 printf(" %"PRIu64"\n", value);
346 static void dump_connectors(struct device *dev)
350 printf("Connectors:\n");
351 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
352 for (i = 0; i < dev->resources->res->count_connectors; i++) {
353 struct connector *_connector = &dev->resources->connectors[i];
354 drmModeConnector *connector = _connector->connector;
358 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
359 connector->connector_id,
360 connector->encoder_id,
361 connector_status_str(connector->connection),
362 connector_type_str(connector->connector_type),
363 connector->mmWidth, connector->mmHeight,
364 connector->count_modes);
366 for (j = 0; j < connector->count_encoders; j++)
367 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
370 if (connector->count_modes) {
372 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
374 for (j = 0; j < connector->count_modes; j++)
375 dump_mode(&connector->modes[j]);
378 if (_connector->props) {
380 for (j = 0; j < (int)_connector->props->count_props; j++)
381 dump_prop(dev, _connector->props_info[j],
382 _connector->props->props[j],
383 _connector->props->prop_values[j]);
389 static void dump_crtcs(struct device *dev)
395 printf("id\tfb\tpos\tsize\n");
396 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
397 struct crtc *_crtc = &dev->resources->crtcs[i];
398 drmModeCrtc *crtc = _crtc->crtc;
402 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
406 crtc->width, crtc->height);
407 dump_mode(&crtc->mode);
411 for (j = 0; j < _crtc->props->count_props; j++)
412 dump_prop(dev, _crtc->props_info[j],
413 _crtc->props->props[j],
414 _crtc->props->prop_values[j]);
416 printf(" no properties found\n");
422 static void dump_framebuffers(struct device *dev)
427 printf("Frame buffers:\n");
428 printf("id\tsize\tpitch\n");
429 for (i = 0; i < dev->resources->res->count_fbs; i++) {
430 fb = dev->resources->fbs[i].fb;
434 printf("%u\t(%ux%u)\t%u\n",
436 fb->width, fb->height,
442 static void dump_planes(struct device *dev)
447 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
449 if (!dev->resources->plane_res)
452 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
453 struct plane *plane = &dev->resources->planes[i];
454 drmModePlane *ovr = plane->plane;
458 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
459 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
460 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
461 ovr->gamma_size, ovr->possible_crtcs);
463 if (!ovr->count_formats)
467 for (j = 0; j < ovr->count_formats; j++)
468 printf(" %4.4s", (char *)&ovr->formats[j]);
473 for (j = 0; j < plane->props->count_props; j++)
474 dump_prop(dev, plane->props_info[j],
475 plane->props->props[j],
476 plane->props->prop_values[j]);
478 printf(" no properties found\n");
486 static void free_resources(struct resources *res)
491 #define free_resource(_res, __res, type, Type) \
494 if (!(_res)->type##s) \
496 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
497 if (!(_res)->type##s[i].type) \
499 drmModeFree##Type((_res)->type##s[i].type); \
501 free((_res)->type##s); \
504 #define free_properties(_res, __res, type) \
507 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
508 drmModeFreeObjectProperties(res->type##s[i].props); \
509 free(res->type##s[i].props_info); \
514 free_properties(res, res, crtc);
516 free_resource(res, res, crtc, Crtc);
517 free_resource(res, res, encoder, Encoder);
518 free_resource(res, res, connector, Connector);
519 free_resource(res, res, fb, FB);
521 drmModeFreeResources(res->res);
524 if (res->plane_res) {
525 free_properties(res, plane_res, plane);
527 free_resource(res, plane_res, plane, Plane);
529 drmModeFreePlaneResources(res->plane_res);
535 static struct resources *get_resources(struct device *dev)
537 struct resources *res;
540 res = malloc(sizeof *res);
544 memset(res, 0, sizeof *res);
546 res->res = drmModeGetResources(dev->fd);
548 fprintf(stderr, "drmModeGetResources failed: %s\n",
553 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
554 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
555 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
556 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
558 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
561 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
562 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
563 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
564 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
566 #define get_resource(_res, __res, type, Type) \
569 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
570 (_res)->type##s[i].type = \
571 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
572 if (!(_res)->type##s[i].type) \
573 fprintf(stderr, "could not get %s %i: %s\n", \
574 #type, (_res)->__res->type##s[i], \
579 get_resource(res, res, crtc, Crtc);
580 get_resource(res, res, encoder, Encoder);
581 get_resource(res, res, connector, Connector);
582 get_resource(res, res, fb, FB);
584 #define get_properties(_res, __res, type, Type) \
587 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
588 struct type *obj = &res->type##s[i]; \
591 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
592 DRM_MODE_OBJECT_##Type); \
595 "could not get %s %i properties: %s\n", \
596 #type, obj->type->type##_id, \
600 obj->props_info = malloc(obj->props->count_props * \
601 sizeof *obj->props_info); \
602 if (!obj->props_info) \
604 for (j = 0; j < obj->props->count_props; ++j) \
605 obj->props_info[j] = \
606 drmModeGetProperty(dev->fd, obj->props->props[j]); \
610 get_properties(res, res, crtc, CRTC);
611 get_properties(res, res, connector, CONNECTOR);
613 for (i = 0; i < res->res->count_crtcs; ++i)
614 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
616 res->plane_res = drmModeGetPlaneResources(dev->fd);
617 if (!res->plane_res) {
618 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
623 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
627 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
629 get_resource(res, plane_res, plane, Plane);
630 get_properties(res, plane_res, plane, PLANE);
639 static int get_crtc_index(struct device *dev, uint32_t id)
643 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
644 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
645 if (crtc && crtc->crtc_id == id)
652 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
654 drmModeConnector *connector;
657 for (i = 0; i < dev->resources->res->count_connectors; i++) {
658 connector = dev->resources->connectors[i].connector;
659 if (connector && connector->connector_id == id)
666 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
668 drmModeEncoder *encoder;
671 for (i = 0; i < dev->resources->res->count_encoders; i++) {
672 encoder = dev->resources->encoders[i].encoder;
673 if (encoder && encoder->encoder_id == id)
680 /* -----------------------------------------------------------------------------
685 * Mode setting with the kernel interfaces is a bit of a chore.
686 * First you have to find the connector in question and make sure the
687 * requested mode is available.
688 * Then you need to find the encoder attached to that connector so you
689 * can bind it with a free crtc.
693 unsigned int num_cons;
697 unsigned int vrefresh;
699 drmModeModeInfo *mode;
701 unsigned int fb_id[2], current_fb_id;
702 struct timeval start;
708 uint32_t crtc_id; /* the id of CRTC to bind to */
714 char format_str[5]; /* need to leave room for terminating \0 */
718 static drmModeModeInfo *
719 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
720 const unsigned int vrefresh)
722 drmModeConnector *connector;
723 drmModeModeInfo *mode;
726 connector = get_connector_by_id(dev, con_id);
727 if (!connector || !connector->count_modes)
730 for (i = 0; i < connector->count_modes; i++) {
731 mode = &connector->modes[i];
732 if (!strcmp(mode->name, mode_str)) {
733 /* If the vertical refresh frequency is not specified then return the
734 * first mode that match with the name. Else, return the mode that match
735 * the name and the specified vertical refresh frequency.
739 else if (mode->vrefresh == vrefresh)
747 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
749 uint32_t possible_crtcs = ~0;
750 uint32_t active_crtcs = 0;
751 unsigned int crtc_idx;
755 for (i = 0; i < pipe->num_cons; ++i) {
756 uint32_t crtcs_for_connector = 0;
757 drmModeConnector *connector;
758 drmModeEncoder *encoder;
761 connector = get_connector_by_id(dev, pipe->con_ids[i]);
765 for (j = 0; j < connector->count_encoders; ++j) {
766 encoder = get_encoder_by_id(dev, connector->encoders[j]);
770 crtcs_for_connector |= encoder->possible_crtcs;
772 idx = get_crtc_index(dev, encoder->crtc_id);
774 active_crtcs |= 1 << idx;
777 possible_crtcs &= crtcs_for_connector;
783 /* Return the first possible and active CRTC if one exists, or the first
784 * possible CRTC otherwise.
786 if (possible_crtcs & active_crtcs)
787 crtc_idx = ffs(possible_crtcs & active_crtcs);
789 crtc_idx = ffs(possible_crtcs);
791 return &dev->resources->crtcs[crtc_idx - 1];
794 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
796 drmModeModeInfo *mode = NULL;
801 for (i = 0; i < (int)pipe->num_cons; i++) {
802 mode = connector_find_mode(dev, pipe->con_ids[i],
803 pipe->mode_str, pipe->vrefresh);
806 "failed to find mode \"%s\" for connector %u\n",
807 pipe->mode_str, pipe->con_ids[i]);
812 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
813 * locate a CRTC that can be attached to all the connectors.
815 if (pipe->crtc_id != (uint32_t)-1) {
816 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
817 struct crtc *crtc = &dev->resources->crtcs[i];
819 if (pipe->crtc_id == crtc->crtc->crtc_id) {
825 pipe->crtc = pipe_find_crtc(dev, pipe);
829 fprintf(stderr, "failed to find CRTC for pipe\n");
834 pipe->crtc->mode = mode;
839 /* -----------------------------------------------------------------------------
843 struct property_arg {
846 char name[DRM_PROP_NAME_LEN+1];
851 static void set_property(struct device *dev, struct property_arg *p)
853 drmModeObjectProperties *props = NULL;
854 drmModePropertyRes **props_info = NULL;
855 const char *obj_type;
862 #define find_object(_res, __res, type, Type) \
864 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
865 struct type *obj = &(_res)->type##s[i]; \
866 if (obj->type->type##_id != p->obj_id) \
868 p->obj_type = DRM_MODE_OBJECT_##Type; \
870 props = obj->props; \
871 props_info = obj->props_info; \
875 find_object(dev->resources, res, crtc, CRTC);
876 if (p->obj_type == 0)
877 find_object(dev->resources, res, connector, CONNECTOR);
878 if (p->obj_type == 0)
879 find_object(dev->resources, plane_res, plane, PLANE);
880 if (p->obj_type == 0) {
881 fprintf(stderr, "Object %i not found, can't set property\n",
887 fprintf(stderr, "%s %i has no properties\n",
888 obj_type, p->obj_id);
892 for (i = 0; i < (int)props->count_props; ++i) {
895 if (strcmp(props_info[i]->name, p->name) == 0)
899 if (i == (int)props->count_props) {
900 fprintf(stderr, "%s %i has no %s property\n",
901 obj_type, p->obj_id, p->name);
905 p->prop_id = props->props[i];
907 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
908 p->prop_id, p->value);
910 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
911 obj_type, p->obj_id, p->name, p->value, strerror(errno));
914 /* -------------------------------------------------------------------------- */
917 page_flip_handler(int fd, unsigned int frame,
918 unsigned int sec, unsigned int usec, void *data)
920 struct pipe_arg *pipe;
921 unsigned int new_fb_id;
926 if (pipe->current_fb_id == pipe->fb_id[0])
927 new_fb_id = pipe->fb_id[1];
929 new_fb_id = pipe->fb_id[0];
931 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
932 DRM_MODE_PAGE_FLIP_EVENT, pipe);
933 pipe->current_fb_id = new_fb_id;
935 if (pipe->swap_count == 60) {
936 gettimeofday(&end, NULL);
937 t = end.tv_sec + end.tv_usec * 1e-6 -
938 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
939 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
940 pipe->swap_count = 0;
945 static int set_plane(struct device *dev, struct plane_arg *p)
948 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
949 uint32_t plane_id = 0;
950 struct kms_bo *plane_bo;
951 uint32_t plane_flags = 0;
952 int crtc_x, crtc_y, crtc_w, crtc_h;
953 struct crtc *crtc = NULL;
957 /* Find an unused plane which can be connected to our CRTC. Find the
958 * CRTC index first, then iterate over available planes.
960 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
961 if (p->crtc_id == dev->resources->res->crtcs[i]) {
962 crtc = &dev->resources->crtcs[i];
969 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
973 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
974 ovr = dev->resources->planes[i].plane;
978 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
979 plane_id = ovr->plane_id;
983 fprintf(stderr, "no unused plane available for CRTC %u\n",
984 crtc->crtc->crtc_id);
988 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
989 p->w, p->h, p->format_str, plane_id);
991 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
992 pitches, offsets, PATTERN_TILES);
993 if (plane_bo == NULL)
996 /* just use single plane format for now.. */
997 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
998 handles, pitches, offsets, &p->fb_id, plane_flags)) {
999 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1003 crtc_w = p->w * p->scale;
1004 crtc_h = p->h * p->scale;
1005 if (!p->has_position) {
1006 /* Default to the middle of the screen */
1007 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1008 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1014 /* note src coords (last 4 args) are in Q16 format */
1015 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1016 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1017 0, 0, p->w << 16, p->h << 16)) {
1018 fprintf(stderr, "failed to enable plane: %s\n",
1023 ovr->crtc_id = crtc->crtc->crtc_id;
1028 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1030 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1037 dev->mode.width = 0;
1038 dev->mode.height = 0;
1040 for (i = 0; i < count; i++) {
1041 struct pipe_arg *pipe = &pipes[i];
1043 ret = pipe_find_crtc_and_mode(dev, pipe);
1047 dev->mode.width += pipe->mode->hdisplay;
1048 if (dev->mode.height < pipe->mode->vdisplay)
1049 dev->mode.height = pipe->mode->vdisplay;
1052 bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1053 dev->mode.width, dev->mode.height,
1054 handles, pitches, offsets, PATTERN_SMPTE);
1058 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1059 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1061 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1062 dev->mode.width, dev->mode.height, strerror(errno));
1067 for (i = 0; i < count; i++) {
1068 struct pipe_arg *pipe = &pipes[i];
1070 if (pipe->mode == NULL)
1073 printf("setting mode %s-%dHz@%s on connectors ",
1074 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1075 for (j = 0; j < pipe->num_cons; ++j)
1076 printf("%u, ", pipe->con_ids[j]);
1077 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1079 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1080 x, 0, pipe->con_ids, pipe->num_cons,
1083 /* XXX: Actually check if this is needed */
1084 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1086 x += pipe->mode->hdisplay;
1089 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1095 dev->mode.fb_id = fb_id;
1098 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1102 /* set up planes/overlays */
1103 for (i = 0; i < count; i++)
1104 if (set_plane(dev, &p[i]))
1108 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1110 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1115 /* maybe make cursor width/height configurable some day */
1119 /* create cursor bo.. just using PATTERN_PLAIN as it has
1122 bo = create_test_buffer(dev->kms, DRM_FORMAT_ARGB8888,
1123 cw, ch, handles, pitches, offsets, PATTERN_PLAIN);
1127 for (i = 0; i < count; i++) {
1128 struct pipe_arg *pipe = &pipes[i];
1129 ret = cursor_init(dev->fd, handles[0],
1130 pipe->crtc->crtc->crtc_id,
1131 pipe->mode->hdisplay, pipe->mode->vdisplay,
1134 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1143 static void clear_cursors(struct device *dev)
1148 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1150 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1151 unsigned int other_fb_id;
1152 struct kms_bo *other_bo;
1153 drmEventContext evctx;
1157 other_bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1158 dev->mode.width, dev->mode.height,
1159 handles, pitches, offsets, PATTERN_PLAIN);
1160 if (other_bo == NULL)
1163 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1164 pipes[0].fourcc, handles, pitches, offsets,
1167 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1171 for (i = 0; i < count; i++) {
1172 struct pipe_arg *pipe = &pipes[i];
1174 if (pipe->mode == NULL)
1177 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1178 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1181 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1184 gettimeofday(&pipe->start, NULL);
1185 pipe->swap_count = 0;
1186 pipe->fb_id[0] = dev->mode.fb_id;
1187 pipe->fb_id[1] = other_fb_id;
1188 pipe->current_fb_id = other_fb_id;
1191 memset(&evctx, 0, sizeof evctx);
1192 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1193 evctx.vblank_handler = NULL;
1194 evctx.page_flip_handler = page_flip_handler;
1198 struct pollfd pfd[2];
1201 pfd[0].events = POLLIN;
1203 pfd[1].events = POLLIN;
1205 if (poll(pfd, 2, -1) < 0) {
1206 fprintf(stderr, "poll error\n");
1213 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1219 FD_SET(dev->fd, &fds);
1220 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1223 fprintf(stderr, "select timed out or error (ret %d)\n",
1226 } else if (FD_ISSET(0, &fds)) {
1231 drmHandleEvent(dev->fd, &evctx);
1234 kms_bo_destroy(&other_bo);
1237 #define min(a, b) ((a) < (b) ? (a) : (b))
1239 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1247 pipe->crtc_id = (uint32_t)-1;
1248 strcpy(pipe->format_str, "XR24");
1250 /* Count the number of connectors and allocate them. */
1252 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1257 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1258 if (pipe->con_ids == NULL)
1261 /* Parse the connectors. */
1262 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1263 pipe->con_ids[i] = strtoul(p, &endp, 10);
1268 if (i != pipe->num_cons - 1)
1271 /* Parse the remaining parameters. */
1274 pipe->crtc_id = strtoul(arg, &endp, 10);
1281 /* Search for the vertical refresh or the format. */
1282 p = strpbrk(arg, "-@");
1284 p = arg + strlen(arg);
1285 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1286 strncpy(pipe->mode_str, arg, len);
1287 pipe->mode_str[len] = '\0';
1290 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1295 strncpy(pipe->format_str, p + 1, 4);
1296 pipe->format_str[4] = '\0';
1299 pipe->fourcc = format_fourcc(pipe->format_str);
1300 if (pipe->fourcc == 0) {
1301 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1308 static int parse_plane(struct plane_arg *plane, const char *p)
1312 memset(plane, 0, sizeof *plane);
1314 plane->crtc_id = strtoul(p, &end, 10);
1319 plane->w = strtoul(p, &end, 10);
1324 plane->h = strtoul(p, &end, 10);
1326 if (*end == '+' || *end == '-') {
1327 plane->x = strtol(end, &end, 10);
1328 if (*end != '+' && *end != '-')
1330 plane->y = strtol(end, &end, 10);
1332 plane->has_position = true;
1337 plane->scale = strtod(p, &end);
1338 if (plane->scale <= 0.0)
1349 strcpy(plane->format_str, p);
1351 strcpy(plane->format_str, "XR24");
1354 plane->fourcc = format_fourcc(plane->format_str);
1355 if (plane->fourcc == 0) {
1356 fprintf(stderr, "unknown format %s\n", plane->format_str);
1363 static int parse_property(struct property_arg *p, const char *arg)
1365 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1369 p->name[DRM_PROP_NAME_LEN] = '\0';
1374 static void usage(char *name)
1376 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1378 fprintf(stderr, "\n Query options:\n\n");
1379 fprintf(stderr, "\t-c\tlist connectors\n");
1380 fprintf(stderr, "\t-e\tlist encoders\n");
1381 fprintf(stderr, "\t-f\tlist framebuffers\n");
1382 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1384 fprintf(stderr, "\n Test options:\n\n");
1385 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1386 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1387 fprintf(stderr, "\t-C\ttest hw cursor\n");
1388 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1389 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1391 fprintf(stderr, "\n Generic options:\n\n");
1392 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1393 fprintf(stderr, "\t-M module\tuse the given driver\n");
1394 fprintf(stderr, "\t-D device\tuse the given device\n");
1396 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1400 static int page_flipping_supported(void)
1402 /*FIXME: generic ioctl needed? */
1406 struct drm_i915_getparam gp;
1408 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1411 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1413 fprintf(stderr, "drm_i915_getparam: %m\n");
1421 static int cursor_supported(void)
1423 /*FIXME: generic ioctl needed? */
1427 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1429 int main(int argc, char **argv)
1434 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1435 int drop_master = 0;
1437 int test_cursor = 0;
1438 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm" };
1439 char *device = NULL;
1440 char *module = NULL;
1442 int count = 0, plane_count = 0;
1443 unsigned int prop_count = 0;
1444 struct pipe_arg *pipe_args = NULL;
1445 struct plane_arg *plane_args = NULL;
1446 struct property_arg *prop_args = NULL;
1447 unsigned int args = 0;
1450 memset(&dev, 0, sizeof dev);
1453 while ((c = getopt(argc, argv, optstr)) != -1) {
1475 /* Preserve the default behaviour of dumping all information. */
1479 plane_args = realloc(plane_args,
1480 (plane_count + 1) * sizeof *plane_args);
1481 if (plane_args == NULL) {
1482 fprintf(stderr, "memory allocation failed\n");
1486 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1496 pipe_args = realloc(pipe_args,
1497 (count + 1) * sizeof *pipe_args);
1498 if (pipe_args == NULL) {
1499 fprintf(stderr, "memory allocation failed\n");
1503 if (parse_connector(&pipe_args[count], optarg) < 0)
1515 prop_args = realloc(prop_args,
1516 (prop_count + 1) * sizeof *prop_args);
1517 if (prop_args == NULL) {
1518 fprintf(stderr, "memory allocation failed\n");
1522 if (parse_property(&prop_args[prop_count], optarg) < 0)
1534 encoders = connectors = crtcs = planes = framebuffers = 1;
1537 dev.fd = drmOpen(module, device);
1539 fprintf(stderr, "failed to open device '%s'.\n", module);
1543 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1544 printf("trying to open device '%s'...", modules[i]);
1545 dev.fd = drmOpen(modules[i], device);
1547 printf("failed.\n");
1549 printf("success.\n");
1555 fprintf(stderr, "no device found.\n");
1560 if (test_vsync && !page_flipping_supported()) {
1561 fprintf(stderr, "page flipping not supported by drm.\n");
1565 if (test_vsync && !count) {
1566 fprintf(stderr, "page flipping requires at least one -s option.\n");
1570 if (test_cursor && !cursor_supported()) {
1571 fprintf(stderr, "hw cursor not supported by drm.\n");
1575 dev.resources = get_resources(&dev);
1576 if (!dev.resources) {
1581 #define dump_resource(dev, res) if (res) dump_##res(dev)
1583 dump_resource(&dev, encoders);
1584 dump_resource(&dev, connectors);
1585 dump_resource(&dev, crtcs);
1586 dump_resource(&dev, planes);
1587 dump_resource(&dev, framebuffers);
1589 for (i = 0; i < prop_count; ++i)
1590 set_property(&dev, &prop_args[i]);
1592 if (count || plane_count) {
1593 ret = kms_create(dev.fd, &dev.kms);
1595 fprintf(stderr, "failed to create kms driver: %s\n",
1601 set_mode(&dev, pipe_args, count);
1604 set_planes(&dev, plane_args, plane_count);
1607 set_cursors(&dev, pipe_args, count);
1610 test_page_flip(&dev, pipe_args, count);
1613 drmDropMaster(dev.fd);
1618 clear_cursors(&dev);
1620 kms_bo_destroy(&dev.mode.bo);
1621 kms_destroy(&dev.kms);
1624 free_resources(dev.resources);