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 unsigned int num, den;
144 den = mode->htotal * mode->vtotal;
146 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
148 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
153 return num * 1000.00 / den;
156 #define bit_name_fn(res) \
157 const char * res##_str(int type) { \
159 const char *sep = ""; \
160 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
161 if (type & (1 << i)) { \
162 printf("%s%s", sep, res##_names[i]); \
169 static const char *mode_type_names[] = {
179 static bit_name_fn(mode_type)
181 static const char *mode_flag_names[] = {
198 static bit_name_fn(mode_flag)
200 static void dump_fourcc(uint32_t fourcc)
202 char *name = drmGetFormatName(fourcc);
207 static void dump_encoders(struct device *dev)
209 drmModeEncoder *encoder;
212 printf("Encoders:\n");
213 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
214 for (i = 0; i < dev->resources->count_encoders; i++) {
215 encoder = dev->resources->encoders[i].encoder;
219 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
222 util_lookup_encoder_type_name(encoder->encoder_type),
223 encoder->possible_crtcs,
224 encoder->possible_clones);
229 static void dump_mode(drmModeModeInfo *mode, int index)
231 printf(" #%i %s %.2f %d %d %d %d %d %d %d %d %d",
246 mode_flag_str(mode->flags);
248 mode_type_str(mode->type);
252 static void dump_blob(struct device *dev, uint32_t blob_id)
255 unsigned char *blob_data;
256 drmModePropertyBlobPtr blob;
258 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
264 blob_data = blob->data;
266 for (i = 0; i < blob->length; i++) {
269 printf("%.2hhx", blob_data[i]);
273 drmModeFreePropertyBlob(blob);
276 static const char *modifier_to_string(uint64_t modifier)
278 static char mod_string[4096];
280 char *modifier_name = drmGetFormatModifierName(modifier);
281 char *vendor_name = drmGetFormatModifierVendor(modifier);
282 memset(mod_string, 0x00, sizeof(mod_string));
284 if (!modifier_name) {
286 snprintf(mod_string, sizeof(mod_string), "%s_%s",
287 vendor_name, "UNKNOWN_MODIFIER");
289 snprintf(mod_string, sizeof(mod_string), "%s_%s",
290 "UNKNOWN_VENDOR", "UNKNOWN_MODIFIER");
291 /* safe, as free is no-op for NULL */
296 if (modifier == DRM_FORMAT_MOD_LINEAR) {
297 snprintf(mod_string, sizeof(mod_string), "%s", modifier_name);
303 snprintf(mod_string, sizeof(mod_string), "%s_%s",
304 vendor_name, modifier_name);
311 static void dump_in_formats(struct device *dev, uint32_t blob_id)
313 drmModeFormatModifierIterator iter = {0};
314 drmModePropertyBlobPtr blob;
317 printf("\t\tin_formats blob decoded:\n");
318 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
324 while (drmModeFormatModifierBlobIterNext(blob, &iter)) {
325 if (!fmt || fmt != iter.fmt) {
326 printf("%s\t\t\t", !fmt ? "" : "\n");
332 printf(" %s", modifier_to_string(iter.mod));
337 drmModeFreePropertyBlob(blob);
340 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
341 uint32_t prop_id, uint64_t value)
344 printf("\t%d", prop_id);
350 printf(" %s:\n", prop->name);
352 printf("\t\tflags:");
353 if (prop->flags & DRM_MODE_PROP_PENDING)
355 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
356 printf(" immutable");
357 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
358 printf(" signed range");
359 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
361 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
363 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
365 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
367 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
371 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
372 printf("\t\tvalues:");
373 for (i = 0; i < prop->count_values; i++)
374 printf(" %"PRId64, U642I64(prop->values[i]));
378 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
379 printf("\t\tvalues:");
380 for (i = 0; i < prop->count_values; i++)
381 printf(" %"PRIu64, prop->values[i]);
385 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
386 printf("\t\tenums:");
387 for (i = 0; i < prop->count_enums; i++)
388 printf(" %s=%"PRIu64, prop->enums[i].name,
389 (uint64_t)prop->enums[i].value);
391 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
392 printf("\t\tvalues:");
393 for (i = 0; i < prop->count_enums; i++)
394 printf(" %s=0x%llx", prop->enums[i].name,
395 (1LL << prop->enums[i].value));
398 assert(prop->count_enums == 0);
401 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
402 printf("\t\tblobs:\n");
403 for (i = 0; i < prop->count_blobs; i++)
404 dump_blob(dev, prop->blob_ids[i]);
407 assert(prop->count_blobs == 0);
410 printf("\t\tvalue:");
411 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
412 dump_blob(dev, value);
413 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
414 printf(" %"PRId64"\n", value);
416 printf(" %"PRIu64"\n", value);
418 if (strcmp(prop->name, "IN_FORMATS") == 0)
419 dump_in_formats(dev, value);
422 static void dump_connectors(struct device *dev)
426 printf("Connectors:\n");
427 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
428 for (i = 0; i < dev->resources->count_connectors; i++) {
429 struct connector *_connector = &dev->resources->connectors[i];
430 drmModeConnector *connector = _connector->connector;
434 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
435 connector->connector_id,
436 connector->encoder_id,
437 util_lookup_connector_status_name(connector->connection),
439 connector->mmWidth, connector->mmHeight,
440 connector->count_modes);
442 for (j = 0; j < connector->count_encoders; j++)
443 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
446 if (connector->count_modes) {
448 printf("\tindex name refresh (Hz) hdisp hss hse htot vdisp "
450 for (j = 0; j < connector->count_modes; j++)
451 dump_mode(&connector->modes[j], j);
454 if (_connector->props) {
456 for (j = 0; j < (int)_connector->props->count_props; j++)
457 dump_prop(dev, _connector->props_info[j],
458 _connector->props->props[j],
459 _connector->props->prop_values[j]);
465 static void dump_crtcs(struct device *dev)
471 printf("id\tfb\tpos\tsize\n");
472 for (i = 0; i < dev->resources->count_crtcs; i++) {
473 struct crtc *_crtc = &dev->resources->crtcs[i];
474 drmModeCrtc *crtc = _crtc->crtc;
478 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
482 crtc->width, crtc->height);
483 dump_mode(&crtc->mode, 0);
487 for (j = 0; j < _crtc->props->count_props; j++)
488 dump_prop(dev, _crtc->props_info[j],
489 _crtc->props->props[j],
490 _crtc->props->prop_values[j]);
492 printf(" no properties found\n");
498 static void dump_framebuffers(struct device *dev)
503 printf("Frame buffers:\n");
504 printf("id\tsize\tpitch\n");
505 for (i = 0; i < dev->resources->count_fbs; i++) {
506 fb = dev->resources->fbs[i].fb;
510 printf("%u\t(%ux%u)\t%u\n",
512 fb->width, fb->height,
518 static void dump_planes(struct device *dev)
523 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
525 for (i = 0; i < dev->resources->count_planes; i++) {
526 struct plane *plane = &dev->resources->planes[i];
527 drmModePlane *ovr = plane->plane;
531 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
532 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
533 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
534 ovr->gamma_size, ovr->possible_crtcs);
536 if (!ovr->count_formats)
540 for (j = 0; j < ovr->count_formats; j++)
541 dump_fourcc(ovr->formats[j]);
546 for (j = 0; j < plane->props->count_props; j++)
547 dump_prop(dev, plane->props_info[j],
548 plane->props->props[j],
549 plane->props->prop_values[j]);
551 printf(" no properties found\n");
559 static void free_resources(struct resources *res)
566 #define free_resource(_res, type, Type) \
568 if (!(_res)->type##s) \
570 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
571 if (!(_res)->type##s[i].type) \
573 drmModeFree##Type((_res)->type##s[i].type); \
575 free((_res)->type##s); \
578 #define free_properties(_res, type) \
580 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
582 for (j = 0; j < res->type##s[i].props->count_props; ++j)\
583 drmModeFreeProperty(res->type##s[i].props_info[j]);\
584 free(res->type##s[i].props_info); \
585 drmModeFreeObjectProperties(res->type##s[i].props); \
589 free_properties(res, plane);
590 free_resource(res, plane, Plane);
592 free_properties(res, connector);
593 free_properties(res, crtc);
595 for (i = 0; i < res->count_connectors; i++)
596 free(res->connectors[i].name);
598 free_resource(res, fb, FB);
599 free_resource(res, connector, Connector);
600 free_resource(res, encoder, Encoder);
601 free_resource(res, crtc, Crtc);
606 static struct resources *get_resources(struct device *dev)
609 drmModePlaneRes *plane_res;
610 struct resources *res;
613 res = calloc(1, sizeof(*res));
617 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
619 _res = drmModeGetResources(dev->fd);
621 fprintf(stderr, "drmModeGetResources failed: %s\n",
627 res->count_crtcs = _res->count_crtcs;
628 res->count_encoders = _res->count_encoders;
629 res->count_connectors = _res->count_connectors;
630 res->count_fbs = _res->count_fbs;
632 res->crtcs = calloc(res->count_crtcs, sizeof(*res->crtcs));
633 res->encoders = calloc(res->count_encoders, sizeof(*res->encoders));
634 res->connectors = calloc(res->count_connectors, sizeof(*res->connectors));
635 res->fbs = calloc(res->count_fbs, sizeof(*res->fbs));
637 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs) {
638 drmModeFreeResources(_res);
642 #define get_resource(_res, __res, type, Type) \
644 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
645 uint32_t type##id = (__res)->type##s[i]; \
646 (_res)->type##s[i].type = \
647 drmModeGet##Type(dev->fd, type##id); \
648 if (!(_res)->type##s[i].type) \
649 fprintf(stderr, "could not get %s %i: %s\n", \
655 get_resource(res, _res, crtc, Crtc);
656 get_resource(res, _res, encoder, Encoder);
657 get_resource(res, _res, connector, Connector);
658 get_resource(res, _res, fb, FB);
660 drmModeFreeResources(_res);
662 /* Set the name of all connectors based on the type name and the per-type ID. */
663 for (i = 0; i < res->count_connectors; i++) {
664 struct connector *connector = &res->connectors[i];
665 drmModeConnector *conn = connector->connector;
668 num = asprintf(&connector->name, "%s-%u",
669 drmModeGetConnectorTypeName(conn->connector_type),
670 conn->connector_type_id);
675 #define get_properties(_res, type, Type) \
677 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
678 struct type *obj = &res->type##s[i]; \
681 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
682 DRM_MODE_OBJECT_##Type); \
685 "could not get %s %i properties: %s\n", \
686 #type, obj->type->type##_id, \
690 obj->props_info = calloc(obj->props->count_props, \
691 sizeof(*obj->props_info)); \
692 if (!obj->props_info) \
694 for (j = 0; j < obj->props->count_props; ++j) \
695 obj->props_info[j] = \
696 drmModeGetProperty(dev->fd, obj->props->props[j]); \
700 get_properties(res, crtc, CRTC);
701 get_properties(res, connector, CONNECTOR);
703 for (i = 0; i < res->count_crtcs; ++i)
704 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
706 plane_res = drmModeGetPlaneResources(dev->fd);
708 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
713 res->count_planes = plane_res->count_planes;
715 res->planes = calloc(res->count_planes, sizeof(*res->planes));
717 drmModeFreePlaneResources(plane_res);
721 get_resource(res, plane_res, plane, Plane);
722 drmModeFreePlaneResources(plane_res);
723 get_properties(res, plane, PLANE);
732 static struct crtc *get_crtc_by_id(struct device *dev, uint32_t id)
736 for (i = 0; i < dev->resources->count_crtcs; ++i) {
737 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
738 if (crtc && crtc->crtc_id == id)
739 return &dev->resources->crtcs[i];
745 static uint32_t get_crtc_mask(struct device *dev, struct crtc *crtc)
749 for (i = 0; i < (unsigned int)dev->resources->count_crtcs; i++) {
750 if (crtc->crtc->crtc_id == dev->resources->crtcs[i].crtc->crtc_id)
753 /* Unreachable: crtc->crtc is one of resources->crtcs[] */
754 /* Don't return zero or static analysers will complain */
759 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
761 struct connector *connector;
764 for (i = 0; i < dev->resources->count_connectors; i++) {
765 connector = &dev->resources->connectors[i];
767 if (strcmp(connector->name, name) == 0)
768 return connector->connector;
774 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
776 drmModeConnector *connector;
779 for (i = 0; i < dev->resources->count_connectors; i++) {
780 connector = dev->resources->connectors[i].connector;
781 if (connector && connector->connector_id == id)
788 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
790 drmModeEncoder *encoder;
793 for (i = 0; i < dev->resources->count_encoders; i++) {
794 encoder = dev->resources->encoders[i].encoder;
795 if (encoder && encoder->encoder_id == id)
802 /* -----------------------------------------------------------------------------
807 * Mode setting with the kernel interfaces is a bit of a chore.
808 * First you have to find the connector in question and make sure the
809 * requested mode is available.
810 * Then you need to find the encoder attached to that connector so you
811 * can bind it with a free crtc.
816 unsigned int num_cons;
822 drmModeModeInfo *mode;
824 unsigned int fb_id[2], current_fb_id;
825 struct timeval start;
826 unsigned int out_fb_id;
833 uint32_t plane_id; /* the id of plane to use */
834 uint32_t crtc_id; /* the id of CRTC to bind to */
840 unsigned int old_fb_id;
843 char format_str[5]; /* need to leave room for terminating \0 */
847 static drmModeModeInfo *
848 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
849 const float vrefresh)
851 drmModeConnector *connector;
852 drmModeModeInfo *mode;
855 connector = get_connector_by_id(dev, con_id);
856 if (!connector || !connector->count_modes)
860 if (mode_str[0] == '#') {
861 int index = atoi(mode_str + 1);
863 if (index >= connector->count_modes || index < 0)
865 return &connector->modes[index];
869 for (i = 0; i < connector->count_modes; i++) {
870 mode = &connector->modes[i];
871 if (!strcmp(mode->name, mode_str)) {
872 /* If the vertical refresh frequency is not specified
873 * then return the first mode that match with the name.
874 * Else, return the mode that match the name and
875 * the specified vertical refresh frequency.
879 else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005)
887 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
889 uint32_t possible_crtcs = ~0;
890 uint32_t active_crtcs = 0;
891 unsigned int crtc_idx;
895 for (i = 0; i < pipe->num_cons; ++i) {
896 uint32_t crtcs_for_connector = 0;
897 drmModeConnector *connector;
898 drmModeEncoder *encoder;
901 connector = get_connector_by_id(dev, pipe->con_ids[i]);
905 for (j = 0; j < connector->count_encoders; ++j) {
906 encoder = get_encoder_by_id(dev, connector->encoders[j]);
910 crtcs_for_connector |= encoder->possible_crtcs;
911 crtc = get_crtc_by_id(dev, encoder->crtc_id);
914 active_crtcs |= get_crtc_mask(dev, crtc);
917 possible_crtcs &= crtcs_for_connector;
923 /* Return the first possible and active CRTC if one exists, or the first
924 * possible CRTC otherwise.
926 if (possible_crtcs & active_crtcs)
927 crtc_idx = ffs(possible_crtcs & active_crtcs);
929 crtc_idx = ffs(possible_crtcs);
931 return &dev->resources->crtcs[crtc_idx - 1];
934 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
936 drmModeModeInfo *mode = NULL;
941 for (i = 0; i < (int)pipe->num_cons; i++) {
942 mode = connector_find_mode(dev, pipe->con_ids[i],
943 pipe->mode_str, pipe->vrefresh);
947 "failed to find mode "
948 "\"%s-%.2fHz\" for connector %s\n",
949 pipe->mode_str, pipe->vrefresh, pipe->cons[i]);
952 "failed to find mode \"%s\" for connector %s\n",
953 pipe->mode_str, pipe->cons[i]);
958 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
959 * locate a CRTC that can be attached to all the connectors.
961 if (pipe->crtc_id != (uint32_t)-1) {
962 pipe->crtc = get_crtc_by_id(dev, pipe->crtc_id);
964 pipe->crtc = pipe_find_crtc(dev, pipe);
965 pipe->crtc_id = pipe->crtc->crtc->crtc_id;
969 fprintf(stderr, "failed to find CRTC for pipe\n");
974 pipe->crtc->mode = mode;
979 /* -----------------------------------------------------------------------------
983 struct property_arg {
986 char name[DRM_PROP_NAME_LEN+1];
992 static bool set_property(struct device *dev, struct property_arg *p)
994 drmModeObjectProperties *props = NULL;
995 drmModePropertyRes **props_info = NULL;
996 const char *obj_type;
1003 #define find_object(_res, type, Type) \
1005 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
1006 struct type *obj = &(_res)->type##s[i]; \
1007 if (obj->type->type##_id != p->obj_id) \
1009 p->obj_type = DRM_MODE_OBJECT_##Type; \
1011 props = obj->props; \
1012 props_info = obj->props_info; \
1016 find_object(dev->resources, crtc, CRTC);
1017 if (p->obj_type == 0)
1018 find_object(dev->resources, connector, CONNECTOR);
1019 if (p->obj_type == 0)
1020 find_object(dev->resources, plane, PLANE);
1021 if (p->obj_type == 0) {
1022 fprintf(stderr, "Object %i not found, can't set property\n",
1028 fprintf(stderr, "%s %i has no properties\n",
1029 obj_type, p->obj_id);
1033 for (i = 0; i < (int)props->count_props; ++i) {
1036 if (strcmp(props_info[i]->name, p->name) == 0)
1040 if (i == (int)props->count_props) {
1042 fprintf(stderr, "%s %i has no %s property\n",
1043 obj_type, p->obj_id, p->name);
1047 p->prop_id = props->props[i];
1049 if (!dev->use_atomic)
1050 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1051 p->prop_id, p->value);
1053 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
1056 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1057 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1062 /* -------------------------------------------------------------------------- */
1065 page_flip_handler(int fd, unsigned int frame,
1066 unsigned int sec, unsigned int usec, void *data)
1068 struct pipe_arg *pipe;
1069 unsigned int new_fb_id;
1074 if (pipe->current_fb_id == pipe->fb_id[0])
1075 new_fb_id = pipe->fb_id[1];
1077 new_fb_id = pipe->fb_id[0];
1079 drmModePageFlip(fd, pipe->crtc_id, new_fb_id,
1080 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1081 pipe->current_fb_id = new_fb_id;
1083 if (pipe->swap_count == 60) {
1084 gettimeofday(&end, NULL);
1085 t = end.tv_sec + end.tv_usec * 1e-6 -
1086 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1087 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1088 pipe->swap_count = 0;
1093 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1097 for (i = 0; i < ovr->count_formats; ++i) {
1098 if (ovr->formats[i] == fmt)
1105 static void add_property(struct device *dev, uint32_t obj_id,
1106 const char *name, uint64_t value)
1108 struct property_arg p;
1111 strcpy(p.name, name);
1114 set_property(dev, &p);
1117 static bool add_property_optional(struct device *dev, uint32_t obj_id,
1118 const char *name, uint64_t value)
1120 struct property_arg p;
1123 strcpy(p.name, name);
1127 return set_property(dev, &p);
1130 static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
1132 unsigned blob_id = 0;
1133 /* TODO: support 1024-sized LUTs, when the use-case arises */
1134 struct drm_color_lut gamma_lut[256];
1137 if (fourcc == DRM_FORMAT_C8) {
1138 /* TODO: Add C8 support for more patterns */
1139 util_smpte_c8_gamma(256, gamma_lut);
1140 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
1142 for (i = 0; i < 256; i++) {
1144 gamma_lut[i].green =
1145 gamma_lut[i].blue = i << 8;
1149 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
1150 add_property_optional(dev, crtc_id, "CTM", 0);
1151 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
1152 uint16_t r[256], g[256], b[256];
1154 for (i = 0; i < 256; i++) {
1155 r[i] = gamma_lut[i].red;
1156 g[i] = gamma_lut[i].green;
1157 b[i] = gamma_lut[i].blue;
1160 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
1162 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
1167 bo_fb_create(int fd, unsigned int fourcc, const uint32_t w, const uint32_t h,
1168 enum util_fill_pattern pat, struct bo **out_bo, unsigned int *out_fb_id)
1170 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1174 bo = bo_create(fd, fourcc, w, h, handles, pitches, offsets, pat);
1179 if (drmModeAddFB2(fd, w, h, fourcc, handles, pitches, offsets, &fb_id, 0)) {
1180 fprintf(stderr, "failed to add fb (%ux%u): %s\n", w, h, strerror(errno));
1189 static int atomic_set_plane(struct device *dev, struct plane_arg *p,
1190 int pattern, bool update)
1192 struct bo *plane_bo;
1193 int crtc_x, crtc_y, crtc_w, crtc_h;
1194 struct crtc *crtc = NULL;
1195 unsigned int old_fb_id;
1197 /* Find an unused plane which can be connected to our CRTC. Find the
1198 * CRTC index first, then iterate over available planes.
1200 crtc = get_crtc_by_id(dev, p->crtc_id);
1202 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1207 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
1208 p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
1210 plane_bo = p->old_bo;
1214 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h,
1215 pattern, &plane_bo, &p->fb_id))
1221 old_fb_id = p->fb_id;
1222 p->old_fb_id = old_fb_id;
1224 crtc_w = p->w * p->scale;
1225 crtc_h = p->h * p->scale;
1226 if (!p->has_position) {
1227 /* Default to the middle of the screen */
1228 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1229 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1235 add_property(dev, p->plane_id, "FB_ID", p->fb_id);
1236 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
1237 add_property(dev, p->plane_id, "SRC_X", 0);
1238 add_property(dev, p->plane_id, "SRC_Y", 0);
1239 add_property(dev, p->plane_id, "SRC_W", p->w << 16);
1240 add_property(dev, p->plane_id, "SRC_H", p->h << 16);
1241 add_property(dev, p->plane_id, "CRTC_X", crtc_x);
1242 add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
1243 add_property(dev, p->plane_id, "CRTC_W", crtc_w);
1244 add_property(dev, p->plane_id, "CRTC_H", crtc_h);
1249 static int set_plane(struct device *dev, struct plane_arg *p)
1253 int crtc_x, crtc_y, crtc_w, crtc_h;
1254 struct crtc *crtc = NULL;
1255 unsigned int i, crtc_mask;
1257 /* Find an unused plane which can be connected to our CRTC. Find the
1258 * CRTC index first, then iterate over available planes.
1260 crtc = get_crtc_by_id(dev, p->crtc_id);
1262 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1265 crtc_mask = get_crtc_mask(dev, crtc);
1266 plane_id = p->plane_id;
1268 for (i = 0; i < dev->resources->count_planes; i++) {
1269 ovr = dev->resources->planes[i].plane;
1273 if (plane_id && plane_id != ovr->plane_id)
1276 if (!format_support(ovr, p->fourcc))
1279 if ((ovr->possible_crtcs & crtc_mask) &&
1280 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1281 plane_id = ovr->plane_id;
1286 if (i == dev->resources->count_planes) {
1287 fprintf(stderr, "no unused plane available for CRTC %u\n",
1292 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1293 p->w, p->h, p->format_str, plane_id);
1295 /* just use single plane format for now.. */
1296 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h,
1297 secondary_fill, &p->bo, &p->fb_id))
1300 crtc_w = p->w * p->scale;
1301 crtc_h = p->h * p->scale;
1302 if (!p->has_position) {
1303 /* Default to the middle of the screen */
1304 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1305 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1311 /* note src coords (last 4 args) are in Q16 format */
1312 if (drmModeSetPlane(dev->fd, plane_id, p->crtc_id, p->fb_id,
1313 0, crtc_x, crtc_y, crtc_w, crtc_h,
1314 0, 0, p->w << 16, p->h << 16)) {
1315 fprintf(stderr, "failed to enable plane: %s\n",
1320 ovr->crtc_id = p->crtc_id;
1325 static void atomic_set_planes(struct device *dev, struct plane_arg *p,
1326 unsigned int count, bool update)
1328 unsigned int i, pattern = primary_fill;
1331 for (i = 0; i < count; i++) {
1333 pattern = secondary_fill;
1335 set_gamma(dev, p[i].crtc_id, p[i].fourcc);
1337 if (atomic_set_plane(dev, &p[i], pattern, update))
1343 atomic_test_page_flip(struct device *dev, struct pipe_arg *pipe_args,
1344 struct plane_arg *plane_args, unsigned int plane_count)
1348 gettimeofday(&pipe_args->start, NULL);
1349 pipe_args->swap_count = 0;
1352 drmModeAtomicFree(dev->req);
1353 dev->req = drmModeAtomicAlloc();
1354 atomic_set_planes(dev, plane_args, plane_count, true);
1356 ret = drmModeAtomicCommit(dev->fd, dev->req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
1358 fprintf(stderr, "Atomic Commit failed [2]\n");
1362 pipe_args->swap_count++;
1363 if (pipe_args->swap_count == 60) {
1367 gettimeofday(&end, NULL);
1368 t = end.tv_sec + end.tv_usec * 1e-6 -
1369 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
1370 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
1371 pipe_args->swap_count = 0;
1372 pipe_args->start = end;
1377 static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1381 for (i = 0; i < count; i++) {
1382 add_property(dev, p[i].plane_id, "FB_ID", 0);
1383 add_property(dev, p[i].plane_id, "CRTC_ID", 0);
1384 add_property(dev, p[i].plane_id, "SRC_X", 0);
1385 add_property(dev, p[i].plane_id, "SRC_Y", 0);
1386 add_property(dev, p[i].plane_id, "SRC_W", 0);
1387 add_property(dev, p[i].plane_id, "SRC_H", 0);
1388 add_property(dev, p[i].plane_id, "CRTC_X", 0);
1389 add_property(dev, p[i].plane_id, "CRTC_Y", 0);
1390 add_property(dev, p[i].plane_id, "CRTC_W", 0);
1391 add_property(dev, p[i].plane_id, "CRTC_H", 0);
1395 static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
1399 for (i = 0; i < count; i++) {
1401 drmModeRmFB(dev->fd, p[i].fb_id);
1404 if (p[i].old_fb_id) {
1405 drmModeRmFB(dev->fd, p[i].old_fb_id);
1409 bo_destroy(p[i].bo);
1413 bo_destroy(p[i].old_bo);
1420 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1424 for (i = 0; i < count; i++) {
1426 drmModeRmFB(dev->fd, p[i].fb_id);
1428 bo_destroy(p[i].bo);
1432 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1434 drmModeConnector *connector;
1439 for (i = 0; i < pipe->num_cons; i++) {
1440 id = strtoul(pipe->cons[i], &endp, 10);
1441 if (endp == pipe->cons[i]) {
1442 connector = get_connector_by_name(dev, pipe->cons[i]);
1444 fprintf(stderr, "no connector named '%s'\n",
1449 id = connector->connector_id;
1452 pipe->con_ids[i] = id;
1458 static bool pipe_has_writeback_connector(struct device *dev, struct pipe_arg *pipes,
1461 drmModeConnector *connector;
1464 for (j = 0; j < count; j++) {
1465 struct pipe_arg *pipe = &pipes[j];
1467 for (i = 0; i < pipe->num_cons; i++) {
1468 connector = get_connector_by_id(dev, pipe->con_ids[i]);
1469 if (connector && connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1476 static int pipe_attempt_connector(struct device *dev, drmModeConnector *con,
1477 struct pipe_arg *pipe)
1482 con_str = calloc(8, sizeof(char));
1486 sprintf(con_str, "%d", con->connector_id);
1487 strcpy(pipe->format_str, "XR24");
1488 pipe->fourcc = util_format_fourcc(pipe->format_str);
1490 pipe->con_ids = calloc(1, sizeof(*pipe->con_ids));
1491 pipe->cons = calloc(1, sizeof(*pipe->cons));
1493 if (!pipe->con_ids || !pipe->cons)
1496 pipe->con_ids[0] = con->connector_id;
1497 pipe->cons[0] = (const char*)con_str;
1499 pipe->crtc = pipe_find_crtc(dev, pipe);
1503 pipe->crtc_id = pipe->crtc->crtc->crtc_id;
1505 /* Return the first mode if no preferred. */
1506 pipe->mode = &con->modes[0];
1508 for (i = 0; i < con->count_modes; i++) {
1509 drmModeModeInfo *current_mode = &con->modes[i];
1511 if (current_mode->type & DRM_MODE_TYPE_PREFERRED) {
1512 pipe->mode = current_mode;
1517 sprintf(pipe->mode_str, "%dx%d", pipe->mode->hdisplay, pipe->mode->vdisplay);
1523 free(pipe->con_ids);
1529 static int pipe_find_preferred(struct device *dev, struct pipe_arg **out_pipes)
1531 struct pipe_arg *pipes;
1532 struct resources *res = dev->resources;
1533 drmModeConnector *con = NULL;
1534 int i, connected = 0, attempted = 0;
1536 for (i = 0; i < res->count_connectors; i++) {
1537 con = res->connectors[i].connector;
1538 if (!con || con->connection != DRM_MODE_CONNECTED ||
1539 con->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1544 printf("no connected connector!\n");
1548 pipes = calloc(connected, sizeof(struct pipe_arg));
1552 for (i = 0; i < res->count_connectors && attempted < connected; i++) {
1553 con = res->connectors[i].connector;
1554 if (!con || con->connection != DRM_MODE_CONNECTED)
1557 if (pipe_attempt_connector(dev, con, &pipes[attempted]) < 0) {
1558 printf("failed fetching preferred mode for connector\n");
1568 static struct plane *get_primary_plane_by_crtc(struct device *dev, struct crtc *crtc)
1572 for (i = 0; i < dev->resources->count_planes; i++) {
1573 struct plane *plane = &dev->resources->planes[i];
1574 drmModePlane *ovr = plane->plane;
1578 // XXX: add is_primary_plane and (?) format checks
1580 if (ovr->possible_crtcs & get_crtc_mask(dev, crtc))
1586 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1590 int preferred = count == 0;
1592 for (i = 0; i < count; i++) {
1593 struct pipe_arg *pipe = &pipes[i];
1595 ret = pipe_resolve_connectors(dev, pipe);
1599 ret = pipe_find_crtc_and_mode(dev, pipe);
1604 struct pipe_arg *pipe_args;
1606 count = pipe_find_preferred(dev, &pipe_args);
1608 fprintf(stderr, "can't find any preferred connector/mode.\n");
1614 if (!dev->use_atomic) {
1615 for (i = 0; i < count; i++) {
1616 struct pipe_arg *pipe = &pipes[i];
1618 if (pipe->mode == NULL)
1622 dev->mode.width += pipe->mode->hdisplay;
1623 if (dev->mode.height < pipe->mode->vdisplay)
1624 dev->mode.height = pipe->mode->vdisplay;
1626 /* XXX: Use a clone mode, more like atomic. We could do per
1627 * connector bo/fb, so we don't have the stretched image.
1629 if (dev->mode.width < pipe->mode->hdisplay)
1630 dev->mode.width = pipe->mode->hdisplay;
1631 if (dev->mode.height < pipe->mode->vdisplay)
1632 dev->mode.height = pipe->mode->vdisplay;
1636 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1637 primary_fill, &dev->mode.bo, &dev->mode.fb_id))
1641 for (i = 0; i < count; i++) {
1642 struct pipe_arg *pipe = &pipes[i];
1645 if (pipe->mode == NULL)
1648 printf("setting mode %s-%.2fHz on connectors ",
1649 pipe->mode->name, mode_vrefresh(pipe->mode));
1650 for (j = 0; j < pipe->num_cons; ++j) {
1651 printf("%s, ", pipe->cons[j]);
1652 if (dev->use_atomic)
1653 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc_id);
1655 printf("crtc %d\n", pipe->crtc_id);
1657 if (!dev->use_atomic) {
1658 ret = drmModeSetCrtc(dev->fd, pipe->crtc_id, dev->mode.fb_id,
1659 x, 0, pipe->con_ids, pipe->num_cons,
1662 /* XXX: Actually check if this is needed */
1663 drmModeDirtyFB(dev->fd, dev->mode.fb_id, NULL, 0);
1666 x += pipe->mode->hdisplay;
1669 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1673 set_gamma(dev, pipe->crtc_id, pipe->fourcc);
1675 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
1676 add_property(dev, pipe->crtc_id, "MODE_ID", blob_id);
1677 add_property(dev, pipe->crtc_id, "ACTIVE", 1);
1679 /* By default atomic modeset does not set a primary plane, shrug */
1681 struct plane *plane = get_primary_plane_by_crtc(dev, pipe->crtc);
1682 struct plane_arg plane_args = {
1683 .plane_id = plane->plane->plane_id,
1684 .crtc_id = pipe->crtc_id,
1685 .w = pipe->mode->hdisplay,
1686 .h = pipe->mode->vdisplay,
1688 .format_str = "XR24",
1689 .fourcc = util_format_fourcc(pipe->format_str),
1692 atomic_set_planes(dev, &plane_args, 1, false);
1698 static void writeback_config(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1700 drmModeConnector *connector;
1703 for (j = 0; j < count; j++) {
1704 struct pipe_arg *pipe = &pipes[j];
1706 for (i = 0; i < pipe->num_cons; i++) {
1707 connector = get_connector_by_id(dev, pipe->con_ids[i]);
1708 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
1710 fprintf(stderr, "no mode for writeback\n");
1713 bo_fb_create(dev->fd, pipes[j].fourcc,
1714 pipe->mode->hdisplay, pipe->mode->vdisplay,
1716 &pipe->out_bo, &pipe->out_fb_id);
1717 add_property(dev, pipe->con_ids[i], "WRITEBACK_FB_ID",
1719 add_property(dev, pipe->con_ids[i], "WRITEBACK_OUT_FENCE_PTR",
1720 (uintptr_t)(&dev->writeback_fence_fd));
1726 static int poll_writeback_fence(int fd, int timeout)
1728 struct pollfd fds = { fd, POLLIN };
1732 ret = poll(&fds, 1, timeout);
1734 if (fds.revents & (POLLERR | POLLNVAL))
1738 } else if (ret == 0) {
1742 if (ret == -EINTR || ret == -EAGAIN)
1750 static void dump_output_fb(struct device *dev, struct pipe_arg *pipes, char *dump_path,
1753 drmModeConnector *connector;
1756 for (j = 0; j < count; j++) {
1757 struct pipe_arg *pipe = &pipes[j];
1759 for (i = 0; i < pipe->num_cons; i++) {
1760 connector = get_connector_by_id(dev, pipe->con_ids[i]);
1761 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1762 bo_dump(pipe->out_bo, dump_path);
1767 static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1772 for (i = 0; i < count; i++) {
1773 struct pipe_arg *pipe = &pipes[i];
1775 if (pipe->mode == NULL)
1778 for (j = 0; j < pipe->num_cons; ++j)
1779 add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
1781 add_property(dev, pipe->crtc_id, "MODE_ID", 0);
1782 add_property(dev, pipe->crtc_id, "ACTIVE", 0);
1786 static void clear_mode(struct device *dev)
1788 if (dev->mode.fb_id)
1789 drmModeRmFB(dev->fd, dev->mode.fb_id);
1791 bo_destroy(dev->mode.bo);
1794 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1798 /* set up planes/overlays */
1799 for (i = 0; i < count; i++)
1800 if (set_plane(dev, &p[i]))
1804 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1806 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1814 ret = drmGetCap(dev->fd, DRM_CAP_CURSOR_WIDTH, &value);
1818 ret = drmGetCap(dev->fd, DRM_CAP_CURSOR_HEIGHT, &value);
1823 /* create cursor bo.. just using PATTERN_PLAIN as it has
1826 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1827 offsets, UTIL_PATTERN_PLAIN);
1831 dev->mode.cursor_bo = bo;
1833 for (i = 0; i < count; i++) {
1834 struct pipe_arg *pipe = &pipes[i];
1835 ret = cursor_init(dev->fd, handles[0],
1837 pipe->mode->hdisplay, pipe->mode->vdisplay,
1840 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1849 static void clear_cursors(struct device *dev)
1853 if (dev->mode.cursor_bo)
1854 bo_destroy(dev->mode.cursor_bo);
1857 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1859 unsigned int other_fb_id;
1860 struct bo *other_bo;
1861 drmEventContext evctx;
1865 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1866 UTIL_PATTERN_PLAIN, &other_bo, &other_fb_id))
1869 for (i = 0; i < count; i++) {
1870 struct pipe_arg *pipe = &pipes[i];
1872 if (pipe->mode == NULL)
1875 ret = drmModePageFlip(dev->fd, pipe->crtc_id,
1876 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1879 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1882 gettimeofday(&pipe->start, NULL);
1883 pipe->swap_count = 0;
1884 pipe->fb_id[0] = dev->mode.fb_id;
1885 pipe->fb_id[1] = other_fb_id;
1886 pipe->current_fb_id = other_fb_id;
1889 memset(&evctx, 0, sizeof evctx);
1890 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1891 evctx.vblank_handler = NULL;
1892 evctx.page_flip_handler = page_flip_handler;
1896 struct pollfd pfd[2];
1899 pfd[0].events = POLLIN;
1901 pfd[1].events = POLLIN;
1903 if (poll(pfd, 2, -1) < 0) {
1904 fprintf(stderr, "poll error\n");
1911 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1916 FD_SET(dev->fd, &fds);
1917 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1920 fprintf(stderr, "select timed out or error (ret %d)\n",
1923 } else if (FD_ISSET(0, &fds)) {
1928 drmHandleEvent(dev->fd, &evctx);
1932 drmModeRmFB(dev->fd, other_fb_id);
1933 bo_destroy(other_bo);
1936 #define min(a, b) ((a) < (b) ? (a) : (b))
1938 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1946 pipe->crtc_id = (uint32_t)-1;
1947 strcpy(pipe->format_str, "XR24");
1949 /* Count the number of connectors and allocate them. */
1951 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1956 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1957 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1958 if (pipe->con_ids == NULL || pipe->cons == NULL)
1961 /* Parse the connectors. */
1962 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1963 endp = strpbrk(p, ",@:");
1967 pipe->cons[i] = strndup(p, endp - p);
1973 if (i != pipe->num_cons - 1)
1976 /* Parse the remaining parameters. */
1981 pipe->crtc_id = strtoul(arg, &endp, 10);
1988 /* Search for the vertical refresh or the format. */
1989 p = strpbrk(arg, "-@");
1991 p = arg + strlen(arg);
1992 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1993 strncpy(pipe->mode_str, arg, len);
1994 pipe->mode_str[len] = '\0';
1997 pipe->vrefresh = strtof(p + 1, &endp);
2002 strncpy(pipe->format_str, p + 1, 4);
2003 pipe->format_str[4] = '\0';
2006 pipe->fourcc = util_format_fourcc(pipe->format_str);
2007 if (pipe->fourcc == 0) {
2008 fprintf(stderr, "unknown format %s\n", pipe->format_str);
2015 static int parse_plane(struct plane_arg *plane, const char *p)
2019 plane->plane_id = strtoul(p, &end, 10);
2024 plane->crtc_id = strtoul(p, &end, 10);
2029 plane->w = strtoul(p, &end, 10);
2034 plane->h = strtoul(p, &end, 10);
2036 if (*end == '+' || *end == '-') {
2037 plane->x = strtol(end, &end, 10);
2038 if (*end != '+' && *end != '-')
2040 plane->y = strtol(end, &end, 10);
2042 plane->has_position = true;
2047 plane->scale = strtod(p, &end);
2048 if (plane->scale <= 0.0)
2055 strncpy(plane->format_str, end + 1, 4);
2056 plane->format_str[4] = '\0';
2058 strcpy(plane->format_str, "XR24");
2061 plane->fourcc = util_format_fourcc(plane->format_str);
2062 if (plane->fourcc == 0) {
2063 fprintf(stderr, "unknown format %s\n", plane->format_str);
2070 static int parse_property(struct property_arg *p, const char *arg)
2072 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
2076 p->name[DRM_PROP_NAME_LEN] = '\0';
2081 static void parse_fill_patterns(char *arg)
2083 char *fill = strtok(arg, ",");
2086 primary_fill = util_pattern_enum(fill);
2087 fill = strtok(NULL, ",");
2090 secondary_fill = util_pattern_enum(fill);
2093 static void usage(char *name)
2095 fprintf(stderr, "usage: %s [-acDdefMoPpsCvrw]\n", name);
2097 fprintf(stderr, "\n Query options:\n\n");
2098 fprintf(stderr, "\t-c\tlist connectors\n");
2099 fprintf(stderr, "\t-e\tlist encoders\n");
2100 fprintf(stderr, "\t-f\tlist framebuffers\n");
2101 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
2103 fprintf(stderr, "\n Test options:\n\n");
2104 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
2105 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:[#<mode index>]<mode>[-<vrefresh>][@<format>]\tset a mode\n");
2106 fprintf(stderr, "\t-C\ttest hw cursor\n");
2107 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
2108 fprintf(stderr, "\t-r\tset the preferred mode for all connectors\n");
2109 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
2110 fprintf(stderr, "\t-a \tuse atomic API\n");
2111 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
2112 fprintf(stderr, "\t-o <desired file path> \t Dump writeback output buffer to file\n");
2114 fprintf(stderr, "\n Generic options:\n\n");
2115 fprintf(stderr, "\t-d\tdrop master after mode set\n");
2116 fprintf(stderr, "\t-M module\tuse the given driver\n");
2117 fprintf(stderr, "\t-D device\tuse the given device\n");
2119 fprintf(stderr, "\n\tDefault is to dump all info.\n");
2123 static char optstr[] = "acdD:efF:M:P:ps:Cvrw:o:";
2125 int main(int argc, char **argv)
2130 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
2131 int drop_master = 0;
2133 int test_cursor = 0;
2134 int set_preferred = 0;
2136 char *device = NULL;
2137 char *module = NULL;
2139 unsigned int count = 0, plane_count = 0;
2140 unsigned int prop_count = 0;
2141 struct pipe_arg *pipe_args = NULL;
2142 struct plane_arg *plane_args = NULL;
2143 struct property_arg *prop_args = NULL;
2144 unsigned int args = 0;
2146 char *dump_path = NULL;
2148 memset(&dev, 0, sizeof dev);
2151 while ((c = getopt(argc, argv, optstr)) != -1) {
2157 /* Preserve the default behaviour of dumping all information. */
2165 /* Preserve the default behaviour of dumping all information. */
2178 parse_fill_patterns(optarg);
2182 /* Preserve the default behaviour of dumping all information. */
2189 plane_args = realloc(plane_args,
2190 (plane_count + 1) * sizeof *plane_args);
2191 if (plane_args == NULL) {
2192 fprintf(stderr, "memory allocation failed\n");
2195 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
2197 if (parse_plane(&plane_args[plane_count], optarg) < 0)
2207 pipe_args = realloc(pipe_args,
2208 (count + 1) * sizeof *pipe_args);
2209 if (pipe_args == NULL) {
2210 fprintf(stderr, "memory allocation failed\n");
2213 memset(&pipe_args[count], 0, sizeof(*pipe_args));
2215 if (parse_connector(&pipe_args[count], optarg) < 0)
2230 prop_args = realloc(prop_args,
2231 (prop_count + 1) * sizeof *prop_args);
2232 if (prop_args == NULL) {
2233 fprintf(stderr, "memory allocation failed\n");
2236 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
2238 if (parse_property(&prop_args[prop_count], optarg) < 0)
2249 /* Dump all the details when no* arguments are provided. */
2251 encoders = connectors = crtcs = planes = framebuffers = 1;
2253 if (test_vsync && !count) {
2254 fprintf(stderr, "page flipping requires at least one -s option.\n");
2257 if (set_preferred && count) {
2258 fprintf(stderr, "cannot use -r (preferred) when -s (mode) is set\n");
2262 if (set_preferred && plane_count) {
2263 fprintf(stderr, "cannot use -r (preferred) when -P (plane) is set\n");
2267 dev.fd = util_open(device, module);
2272 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
2273 drmSetClientCap(dev.fd, DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
2275 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
2281 dev.use_atomic = use_atomic;
2283 dev.resources = get_resources(&dev);
2284 if (!dev.resources) {
2289 #define dump_resource(dev, res) if (res) dump_##res(dev)
2291 dump_resource(&dev, encoders);
2292 dump_resource(&dev, connectors);
2293 dump_resource(&dev, crtcs);
2294 dump_resource(&dev, planes);
2295 dump_resource(&dev, framebuffers);
2297 for (i = 0; i < prop_count; ++i)
2298 set_property(&dev, &prop_args[i]);
2300 if (dev.use_atomic) {
2301 dev.req = drmModeAtomicAlloc();
2303 if (set_preferred || (count && plane_count)) {
2306 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2307 if (ret || cap == 0) {
2308 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2312 if (set_preferred || count)
2313 set_mode(&dev, pipe_args, count);
2316 if (!pipe_has_writeback_connector(&dev, pipe_args, count)) {
2317 fprintf(stderr, "No writeback connector found, can not dump.\n");
2321 writeback_config(&dev, pipe_args, count);
2325 atomic_set_planes(&dev, plane_args, plane_count, false);
2327 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2329 fprintf(stderr, "Atomic Commit failed [1]\n");
2334 * Since only writeback connectors have an output fb, this should only be
2335 * called for writeback.
2338 ret = poll_writeback_fence(dev.writeback_fence_fd, 1000);
2340 fprintf(stderr, "Poll for writeback error: %d. Skipping Dump.\n",
2342 dump_output_fb(&dev, pipe_args, dump_path, count);
2346 atomic_test_page_flip(&dev, pipe_args, plane_args, plane_count);
2349 drmDropMaster(dev.fd);
2353 drmModeAtomicFree(dev.req);
2354 dev.req = drmModeAtomicAlloc();
2356 /* XXX: properly teardown the preferred mode/plane state */
2358 atomic_clear_planes(&dev, plane_args, plane_count);
2361 atomic_clear_mode(&dev, pipe_args, count);
2363 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2365 fprintf(stderr, "Atomic Commit failed\n");
2368 atomic_clear_FB(&dev, plane_args, plane_count);
2371 drmModeAtomicFree(dev.req);
2374 fprintf(stderr, "writeback / dump is only supported in atomic mode\n");
2378 if (set_preferred || count || plane_count) {
2381 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2382 if (ret || cap == 0) {
2383 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2387 if (set_preferred || count)
2388 set_mode(&dev, pipe_args, count);
2391 set_planes(&dev, plane_args, plane_count);
2394 set_cursors(&dev, pipe_args, count);
2397 test_page_flip(&dev, pipe_args, count);
2400 drmDropMaster(dev.fd);
2405 clear_cursors(&dev);
2408 clear_planes(&dev, plane_args, plane_count);
2410 if (set_preferred || count)
2415 free_resources(dev.resources);