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;
133 static inline int64_t U642I64(uint64_t val)
135 return (int64_t)*((int64_t *)&val);
138 static float mode_vrefresh(drmModeModeInfo *mode)
140 return mode->clock * 1000.00
141 / (mode->htotal * mode->vtotal);
144 #define bit_name_fn(res) \
145 const char * res##_str(int type) { \
147 const char *sep = ""; \
148 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
149 if (type & (1 << i)) { \
150 printf("%s%s", sep, res##_names[i]); \
157 static const char *mode_type_names[] = {
167 static bit_name_fn(mode_type)
169 static const char *mode_flag_names[] = {
186 static bit_name_fn(mode_flag)
188 static void dump_fourcc(uint32_t fourcc)
197 static void dump_encoders(struct device *dev)
199 drmModeEncoder *encoder;
202 printf("Encoders:\n");
203 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
204 for (i = 0; i < dev->resources->count_encoders; i++) {
205 encoder = dev->resources->encoders[i].encoder;
209 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
212 util_lookup_encoder_type_name(encoder->encoder_type),
213 encoder->possible_crtcs,
214 encoder->possible_clones);
219 static void dump_mode(drmModeModeInfo *mode, int index)
221 printf(" #%i %s %.2f %d %d %d %d %d %d %d %d %d",
236 mode_flag_str(mode->flags);
238 mode_type_str(mode->type);
242 static void dump_blob(struct device *dev, uint32_t blob_id)
245 unsigned char *blob_data;
246 drmModePropertyBlobPtr blob;
248 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
254 blob_data = blob->data;
256 for (i = 0; i < blob->length; i++) {
259 printf("%.2hhx", blob_data[i]);
263 drmModeFreePropertyBlob(blob);
266 static const char *modifier_to_string(uint64_t modifier)
269 case DRM_FORMAT_MOD_INVALID:
271 case DRM_FORMAT_MOD_LINEAR:
273 case I915_FORMAT_MOD_X_TILED:
275 case I915_FORMAT_MOD_Y_TILED:
277 case I915_FORMAT_MOD_Yf_TILED:
279 case I915_FORMAT_MOD_Y_TILED_CCS:
280 return "Y_TILED_CCS";
281 case I915_FORMAT_MOD_Yf_TILED_CCS:
282 return "Yf_TILED_CCS";
283 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
284 return "SAMSUNG_64_32_TILE";
285 case DRM_FORMAT_MOD_VIVANTE_TILED:
286 return "VIVANTE_TILED";
287 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
288 return "VIVANTE_SUPER_TILED";
289 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
290 return "VIVANTE_SPLIT_TILED";
291 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
292 return "VIVANTE_SPLIT_SUPER_TILED";
293 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
294 return "NVIDIA_TEGRA_TILED";
295 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
296 return "NVIDIA_16BX2_BLOCK(0)";
297 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
298 return "NVIDIA_16BX2_BLOCK(1)";
299 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
300 return "NVIDIA_16BX2_BLOCK(2)";
301 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
302 return "NVIDIA_16BX2_BLOCK(3)";
303 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
304 return "NVIDIA_16BX2_BLOCK(4)";
305 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
306 return "NVIDIA_16BX2_BLOCK(5)";
307 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
308 return "MOD_BROADCOM_VC4_T_TILED";
309 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
310 return "QCOM_COMPRESSED";
312 return "(UNKNOWN MODIFIER)";
316 static void dump_in_formats(struct device *dev, uint32_t blob_id)
319 drmModePropertyBlobPtr blob;
320 struct drm_format_modifier_blob *header;
322 struct drm_format_modifier *modifiers;
324 printf("\t\tin_formats blob decoded:\n");
325 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
332 formats = (uint32_t *) ((char *) header + header->formats_offset);
333 modifiers = (struct drm_format_modifier *)
334 ((char *) header + header->modifiers_offset);
336 for (i = 0; i < header->count_formats; i++) {
338 dump_fourcc(formats[i]);
340 for (j = 0; j < header->count_modifiers; j++) {
341 uint64_t mask = 1ULL << i;
342 if (modifiers[j].formats & mask)
343 printf(" %s", modifier_to_string(modifiers[j].modifier));
348 drmModeFreePropertyBlob(blob);
351 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
352 uint32_t prop_id, uint64_t value)
355 printf("\t%d", prop_id);
361 printf(" %s:\n", prop->name);
363 printf("\t\tflags:");
364 if (prop->flags & DRM_MODE_PROP_PENDING)
366 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
367 printf(" immutable");
368 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
369 printf(" signed range");
370 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
372 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
374 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
376 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
378 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
382 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
383 printf("\t\tvalues:");
384 for (i = 0; i < prop->count_values; i++)
385 printf(" %"PRId64, U642I64(prop->values[i]));
389 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
390 printf("\t\tvalues:");
391 for (i = 0; i < prop->count_values; i++)
392 printf(" %"PRIu64, prop->values[i]);
396 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
397 printf("\t\tenums:");
398 for (i = 0; i < prop->count_enums; i++)
399 printf(" %s=%llu", prop->enums[i].name,
400 prop->enums[i].value);
402 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
403 printf("\t\tvalues:");
404 for (i = 0; i < prop->count_enums; i++)
405 printf(" %s=0x%llx", prop->enums[i].name,
406 (1LL << prop->enums[i].value));
409 assert(prop->count_enums == 0);
412 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
413 printf("\t\tblobs:\n");
414 for (i = 0; i < prop->count_blobs; i++)
415 dump_blob(dev, prop->blob_ids[i]);
418 assert(prop->count_blobs == 0);
421 printf("\t\tvalue:");
422 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
423 dump_blob(dev, value);
424 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
425 printf(" %"PRId64"\n", value);
427 printf(" %"PRIu64"\n", value);
429 if (strcmp(prop->name, "IN_FORMATS") == 0)
430 dump_in_formats(dev, value);
433 static void dump_connectors(struct device *dev)
437 printf("Connectors:\n");
438 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
439 for (i = 0; i < dev->resources->count_connectors; i++) {
440 struct connector *_connector = &dev->resources->connectors[i];
441 drmModeConnector *connector = _connector->connector;
445 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
446 connector->connector_id,
447 connector->encoder_id,
448 util_lookup_connector_status_name(connector->connection),
450 connector->mmWidth, connector->mmHeight,
451 connector->count_modes);
453 for (j = 0; j < connector->count_encoders; j++)
454 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
457 if (connector->count_modes) {
459 printf("\tindex name refresh (Hz) hdisp hss hse htot vdisp "
461 for (j = 0; j < connector->count_modes; j++)
462 dump_mode(&connector->modes[j], j);
465 if (_connector->props) {
467 for (j = 0; j < (int)_connector->props->count_props; j++)
468 dump_prop(dev, _connector->props_info[j],
469 _connector->props->props[j],
470 _connector->props->prop_values[j]);
476 static void dump_crtcs(struct device *dev)
482 printf("id\tfb\tpos\tsize\n");
483 for (i = 0; i < dev->resources->count_crtcs; i++) {
484 struct crtc *_crtc = &dev->resources->crtcs[i];
485 drmModeCrtc *crtc = _crtc->crtc;
489 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
493 crtc->width, crtc->height);
494 dump_mode(&crtc->mode, 0);
498 for (j = 0; j < _crtc->props->count_props; j++)
499 dump_prop(dev, _crtc->props_info[j],
500 _crtc->props->props[j],
501 _crtc->props->prop_values[j]);
503 printf(" no properties found\n");
509 static void dump_framebuffers(struct device *dev)
514 printf("Frame buffers:\n");
515 printf("id\tsize\tpitch\n");
516 for (i = 0; i < dev->resources->count_fbs; i++) {
517 fb = dev->resources->fbs[i].fb;
521 printf("%u\t(%ux%u)\t%u\n",
523 fb->width, fb->height,
529 static void dump_planes(struct device *dev)
534 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
536 for (i = 0; i < dev->resources->count_planes; i++) {
537 struct plane *plane = &dev->resources->planes[i];
538 drmModePlane *ovr = plane->plane;
542 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
543 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
544 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
545 ovr->gamma_size, ovr->possible_crtcs);
547 if (!ovr->count_formats)
551 for (j = 0; j < ovr->count_formats; j++)
552 dump_fourcc(ovr->formats[j]);
557 for (j = 0; j < plane->props->count_props; j++)
558 dump_prop(dev, plane->props_info[j],
559 plane->props->props[j],
560 plane->props->prop_values[j]);
562 printf(" no properties found\n");
570 static void free_resources(struct resources *res)
577 #define free_resource(_res, type, Type) \
579 if (!(_res)->type##s) \
581 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
582 if (!(_res)->type##s[i].type) \
584 drmModeFree##Type((_res)->type##s[i].type); \
586 free((_res)->type##s); \
589 #define free_properties(_res, type) \
591 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
593 for (j = 0; j < res->type##s[i].props->count_props; ++j)\
594 drmModeFreeProperty(res->type##s[i].props_info[j]);\
595 free(res->type##s[i].props_info); \
596 drmModeFreeObjectProperties(res->type##s[i].props); \
600 free_properties(res, plane);
601 free_resource(res, plane, Plane);
603 free_properties(res, connector);
604 free_properties(res, crtc);
606 for (i = 0; i < res->count_connectors; i++)
607 free(res->connectors[i].name);
609 free_resource(res, fb, FB);
610 free_resource(res, connector, Connector);
611 free_resource(res, encoder, Encoder);
612 free_resource(res, crtc, Crtc);
617 static struct resources *get_resources(struct device *dev)
620 drmModePlaneRes *plane_res;
621 struct resources *res;
624 res = calloc(1, sizeof(*res));
628 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
630 _res = drmModeGetResources(dev->fd);
632 fprintf(stderr, "drmModeGetResources failed: %s\n",
638 res->count_crtcs = _res->count_crtcs;
639 res->count_encoders = _res->count_encoders;
640 res->count_connectors = _res->count_connectors;
641 res->count_fbs = _res->count_fbs;
643 res->crtcs = calloc(res->count_crtcs, sizeof(*res->crtcs));
644 res->encoders = calloc(res->count_encoders, sizeof(*res->encoders));
645 res->connectors = calloc(res->count_connectors, sizeof(*res->connectors));
646 res->fbs = calloc(res->count_fbs, sizeof(*res->fbs));
648 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs) {
649 drmModeFreeResources(_res);
653 #define get_resource(_res, __res, type, Type) \
655 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
656 uint32_t type##id = (__res)->type##s[i]; \
657 (_res)->type##s[i].type = \
658 drmModeGet##Type(dev->fd, type##id); \
659 if (!(_res)->type##s[i].type) \
660 fprintf(stderr, "could not get %s %i: %s\n", \
666 get_resource(res, _res, crtc, Crtc);
667 get_resource(res, _res, encoder, Encoder);
668 get_resource(res, _res, connector, Connector);
669 get_resource(res, _res, fb, FB);
671 drmModeFreeResources(_res);
673 /* Set the name of all connectors based on the type name and the per-type ID. */
674 for (i = 0; i < res->count_connectors; i++) {
675 struct connector *connector = &res->connectors[i];
676 drmModeConnector *conn = connector->connector;
679 num = asprintf(&connector->name, "%s-%u",
680 util_lookup_connector_type_name(conn->connector_type),
681 conn->connector_type_id);
686 #define get_properties(_res, type, Type) \
688 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
689 struct type *obj = &res->type##s[i]; \
692 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
693 DRM_MODE_OBJECT_##Type); \
696 "could not get %s %i properties: %s\n", \
697 #type, obj->type->type##_id, \
701 obj->props_info = calloc(obj->props->count_props, \
702 sizeof(*obj->props_info)); \
703 if (!obj->props_info) \
705 for (j = 0; j < obj->props->count_props; ++j) \
706 obj->props_info[j] = \
707 drmModeGetProperty(dev->fd, obj->props->props[j]); \
711 get_properties(res, crtc, CRTC);
712 get_properties(res, connector, CONNECTOR);
714 for (i = 0; i < res->count_crtcs; ++i)
715 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
717 plane_res = drmModeGetPlaneResources(dev->fd);
719 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
724 res->count_planes = plane_res->count_planes;
726 res->planes = calloc(res->count_planes, sizeof(*res->planes));
728 drmModeFreePlaneResources(plane_res);
732 get_resource(res, plane_res, plane, Plane);
733 drmModeFreePlaneResources(plane_res);
734 get_properties(res, plane, PLANE);
743 static struct crtc *get_crtc_by_id(struct device *dev, uint32_t id)
747 for (i = 0; i < dev->resources->count_crtcs; ++i) {
748 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
749 if (crtc && crtc->crtc_id == id)
750 return &dev->resources->crtcs[i];
756 static uint32_t get_crtc_mask(struct device *dev, struct crtc *crtc)
760 for (i = 0; i < (unsigned int)dev->resources->count_crtcs; i++) {
761 if (crtc->crtc->crtc_id == dev->resources->crtcs[i].crtc->crtc_id)
764 /* Unreachable: crtc->crtc is one of resources->crtcs[] */
765 /* Don't return zero or static analysers will complain */
770 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
772 struct connector *connector;
775 for (i = 0; i < dev->resources->count_connectors; i++) {
776 connector = &dev->resources->connectors[i];
778 if (strcmp(connector->name, name) == 0)
779 return connector->connector;
785 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
787 drmModeConnector *connector;
790 for (i = 0; i < dev->resources->count_connectors; i++) {
791 connector = dev->resources->connectors[i].connector;
792 if (connector && connector->connector_id == id)
799 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
801 drmModeEncoder *encoder;
804 for (i = 0; i < dev->resources->count_encoders; i++) {
805 encoder = dev->resources->encoders[i].encoder;
806 if (encoder && encoder->encoder_id == id)
813 /* -----------------------------------------------------------------------------
818 * Mode setting with the kernel interfaces is a bit of a chore.
819 * First you have to find the connector in question and make sure the
820 * requested mode is available.
821 * Then you need to find the encoder attached to that connector so you
822 * can bind it with a free crtc.
827 unsigned int num_cons;
833 drmModeModeInfo *mode;
835 unsigned int fb_id[2], current_fb_id;
836 struct timeval start;
842 uint32_t plane_id; /* the id of plane to use */
843 uint32_t crtc_id; /* the id of CRTC to bind to */
849 unsigned int old_fb_id;
852 char format_str[5]; /* need to leave room for terminating \0 */
856 static drmModeModeInfo *
857 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
858 const float vrefresh)
860 drmModeConnector *connector;
861 drmModeModeInfo *mode;
864 connector = get_connector_by_id(dev, con_id);
865 if (!connector || !connector->count_modes)
869 if (mode_str[0] == '#') {
870 int index = atoi(mode_str + 1);
872 if (index >= connector->count_modes || index < 0)
874 return &connector->modes[index];
878 for (i = 0; i < connector->count_modes; i++) {
879 mode = &connector->modes[i];
880 if (!strcmp(mode->name, mode_str)) {
881 /* If the vertical refresh frequency is not specified
882 * then return the first mode that match with the name.
883 * Else, return the mode that match the name and
884 * the specified vertical refresh frequency.
888 else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005)
896 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
898 uint32_t possible_crtcs = ~0;
899 uint32_t active_crtcs = 0;
900 unsigned int crtc_idx;
904 for (i = 0; i < pipe->num_cons; ++i) {
905 uint32_t crtcs_for_connector = 0;
906 drmModeConnector *connector;
907 drmModeEncoder *encoder;
910 connector = get_connector_by_id(dev, pipe->con_ids[i]);
914 for (j = 0; j < connector->count_encoders; ++j) {
915 encoder = get_encoder_by_id(dev, connector->encoders[j]);
919 crtcs_for_connector |= encoder->possible_crtcs;
920 crtc = get_crtc_by_id(dev, encoder->crtc_id);
923 active_crtcs |= get_crtc_mask(dev, crtc);
926 possible_crtcs &= crtcs_for_connector;
932 /* Return the first possible and active CRTC if one exists, or the first
933 * possible CRTC otherwise.
935 if (possible_crtcs & active_crtcs)
936 crtc_idx = ffs(possible_crtcs & active_crtcs);
938 crtc_idx = ffs(possible_crtcs);
940 return &dev->resources->crtcs[crtc_idx - 1];
943 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
945 drmModeModeInfo *mode = NULL;
950 for (i = 0; i < (int)pipe->num_cons; i++) {
951 mode = connector_find_mode(dev, pipe->con_ids[i],
952 pipe->mode_str, pipe->vrefresh);
956 "failed to find mode "
957 "\"%s-%.2fHz\" for connector %s\n",
958 pipe->mode_str, pipe->vrefresh, pipe->cons[i]);
961 "failed to find mode \"%s\" for connector %s\n",
962 pipe->mode_str, pipe->cons[i]);
967 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
968 * locate a CRTC that can be attached to all the connectors.
970 if (pipe->crtc_id != (uint32_t)-1) {
971 pipe->crtc = get_crtc_by_id(dev, pipe->crtc_id);
973 pipe->crtc = pipe_find_crtc(dev, pipe);
974 pipe->crtc_id = pipe->crtc->crtc->crtc_id;
978 fprintf(stderr, "failed to find CRTC for pipe\n");
983 pipe->crtc->mode = mode;
988 /* -----------------------------------------------------------------------------
992 struct property_arg {
995 char name[DRM_PROP_NAME_LEN+1];
1001 static bool set_property(struct device *dev, struct property_arg *p)
1003 drmModeObjectProperties *props = NULL;
1004 drmModePropertyRes **props_info = NULL;
1005 const char *obj_type;
1012 #define find_object(_res, type, Type) \
1014 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \
1015 struct type *obj = &(_res)->type##s[i]; \
1016 if (obj->type->type##_id != p->obj_id) \
1018 p->obj_type = DRM_MODE_OBJECT_##Type; \
1020 props = obj->props; \
1021 props_info = obj->props_info; \
1025 find_object(dev->resources, crtc, CRTC);
1026 if (p->obj_type == 0)
1027 find_object(dev->resources, connector, CONNECTOR);
1028 if (p->obj_type == 0)
1029 find_object(dev->resources, plane, PLANE);
1030 if (p->obj_type == 0) {
1031 fprintf(stderr, "Object %i not found, can't set property\n",
1037 fprintf(stderr, "%s %i has no properties\n",
1038 obj_type, p->obj_id);
1042 for (i = 0; i < (int)props->count_props; ++i) {
1045 if (strcmp(props_info[i]->name, p->name) == 0)
1049 if (i == (int)props->count_props) {
1051 fprintf(stderr, "%s %i has no %s property\n",
1052 obj_type, p->obj_id, p->name);
1056 p->prop_id = props->props[i];
1058 if (!dev->use_atomic)
1059 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1060 p->prop_id, p->value);
1062 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
1065 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1066 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1071 /* -------------------------------------------------------------------------- */
1074 page_flip_handler(int fd, unsigned int frame,
1075 unsigned int sec, unsigned int usec, void *data)
1077 struct pipe_arg *pipe;
1078 unsigned int new_fb_id;
1083 if (pipe->current_fb_id == pipe->fb_id[0])
1084 new_fb_id = pipe->fb_id[1];
1086 new_fb_id = pipe->fb_id[0];
1088 drmModePageFlip(fd, pipe->crtc_id, new_fb_id,
1089 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1090 pipe->current_fb_id = new_fb_id;
1092 if (pipe->swap_count == 60) {
1093 gettimeofday(&end, NULL);
1094 t = end.tv_sec + end.tv_usec * 1e-6 -
1095 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1096 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1097 pipe->swap_count = 0;
1102 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1106 for (i = 0; i < ovr->count_formats; ++i) {
1107 if (ovr->formats[i] == fmt)
1114 static void add_property(struct device *dev, uint32_t obj_id,
1115 const char *name, uint64_t value)
1117 struct property_arg p;
1120 strcpy(p.name, name);
1123 set_property(dev, &p);
1126 static bool add_property_optional(struct device *dev, uint32_t obj_id,
1127 const char *name, uint64_t value)
1129 struct property_arg p;
1132 strcpy(p.name, name);
1136 return set_property(dev, &p);
1139 static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
1141 unsigned blob_id = 0;
1142 /* TODO: support 1024-sized LUTs, when the use-case arises */
1143 struct drm_color_lut gamma_lut[256];
1146 if (fourcc == DRM_FORMAT_C8) {
1147 /* TODO: Add C8 support for more patterns */
1148 util_smpte_c8_gamma(256, gamma_lut);
1149 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
1151 for (i = 0; i < 256; i++) {
1153 gamma_lut[i].green =
1154 gamma_lut[i].blue = i << 8;
1158 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
1159 add_property_optional(dev, crtc_id, "CTM", 0);
1160 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
1161 uint16_t r[256], g[256], b[256];
1163 for (i = 0; i < 256; i++) {
1164 r[i] = gamma_lut[i].red;
1165 g[i] = gamma_lut[i].green;
1166 b[i] = gamma_lut[i].blue;
1169 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
1171 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
1176 bo_fb_create(int fd, unsigned int fourcc, const uint32_t w, const uint32_t h,
1177 enum util_fill_pattern pat, struct bo **out_bo, unsigned int *out_fb_id)
1179 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1183 bo = bo_create(fd, fourcc, w, h, handles, pitches, offsets, pat);
1188 if (drmModeAddFB2(fd, w, h, fourcc, handles, pitches, offsets, &fb_id, 0)) {
1189 fprintf(stderr, "failed to add fb (%ux%u): %s\n", w, h, strerror(errno));
1198 static int atomic_set_plane(struct device *dev, struct plane_arg *p,
1199 int pattern, bool update)
1201 struct bo *plane_bo;
1202 int crtc_x, crtc_y, crtc_w, crtc_h;
1203 struct crtc *crtc = NULL;
1204 unsigned int old_fb_id;
1206 /* Find an unused plane which can be connected to our CRTC. Find the
1207 * CRTC index first, then iterate over available planes.
1209 crtc = get_crtc_by_id(dev, p->crtc_id);
1211 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1216 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
1217 p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
1219 plane_bo = p->old_bo;
1223 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h,
1224 pattern, &plane_bo, &p->fb_id))
1230 old_fb_id = p->fb_id;
1231 p->old_fb_id = old_fb_id;
1233 crtc_w = p->w * p->scale;
1234 crtc_h = p->h * p->scale;
1235 if (!p->has_position) {
1236 /* Default to the middle of the screen */
1237 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1238 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1244 add_property(dev, p->plane_id, "FB_ID", p->fb_id);
1245 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
1246 add_property(dev, p->plane_id, "SRC_X", 0);
1247 add_property(dev, p->plane_id, "SRC_Y", 0);
1248 add_property(dev, p->plane_id, "SRC_W", p->w << 16);
1249 add_property(dev, p->plane_id, "SRC_H", p->h << 16);
1250 add_property(dev, p->plane_id, "CRTC_X", crtc_x);
1251 add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
1252 add_property(dev, p->plane_id, "CRTC_W", crtc_w);
1253 add_property(dev, p->plane_id, "CRTC_H", crtc_h);
1258 static int set_plane(struct device *dev, struct plane_arg *p)
1262 int crtc_x, crtc_y, crtc_w, crtc_h;
1263 struct crtc *crtc = NULL;
1264 unsigned int i, crtc_mask;
1266 /* Find an unused plane which can be connected to our CRTC. Find the
1267 * CRTC index first, then iterate over available planes.
1269 crtc = get_crtc_by_id(dev, p->crtc_id);
1271 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1274 crtc_mask = get_crtc_mask(dev, crtc);
1275 plane_id = p->plane_id;
1277 for (i = 0; i < dev->resources->count_planes; i++) {
1278 ovr = dev->resources->planes[i].plane;
1282 if (plane_id && plane_id != ovr->plane_id)
1285 if (!format_support(ovr, p->fourcc))
1288 if ((ovr->possible_crtcs & crtc_mask) &&
1289 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1290 plane_id = ovr->plane_id;
1295 if (i == dev->resources->count_planes) {
1296 fprintf(stderr, "no unused plane available for CRTC %u\n",
1301 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1302 p->w, p->h, p->format_str, plane_id);
1304 /* just use single plane format for now.. */
1305 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h,
1306 secondary_fill, &p->bo, &p->fb_id))
1309 crtc_w = p->w * p->scale;
1310 crtc_h = p->h * p->scale;
1311 if (!p->has_position) {
1312 /* Default to the middle of the screen */
1313 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1314 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1320 /* note src coords (last 4 args) are in Q16 format */
1321 if (drmModeSetPlane(dev->fd, plane_id, p->crtc_id, p->fb_id,
1322 0, crtc_x, crtc_y, crtc_w, crtc_h,
1323 0, 0, p->w << 16, p->h << 16)) {
1324 fprintf(stderr, "failed to enable plane: %s\n",
1329 ovr->crtc_id = p->crtc_id;
1334 static void atomic_set_planes(struct device *dev, struct plane_arg *p,
1335 unsigned int count, bool update)
1337 unsigned int i, pattern = primary_fill;
1340 for (i = 0; i < count; i++) {
1342 pattern = secondary_fill;
1344 set_gamma(dev, p[i].crtc_id, p[i].fourcc);
1346 if (atomic_set_plane(dev, &p[i], pattern, update))
1351 static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1355 for (i = 0; i < count; i++) {
1356 add_property(dev, p[i].plane_id, "FB_ID", 0);
1357 add_property(dev, p[i].plane_id, "CRTC_ID", 0);
1358 add_property(dev, p[i].plane_id, "SRC_X", 0);
1359 add_property(dev, p[i].plane_id, "SRC_Y", 0);
1360 add_property(dev, p[i].plane_id, "SRC_W", 0);
1361 add_property(dev, p[i].plane_id, "SRC_H", 0);
1362 add_property(dev, p[i].plane_id, "CRTC_X", 0);
1363 add_property(dev, p[i].plane_id, "CRTC_Y", 0);
1364 add_property(dev, p[i].plane_id, "CRTC_W", 0);
1365 add_property(dev, p[i].plane_id, "CRTC_H", 0);
1369 static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
1373 for (i = 0; i < count; i++) {
1375 drmModeRmFB(dev->fd, p[i].fb_id);
1378 if (p[i].old_fb_id) {
1379 drmModeRmFB(dev->fd, p[i].old_fb_id);
1383 bo_destroy(p[i].bo);
1387 bo_destroy(p[i].old_bo);
1394 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1398 for (i = 0; i < count; i++) {
1400 drmModeRmFB(dev->fd, p[i].fb_id);
1402 bo_destroy(p[i].bo);
1406 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1408 drmModeConnector *connector;
1413 for (i = 0; i < pipe->num_cons; i++) {
1414 id = strtoul(pipe->cons[i], &endp, 10);
1415 if (endp == pipe->cons[i]) {
1416 connector = get_connector_by_name(dev, pipe->cons[i]);
1418 fprintf(stderr, "no connector named '%s'\n",
1423 id = connector->connector_id;
1426 pipe->con_ids[i] = id;
1432 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1437 for (i = 0; i < count; i++) {
1438 struct pipe_arg *pipe = &pipes[i];
1440 ret = pipe_find_crtc_and_mode(dev, pipe);
1445 if (!dev->use_atomic) {
1446 for (i = 0; i < count; i++) {
1447 struct pipe_arg *pipe = &pipes[i];
1449 if (pipe->mode == NULL)
1452 dev->mode.width += pipe->mode->hdisplay;
1453 if (dev->mode.height < pipe->mode->vdisplay)
1454 dev->mode.height = pipe->mode->vdisplay;
1457 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1458 primary_fill, &dev->mode.bo, &dev->mode.fb_id))
1462 for (i = 0; i < count; i++) {
1463 struct pipe_arg *pipe = &pipes[i];
1466 if (pipe->mode == NULL)
1469 printf("setting mode %s-%.2fHz on connectors ",
1470 pipe->mode->name, mode_vrefresh(pipe->mode));
1471 for (j = 0; j < pipe->num_cons; ++j) {
1472 printf("%s, ", pipe->cons[j]);
1473 if (dev->use_atomic)
1474 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc_id);
1476 printf("crtc %d\n", pipe->crtc_id);
1478 if (!dev->use_atomic) {
1479 ret = drmModeSetCrtc(dev->fd, pipe->crtc_id, dev->mode.fb_id,
1480 x, 0, pipe->con_ids, pipe->num_cons,
1483 /* XXX: Actually check if this is needed */
1484 drmModeDirtyFB(dev->fd, dev->mode.fb_id, NULL, 0);
1486 x += pipe->mode->hdisplay;
1489 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1493 set_gamma(dev, pipe->crtc_id, pipe->fourcc);
1495 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
1496 add_property(dev, pipe->crtc_id, "MODE_ID", blob_id);
1497 add_property(dev, pipe->crtc_id, "ACTIVE", 1);
1502 static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1507 for (i = 0; i < count; i++) {
1508 struct pipe_arg *pipe = &pipes[i];
1510 if (pipe->mode == NULL)
1513 for (j = 0; j < pipe->num_cons; ++j)
1514 add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
1516 add_property(dev, pipe->crtc_id, "MODE_ID", 0);
1517 add_property(dev, pipe->crtc_id, "ACTIVE", 0);
1521 static void clear_mode(struct device *dev)
1523 if (dev->mode.fb_id)
1524 drmModeRmFB(dev->fd, dev->mode.fb_id);
1526 bo_destroy(dev->mode.bo);
1529 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1533 /* set up planes/overlays */
1534 for (i = 0; i < count; i++)
1535 if (set_plane(dev, &p[i]))
1539 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1541 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1546 /* maybe make cursor width/height configurable some day */
1550 /* create cursor bo.. just using PATTERN_PLAIN as it has
1553 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1554 offsets, UTIL_PATTERN_PLAIN);
1558 dev->mode.cursor_bo = bo;
1560 for (i = 0; i < count; i++) {
1561 struct pipe_arg *pipe = &pipes[i];
1562 ret = cursor_init(dev->fd, handles[0],
1564 pipe->mode->hdisplay, pipe->mode->vdisplay,
1567 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1576 static void clear_cursors(struct device *dev)
1580 if (dev->mode.cursor_bo)
1581 bo_destroy(dev->mode.cursor_bo);
1584 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1586 unsigned int other_fb_id;
1587 struct bo *other_bo;
1588 drmEventContext evctx;
1592 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
1593 UTIL_PATTERN_PLAIN, &other_bo, &other_fb_id))
1596 for (i = 0; i < count; i++) {
1597 struct pipe_arg *pipe = &pipes[i];
1599 if (pipe->mode == NULL)
1602 ret = drmModePageFlip(dev->fd, pipe->crtc_id,
1603 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1606 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1609 gettimeofday(&pipe->start, NULL);
1610 pipe->swap_count = 0;
1611 pipe->fb_id[0] = dev->mode.fb_id;
1612 pipe->fb_id[1] = other_fb_id;
1613 pipe->current_fb_id = other_fb_id;
1616 memset(&evctx, 0, sizeof evctx);
1617 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1618 evctx.vblank_handler = NULL;
1619 evctx.page_flip_handler = page_flip_handler;
1623 struct pollfd pfd[2];
1626 pfd[0].events = POLLIN;
1628 pfd[1].events = POLLIN;
1630 if (poll(pfd, 2, -1) < 0) {
1631 fprintf(stderr, "poll error\n");
1638 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1643 FD_SET(dev->fd, &fds);
1644 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1647 fprintf(stderr, "select timed out or error (ret %d)\n",
1650 } else if (FD_ISSET(0, &fds)) {
1655 drmHandleEvent(dev->fd, &evctx);
1659 drmModeRmFB(dev->fd, other_fb_id);
1660 bo_destroy(other_bo);
1663 #define min(a, b) ((a) < (b) ? (a) : (b))
1665 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1673 pipe->crtc_id = (uint32_t)-1;
1674 strcpy(pipe->format_str, "XR24");
1676 /* Count the number of connectors and allocate them. */
1678 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1683 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1684 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1685 if (pipe->con_ids == NULL || pipe->cons == NULL)
1688 /* Parse the connectors. */
1689 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1690 endp = strpbrk(p, ",@:");
1694 pipe->cons[i] = strndup(p, endp - p);
1700 if (i != pipe->num_cons - 1)
1703 /* Parse the remaining parameters. */
1708 pipe->crtc_id = strtoul(arg, &endp, 10);
1715 /* Search for the vertical refresh or the format. */
1716 p = strpbrk(arg, "-@");
1718 p = arg + strlen(arg);
1719 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1720 strncpy(pipe->mode_str, arg, len);
1721 pipe->mode_str[len] = '\0';
1724 pipe->vrefresh = strtof(p + 1, &endp);
1729 strncpy(pipe->format_str, p + 1, 4);
1730 pipe->format_str[4] = '\0';
1733 pipe->fourcc = util_format_fourcc(pipe->format_str);
1734 if (pipe->fourcc == 0) {
1735 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1742 static int parse_plane(struct plane_arg *plane, const char *p)
1746 plane->plane_id = strtoul(p, &end, 10);
1751 plane->crtc_id = strtoul(p, &end, 10);
1756 plane->w = strtoul(p, &end, 10);
1761 plane->h = strtoul(p, &end, 10);
1763 if (*end == '+' || *end == '-') {
1764 plane->x = strtol(end, &end, 10);
1765 if (*end != '+' && *end != '-')
1767 plane->y = strtol(end, &end, 10);
1769 plane->has_position = true;
1774 plane->scale = strtod(p, &end);
1775 if (plane->scale <= 0.0)
1782 strncpy(plane->format_str, end + 1, 4);
1783 plane->format_str[4] = '\0';
1785 strcpy(plane->format_str, "XR24");
1788 plane->fourcc = util_format_fourcc(plane->format_str);
1789 if (plane->fourcc == 0) {
1790 fprintf(stderr, "unknown format %s\n", plane->format_str);
1797 static int parse_property(struct property_arg *p, const char *arg)
1799 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1803 p->name[DRM_PROP_NAME_LEN] = '\0';
1808 static void parse_fill_patterns(char *arg)
1810 char *fill = strtok(arg, ",");
1813 primary_fill = util_pattern_enum(fill);
1814 fill = strtok(NULL, ",");
1817 secondary_fill = util_pattern_enum(fill);
1820 static void usage(char *name)
1822 fprintf(stderr, "usage: %s [-acDdefMPpsCvw]\n", name);
1824 fprintf(stderr, "\n Query options:\n\n");
1825 fprintf(stderr, "\t-c\tlist connectors\n");
1826 fprintf(stderr, "\t-e\tlist encoders\n");
1827 fprintf(stderr, "\t-f\tlist framebuffers\n");
1828 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1830 fprintf(stderr, "\n Test options:\n\n");
1831 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1832 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:[#<mode index>]<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1833 fprintf(stderr, "\t-C\ttest hw cursor\n");
1834 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1835 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1836 fprintf(stderr, "\t-a \tuse atomic API\n");
1837 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
1839 fprintf(stderr, "\n Generic options:\n\n");
1840 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1841 fprintf(stderr, "\t-M module\tuse the given driver\n");
1842 fprintf(stderr, "\t-D device\tuse the given device\n");
1844 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1848 static char optstr[] = "acdD:efF:M:P:ps:Cvw:";
1850 int main(int argc, char **argv)
1855 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1856 int drop_master = 0;
1858 int test_cursor = 0;
1860 char *device = NULL;
1861 char *module = NULL;
1863 unsigned int count = 0, plane_count = 0;
1864 unsigned int prop_count = 0;
1865 struct pipe_arg *pipe_args = NULL;
1866 struct plane_arg *plane_args = NULL;
1867 struct property_arg *prop_args = NULL;
1868 unsigned int args = 0;
1871 memset(&dev, 0, sizeof dev);
1874 while ((c = getopt(argc, argv, optstr)) != -1) {
1880 /* Preserve the default behaviour of dumping all information. */
1888 /* Preserve the default behaviour of dumping all information. */
1901 parse_fill_patterns(optarg);
1905 /* Preserve the default behaviour of dumping all information. */
1909 plane_args = realloc(plane_args,
1910 (plane_count + 1) * sizeof *plane_args);
1911 if (plane_args == NULL) {
1912 fprintf(stderr, "memory allocation failed\n");
1915 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1917 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1927 pipe_args = realloc(pipe_args,
1928 (count + 1) * sizeof *pipe_args);
1929 if (pipe_args == NULL) {
1930 fprintf(stderr, "memory allocation failed\n");
1933 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1935 if (parse_connector(&pipe_args[count], optarg) < 0)
1947 prop_args = realloc(prop_args,
1948 (prop_count + 1) * sizeof *prop_args);
1949 if (prop_args == NULL) {
1950 fprintf(stderr, "memory allocation failed\n");
1953 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1955 if (parse_property(&prop_args[prop_count], optarg) < 0)
1966 /* Dump all the details when no* arguments are provided. */
1968 encoders = connectors = crtcs = planes = framebuffers = 1;
1970 if (test_vsync && !count) {
1971 fprintf(stderr, "page flipping requires at least one -s option.\n");
1975 dev.fd = util_open(device, module);
1980 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
1982 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
1988 dev.use_atomic = use_atomic;
1990 dev.resources = get_resources(&dev);
1991 if (!dev.resources) {
1996 for (i = 0; i < count; i++) {
1997 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1998 free_resources(dev.resources);
2004 #define dump_resource(dev, res) if (res) dump_##res(dev)
2006 dump_resource(&dev, encoders);
2007 dump_resource(&dev, connectors);
2008 dump_resource(&dev, crtcs);
2009 dump_resource(&dev, planes);
2010 dump_resource(&dev, framebuffers);
2012 for (i = 0; i < prop_count; ++i)
2013 set_property(&dev, &prop_args[i]);
2015 if (dev.use_atomic) {
2016 dev.req = drmModeAtomicAlloc();
2018 if (count && plane_count) {
2021 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2022 if (ret || cap == 0) {
2023 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2027 set_mode(&dev, pipe_args, count);
2028 atomic_set_planes(&dev, plane_args, plane_count, false);
2030 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2032 fprintf(stderr, "Atomic Commit failed [1]\n");
2036 gettimeofday(&pipe_args->start, NULL);
2037 pipe_args->swap_count = 0;
2039 while (test_vsync) {
2040 drmModeAtomicFree(dev.req);
2041 dev.req = drmModeAtomicAlloc();
2042 atomic_set_planes(&dev, plane_args, plane_count, true);
2044 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2046 fprintf(stderr, "Atomic Commit failed [2]\n");
2050 pipe_args->swap_count++;
2051 if (pipe_args->swap_count == 60) {
2055 gettimeofday(&end, NULL);
2056 t = end.tv_sec + end.tv_usec * 1e-6 -
2057 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
2058 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
2059 pipe_args->swap_count = 0;
2060 pipe_args->start = end;
2065 drmDropMaster(dev.fd);
2069 drmModeAtomicFree(dev.req);
2070 dev.req = drmModeAtomicAlloc();
2072 atomic_clear_mode(&dev, pipe_args, count);
2073 atomic_clear_planes(&dev, plane_args, plane_count);
2074 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2076 fprintf(stderr, "Atomic Commit failed\n");
2080 atomic_clear_FB(&dev, plane_args, plane_count);
2083 drmModeAtomicFree(dev.req);
2085 if (count || plane_count) {
2088 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2089 if (ret || cap == 0) {
2090 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2095 set_mode(&dev, pipe_args, count);
2098 set_planes(&dev, plane_args, plane_count);
2101 set_cursors(&dev, pipe_args, count);
2104 test_page_flip(&dev, pipe_args, count);
2107 drmDropMaster(dev.fd);
2112 clear_cursors(&dev);
2115 clear_planes(&dev, plane_args, plane_count);
2122 free_resources(dev.resources);