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.
59 #include "xf86drmMode.h"
60 #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;
114 struct bo *cursor_bo;
118 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
119 static inline int64_t U642I64(uint64_t val)
121 return (int64_t)*((int64_t *)&val);
129 #define type_name_fn(res) \
130 const char * res##_str(int type) { \
132 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
133 if (res##_names[i].type == type) \
134 return res##_names[i].name; \
136 return "(invalid)"; \
139 struct type_name encoder_type_names[] = {
140 { DRM_MODE_ENCODER_NONE, "none" },
141 { DRM_MODE_ENCODER_DAC, "DAC" },
142 { DRM_MODE_ENCODER_TMDS, "TMDS" },
143 { DRM_MODE_ENCODER_LVDS, "LVDS" },
144 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
145 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
146 { DRM_MODE_ENCODER_DSI, "DSI" },
147 { DRM_MODE_ENCODER_DPMST, "DPMST" },
150 static type_name_fn(encoder_type)
152 struct type_name connector_status_names[] = {
153 { DRM_MODE_CONNECTED, "connected" },
154 { DRM_MODE_DISCONNECTED, "disconnected" },
155 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
158 static type_name_fn(connector_status)
160 struct type_name connector_type_names[] = {
161 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
162 { DRM_MODE_CONNECTOR_VGA, "VGA" },
163 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
164 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
165 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
166 { DRM_MODE_CONNECTOR_Composite, "composite" },
167 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
168 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
169 { DRM_MODE_CONNECTOR_Component, "component" },
170 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
171 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
172 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
173 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
174 { DRM_MODE_CONNECTOR_TV, "TV" },
175 { DRM_MODE_CONNECTOR_eDP, "eDP" },
176 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
177 { DRM_MODE_CONNECTOR_DSI, "DSI" },
180 static type_name_fn(connector_type)
182 #define bit_name_fn(res) \
183 const char * res##_str(int type) { \
185 const char *sep = ""; \
186 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
187 if (type & (1 << i)) { \
188 printf("%s%s", sep, res##_names[i]); \
195 static const char *mode_type_names[] = {
205 static bit_name_fn(mode_type)
207 static const char *mode_flag_names[] = {
224 static bit_name_fn(mode_flag)
226 static void dump_encoders(struct device *dev)
228 drmModeEncoder *encoder;
231 printf("Encoders:\n");
232 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
233 for (i = 0; i < dev->resources->res->count_encoders; i++) {
234 encoder = dev->resources->encoders[i].encoder;
238 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
241 encoder_type_str(encoder->encoder_type),
242 encoder->possible_crtcs,
243 encoder->possible_clones);
248 static void dump_mode(drmModeModeInfo *mode)
250 printf(" %s %d %d %d %d %d %d %d %d %d",
263 mode_flag_str(mode->flags);
265 mode_type_str(mode->type);
269 static void dump_blob(struct device *dev, uint32_t blob_id)
272 unsigned char *blob_data;
273 drmModePropertyBlobPtr blob;
275 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
281 blob_data = blob->data;
283 for (i = 0; i < blob->length; i++) {
286 printf("%.2hhx", blob_data[i]);
290 drmModeFreePropertyBlob(blob);
293 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
294 uint32_t prop_id, uint64_t value)
297 printf("\t%d", prop_id);
303 printf(" %s:\n", prop->name);
305 printf("\t\tflags:");
306 if (prop->flags & DRM_MODE_PROP_PENDING)
308 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
309 printf(" immutable");
310 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
311 printf(" signed range");
312 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
314 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
316 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
318 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
320 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
324 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
325 printf("\t\tvalues:");
326 for (i = 0; i < prop->count_values; i++)
327 printf(" %"PRId64, U642I64(prop->values[i]));
331 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
332 printf("\t\tvalues:");
333 for (i = 0; i < prop->count_values; i++)
334 printf(" %"PRIu64, prop->values[i]);
338 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
339 printf("\t\tenums:");
340 for (i = 0; i < prop->count_enums; i++)
341 printf(" %s=%llu", prop->enums[i].name,
342 prop->enums[i].value);
344 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
345 printf("\t\tvalues:");
346 for (i = 0; i < prop->count_enums; i++)
347 printf(" %s=0x%llx", prop->enums[i].name,
348 (1LL << prop->enums[i].value));
351 assert(prop->count_enums == 0);
354 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
355 printf("\t\tblobs:\n");
356 for (i = 0; i < prop->count_blobs; i++)
357 dump_blob(dev, prop->blob_ids[i]);
360 assert(prop->count_blobs == 0);
363 printf("\t\tvalue:");
364 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
365 dump_blob(dev, value);
367 printf(" %"PRIu64"\n", value);
370 static void dump_connectors(struct device *dev)
374 printf("Connectors:\n");
375 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
376 for (i = 0; i < dev->resources->res->count_connectors; i++) {
377 struct connector *_connector = &dev->resources->connectors[i];
378 drmModeConnector *connector = _connector->connector;
382 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
383 connector->connector_id,
384 connector->encoder_id,
385 connector_status_str(connector->connection),
386 connector_type_str(connector->connector_type),
387 connector->mmWidth, connector->mmHeight,
388 connector->count_modes);
390 for (j = 0; j < connector->count_encoders; j++)
391 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
394 if (connector->count_modes) {
396 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
398 for (j = 0; j < connector->count_modes; j++)
399 dump_mode(&connector->modes[j]);
402 if (_connector->props) {
404 for (j = 0; j < (int)_connector->props->count_props; j++)
405 dump_prop(dev, _connector->props_info[j],
406 _connector->props->props[j],
407 _connector->props->prop_values[j]);
413 static void dump_crtcs(struct device *dev)
419 printf("id\tfb\tpos\tsize\n");
420 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
421 struct crtc *_crtc = &dev->resources->crtcs[i];
422 drmModeCrtc *crtc = _crtc->crtc;
426 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
430 crtc->width, crtc->height);
431 dump_mode(&crtc->mode);
435 for (j = 0; j < _crtc->props->count_props; j++)
436 dump_prop(dev, _crtc->props_info[j],
437 _crtc->props->props[j],
438 _crtc->props->prop_values[j]);
440 printf(" no properties found\n");
446 static void dump_framebuffers(struct device *dev)
451 printf("Frame buffers:\n");
452 printf("id\tsize\tpitch\n");
453 for (i = 0; i < dev->resources->res->count_fbs; i++) {
454 fb = dev->resources->fbs[i].fb;
458 printf("%u\t(%ux%u)\t%u\n",
460 fb->width, fb->height,
466 static void dump_planes(struct device *dev)
471 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
473 if (!dev->resources->plane_res)
476 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
477 struct plane *plane = &dev->resources->planes[i];
478 drmModePlane *ovr = plane->plane;
482 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
483 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
484 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
485 ovr->gamma_size, ovr->possible_crtcs);
487 if (!ovr->count_formats)
491 for (j = 0; j < ovr->count_formats; j++)
492 printf(" %4.4s", (char *)&ovr->formats[j]);
497 for (j = 0; j < plane->props->count_props; j++)
498 dump_prop(dev, plane->props_info[j],
499 plane->props->props[j],
500 plane->props->prop_values[j]);
502 printf(" no properties found\n");
510 static void free_resources(struct resources *res)
515 #define free_resource(_res, __res, type, Type) \
518 if (!(_res)->type##s) \
520 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
521 if (!(_res)->type##s[i].type) \
523 drmModeFree##Type((_res)->type##s[i].type); \
525 free((_res)->type##s); \
528 #define free_properties(_res, __res, type) \
531 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
532 drmModeFreeObjectProperties(res->type##s[i].props); \
533 free(res->type##s[i].props_info); \
538 free_properties(res, res, crtc);
540 free_resource(res, res, crtc, Crtc);
541 free_resource(res, res, encoder, Encoder);
542 free_resource(res, res, connector, Connector);
543 free_resource(res, res, fb, FB);
545 drmModeFreeResources(res->res);
548 if (res->plane_res) {
549 free_properties(res, plane_res, plane);
551 free_resource(res, plane_res, plane, Plane);
553 drmModeFreePlaneResources(res->plane_res);
559 static struct resources *get_resources(struct device *dev)
561 struct resources *res;
564 res = malloc(sizeof *res);
568 memset(res, 0, sizeof *res);
570 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
572 res->res = drmModeGetResources(dev->fd);
574 fprintf(stderr, "drmModeGetResources failed: %s\n",
579 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
580 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
581 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
582 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
584 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
587 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
588 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
589 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
590 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
592 #define get_resource(_res, __res, type, Type) \
595 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
596 (_res)->type##s[i].type = \
597 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
598 if (!(_res)->type##s[i].type) \
599 fprintf(stderr, "could not get %s %i: %s\n", \
600 #type, (_res)->__res->type##s[i], \
605 get_resource(res, res, crtc, Crtc);
606 get_resource(res, res, encoder, Encoder);
607 get_resource(res, res, connector, Connector);
608 get_resource(res, res, fb, FB);
610 #define get_properties(_res, __res, type, Type) \
613 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
614 struct type *obj = &res->type##s[i]; \
617 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
618 DRM_MODE_OBJECT_##Type); \
621 "could not get %s %i properties: %s\n", \
622 #type, obj->type->type##_id, \
626 obj->props_info = malloc(obj->props->count_props * \
627 sizeof *obj->props_info); \
628 if (!obj->props_info) \
630 for (j = 0; j < obj->props->count_props; ++j) \
631 obj->props_info[j] = \
632 drmModeGetProperty(dev->fd, obj->props->props[j]); \
636 get_properties(res, res, crtc, CRTC);
637 get_properties(res, res, connector, CONNECTOR);
639 for (i = 0; i < res->res->count_crtcs; ++i)
640 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
642 res->plane_res = drmModeGetPlaneResources(dev->fd);
643 if (!res->plane_res) {
644 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
649 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
653 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
655 get_resource(res, plane_res, plane, Plane);
656 get_properties(res, plane_res, plane, PLANE);
665 static int get_crtc_index(struct device *dev, uint32_t id)
669 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
670 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
671 if (crtc && crtc->crtc_id == id)
678 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
680 drmModeConnector *connector;
683 for (i = 0; i < dev->resources->res->count_connectors; i++) {
684 connector = dev->resources->connectors[i].connector;
685 if (connector && connector->connector_id == id)
692 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
694 drmModeEncoder *encoder;
697 for (i = 0; i < dev->resources->res->count_encoders; i++) {
698 encoder = dev->resources->encoders[i].encoder;
699 if (encoder && encoder->encoder_id == id)
706 /* -----------------------------------------------------------------------------
711 * Mode setting with the kernel interfaces is a bit of a chore.
712 * First you have to find the connector in question and make sure the
713 * requested mode is available.
714 * Then you need to find the encoder attached to that connector so you
715 * can bind it with a free crtc.
719 unsigned int num_cons;
723 unsigned int vrefresh;
725 drmModeModeInfo *mode;
727 unsigned int fb_id[2], current_fb_id;
728 struct timeval start;
734 uint32_t crtc_id; /* the id of CRTC to bind to */
741 char format_str[5]; /* need to leave room for terminating \0 */
745 static drmModeModeInfo *
746 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
747 const unsigned int vrefresh)
749 drmModeConnector *connector;
750 drmModeModeInfo *mode;
753 connector = get_connector_by_id(dev, con_id);
754 if (!connector || !connector->count_modes)
757 for (i = 0; i < connector->count_modes; i++) {
758 mode = &connector->modes[i];
759 if (!strcmp(mode->name, mode_str)) {
760 /* If the vertical refresh frequency is not specified then return the
761 * first mode that match with the name. Else, return the mode that match
762 * the name and the specified vertical refresh frequency.
766 else if (mode->vrefresh == vrefresh)
774 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
776 uint32_t possible_crtcs = ~0;
777 uint32_t active_crtcs = 0;
778 unsigned int crtc_idx;
782 for (i = 0; i < pipe->num_cons; ++i) {
783 uint32_t crtcs_for_connector = 0;
784 drmModeConnector *connector;
785 drmModeEncoder *encoder;
788 connector = get_connector_by_id(dev, pipe->con_ids[i]);
792 for (j = 0; j < connector->count_encoders; ++j) {
793 encoder = get_encoder_by_id(dev, connector->encoders[j]);
797 crtcs_for_connector |= encoder->possible_crtcs;
799 idx = get_crtc_index(dev, encoder->crtc_id);
801 active_crtcs |= 1 << idx;
804 possible_crtcs &= crtcs_for_connector;
810 /* Return the first possible and active CRTC if one exists, or the first
811 * possible CRTC otherwise.
813 if (possible_crtcs & active_crtcs)
814 crtc_idx = ffs(possible_crtcs & active_crtcs);
816 crtc_idx = ffs(possible_crtcs);
818 return &dev->resources->crtcs[crtc_idx - 1];
821 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
823 drmModeModeInfo *mode = NULL;
828 for (i = 0; i < (int)pipe->num_cons; i++) {
829 mode = connector_find_mode(dev, pipe->con_ids[i],
830 pipe->mode_str, pipe->vrefresh);
833 "failed to find mode \"%s\" for connector %u\n",
834 pipe->mode_str, pipe->con_ids[i]);
839 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
840 * locate a CRTC that can be attached to all the connectors.
842 if (pipe->crtc_id != (uint32_t)-1) {
843 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
844 struct crtc *crtc = &dev->resources->crtcs[i];
846 if (pipe->crtc_id == crtc->crtc->crtc_id) {
852 pipe->crtc = pipe_find_crtc(dev, pipe);
856 fprintf(stderr, "failed to find CRTC for pipe\n");
861 pipe->crtc->mode = mode;
866 /* -----------------------------------------------------------------------------
870 struct property_arg {
873 char name[DRM_PROP_NAME_LEN+1];
878 static void set_property(struct device *dev, struct property_arg *p)
880 drmModeObjectProperties *props = NULL;
881 drmModePropertyRes **props_info = NULL;
882 const char *obj_type;
889 #define find_object(_res, __res, type, Type) \
891 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
892 struct type *obj = &(_res)->type##s[i]; \
893 if (obj->type->type##_id != p->obj_id) \
895 p->obj_type = DRM_MODE_OBJECT_##Type; \
897 props = obj->props; \
898 props_info = obj->props_info; \
902 find_object(dev->resources, res, crtc, CRTC);
903 if (p->obj_type == 0)
904 find_object(dev->resources, res, connector, CONNECTOR);
905 if (p->obj_type == 0)
906 find_object(dev->resources, plane_res, plane, PLANE);
907 if (p->obj_type == 0) {
908 fprintf(stderr, "Object %i not found, can't set property\n",
914 fprintf(stderr, "%s %i has no properties\n",
915 obj_type, p->obj_id);
919 for (i = 0; i < (int)props->count_props; ++i) {
922 if (strcmp(props_info[i]->name, p->name) == 0)
926 if (i == (int)props->count_props) {
927 fprintf(stderr, "%s %i has no %s property\n",
928 obj_type, p->obj_id, p->name);
932 p->prop_id = props->props[i];
934 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
935 p->prop_id, p->value);
937 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
938 obj_type, p->obj_id, p->name, p->value, strerror(errno));
941 /* -------------------------------------------------------------------------- */
944 page_flip_handler(int fd, unsigned int frame,
945 unsigned int sec, unsigned int usec, void *data)
947 struct pipe_arg *pipe;
948 unsigned int new_fb_id;
953 if (pipe->current_fb_id == pipe->fb_id[0])
954 new_fb_id = pipe->fb_id[1];
956 new_fb_id = pipe->fb_id[0];
958 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
959 DRM_MODE_PAGE_FLIP_EVENT, pipe);
960 pipe->current_fb_id = new_fb_id;
962 if (pipe->swap_count == 60) {
963 gettimeofday(&end, NULL);
964 t = end.tv_sec + end.tv_usec * 1e-6 -
965 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
966 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
967 pipe->swap_count = 0;
972 static int set_plane(struct device *dev, struct plane_arg *p)
975 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
976 uint32_t plane_id = 0;
978 uint32_t plane_flags = 0;
979 int crtc_x, crtc_y, crtc_w, crtc_h;
980 struct crtc *crtc = NULL;
984 /* Find an unused plane which can be connected to our CRTC. Find the
985 * CRTC index first, then iterate over available planes.
987 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
988 if (p->crtc_id == dev->resources->res->crtcs[i]) {
989 crtc = &dev->resources->crtcs[i];
996 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1000 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
1001 ovr = dev->resources->planes[i].plane;
1005 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
1006 plane_id = ovr->plane_id;
1010 fprintf(stderr, "no unused plane available for CRTC %u\n",
1011 crtc->crtc->crtc_id);
1015 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1016 p->w, p->h, p->format_str, plane_id);
1018 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1019 pitches, offsets, PATTERN_TILES);
1020 if (plane_bo == NULL)
1025 /* just use single plane format for now.. */
1026 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1027 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1028 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1032 crtc_w = p->w * p->scale;
1033 crtc_h = p->h * p->scale;
1034 if (!p->has_position) {
1035 /* Default to the middle of the screen */
1036 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1037 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1043 /* note src coords (last 4 args) are in Q16 format */
1044 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1045 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1046 0, 0, p->w << 16, p->h << 16)) {
1047 fprintf(stderr, "failed to enable plane: %s\n",
1052 ovr->crtc_id = crtc->crtc->crtc_id;
1057 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1061 for (i = 0; i < count; i++) {
1063 drmModeRmFB(dev->fd, p[i].fb_id);
1065 bo_destroy(p[i].bo);
1070 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1072 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1079 dev->mode.width = 0;
1080 dev->mode.height = 0;
1081 dev->mode.fb_id = 0;
1083 for (i = 0; i < count; i++) {
1084 struct pipe_arg *pipe = &pipes[i];
1086 ret = pipe_find_crtc_and_mode(dev, pipe);
1090 dev->mode.width += pipe->mode->hdisplay;
1091 if (dev->mode.height < pipe->mode->vdisplay)
1092 dev->mode.height = pipe->mode->vdisplay;
1095 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1096 handles, pitches, offsets, PATTERN_SMPTE);
1102 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1103 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1105 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1106 dev->mode.width, dev->mode.height, strerror(errno));
1110 dev->mode.fb_id = fb_id;
1113 for (i = 0; i < count; i++) {
1114 struct pipe_arg *pipe = &pipes[i];
1116 if (pipe->mode == NULL)
1119 printf("setting mode %s-%dHz@%s on connectors ",
1120 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1121 for (j = 0; j < pipe->num_cons; ++j)
1122 printf("%u, ", pipe->con_ids[j]);
1123 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1125 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1126 x, 0, pipe->con_ids, pipe->num_cons,
1129 /* XXX: Actually check if this is needed */
1130 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1132 x += pipe->mode->hdisplay;
1135 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1141 static void clear_mode(struct device *dev)
1143 if (dev->mode.fb_id)
1144 drmModeRmFB(dev->fd, dev->mode.fb_id);
1146 bo_destroy(dev->mode.bo);
1149 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1153 /* set up planes/overlays */
1154 for (i = 0; i < count; i++)
1155 if (set_plane(dev, &p[i]))
1159 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1161 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1166 /* maybe make cursor width/height configurable some day */
1170 /* create cursor bo.. just using PATTERN_PLAIN as it has
1173 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1174 offsets, PATTERN_PLAIN);
1178 dev->mode.cursor_bo = bo;
1180 for (i = 0; i < count; i++) {
1181 struct pipe_arg *pipe = &pipes[i];
1182 ret = cursor_init(dev->fd, handles[0],
1183 pipe->crtc->crtc->crtc_id,
1184 pipe->mode->hdisplay, pipe->mode->vdisplay,
1187 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1196 static void clear_cursors(struct device *dev)
1200 if (dev->mode.cursor_bo)
1201 bo_destroy(dev->mode.cursor_bo);
1204 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1206 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1207 unsigned int other_fb_id;
1208 struct bo *other_bo;
1209 drmEventContext evctx;
1213 other_bo = bo_create(dev->fd, pipes[0].fourcc,
1214 dev->mode.width, dev->mode.height,
1215 handles, pitches, offsets, PATTERN_PLAIN);
1216 if (other_bo == NULL)
1219 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1220 pipes[0].fourcc, handles, pitches, offsets,
1223 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1227 for (i = 0; i < count; i++) {
1228 struct pipe_arg *pipe = &pipes[i];
1230 if (pipe->mode == NULL)
1233 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1234 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1237 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1240 gettimeofday(&pipe->start, NULL);
1241 pipe->swap_count = 0;
1242 pipe->fb_id[0] = dev->mode.fb_id;
1243 pipe->fb_id[1] = other_fb_id;
1244 pipe->current_fb_id = other_fb_id;
1247 memset(&evctx, 0, sizeof evctx);
1248 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1249 evctx.vblank_handler = NULL;
1250 evctx.page_flip_handler = page_flip_handler;
1254 struct pollfd pfd[2];
1257 pfd[0].events = POLLIN;
1259 pfd[1].events = POLLIN;
1261 if (poll(pfd, 2, -1) < 0) {
1262 fprintf(stderr, "poll error\n");
1269 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1275 FD_SET(dev->fd, &fds);
1276 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1279 fprintf(stderr, "select timed out or error (ret %d)\n",
1282 } else if (FD_ISSET(0, &fds)) {
1287 drmHandleEvent(dev->fd, &evctx);
1291 drmModeRmFB(dev->fd, other_fb_id);
1293 bo_destroy(other_bo);
1296 #define min(a, b) ((a) < (b) ? (a) : (b))
1298 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1306 pipe->crtc_id = (uint32_t)-1;
1307 strcpy(pipe->format_str, "XR24");
1309 /* Count the number of connectors and allocate them. */
1311 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1316 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1317 if (pipe->con_ids == NULL)
1320 /* Parse the connectors. */
1321 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1322 pipe->con_ids[i] = strtoul(p, &endp, 10);
1327 if (i != pipe->num_cons - 1)
1330 /* Parse the remaining parameters. */
1333 pipe->crtc_id = strtoul(arg, &endp, 10);
1340 /* Search for the vertical refresh or the format. */
1341 p = strpbrk(arg, "-@");
1343 p = arg + strlen(arg);
1344 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1345 strncpy(pipe->mode_str, arg, len);
1346 pipe->mode_str[len] = '\0';
1349 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1354 strncpy(pipe->format_str, p + 1, 4);
1355 pipe->format_str[4] = '\0';
1358 pipe->fourcc = format_fourcc(pipe->format_str);
1359 if (pipe->fourcc == 0) {
1360 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1367 static int parse_plane(struct plane_arg *plane, const char *p)
1371 memset(plane, 0, sizeof *plane);
1373 plane->crtc_id = strtoul(p, &end, 10);
1378 plane->w = strtoul(p, &end, 10);
1383 plane->h = strtoul(p, &end, 10);
1385 if (*end == '+' || *end == '-') {
1386 plane->x = strtol(end, &end, 10);
1387 if (*end != '+' && *end != '-')
1389 plane->y = strtol(end, &end, 10);
1391 plane->has_position = true;
1396 plane->scale = strtod(p, &end);
1397 if (plane->scale <= 0.0)
1408 strcpy(plane->format_str, p);
1410 strcpy(plane->format_str, "XR24");
1413 plane->fourcc = format_fourcc(plane->format_str);
1414 if (plane->fourcc == 0) {
1415 fprintf(stderr, "unknown format %s\n", plane->format_str);
1422 static int parse_property(struct property_arg *p, const char *arg)
1424 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1428 p->name[DRM_PROP_NAME_LEN] = '\0';
1433 static void usage(char *name)
1435 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1437 fprintf(stderr, "\n Query options:\n\n");
1438 fprintf(stderr, "\t-c\tlist connectors\n");
1439 fprintf(stderr, "\t-e\tlist encoders\n");
1440 fprintf(stderr, "\t-f\tlist framebuffers\n");
1441 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1443 fprintf(stderr, "\n Test options:\n\n");
1444 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1445 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1446 fprintf(stderr, "\t-C\ttest hw cursor\n");
1447 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1448 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1450 fprintf(stderr, "\n Generic options:\n\n");
1451 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1452 fprintf(stderr, "\t-M module\tuse the given driver\n");
1453 fprintf(stderr, "\t-D device\tuse the given device\n");
1455 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1459 static int page_flipping_supported(void)
1461 /*FIXME: generic ioctl needed? */
1465 struct drm_i915_getparam gp;
1467 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1470 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1472 fprintf(stderr, "drm_i915_getparam: %m\n");
1480 static int cursor_supported(void)
1482 /*FIXME: generic ioctl needed? */
1486 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1488 int main(int argc, char **argv)
1493 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1494 int drop_master = 0;
1496 int test_cursor = 0;
1497 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm", "sti", "tegra", "imx-drm", "rockchip" };
1498 char *device = NULL;
1499 char *module = NULL;
1501 int count = 0, plane_count = 0;
1502 unsigned int prop_count = 0;
1503 struct pipe_arg *pipe_args = NULL;
1504 struct plane_arg *plane_args = NULL;
1505 struct property_arg *prop_args = NULL;
1506 unsigned int args = 0;
1509 memset(&dev, 0, sizeof dev);
1512 while ((c = getopt(argc, argv, optstr)) != -1) {
1534 /* Preserve the default behaviour of dumping all information. */
1538 plane_args = realloc(plane_args,
1539 (plane_count + 1) * sizeof *plane_args);
1540 if (plane_args == NULL) {
1541 fprintf(stderr, "memory allocation failed\n");
1545 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1548 plane_args[plane_count].fb_id = 0;
1556 pipe_args = realloc(pipe_args,
1557 (count + 1) * sizeof *pipe_args);
1558 if (pipe_args == NULL) {
1559 fprintf(stderr, "memory allocation failed\n");
1563 if (parse_connector(&pipe_args[count], optarg) < 0)
1575 prop_args = realloc(prop_args,
1576 (prop_count + 1) * sizeof *prop_args);
1577 if (prop_args == NULL) {
1578 fprintf(stderr, "memory allocation failed\n");
1582 if (parse_property(&prop_args[prop_count], optarg) < 0)
1594 encoders = connectors = crtcs = planes = framebuffers = 1;
1597 dev.fd = drmOpen(module, device);
1599 fprintf(stderr, "failed to open device '%s'.\n", module);
1603 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1604 printf("trying to open device '%s'...", modules[i]);
1605 dev.fd = drmOpen(modules[i], device);
1607 printf("failed.\n");
1609 printf("success.\n");
1615 fprintf(stderr, "no device found.\n");
1620 if (test_vsync && !page_flipping_supported()) {
1621 fprintf(stderr, "page flipping not supported by drm.\n");
1625 if (test_vsync && !count) {
1626 fprintf(stderr, "page flipping requires at least one -s option.\n");
1630 if (test_cursor && !cursor_supported()) {
1631 fprintf(stderr, "hw cursor not supported by drm.\n");
1635 dev.resources = get_resources(&dev);
1636 if (!dev.resources) {
1641 #define dump_resource(dev, res) if (res) dump_##res(dev)
1643 dump_resource(&dev, encoders);
1644 dump_resource(&dev, connectors);
1645 dump_resource(&dev, crtcs);
1646 dump_resource(&dev, planes);
1647 dump_resource(&dev, framebuffers);
1649 for (i = 0; i < prop_count; ++i)
1650 set_property(&dev, &prop_args[i]);
1652 if (count || plane_count) {
1655 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1656 if (ret || cap == 0) {
1657 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1662 set_mode(&dev, pipe_args, count);
1665 set_planes(&dev, plane_args, plane_count);
1668 set_cursors(&dev, pipe_args, count);
1671 test_page_flip(&dev, pipe_args, count);
1674 drmDropMaster(dev.fd);
1679 clear_cursors(&dev);
1682 clear_planes(&dev, plane_args, plane_count);
1688 free_resources(dev.resources);