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.
55 #include <sys/select.h>
60 #include "xf86drmMode.h"
61 #include "drm_fourcc.h"
63 #include "util/common.h"
64 #include "util/format.h"
66 #include "util/pattern.h"
71 static enum util_fill_pattern primary_fill = UTIL_PATTERN_SMPTE;
72 static enum util_fill_pattern secondary_fill = UTIL_PATTERN_TILES;
76 drmModeObjectProperties *props;
77 drmModePropertyRes **props_info;
78 drmModeModeInfo *mode;
82 drmModeEncoder *encoder;
86 drmModeConnector *connector;
87 drmModeObjectProperties *props;
88 drmModePropertyRes **props_info;
98 drmModeObjectProperties *props;
99 drmModePropertyRes **props_info;
105 struct encoder *encoders;
107 struct connector *connectors;
108 int count_connectors;
111 struct plane *planes;
112 uint32_t count_planes;
118 struct resources *resources;
126 struct bo *cursor_bo;
130 drmModeAtomicReq *req;
131 int32_t writeback_fence_fd;
134 static inline int64_t U642I64(uint64_t val)
136 return (int64_t)*((int64_t *)&val);
139 static float mode_vrefresh(drmModeModeInfo *mode)
141 return mode->clock * 1000.00
142 / (mode->htotal * mode->vtotal);
145 #define bit_name_fn(res) \
146 const char * res##_str(int type) { \
148 const char *sep = ""; \
149 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
150 if (type & (1 << i)) { \
151 printf("%s%s", sep, res##_names[i]); \
158 static const char *mode_type_names[] = {
168 static bit_name_fn(mode_type)
170 static const char *mode_flag_names[] = {
187 static bit_name_fn(mode_flag)
189 static void dump_fourcc(uint32_t fourcc)
191 char *name = drmGetFormatName(fourcc);
196 static void dump_encoders(struct device *dev)
198 drmModeEncoder *encoder;
201 printf("Encoders:\n");
202 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
203 for (i = 0; i < dev->resources->count_encoders; i++) {
204 encoder = dev->resources->encoders[i].encoder;
208 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
211 util_lookup_encoder_type_name(encoder->encoder_type),
212 encoder->possible_crtcs,
213 encoder->possible_clones);
218 static void dump_mode(drmModeModeInfo *mode, int index)
220 printf(" #%i %s %.2f %d %d %d %d %d %d %d %d %d",
235 mode_flag_str(mode->flags);
237 mode_type_str(mode->type);
241 static void dump_blob(struct device *dev, uint32_t blob_id)
244 unsigned char *blob_data;
245 drmModePropertyBlobPtr blob;
247 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
253 blob_data = blob->data;
255 for (i = 0; i < blob->length; i++) {
258 printf("%.2hhx", blob_data[i]);
262 drmModeFreePropertyBlob(blob);
265 static const char *modifier_to_string(uint64_t modifier)
267 static char mod_string[4096];
269 char *modifier_name = drmGetFormatModifierName(modifier);
270 char *vendor_name = drmGetFormatModifierVendor(modifier);
271 memset(mod_string, 0x00, sizeof(mod_string));
273 if (!modifier_name) {
275 snprintf(mod_string, sizeof(mod_string), "%s_%s",
276 vendor_name, "UNKNOWN_MODIFIER");
278 snprintf(mod_string, sizeof(mod_string), "%s_%s",
279 "UNKNOWN_VENDOR", "UNKNOWN_MODIFIER");
280 /* safe, as free is no-op for NULL */
285 if (modifier == DRM_FORMAT_MOD_LINEAR) {
286 snprintf(mod_string, sizeof(mod_string), "%s", modifier_name);
292 snprintf(mod_string, sizeof(mod_string), "%s_%s",
293 vendor_name, modifier_name);
300 static void dump_in_formats(struct device *dev, uint32_t blob_id)
302 drmModeFormatModifierIterator iter = {0};
303 drmModePropertyBlobPtr blob;
306 printf("\t\tin_formats blob decoded:\n");
307 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
313 while (drmModeFormatModifierBlobIterNext(blob, &iter)) {
314 if (!fmt || fmt != iter.fmt) {
315 printf("%s\t\t\t", !fmt ? "" : "\n");
321 printf(" %s", modifier_to_string(iter.mod));
326 drmModeFreePropertyBlob(blob);
329 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
330 uint32_t prop_id, uint64_t value)
333 printf("\t%d", prop_id);
339 printf(" %s:\n", prop->name);
341 printf("\t\tflags:");
342 if (prop->flags & DRM_MODE_PROP_PENDING)
344 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
345 printf(" immutable");
346 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
347 printf(" signed range");
348 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
350 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
352 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
354 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
356 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
360 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
361 printf("\t\tvalues:");
362 for (i = 0; i < prop->count_values; i++)
363 printf(" %"PRId64, U642I64(prop->values[i]));
367 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
368 printf("\t\tvalues:");
369 for (i = 0; i < prop->count_values; i++)
370 printf(" %"PRIu64, prop->values[i]);
374 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
375 printf("\t\tenums:");
376 for (i = 0; i < prop->count_enums; i++)
377 printf(" %s=%"PRIu64, prop->enums[i].name,
378 (uint64_t)prop->enums[i].value);
380 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
381 printf("\t\tvalues:");
382 for (i = 0; i < prop->count_enums; i++)
383 printf(" %s=0x%llx", prop->enums[i].name,
384 (1LL << prop->enums[i].value));
387 assert(prop->count_enums == 0);
390 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
391 printf("\t\tblobs:\n");
392 for (i = 0; i < prop->count_blobs; i++)
393 dump_blob(dev, prop->blob_ids[i]);
396 assert(prop->count_blobs == 0);
399 printf("\t\tvalue:");
400 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
401 dump_blob(dev, value);
402 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
403 printf(" %"PRId64"\n", value);
405 printf(" %"PRIu64"\n", value);
407 if (strcmp(prop->name, "IN_FORMATS") == 0)
408 dump_in_formats(dev, value);
411 static void dump_connectors(struct device *dev)
415 printf("Connectors:\n");
416 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
417 for (i = 0; i < dev->resources->count_connectors; i++) {
418 struct connector *_connector = &dev->resources->connectors[i];
419 drmModeConnector *connector = _connector->connector;
423 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
424 connector->connector_id,
425 connector->encoder_id,
426 util_lookup_connector_status_name(connector->connection),
428 connector->mmWidth, connector->mmHeight,
429 connector->count_modes);
431 for (j = 0; j < connector->count_encoders; j++)
432 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
435 if (connector->count_modes) {
437 printf("\tindex name refresh (Hz) hdisp hss hse htot vdisp "
439 for (j = 0; j < connector->count_modes; j++)
440 dump_mode(&connector->modes[j], j);
443 if (_connector->props) {
445 for (j = 0; j < (int)_connector->props->count_props; j++)
446 dump_prop(dev, _connector->props_info[j],
447 _connector->props->props[j],
448 _connector->props->prop_values[j]);
454 static void dump_crtcs(struct device *dev)
460 printf("id\tfb\tpos\tsize\n");
461 for (i = 0; i < dev->resources->count_crtcs; i++) {
462 struct crtc *_crtc = &dev->resources->crtcs[i];
463 drmModeCrtc *crtc = _crtc->crtc;
467 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
471 crtc->width, crtc->height);
472 dump_mode(&crtc->mode, 0);
476 for (j = 0; j < _crtc->props->count_props; j++)
477 dump_prop(dev, _crtc->props_info[j],
478 _crtc->props->props[j],
479 _crtc->props->prop_values[j]);
481 printf(" no properties found\n");
487 static void dump_framebuffers(struct device *dev)
492 printf("Frame buffers:\n");
493 printf("id\tsize\tpitch\n");
494 for (i = 0; i < dev->resources->count_fbs; i++) {
495 fb = dev->resources->fbs[i].fb;
499 printf("%u\t(%ux%u)\t%u\n",
501 fb->width, fb->height,
507 static void dump_planes(struct device *dev)
512 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
514 for (i = 0; i < dev->resources->count_planes; i++) {
515 struct plane *plane = &dev->resources->planes[i];
516 drmModePlane *ovr = plane->plane;
520 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
521 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
522 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
523 ovr->gamma_size, ovr->possible_crtcs);
525 if (!ovr->count_formats)
529 for (j = 0; j < ovr->count_formats; j++)
530 dump_fourcc(ovr->formats[j]);
535 for (j = 0; j < plane->props->count_props; j++)
536 dump_prop(dev, plane->props_info[j],
537 plane->props->props[j],
538 plane->props->prop_values[j]);
540 printf(" no properties found\n");
548 static void free_resources(struct resources *res)
555 #define free_resource(_res, type, Type) \
557 if (!(_res)->type##s) \
559 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
560 if (!(_res)->type##s[i].type) \
562 drmModeFree##Type((_res)->type##s[i].type); \
564 free((_res)->type##s); \
567 #define free_properties(_res, type) \
569 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
571 for (j = 0; j < res->type##s[i].props->count_props; ++j)\
572 drmModeFreeProperty(res->type##s[i].props_info[j]);\
573 free(res->type##s[i].props_info); \
574 drmModeFreeObjectProperties(res->type##s[i].props); \
578 free_properties(res, plane);
579 free_resource(res, plane, Plane);
581 free_properties(res, connector);
582 free_properties(res, crtc);
584 for (i = 0; i < res->count_connectors; i++)
585 free(res->connectors[i].name);
587 free_resource(res, fb, FB);
588 free_resource(res, connector, Connector);
589 free_resource(res, encoder, Encoder);
590 free_resource(res, crtc, Crtc);
595 static struct resources *get_resources(struct device *dev)
598 drmModePlaneRes *plane_res;
599 struct resources *res;
602 res = calloc(1, sizeof(*res));
606 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
608 _res = drmModeGetResources(dev->fd);
610 fprintf(stderr, "drmModeGetResources failed: %s\n",
616 res->count_crtcs = _res->count_crtcs;
617 res->count_encoders = _res->count_encoders;
618 res->count_connectors = _res->count_connectors;
619 res->count_fbs = _res->count_fbs;
621 res->crtcs = calloc(res->count_crtcs, sizeof(*res->crtcs));
622 res->encoders = calloc(res->count_encoders, sizeof(*res->encoders));
623 res->connectors = calloc(res->count_connectors, sizeof(*res->connectors));
624 res->fbs = calloc(res->count_fbs, sizeof(*res->fbs));
626 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs) {
627 drmModeFreeResources(_res);
631 #define get_resource(_res, __res, type, Type) \
633 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
634 uint32_t type##id = (__res)->type##s[i]; \
635 (_res)->type##s[i].type = \
636 drmModeGet##Type(dev->fd, type##id); \
637 if (!(_res)->type##s[i].type) \
638 fprintf(stderr, "could not get %s %i: %s\n", \
644 get_resource(res, _res, crtc, Crtc);
645 get_resource(res, _res, encoder, Encoder);
646 get_resource(res, _res, connector, Connector);
647 get_resource(res, _res, fb, FB);
649 drmModeFreeResources(_res);
651 /* Set the name of all connectors based on the type name and the per-type ID. */
652 for (i = 0; i < res->count_connectors; i++) {
653 struct connector *connector = &res->connectors[i];
654 drmModeConnector *conn = connector->connector;
657 num = asprintf(&connector->name, "%s-%u",
658 drmModeGetConnectorTypeName(conn->connector_type),
659 conn->connector_type_id);
664 #define get_properties(_res, type, Type) \
666 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
667 struct type *obj = &res->type##s[i]; \
670 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
671 DRM_MODE_OBJECT_##Type); \
674 "could not get %s %i properties: %s\n", \
675 #type, obj->type->type##_id, \
679 obj->props_info = calloc(obj->props->count_props, \
680 sizeof(*obj->props_info)); \
681 if (!obj->props_info) \
683 for (j = 0; j < obj->props->count_props; ++j) \
684 obj->props_info[j] = \
685 drmModeGetProperty(dev->fd, obj->props->props[j]); \
689 get_properties(res, crtc, CRTC);
690 get_properties(res, connector, CONNECTOR);
692 for (i = 0; i < res->count_crtcs; ++i)
693 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
695 plane_res = drmModeGetPlaneResources(dev->fd);
697 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
702 res->count_planes = plane_res->count_planes;
704 res->planes = calloc(res->count_planes, sizeof(*res->planes));
706 drmModeFreePlaneResources(plane_res);
710 get_resource(res, plane_res, plane, Plane);
711 drmModeFreePlaneResources(plane_res);
712 get_properties(res, plane, PLANE);
721 static struct crtc *get_crtc_by_id(struct device *dev, uint32_t id)
725 for (i = 0; i < dev->resources->count_crtcs; ++i) {
726 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
727 if (crtc && crtc->crtc_id == id)
728 return &dev->resources->crtcs[i];
734 static uint32_t get_crtc_mask(struct device *dev, struct crtc *crtc)
738 for (i = 0; i < (unsigned int)dev->resources->count_crtcs; i++) {
739 if (crtc->crtc->crtc_id == dev->resources->crtcs[i].crtc->crtc_id)
742 /* Unreachable: crtc->crtc is one of resources->crtcs[] */
743 /* Don't return zero or static analysers will complain */
748 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
750 struct connector *connector;
753 for (i = 0; i < dev->resources->count_connectors; i++) {
754 connector = &dev->resources->connectors[i];
756 if (strcmp(connector->name, name) == 0)
757 return connector->connector;
763 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
765 drmModeConnector *connector;
768 for (i = 0; i < dev->resources->count_connectors; i++) {
769 connector = dev->resources->connectors[i].connector;
770 if (connector && connector->connector_id == id)
777 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
779 drmModeEncoder *encoder;
782 for (i = 0; i < dev->resources->count_encoders; i++) {
783 encoder = dev->resources->encoders[i].encoder;
784 if (encoder && encoder->encoder_id == id)
791 /* -----------------------------------------------------------------------------
796 * Mode setting with the kernel interfaces is a bit of a chore.
797 * First you have to find the connector in question and make sure the
798 * requested mode is available.
799 * Then you need to find the encoder attached to that connector so you
800 * can bind it with a free crtc.
805 unsigned int num_cons;
811 drmModeModeInfo *mode;
813 unsigned int fb_id[2], current_fb_id;
814 struct timeval start;
815 unsigned int out_fb_id;
822 uint32_t plane_id; /* the id of plane to use */
823 uint32_t crtc_id; /* the id of CRTC to bind to */
829 unsigned int old_fb_id;
832 char format_str[5]; /* need to leave room for terminating \0 */
836 static drmModeModeInfo *
837 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
838 const float vrefresh)
840 drmModeConnector *connector;
841 drmModeModeInfo *mode;
844 connector = get_connector_by_id(dev, con_id);
845 if (!connector || !connector->count_modes)
849 if (mode_str[0] == '#') {
850 int index = atoi(mode_str + 1);
852 if (index >= connector->count_modes || index < 0)
854 return &connector->modes[index];
858 for (i = 0; i < connector->count_modes; i++) {
859 mode = &connector->modes[i];
860 if (!strcmp(mode->name, mode_str)) {
861 /* If the vertical refresh frequency is not specified
862 * then return the first mode that match with the name.
863 * Else, return the mode that match the name and
864 * the specified vertical refresh frequency.
868 else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005)
876 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
878 uint32_t possible_crtcs = ~0;
879 uint32_t active_crtcs = 0;
880 unsigned int crtc_idx;
884 for (i = 0; i < pipe->num_cons; ++i) {
885 uint32_t crtcs_for_connector = 0;
886 drmModeConnector *connector;
887 drmModeEncoder *encoder;
890 connector = get_connector_by_id(dev, pipe->con_ids[i]);
894 for (j = 0; j < connector->count_encoders; ++j) {
895 encoder = get_encoder_by_id(dev, connector->encoders[j]);
899 crtcs_for_connector |= encoder->possible_crtcs;
900 crtc = get_crtc_by_id(dev, encoder->crtc_id);
903 active_crtcs |= get_crtc_mask(dev, crtc);
906 possible_crtcs &= crtcs_for_connector;
912 /* Return the first possible and active CRTC if one exists, or the first
913 * possible CRTC otherwise.
915 if (possible_crtcs & active_crtcs)
916 crtc_idx = ffs(possible_crtcs & active_crtcs);
918 crtc_idx = ffs(possible_crtcs);
920 return &dev->resources->crtcs[crtc_idx - 1];
923 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
925 drmModeModeInfo *mode = NULL;
930 for (i = 0; i < (int)pipe->num_cons; i++) {
931 mode = connector_find_mode(dev, pipe->con_ids[i],
932 pipe->mode_str, pipe->vrefresh);
936 "failed to find mode "
937 "\"%s-%.2fHz\" for connector %s\n",
938 pipe->mode_str, pipe->vrefresh, pipe->cons[i]);
941 "failed to find mode \"%s\" for connector %s\n",
942 pipe->mode_str, pipe->cons[i]);
947 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
948 * locate a CRTC that can be attached to all the connectors.
950 if (pipe->crtc_id != (uint32_t)-1) {
951 pipe->crtc = get_crtc_by_id(dev, pipe->crtc_id);
953 pipe->crtc = pipe_find_crtc(dev, pipe);
954 pipe->crtc_id = pipe->crtc->crtc->crtc_id;
958 fprintf(stderr, "failed to find CRTC for pipe\n");
963 pipe->crtc->mode = mode;
968 /* -----------------------------------------------------------------------------
972 struct property_arg {
975 char name[DRM_PROP_NAME_LEN+1];
981 static bool set_property(struct device *dev, struct property_arg *p)
983 drmModeObjectProperties *props = NULL;
984 drmModePropertyRes **props_info = NULL;
985 const char *obj_type;
992 #define find_object(_res, type, Type) \
994 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
995 struct type *obj = &(_res)->type##s[i]; \
996 if (obj->type->type##_id != p->obj_id) \
998 p->obj_type = DRM_MODE_OBJECT_##Type; \
1000 props = obj->props; \
1001 props_info = obj->props_info; \
1005 find_object(dev->resources, crtc, CRTC);
1006 if (p->obj_type == 0)
1007 find_object(dev->resources, connector, CONNECTOR);
1008 if (p->obj_type == 0)
1009 find_object(dev->resources, plane, PLANE);
1010 if (p->obj_type == 0) {
1011 fprintf(stderr, "Object %i not found, can't set property\n",
1017 fprintf(stderr, "%s %i has no properties\n",
1018 obj_type, p->obj_id);
1022 for (i = 0; i < (int)props->count_props; ++i) {
1025 if (strcmp(props_info[i]->name, p->name) == 0)
1029 if (i == (int)props->count_props) {
1031 fprintf(stderr, "%s %i has no %s property\n",
1032 obj_type, p->obj_id, p->name);
1036 p->prop_id = props->props[i];
1038 if (!dev->use_atomic)
1039 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1040 p->prop_id, p->value);
1042 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
1045 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1046 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1051 /* -------------------------------------------------------------------------- */
1054 page_flip_handler(int fd, unsigned int frame,
1055 unsigned int sec, unsigned int usec, void *data)
1057 struct pipe_arg *pipe;
1058 unsigned int new_fb_id;
1063 if (pipe->current_fb_id == pipe->fb_id[0])
1064 new_fb_id = pipe->fb_id[1];
1066 new_fb_id = pipe->fb_id[0];
1068 drmModePageFlip(fd, pipe->crtc_id, new_fb_id,
1069 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1070 pipe->current_fb_id = new_fb_id;
1072 if (pipe->swap_count == 60) {
1073 gettimeofday(&end, NULL);
1074 t = end.tv_sec + end.tv_usec * 1e-6 -
1075 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1076 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1077 pipe->swap_count = 0;
1082 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1086 for (i = 0; i < ovr->count_formats; ++i) {
1087 if (ovr->formats[i] == fmt)
1094 static void add_property(struct device *dev, uint32_t obj_id,
1095 const char *name, uint64_t value)
1097 struct property_arg p;
1100 strcpy(p.name, name);
1103 set_property(dev, &p);
1106 static bool add_property_optional(struct device *dev, uint32_t obj_id,
1107 const char *name, uint64_t value)
1109 struct property_arg p;
1112 strcpy(p.name, name);
1116 return set_property(dev, &p);
1119 static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
1121 unsigned blob_id = 0;
1122 /* TODO: support 1024-sized LUTs, when the use-case arises */
1123 struct drm_color_lut gamma_lut[256];
1126 if (fourcc == DRM_FORMAT_C8) {
1127 /* TODO: Add C8 support for more patterns */
1128 util_smpte_c8_gamma(256, gamma_lut);
1129 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
1131 for (i = 0; i < 256; i++) {
1133 gamma_lut[i].green =
1134 gamma_lut[i].blue = i << 8;
1138 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
1139 add_property_optional(dev, crtc_id, "CTM", 0);
1140 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
1141 uint16_t r[256], g[256], b[256];
1143 for (i = 0; i < 256; i++) {
1144 r[i] = gamma_lut[i].red;
1145 g[i] = gamma_lut[i].green;
1146 b[i] = gamma_lut[i].blue;
1149 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
1151 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
1156 bo_fb_create(int fd, unsigned int fourcc, const uint32_t w, const uint32_t h,
1157 enum util_fill_pattern pat, struct bo **out_bo, unsigned int *out_fb_id)
1159 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1163 bo = bo_create(fd, fourcc, w, h, handles, pitches, offsets, pat);
1168 if (drmModeAddFB2(fd, w, h, fourcc, handles, pitches, offsets, &fb_id, 0)) {
1169 fprintf(stderr, "failed to add fb (%ux%u): %s\n", w, h, strerror(errno));
1178 static int atomic_set_plane(struct device *dev, struct plane_arg *p,
1179 int pattern, bool update)
1181 struct bo *plane_bo;
1182 int crtc_x, crtc_y, crtc_w, crtc_h;
1183 struct crtc *crtc = NULL;
1184 unsigned int old_fb_id;
1186 /* Find an unused plane which can be connected to our CRTC. Find the
1187 * CRTC index first, then iterate over available planes.
1189 crtc = get_crtc_by_id(dev, p->crtc_id);
1191 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1196 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
1197 p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
1199 plane_bo = p->old_bo;
1203 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h,
1204 pattern, &plane_bo, &p->fb_id))
1210 old_fb_id = p->fb_id;
1211 p->old_fb_id = old_fb_id;
1213 crtc_w = p->w * p->scale;
1214 crtc_h = p->h * p->scale;
1215 if (!p->has_position) {
1216 /* Default to the middle of the screen */
1217 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1218 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1224 add_property(dev, p->plane_id, "FB_ID", p->fb_id);
1225 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
1226 add_property(dev, p->plane_id, "SRC_X", 0);
1227 add_property(dev, p->plane_id, "SRC_Y", 0);
1228 add_property(dev, p->plane_id, "SRC_W", p->w << 16);
1229 add_property(dev, p->plane_id, "SRC_H", p->h << 16);
1230 add_property(dev, p->plane_id, "CRTC_X", crtc_x);
1231 add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
1232 add_property(dev, p->plane_id, "CRTC_W", crtc_w);
1233 add_property(dev, p->plane_id, "CRTC_H", crtc_h);
1238 static int set_plane(struct device *dev, struct plane_arg *p)
1242 int crtc_x, crtc_y, crtc_w, crtc_h;
1243 struct crtc *crtc = NULL;
1244 unsigned int i, crtc_mask;
1246 /* Find an unused plane which can be connected to our CRTC. Find the
1247 * CRTC index first, then iterate over available planes.
1249 crtc = get_crtc_by_id(dev, p->crtc_id);
1251 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1254 crtc_mask = get_crtc_mask(dev, crtc);
1255 plane_id = p->plane_id;
1257 for (i = 0; i < dev->resources->count_planes; i++) {
1258 ovr = dev->resources->planes[i].plane;
1262 if (plane_id && plane_id != ovr->plane_id)
1265 if (!format_support(ovr, p->fourcc))
1268 if ((ovr->possible_crtcs & crtc_mask) &&
1269 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1270 plane_id = ovr->plane_id;
1275 if (i == dev->resources->count_planes) {
1276 fprintf(stderr, "no unused plane available for CRTC %u\n",
1281 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1282 p->w, p->h, p->format_str, plane_id);
1284 /* just use single plane format for now.. */
1285 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h,
1286 secondary_fill, &p->bo, &p->fb_id))
1289 crtc_w = p->w * p->scale;
1290 crtc_h = p->h * p->scale;
1291 if (!p->has_position) {
1292 /* Default to the middle of the screen */
1293 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1294 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1300 /* note src coords (last 4 args) are in Q16 format */
1301 if (drmModeSetPlane(dev->fd, plane_id, p->crtc_id, p->fb_id,
1302 0, crtc_x, crtc_y, crtc_w, crtc_h,
1303 0, 0, p->w << 16, p->h << 16)) {
1304 fprintf(stderr, "failed to enable plane: %s\n",
1309 ovr->crtc_id = p->crtc_id;
1314 static void atomic_set_planes(struct device *dev, struct plane_arg *p,
1315 unsigned int count, bool update)
1317 unsigned int i, pattern = primary_fill;
1320 for (i = 0; i < count; i++) {
1322 pattern = secondary_fill;
1324 set_gamma(dev, p[i].crtc_id, p[i].fourcc);
1326 if (atomic_set_plane(dev, &p[i], pattern, update))
1332 atomic_test_page_flip(struct device *dev, struct pipe_arg *pipe_args,
1333 struct plane_arg *plane_args, unsigned int plane_count)
1337 gettimeofday(&pipe_args->start, NULL);
1338 pipe_args->swap_count = 0;
1341 drmModeAtomicFree(dev->req);
1342 dev->req = drmModeAtomicAlloc();
1343 atomic_set_planes(dev, plane_args, plane_count, true);
1345 ret = drmModeAtomicCommit(dev->fd, dev->req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
1347 fprintf(stderr, "Atomic Commit failed [2]\n");
1351 pipe_args->swap_count++;
1352 if (pipe_args->swap_count == 60) {
1356 gettimeofday(&end, NULL);
1357 t = end.tv_sec + end.tv_usec * 1e-6 -
1358 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
1359 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
1360 pipe_args->swap_count = 0;
1361 pipe_args->start = end;
1366 static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1370 for (i = 0; i < count; i++) {
1371 add_property(dev, p[i].plane_id, "FB_ID", 0);
1372 add_property(dev, p[i].plane_id, "CRTC_ID", 0);
1373 add_property(dev, p[i].plane_id, "SRC_X", 0);
1374 add_property(dev, p[i].plane_id, "SRC_Y", 0);
1375 add_property(dev, p[i].plane_id, "SRC_W", 0);
1376 add_property(dev, p[i].plane_id, "SRC_H", 0);
1377 add_property(dev, p[i].plane_id, "CRTC_X", 0);
1378 add_property(dev, p[i].plane_id, "CRTC_Y", 0);
1379 add_property(dev, p[i].plane_id, "CRTC_W", 0);
1380 add_property(dev, p[i].plane_id, "CRTC_H", 0);
1384 static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
1388 for (i = 0; i < count; i++) {
1390 drmModeRmFB(dev->fd, p[i].fb_id);
1393 if (p[i].old_fb_id) {
1394 drmModeRmFB(dev->fd, p[i].old_fb_id);
1398 bo_destroy(p[i].bo);
1402 bo_destroy(p[i].old_bo);
1409 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1413 for (i = 0; i < count; i++) {
1415 drmModeRmFB(dev->fd, p[i].fb_id);
1417 bo_destroy(p[i].bo);
1421 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1423 drmModeConnector *connector;
1428 for (i = 0; i < pipe->num_cons; i++) {
1429 id = strtoul(pipe->cons[i], &endp, 10);
1430 if (endp == pipe->cons[i]) {
1431 connector = get_connector_by_name(dev, pipe->cons[i]);
1433 fprintf(stderr, "no connector named '%s'\n",
1438 id = connector->connector_id;
1441 pipe->con_ids[i] = id;
1447 static bool pipe_has_writeback_connector(struct device *dev, struct pipe_arg *pipes,
1450 drmModeConnector *connector;
1453 for (j = 0; j < count; j++) {
1454 struct pipe_arg *pipe = &pipes[j];
1456 for (i = 0; i < pipe->num_cons; i++) {
1457 connector = get_connector_by_id(dev, pipe->con_ids[i]);
1458 if (connector && connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1465 static int pipe_attempt_connector(struct device *dev, drmModeConnector *con,
1466 struct pipe_arg *pipe)
1471 con_str = calloc(8, sizeof(char));
1475 sprintf(con_str, "%d", con->connector_id);
1476 strcpy(pipe->format_str, "XR24");
1477 pipe->fourcc = util_format_fourcc(pipe->format_str);
1479 pipe->con_ids = calloc(1, sizeof(*pipe->con_ids));
1480 pipe->cons = calloc(1, sizeof(*pipe->cons));
1482 if (!pipe->con_ids || !pipe->cons)
1485 pipe->con_ids[0] = con->connector_id;
1486 pipe->cons[0] = (const char*)con_str;
1488 pipe->crtc = pipe_find_crtc(dev, pipe);
1492 pipe->crtc_id = pipe->crtc->crtc->crtc_id;
1494 /* Return the first mode if no preferred. */
1495 pipe->mode = &con->modes[0];
1497 for (i = 0; i < con->count_modes; i++) {
1498 drmModeModeInfo *current_mode = &con->modes[i];
1500 if (current_mode->type & DRM_MODE_TYPE_PREFERRED) {
1501 pipe->mode = current_mode;
1506 sprintf(pipe->mode_str, "%dx%d", pipe->mode->hdisplay, pipe->mode->vdisplay);
1512 free(pipe->con_ids);
1518 static int pipe_find_preferred(struct device *dev, struct pipe_arg **out_pipes)
1520 struct pipe_arg *pipes;
1521 struct resources *res = dev->resources;
1522 drmModeConnector *con = NULL;
1523 int i, connected = 0, attempted = 0;
1525 for (i = 0; i < res->count_connectors; i++) {
1526 con = res->connectors[i].connector;
1527 if (!con || con->connection != DRM_MODE_CONNECTED ||
1528 con->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1533 printf("no connected connector!\n");
1537 pipes = calloc(connected, sizeof(struct pipe_arg));
1541 for (i = 0; i < res->count_connectors && attempted < connected; i++) {
1542 con = res->connectors[i].connector;
1543 if (!con || con->connection != DRM_MODE_CONNECTED)
1546 if (pipe_attempt_connector(dev, con, &pipes[attempted]) < 0) {
1547 printf("failed fetching preferred mode for connector\n");
1557 static struct plane *get_primary_plane_by_crtc(struct device *dev, struct crtc *crtc)
1561 for (i = 0; i < dev->resources->count_planes; i++) {
1562 struct plane *plane = &dev->resources->planes[i];
1563 drmModePlane *ovr = plane->plane;
1567 // XXX: add is_primary_plane and (?) format checks
1569 if (ovr->possible_crtcs & get_crtc_mask(dev, crtc))
1575 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1579 int preferred = count == 0;
1581 for (i = 0; i < count; i++) {
1582 struct pipe_arg *pipe = &pipes[i];
1584 ret = pipe_resolve_connectors(dev, pipe);
1588 ret = pipe_find_crtc_and_mode(dev, pipe);
1593 struct pipe_arg *pipe_args;
1595 count = pipe_find_preferred(dev, &pipe_args);
1597 fprintf(stderr, "can't find any preferred connector/mode.\n");
1603 if (!dev->use_atomic) {
1604 for (i = 0; i < count; i++) {
1605 struct pipe_arg *pipe = &pipes[i];
1607 if (pipe->mode == NULL)
1611 dev->mode.width += pipe->mode->hdisplay;
1612 if (dev->mode.height < pipe->mode->vdisplay)
1613 dev->mode.height = pipe->mode->vdisplay;
1615 /* XXX: Use a clone mode, more like atomic. We could do per
1616 * connector bo/fb, so we don't have the stretched image.
1618 if (dev->mode.width < pipe->mode->hdisplay)
1619 dev->mode.width = pipe->mode->hdisplay;
1620 if (dev->mode.height < pipe->mode->vdisplay)
1621 dev->mode.height = pipe->mode->vdisplay;
1625 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1626 primary_fill, &dev->mode.bo, &dev->mode.fb_id))
1630 for (i = 0; i < count; i++) {
1631 struct pipe_arg *pipe = &pipes[i];
1634 if (pipe->mode == NULL)
1637 printf("setting mode %s-%.2fHz on connectors ",
1638 pipe->mode->name, mode_vrefresh(pipe->mode));
1639 for (j = 0; j < pipe->num_cons; ++j) {
1640 printf("%s, ", pipe->cons[j]);
1641 if (dev->use_atomic)
1642 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc_id);
1644 printf("crtc %d\n", pipe->crtc_id);
1646 if (!dev->use_atomic) {
1647 ret = drmModeSetCrtc(dev->fd, pipe->crtc_id, dev->mode.fb_id,
1648 x, 0, pipe->con_ids, pipe->num_cons,
1651 /* XXX: Actually check if this is needed */
1652 drmModeDirtyFB(dev->fd, dev->mode.fb_id, NULL, 0);
1655 x += pipe->mode->hdisplay;
1658 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1662 set_gamma(dev, pipe->crtc_id, pipe->fourcc);
1664 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
1665 add_property(dev, pipe->crtc_id, "MODE_ID", blob_id);
1666 add_property(dev, pipe->crtc_id, "ACTIVE", 1);
1668 /* By default atomic modeset does not set a primary plane, shrug */
1670 struct plane *plane = get_primary_plane_by_crtc(dev, pipe->crtc);
1671 struct plane_arg plane_args = {
1672 .plane_id = plane->plane->plane_id,
1673 .crtc_id = pipe->crtc_id,
1674 .w = pipe->mode->hdisplay,
1675 .h = pipe->mode->vdisplay,
1677 .format_str = "XR24",
1678 .fourcc = util_format_fourcc(pipe->format_str),
1681 atomic_set_planes(dev, &plane_args, 1, false);
1687 static void writeback_config(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1689 drmModeConnector *connector;
1692 for (j = 0; j < count; j++) {
1693 struct pipe_arg *pipe = &pipes[j];
1695 for (i = 0; i < pipe->num_cons; i++) {
1696 connector = get_connector_by_id(dev, pipe->con_ids[i]);
1697 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
1699 fprintf(stderr, "no mode for writeback\n");
1702 bo_fb_create(dev->fd, pipes[j].fourcc,
1703 pipe->mode->hdisplay, pipe->mode->vdisplay,
1705 &pipe->out_bo, &pipe->out_fb_id);
1706 add_property(dev, pipe->con_ids[i], "WRITEBACK_FB_ID",
1708 add_property(dev, pipe->con_ids[i], "WRITEBACK_OUT_FENCE_PTR",
1709 (uintptr_t)(&dev->writeback_fence_fd));
1715 static int poll_writeback_fence(int fd, int timeout)
1717 struct pollfd fds = { fd, POLLIN };
1721 ret = poll(&fds, 1, timeout);
1723 if (fds.revents & (POLLERR | POLLNVAL))
1727 } else if (ret == 0) {
1731 if (ret == -EINTR || ret == -EAGAIN)
1739 static void dump_output_fb(struct device *dev, struct pipe_arg *pipes, char *dump_path,
1742 drmModeConnector *connector;
1745 for (j = 0; j < count; j++) {
1746 struct pipe_arg *pipe = &pipes[j];
1748 for (i = 0; i < pipe->num_cons; i++) {
1749 connector = get_connector_by_id(dev, pipe->con_ids[i]);
1750 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1751 bo_dump(pipe->out_bo, dump_path);
1756 static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1761 for (i = 0; i < count; i++) {
1762 struct pipe_arg *pipe = &pipes[i];
1764 if (pipe->mode == NULL)
1767 for (j = 0; j < pipe->num_cons; ++j)
1768 add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
1770 add_property(dev, pipe->crtc_id, "MODE_ID", 0);
1771 add_property(dev, pipe->crtc_id, "ACTIVE", 0);
1775 static void clear_mode(struct device *dev)
1777 if (dev->mode.fb_id)
1778 drmModeRmFB(dev->fd, dev->mode.fb_id);
1780 bo_destroy(dev->mode.bo);
1783 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1787 /* set up planes/overlays */
1788 for (i = 0; i < count; i++)
1789 if (set_plane(dev, &p[i]))
1793 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1795 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1803 ret = drmGetCap(dev->fd, DRM_CAP_CURSOR_WIDTH, &value);
1807 ret = drmGetCap(dev->fd, DRM_CAP_CURSOR_HEIGHT, &value);
1812 /* create cursor bo.. just using PATTERN_PLAIN as it has
1815 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1816 offsets, UTIL_PATTERN_PLAIN);
1820 dev->mode.cursor_bo = bo;
1822 for (i = 0; i < count; i++) {
1823 struct pipe_arg *pipe = &pipes[i];
1824 ret = cursor_init(dev->fd, handles[0],
1826 pipe->mode->hdisplay, pipe->mode->vdisplay,
1829 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1838 static void clear_cursors(struct device *dev)
1842 if (dev->mode.cursor_bo)
1843 bo_destroy(dev->mode.cursor_bo);
1846 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1848 unsigned int other_fb_id;
1849 struct bo *other_bo;
1850 drmEventContext evctx;
1854 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1855 UTIL_PATTERN_PLAIN, &other_bo, &other_fb_id))
1858 for (i = 0; i < count; i++) {
1859 struct pipe_arg *pipe = &pipes[i];
1861 if (pipe->mode == NULL)
1864 ret = drmModePageFlip(dev->fd, pipe->crtc_id,
1865 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1868 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1871 gettimeofday(&pipe->start, NULL);
1872 pipe->swap_count = 0;
1873 pipe->fb_id[0] = dev->mode.fb_id;
1874 pipe->fb_id[1] = other_fb_id;
1875 pipe->current_fb_id = other_fb_id;
1878 memset(&evctx, 0, sizeof evctx);
1879 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1880 evctx.vblank_handler = NULL;
1881 evctx.page_flip_handler = page_flip_handler;
1885 struct pollfd pfd[2];
1888 pfd[0].events = POLLIN;
1890 pfd[1].events = POLLIN;
1892 if (poll(pfd, 2, -1) < 0) {
1893 fprintf(stderr, "poll error\n");
1900 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1905 FD_SET(dev->fd, &fds);
1906 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1909 fprintf(stderr, "select timed out or error (ret %d)\n",
1912 } else if (FD_ISSET(0, &fds)) {
1917 drmHandleEvent(dev->fd, &evctx);
1921 drmModeRmFB(dev->fd, other_fb_id);
1922 bo_destroy(other_bo);
1925 #define min(a, b) ((a) < (b) ? (a) : (b))
1927 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1935 pipe->crtc_id = (uint32_t)-1;
1936 strcpy(pipe->format_str, "XR24");
1938 /* Count the number of connectors and allocate them. */
1940 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1945 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1946 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1947 if (pipe->con_ids == NULL || pipe->cons == NULL)
1950 /* Parse the connectors. */
1951 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1952 endp = strpbrk(p, ",@:");
1956 pipe->cons[i] = strndup(p, endp - p);
1962 if (i != pipe->num_cons - 1)
1965 /* Parse the remaining parameters. */
1970 pipe->crtc_id = strtoul(arg, &endp, 10);
1977 /* Search for the vertical refresh or the format. */
1978 p = strpbrk(arg, "-@");
1980 p = arg + strlen(arg);
1981 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1982 strncpy(pipe->mode_str, arg, len);
1983 pipe->mode_str[len] = '\0';
1986 pipe->vrefresh = strtof(p + 1, &endp);
1991 strncpy(pipe->format_str, p + 1, 4);
1992 pipe->format_str[4] = '\0';
1995 pipe->fourcc = util_format_fourcc(pipe->format_str);
1996 if (pipe->fourcc == 0) {
1997 fprintf(stderr, "unknown format %s\n", pipe->format_str);
2004 static int parse_plane(struct plane_arg *plane, const char *p)
2008 plane->plane_id = strtoul(p, &end, 10);
2013 plane->crtc_id = strtoul(p, &end, 10);
2018 plane->w = strtoul(p, &end, 10);
2023 plane->h = strtoul(p, &end, 10);
2025 if (*end == '+' || *end == '-') {
2026 plane->x = strtol(end, &end, 10);
2027 if (*end != '+' && *end != '-')
2029 plane->y = strtol(end, &end, 10);
2031 plane->has_position = true;
2036 plane->scale = strtod(p, &end);
2037 if (plane->scale <= 0.0)
2044 strncpy(plane->format_str, end + 1, 4);
2045 plane->format_str[4] = '\0';
2047 strcpy(plane->format_str, "XR24");
2050 plane->fourcc = util_format_fourcc(plane->format_str);
2051 if (plane->fourcc == 0) {
2052 fprintf(stderr, "unknown format %s\n", plane->format_str);
2059 static int parse_property(struct property_arg *p, const char *arg)
2061 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
2065 p->name[DRM_PROP_NAME_LEN] = '\0';
2070 static void parse_fill_patterns(char *arg)
2072 char *fill = strtok(arg, ",");
2075 primary_fill = util_pattern_enum(fill);
2076 fill = strtok(NULL, ",");
2079 secondary_fill = util_pattern_enum(fill);
2082 static void usage(char *name)
2084 fprintf(stderr, "usage: %s [-acDdefMoPpsCvrw]\n", name);
2086 fprintf(stderr, "\n Query options:\n\n");
2087 fprintf(stderr, "\t-c\tlist connectors\n");
2088 fprintf(stderr, "\t-e\tlist encoders\n");
2089 fprintf(stderr, "\t-f\tlist framebuffers\n");
2090 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
2092 fprintf(stderr, "\n Test options:\n\n");
2093 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
2094 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:[#<mode index>]<mode>[-<vrefresh>][@<format>]\tset a mode\n");
2095 fprintf(stderr, "\t-C\ttest hw cursor\n");
2096 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
2097 fprintf(stderr, "\t-r\tset the preferred mode for all connectors\n");
2098 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
2099 fprintf(stderr, "\t-a \tuse atomic API\n");
2100 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
2101 fprintf(stderr, "\t-o <desired file path> \t Dump writeback output buffer to file\n");
2103 fprintf(stderr, "\n Generic options:\n\n");
2104 fprintf(stderr, "\t-d\tdrop master after mode set\n");
2105 fprintf(stderr, "\t-M module\tuse the given driver\n");
2106 fprintf(stderr, "\t-D device\tuse the given device\n");
2108 fprintf(stderr, "\n\tDefault is to dump all info.\n");
2112 static char optstr[] = "acdD:efF:M:P:ps:Cvrw:o:";
2114 int main(int argc, char **argv)
2119 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
2120 int drop_master = 0;
2122 int test_cursor = 0;
2123 int set_preferred = 0;
2125 char *device = NULL;
2126 char *module = NULL;
2128 unsigned int count = 0, plane_count = 0;
2129 unsigned int prop_count = 0;
2130 struct pipe_arg *pipe_args = NULL;
2131 struct plane_arg *plane_args = NULL;
2132 struct property_arg *prop_args = NULL;
2133 unsigned int args = 0;
2135 char *dump_path = NULL;
2137 memset(&dev, 0, sizeof dev);
2140 while ((c = getopt(argc, argv, optstr)) != -1) {
2146 /* Preserve the default behaviour of dumping all information. */
2154 /* Preserve the default behaviour of dumping all information. */
2167 parse_fill_patterns(optarg);
2171 /* Preserve the default behaviour of dumping all information. */
2178 plane_args = realloc(plane_args,
2179 (plane_count + 1) * sizeof *plane_args);
2180 if (plane_args == NULL) {
2181 fprintf(stderr, "memory allocation failed\n");
2184 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
2186 if (parse_plane(&plane_args[plane_count], optarg) < 0)
2196 pipe_args = realloc(pipe_args,
2197 (count + 1) * sizeof *pipe_args);
2198 if (pipe_args == NULL) {
2199 fprintf(stderr, "memory allocation failed\n");
2202 memset(&pipe_args[count], 0, sizeof(*pipe_args));
2204 if (parse_connector(&pipe_args[count], optarg) < 0)
2219 prop_args = realloc(prop_args,
2220 (prop_count + 1) * sizeof *prop_args);
2221 if (prop_args == NULL) {
2222 fprintf(stderr, "memory allocation failed\n");
2225 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
2227 if (parse_property(&prop_args[prop_count], optarg) < 0)
2238 /* Dump all the details when no* arguments are provided. */
2240 encoders = connectors = crtcs = planes = framebuffers = 1;
2242 if (test_vsync && !count) {
2243 fprintf(stderr, "page flipping requires at least one -s option.\n");
2246 if (set_preferred && count) {
2247 fprintf(stderr, "cannot use -r (preferred) when -s (mode) is set\n");
2251 if (set_preferred && plane_count) {
2252 fprintf(stderr, "cannot use -r (preferred) when -P (plane) is set\n");
2256 dev.fd = util_open(device, module);
2261 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
2262 drmSetClientCap(dev.fd, DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
2264 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
2270 dev.use_atomic = use_atomic;
2272 dev.resources = get_resources(&dev);
2273 if (!dev.resources) {
2278 #define dump_resource(dev, res) if (res) dump_##res(dev)
2280 dump_resource(&dev, encoders);
2281 dump_resource(&dev, connectors);
2282 dump_resource(&dev, crtcs);
2283 dump_resource(&dev, planes);
2284 dump_resource(&dev, framebuffers);
2286 for (i = 0; i < prop_count; ++i)
2287 set_property(&dev, &prop_args[i]);
2289 if (dev.use_atomic) {
2290 dev.req = drmModeAtomicAlloc();
2292 if (set_preferred || (count && plane_count)) {
2295 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2296 if (ret || cap == 0) {
2297 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2301 if (set_preferred || count)
2302 set_mode(&dev, pipe_args, count);
2305 if (!pipe_has_writeback_connector(&dev, pipe_args, count)) {
2306 fprintf(stderr, "No writeback connector found, can not dump.\n");
2310 writeback_config(&dev, pipe_args, count);
2314 atomic_set_planes(&dev, plane_args, plane_count, false);
2316 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2318 fprintf(stderr, "Atomic Commit failed [1]\n");
2323 * Since only writeback connectors have an output fb, this should only be
2324 * called for writeback.
2327 ret = poll_writeback_fence(dev.writeback_fence_fd, 1000);
2329 fprintf(stderr, "Poll for writeback error: %d. Skipping Dump.\n",
2331 dump_output_fb(&dev, pipe_args, dump_path, count);
2335 atomic_test_page_flip(&dev, pipe_args, plane_args, plane_count);
2338 drmDropMaster(dev.fd);
2342 drmModeAtomicFree(dev.req);
2343 dev.req = drmModeAtomicAlloc();
2345 /* XXX: properly teardown the preferred mode/plane state */
2347 atomic_clear_planes(&dev, plane_args, plane_count);
2350 atomic_clear_mode(&dev, pipe_args, count);
2352 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2354 fprintf(stderr, "Atomic Commit failed\n");
2357 atomic_clear_FB(&dev, plane_args, plane_count);
2360 drmModeAtomicFree(dev.req);
2363 fprintf(stderr, "writeback / dump is only supported in atomic mode\n");
2367 if (set_preferred || count || plane_count) {
2370 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2371 if (ret || cap == 0) {
2372 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2376 if (set_preferred || count)
2377 set_mode(&dev, pipe_args, count);
2380 set_planes(&dev, plane_args, plane_count);
2383 set_cursors(&dev, pipe_args, count);
2386 test_page_flip(&dev, pipe_args, count);
2389 drmDropMaster(dev.fd);
2394 clear_cursors(&dev);
2397 clear_planes(&dev, plane_args, plane_count);
2399 if (set_preferred || count)
2404 free_resources(dev.resources);