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.
54 #ifdef HAVE_SYS_SELECT_H
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;
104 drmModePlaneRes *plane_res;
107 struct encoder *encoders;
108 struct connector *connectors;
110 struct plane *planes;
116 struct resources *resources;
124 struct bo *cursor_bo;
128 drmModeAtomicReq *req;
131 static inline int64_t U642I64(uint64_t val)
133 return (int64_t)*((int64_t *)&val);
136 static float mode_vrefresh(drmModeModeInfo *mode)
138 return mode->clock * 1000.00
139 / (mode->htotal * mode->vtotal);
142 #define bit_name_fn(res) \
143 const char * res##_str(int type) { \
145 const char *sep = ""; \
146 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
147 if (type & (1 << i)) { \
148 printf("%s%s", sep, res##_names[i]); \
155 static const char *mode_type_names[] = {
165 static bit_name_fn(mode_type)
167 static const char *mode_flag_names[] = {
184 static bit_name_fn(mode_flag)
186 static void dump_fourcc(uint32_t fourcc)
195 static void dump_encoders(struct device *dev)
197 drmModeEncoder *encoder;
200 printf("Encoders:\n");
201 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
202 for (i = 0; i < dev->resources->res->count_encoders; i++) {
203 encoder = dev->resources->encoders[i].encoder;
207 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
210 util_lookup_encoder_type_name(encoder->encoder_type),
211 encoder->possible_crtcs,
212 encoder->possible_clones);
217 static void dump_mode(drmModeModeInfo *mode)
219 printf(" %s %.2f %d %d %d %d %d %d %d %d %d",
233 mode_flag_str(mode->flags);
235 mode_type_str(mode->type);
239 static void dump_blob(struct device *dev, uint32_t blob_id)
242 unsigned char *blob_data;
243 drmModePropertyBlobPtr blob;
245 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
251 blob_data = blob->data;
253 for (i = 0; i < blob->length; i++) {
256 printf("%.2hhx", blob_data[i]);
260 drmModeFreePropertyBlob(blob);
263 static const char *modifier_to_string(uint64_t modifier)
266 case DRM_FORMAT_MOD_INVALID:
268 case DRM_FORMAT_MOD_LINEAR:
270 case I915_FORMAT_MOD_X_TILED:
272 case I915_FORMAT_MOD_Y_TILED:
274 case I915_FORMAT_MOD_Yf_TILED:
276 case I915_FORMAT_MOD_Y_TILED_CCS:
277 return "Y_TILED_CCS";
278 case I915_FORMAT_MOD_Yf_TILED_CCS:
279 return "Yf_TILED_CCS";
280 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
281 return "SAMSUNG_64_32_TILE";
282 case DRM_FORMAT_MOD_VIVANTE_TILED:
283 return "VIVANTE_TILED";
284 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
285 return "VIVANTE_SUPER_TILED";
286 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
287 return "VIVANTE_SPLIT_TILED";
288 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
289 return "VIVANTE_SPLIT_SUPER_TILED";
290 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
291 return "NVIDIA_TEGRA_TILED";
292 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
293 return "NVIDIA_16BX2_BLOCK(0)";
294 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
295 return "NVIDIA_16BX2_BLOCK(1)";
296 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
297 return "NVIDIA_16BX2_BLOCK(2)";
298 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
299 return "NVIDIA_16BX2_BLOCK(3)";
300 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
301 return "NVIDIA_16BX2_BLOCK(4)";
302 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
303 return "NVIDIA_16BX2_BLOCK(5)";
304 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
305 return "MOD_BROADCOM_VC4_T_TILED";
306 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
307 return "QCOM_COMPRESSED";
309 return "(UNKNOWN MODIFIER)";
313 static void dump_in_formats(struct device *dev, uint32_t blob_id)
316 drmModePropertyBlobPtr blob;
317 struct drm_format_modifier_blob *header;
319 struct drm_format_modifier *modifiers;
321 printf("\t\tin_formats blob decoded:\n");
322 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
329 formats = (uint32_t *) ((char *) header + header->formats_offset);
330 modifiers = (struct drm_format_modifier *)
331 ((char *) header + header->modifiers_offset);
333 for (i = 0; i < header->count_formats; i++) {
335 dump_fourcc(formats[i]);
337 for (j = 0; j < header->count_modifiers; j++) {
338 uint64_t mask = 1ULL << i;
339 if (modifiers[j].formats & mask)
340 printf(" %s", modifier_to_string(modifiers[j].modifier));
345 drmModeFreePropertyBlob(blob);
348 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
349 uint32_t prop_id, uint64_t value)
352 printf("\t%d", prop_id);
358 printf(" %s:\n", prop->name);
360 printf("\t\tflags:");
361 if (prop->flags & DRM_MODE_PROP_PENDING)
363 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
364 printf(" immutable");
365 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
366 printf(" signed range");
367 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
369 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
371 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
373 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
375 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
379 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
380 printf("\t\tvalues:");
381 for (i = 0; i < prop->count_values; i++)
382 printf(" %"PRId64, U642I64(prop->values[i]));
386 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
387 printf("\t\tvalues:");
388 for (i = 0; i < prop->count_values; i++)
389 printf(" %"PRIu64, prop->values[i]);
393 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
394 printf("\t\tenums:");
395 for (i = 0; i < prop->count_enums; i++)
396 printf(" %s=%llu", prop->enums[i].name,
397 prop->enums[i].value);
399 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
400 printf("\t\tvalues:");
401 for (i = 0; i < prop->count_enums; i++)
402 printf(" %s=0x%llx", prop->enums[i].name,
403 (1LL << prop->enums[i].value));
406 assert(prop->count_enums == 0);
409 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
410 printf("\t\tblobs:\n");
411 for (i = 0; i < prop->count_blobs; i++)
412 dump_blob(dev, prop->blob_ids[i]);
415 assert(prop->count_blobs == 0);
418 printf("\t\tvalue:");
419 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
420 dump_blob(dev, value);
421 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
422 printf(" %"PRId64"\n", value);
424 printf(" %"PRIu64"\n", value);
426 if (strcmp(prop->name, "IN_FORMATS") == 0)
427 dump_in_formats(dev, value);
430 static void dump_connectors(struct device *dev)
434 printf("Connectors:\n");
435 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
436 for (i = 0; i < dev->resources->res->count_connectors; i++) {
437 struct connector *_connector = &dev->resources->connectors[i];
438 drmModeConnector *connector = _connector->connector;
442 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
443 connector->connector_id,
444 connector->encoder_id,
445 util_lookup_connector_status_name(connector->connection),
447 connector->mmWidth, connector->mmHeight,
448 connector->count_modes);
450 for (j = 0; j < connector->count_encoders; j++)
451 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
454 if (connector->count_modes) {
456 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
458 for (j = 0; j < connector->count_modes; j++)
459 dump_mode(&connector->modes[j]);
462 if (_connector->props) {
464 for (j = 0; j < (int)_connector->props->count_props; j++)
465 dump_prop(dev, _connector->props_info[j],
466 _connector->props->props[j],
467 _connector->props->prop_values[j]);
473 static void dump_crtcs(struct device *dev)
479 printf("id\tfb\tpos\tsize\n");
480 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
481 struct crtc *_crtc = &dev->resources->crtcs[i];
482 drmModeCrtc *crtc = _crtc->crtc;
486 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
490 crtc->width, crtc->height);
491 dump_mode(&crtc->mode);
495 for (j = 0; j < _crtc->props->count_props; j++)
496 dump_prop(dev, _crtc->props_info[j],
497 _crtc->props->props[j],
498 _crtc->props->prop_values[j]);
500 printf(" no properties found\n");
506 static void dump_framebuffers(struct device *dev)
511 printf("Frame buffers:\n");
512 printf("id\tsize\tpitch\n");
513 for (i = 0; i < dev->resources->res->count_fbs; i++) {
514 fb = dev->resources->fbs[i].fb;
518 printf("%u\t(%ux%u)\t%u\n",
520 fb->width, fb->height,
526 static void dump_planes(struct device *dev)
531 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
533 if (!dev->resources->plane_res)
536 for (i = 0; i < dev->resources->plane_res->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, __res, type, Type) \
579 if (!(_res)->type##s) \
581 for (i = 0; i < (int)(_res)->__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, __res, type) \
591 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
592 drmModeFreeObjectProperties(res->type##s[i].props); \
593 free(res->type##s[i].props_info); \
598 free_properties(res, res, crtc);
600 free_resource(res, res, crtc, Crtc);
601 free_resource(res, res, encoder, Encoder);
603 for (i = 0; i < res->res->count_connectors; i++)
604 free(res->connectors[i].name);
606 free_resource(res, res, connector, Connector);
607 free_resource(res, res, fb, FB);
609 drmModeFreeResources(res->res);
612 if (res->plane_res) {
613 free_properties(res, plane_res, plane);
615 free_resource(res, plane_res, plane, Plane);
617 drmModeFreePlaneResources(res->plane_res);
623 static struct resources *get_resources(struct device *dev)
625 struct resources *res;
628 res = calloc(1, sizeof(*res));
632 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
634 res->res = drmModeGetResources(dev->fd);
636 fprintf(stderr, "drmModeGetResources failed: %s\n",
641 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
642 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
643 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
644 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
646 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
649 #define get_resource(_res, __res, type, Type) \
651 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
652 (_res)->type##s[i].type = \
653 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
654 if (!(_res)->type##s[i].type) \
655 fprintf(stderr, "could not get %s %i: %s\n", \
656 #type, (_res)->__res->type##s[i], \
661 get_resource(res, res, crtc, Crtc);
662 get_resource(res, res, encoder, Encoder);
663 get_resource(res, res, connector, Connector);
664 get_resource(res, res, fb, FB);
666 /* Set the name of all connectors based on the type name and the per-type ID. */
667 for (i = 0; i < res->res->count_connectors; i++) {
668 struct connector *connector = &res->connectors[i];
669 drmModeConnector *conn = connector->connector;
672 num = asprintf(&connector->name, "%s-%u",
673 util_lookup_connector_type_name(conn->connector_type),
674 conn->connector_type_id);
679 #define get_properties(_res, __res, type, Type) \
681 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
682 struct type *obj = &res->type##s[i]; \
685 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
686 DRM_MODE_OBJECT_##Type); \
689 "could not get %s %i properties: %s\n", \
690 #type, obj->type->type##_id, \
694 obj->props_info = calloc(obj->props->count_props, \
695 sizeof(*obj->props_info)); \
696 if (!obj->props_info) \
698 for (j = 0; j < obj->props->count_props; ++j) \
699 obj->props_info[j] = \
700 drmModeGetProperty(dev->fd, obj->props->props[j]); \
704 get_properties(res, res, crtc, CRTC);
705 get_properties(res, res, connector, CONNECTOR);
707 for (i = 0; i < res->res->count_crtcs; ++i)
708 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
710 res->plane_res = drmModeGetPlaneResources(dev->fd);
711 if (!res->plane_res) {
712 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
717 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
721 get_resource(res, plane_res, plane, Plane);
722 get_properties(res, plane_res, plane, PLANE);
731 static int get_crtc_index(struct device *dev, uint32_t id)
735 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
736 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
737 if (crtc && crtc->crtc_id == id)
744 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
746 struct connector *connector;
749 for (i = 0; i < dev->resources->res->count_connectors; i++) {
750 connector = &dev->resources->connectors[i];
752 if (strcmp(connector->name, name) == 0)
753 return connector->connector;
759 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
761 drmModeConnector *connector;
764 for (i = 0; i < dev->resources->res->count_connectors; i++) {
765 connector = dev->resources->connectors[i].connector;
766 if (connector && connector->connector_id == id)
773 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
775 drmModeEncoder *encoder;
778 for (i = 0; i < dev->resources->res->count_encoders; i++) {
779 encoder = dev->resources->encoders[i].encoder;
780 if (encoder && encoder->encoder_id == id)
787 /* -----------------------------------------------------------------------------
792 * Mode setting with the kernel interfaces is a bit of a chore.
793 * First you have to find the connector in question and make sure the
794 * requested mode is available.
795 * Then you need to find the encoder attached to that connector so you
796 * can bind it with a free crtc.
801 unsigned int num_cons;
807 drmModeModeInfo *mode;
809 unsigned int fb_id[2], current_fb_id;
810 struct timeval start;
816 uint32_t plane_id; /* the id of plane to use */
817 uint32_t crtc_id; /* the id of CRTC to bind to */
823 unsigned int old_fb_id;
826 char format_str[5]; /* need to leave room for terminating \0 */
830 static drmModeModeInfo *
831 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
832 const float vrefresh)
834 drmModeConnector *connector;
835 drmModeModeInfo *mode;
838 connector = get_connector_by_id(dev, con_id);
839 if (!connector || !connector->count_modes)
842 for (i = 0; i < connector->count_modes; i++) {
843 mode = &connector->modes[i];
844 if (!strcmp(mode->name, mode_str)) {
845 /* If the vertical refresh frequency is not specified
846 * then return the first mode that match with the name.
847 * Else, return the mode that match the name and
848 * the specified vertical refresh frequency.
852 else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005)
860 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
862 uint32_t possible_crtcs = ~0;
863 uint32_t active_crtcs = 0;
864 unsigned int crtc_idx;
868 for (i = 0; i < pipe->num_cons; ++i) {
869 uint32_t crtcs_for_connector = 0;
870 drmModeConnector *connector;
871 drmModeEncoder *encoder;
874 connector = get_connector_by_id(dev, pipe->con_ids[i]);
878 for (j = 0; j < connector->count_encoders; ++j) {
879 encoder = get_encoder_by_id(dev, connector->encoders[j]);
883 crtcs_for_connector |= encoder->possible_crtcs;
885 idx = get_crtc_index(dev, encoder->crtc_id);
887 active_crtcs |= 1 << idx;
890 possible_crtcs &= crtcs_for_connector;
896 /* Return the first possible and active CRTC if one exists, or the first
897 * possible CRTC otherwise.
899 if (possible_crtcs & active_crtcs)
900 crtc_idx = ffs(possible_crtcs & active_crtcs);
902 crtc_idx = ffs(possible_crtcs);
904 return &dev->resources->crtcs[crtc_idx - 1];
907 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
909 drmModeModeInfo *mode = NULL;
914 for (i = 0; i < (int)pipe->num_cons; i++) {
915 mode = connector_find_mode(dev, pipe->con_ids[i],
916 pipe->mode_str, pipe->vrefresh);
920 "failed to find mode "
921 "\"%s-%.2fHz\" for connector %s\n",
922 pipe->mode_str, pipe->vrefresh, pipe->cons[i]);
925 "failed to find mode \"%s\" for connector %s\n",
926 pipe->mode_str, pipe->cons[i]);
931 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
932 * locate a CRTC that can be attached to all the connectors.
934 if (pipe->crtc_id != (uint32_t)-1) {
935 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
936 struct crtc *crtc = &dev->resources->crtcs[i];
938 if (pipe->crtc_id == crtc->crtc->crtc_id) {
944 pipe->crtc = pipe_find_crtc(dev, pipe);
948 fprintf(stderr, "failed to find CRTC for pipe\n");
953 pipe->crtc->mode = mode;
958 /* -----------------------------------------------------------------------------
962 struct property_arg {
965 char name[DRM_PROP_NAME_LEN+1];
971 static bool set_property(struct device *dev, struct property_arg *p)
973 drmModeObjectProperties *props = NULL;
974 drmModePropertyRes **props_info = NULL;
975 const char *obj_type;
982 #define find_object(_res, __res, type, Type) \
984 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
985 struct type *obj = &(_res)->type##s[i]; \
986 if (obj->type->type##_id != p->obj_id) \
988 p->obj_type = DRM_MODE_OBJECT_##Type; \
990 props = obj->props; \
991 props_info = obj->props_info; \
995 find_object(dev->resources, res, crtc, CRTC);
996 if (p->obj_type == 0)
997 find_object(dev->resources, res, connector, CONNECTOR);
998 if (p->obj_type == 0)
999 find_object(dev->resources, plane_res, plane, PLANE);
1000 if (p->obj_type == 0) {
1001 fprintf(stderr, "Object %i not found, can't set property\n",
1007 fprintf(stderr, "%s %i has no properties\n",
1008 obj_type, p->obj_id);
1012 for (i = 0; i < (int)props->count_props; ++i) {
1015 if (strcmp(props_info[i]->name, p->name) == 0)
1019 if (i == (int)props->count_props) {
1021 fprintf(stderr, "%s %i has no %s property\n",
1022 obj_type, p->obj_id, p->name);
1026 p->prop_id = props->props[i];
1028 if (!dev->use_atomic)
1029 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1030 p->prop_id, p->value);
1032 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
1035 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1036 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1041 /* -------------------------------------------------------------------------- */
1044 page_flip_handler(int fd, unsigned int frame,
1045 unsigned int sec, unsigned int usec, void *data)
1047 struct pipe_arg *pipe;
1048 unsigned int new_fb_id;
1053 if (pipe->current_fb_id == pipe->fb_id[0])
1054 new_fb_id = pipe->fb_id[1];
1056 new_fb_id = pipe->fb_id[0];
1058 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
1059 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1060 pipe->current_fb_id = new_fb_id;
1062 if (pipe->swap_count == 60) {
1063 gettimeofday(&end, NULL);
1064 t = end.tv_sec + end.tv_usec * 1e-6 -
1065 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1066 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1067 pipe->swap_count = 0;
1072 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1076 for (i = 0; i < ovr->count_formats; ++i) {
1077 if (ovr->formats[i] == fmt)
1084 static void add_property(struct device *dev, uint32_t obj_id,
1085 const char *name, uint64_t value)
1087 struct property_arg p;
1090 strcpy(p.name, name);
1093 set_property(dev, &p);
1096 static bool add_property_optional(struct device *dev, uint32_t obj_id,
1097 const char *name, uint64_t value)
1099 struct property_arg p;
1102 strcpy(p.name, name);
1106 return set_property(dev, &p);
1109 static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
1111 unsigned blob_id = 0;
1112 /* TODO: support 1024-sized LUTs, when the use-case arises */
1113 struct drm_color_lut gamma_lut[256];
1116 if (fourcc == DRM_FORMAT_C8) {
1117 /* TODO: Add C8 support for more patterns */
1118 util_smpte_c8_gamma(256, gamma_lut);
1119 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
1121 for (i = 0; i < 256; i++) {
1123 gamma_lut[i].green =
1124 gamma_lut[i].blue = i << 8;
1128 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
1129 add_property_optional(dev, crtc_id, "CTM", 0);
1130 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
1131 uint16_t r[256], g[256], b[256];
1133 for (i = 0; i < 256; i++) {
1134 r[i] = gamma_lut[i].red;
1135 g[i] = gamma_lut[i].green;
1136 b[i] = gamma_lut[i].blue;
1139 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
1141 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
1145 static int atomic_set_plane(struct device *dev, struct plane_arg *p,
1146 int pattern, bool update)
1148 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1149 struct bo *plane_bo;
1150 int crtc_x, crtc_y, crtc_w, crtc_h;
1151 struct crtc *crtc = NULL;
1153 unsigned int old_fb_id;
1155 /* Find an unused plane which can be connected to our CRTC. Find the
1156 * CRTC index first, then iterate over available planes.
1158 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1159 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1160 crtc = &dev->resources->crtcs[i];
1166 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1171 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
1172 p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
1174 plane_bo = p->old_bo;
1178 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h,
1179 handles, pitches, offsets, pattern);
1181 if (plane_bo == NULL)
1184 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1185 handles, pitches, offsets, &p->fb_id, 0)) {
1186 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1193 old_fb_id = p->fb_id;
1194 p->old_fb_id = old_fb_id;
1196 crtc_w = p->w * p->scale;
1197 crtc_h = p->h * p->scale;
1198 if (!p->has_position) {
1199 /* Default to the middle of the screen */
1200 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1201 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1207 add_property(dev, p->plane_id, "FB_ID", p->fb_id);
1208 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
1209 add_property(dev, p->plane_id, "SRC_X", 0);
1210 add_property(dev, p->plane_id, "SRC_Y", 0);
1211 add_property(dev, p->plane_id, "SRC_W", p->w << 16);
1212 add_property(dev, p->plane_id, "SRC_H", p->h << 16);
1213 add_property(dev, p->plane_id, "CRTC_X", crtc_x);
1214 add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
1215 add_property(dev, p->plane_id, "CRTC_W", crtc_w);
1216 add_property(dev, p->plane_id, "CRTC_H", crtc_h);
1221 static int set_plane(struct device *dev, struct plane_arg *p)
1224 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1226 struct bo *plane_bo;
1227 uint32_t plane_flags = 0;
1228 int crtc_x, crtc_y, crtc_w, crtc_h;
1229 struct crtc *crtc = NULL;
1233 /* Find an unused plane which can be connected to our CRTC. Find the
1234 * CRTC index first, then iterate over available planes.
1236 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1237 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1238 crtc = &dev->resources->crtcs[i];
1245 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1249 plane_id = p->plane_id;
1251 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
1252 ovr = dev->resources->planes[i].plane;
1256 if (plane_id && plane_id != ovr->plane_id)
1259 if (!format_support(ovr, p->fourcc))
1262 if ((ovr->possible_crtcs & (1 << pipe)) &&
1263 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1264 plane_id = ovr->plane_id;
1269 if (i == dev->resources->plane_res->count_planes) {
1270 fprintf(stderr, "no unused plane available for CRTC %u\n",
1271 crtc->crtc->crtc_id);
1275 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1276 p->w, p->h, p->format_str, plane_id);
1278 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1279 pitches, offsets, secondary_fill);
1280 if (plane_bo == NULL)
1285 /* just use single plane format for now.. */
1286 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1287 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1288 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1292 crtc_w = p->w * p->scale;
1293 crtc_h = p->h * p->scale;
1294 if (!p->has_position) {
1295 /* Default to the middle of the screen */
1296 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1297 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1303 /* note src coords (last 4 args) are in Q16 format */
1304 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1305 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1306 0, 0, p->w << 16, p->h << 16)) {
1307 fprintf(stderr, "failed to enable plane: %s\n",
1312 ovr->crtc_id = crtc->crtc->crtc_id;
1317 static void atomic_set_planes(struct device *dev, struct plane_arg *p,
1318 unsigned int count, bool update)
1320 unsigned int i, pattern = primary_fill;
1323 for (i = 0; i < count; i++) {
1325 pattern = secondary_fill;
1327 set_gamma(dev, p[i].crtc_id, p[i].fourcc);
1329 if (atomic_set_plane(dev, &p[i], pattern, update))
1334 static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1338 for (i = 0; i < count; i++) {
1339 add_property(dev, p[i].plane_id, "FB_ID", 0);
1340 add_property(dev, p[i].plane_id, "CRTC_ID", 0);
1341 add_property(dev, p[i].plane_id, "SRC_X", 0);
1342 add_property(dev, p[i].plane_id, "SRC_Y", 0);
1343 add_property(dev, p[i].plane_id, "SRC_W", 0);
1344 add_property(dev, p[i].plane_id, "SRC_H", 0);
1345 add_property(dev, p[i].plane_id, "CRTC_X", 0);
1346 add_property(dev, p[i].plane_id, "CRTC_Y", 0);
1347 add_property(dev, p[i].plane_id, "CRTC_W", 0);
1348 add_property(dev, p[i].plane_id, "CRTC_H", 0);
1352 static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
1356 for (i = 0; i < count; i++) {
1358 drmModeRmFB(dev->fd, p[i].fb_id);
1361 if (p[i].old_fb_id) {
1362 drmModeRmFB(dev->fd, p[i].old_fb_id);
1366 bo_destroy(p[i].bo);
1370 bo_destroy(p[i].old_bo);
1377 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1381 for (i = 0; i < count; i++) {
1383 drmModeRmFB(dev->fd, p[i].fb_id);
1385 bo_destroy(p[i].bo);
1389 static void atomic_set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1395 for (i = 0; i < count; i++) {
1396 struct pipe_arg *pipe = &pipes[i];
1398 ret = pipe_find_crtc_and_mode(dev, pipe);
1403 for (i = 0; i < count; i++) {
1404 struct pipe_arg *pipe = &pipes[i];
1407 if (pipe->mode == NULL)
1410 printf("setting mode %s-%.2fHz on connectors ",
1411 pipe->mode->name, mode_vrefresh(pipe->mode));
1412 for (j = 0; j < pipe->num_cons; ++j) {
1413 printf("%s, ", pipe->cons[j]);
1414 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc->crtc->crtc_id);
1416 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1418 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
1419 add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", blob_id);
1420 add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 1);
1424 static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1429 for (i = 0; i < count; i++) {
1430 struct pipe_arg *pipe = &pipes[i];
1432 if (pipe->mode == NULL)
1435 for (j = 0; j < pipe->num_cons; ++j)
1436 add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
1438 add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", 0);
1439 add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 0);
1443 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1445 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1452 dev->mode.width = 0;
1453 dev->mode.height = 0;
1454 dev->mode.fb_id = 0;
1456 for (i = 0; i < count; i++) {
1457 struct pipe_arg *pipe = &pipes[i];
1459 ret = pipe_find_crtc_and_mode(dev, pipe);
1463 dev->mode.width += pipe->mode->hdisplay;
1464 if (dev->mode.height < pipe->mode->vdisplay)
1465 dev->mode.height = pipe->mode->vdisplay;
1468 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1469 dev->mode.height, handles, pitches, offsets,
1476 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1477 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1479 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1480 dev->mode.width, dev->mode.height, strerror(errno));
1484 dev->mode.fb_id = fb_id;
1487 for (i = 0; i < count; i++) {
1488 struct pipe_arg *pipe = &pipes[i];
1490 if (pipe->mode == NULL)
1493 printf("setting mode %s-%.2fHz@%s on connectors ",
1494 pipe->mode->name, mode_vrefresh(pipe->mode),
1496 for (j = 0; j < pipe->num_cons; ++j)
1497 printf("%s, ", pipe->cons[j]);
1498 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1500 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1501 x, 0, pipe->con_ids, pipe->num_cons,
1504 /* XXX: Actually check if this is needed */
1505 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1507 x += pipe->mode->hdisplay;
1510 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1514 set_gamma(dev, pipe->crtc->crtc->crtc_id, pipe->fourcc);
1518 static void clear_mode(struct device *dev)
1520 if (dev->mode.fb_id)
1521 drmModeRmFB(dev->fd, dev->mode.fb_id);
1523 bo_destroy(dev->mode.bo);
1526 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1530 /* set up planes/overlays */
1531 for (i = 0; i < count; i++)
1532 if (set_plane(dev, &p[i]))
1536 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1538 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1543 /* maybe make cursor width/height configurable some day */
1547 /* create cursor bo.. just using PATTERN_PLAIN as it has
1550 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1551 offsets, UTIL_PATTERN_PLAIN);
1555 dev->mode.cursor_bo = bo;
1557 for (i = 0; i < count; i++) {
1558 struct pipe_arg *pipe = &pipes[i];
1559 ret = cursor_init(dev->fd, handles[0],
1560 pipe->crtc->crtc->crtc_id,
1561 pipe->mode->hdisplay, pipe->mode->vdisplay,
1564 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1573 static void clear_cursors(struct device *dev)
1577 if (dev->mode.cursor_bo)
1578 bo_destroy(dev->mode.cursor_bo);
1581 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1583 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1584 unsigned int other_fb_id;
1585 struct bo *other_bo;
1586 drmEventContext evctx;
1590 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1591 dev->mode.height, handles, pitches, offsets,
1592 UTIL_PATTERN_PLAIN);
1593 if (other_bo == NULL)
1596 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1597 pipes[0].fourcc, handles, pitches, offsets,
1600 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1604 for (i = 0; i < count; i++) {
1605 struct pipe_arg *pipe = &pipes[i];
1607 if (pipe->mode == NULL)
1610 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1611 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1614 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1617 gettimeofday(&pipe->start, NULL);
1618 pipe->swap_count = 0;
1619 pipe->fb_id[0] = dev->mode.fb_id;
1620 pipe->fb_id[1] = other_fb_id;
1621 pipe->current_fb_id = other_fb_id;
1624 memset(&evctx, 0, sizeof evctx);
1625 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1626 evctx.vblank_handler = NULL;
1627 evctx.page_flip_handler = page_flip_handler;
1631 struct pollfd pfd[2];
1634 pfd[0].events = POLLIN;
1636 pfd[1].events = POLLIN;
1638 if (poll(pfd, 2, -1) < 0) {
1639 fprintf(stderr, "poll error\n");
1646 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1651 FD_SET(dev->fd, &fds);
1652 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1655 fprintf(stderr, "select timed out or error (ret %d)\n",
1658 } else if (FD_ISSET(0, &fds)) {
1663 drmHandleEvent(dev->fd, &evctx);
1667 drmModeRmFB(dev->fd, other_fb_id);
1669 bo_destroy(other_bo);
1672 #define min(a, b) ((a) < (b) ? (a) : (b))
1674 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1682 pipe->crtc_id = (uint32_t)-1;
1683 strcpy(pipe->format_str, "XR24");
1685 /* Count the number of connectors and allocate them. */
1687 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1692 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1693 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1694 if (pipe->con_ids == NULL || pipe->cons == NULL)
1697 /* Parse the connectors. */
1698 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1699 endp = strpbrk(p, ",@:");
1703 pipe->cons[i] = strndup(p, endp - p);
1709 if (i != pipe->num_cons - 1)
1712 /* Parse the remaining parameters. */
1715 pipe->crtc_id = strtoul(arg, &endp, 10);
1722 /* Search for the vertical refresh or the format. */
1723 p = strpbrk(arg, "-@");
1725 p = arg + strlen(arg);
1726 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1727 strncpy(pipe->mode_str, arg, len);
1728 pipe->mode_str[len] = '\0';
1731 pipe->vrefresh = strtof(p + 1, &endp);
1736 strncpy(pipe->format_str, p + 1, 4);
1737 pipe->format_str[4] = '\0';
1740 pipe->fourcc = util_format_fourcc(pipe->format_str);
1741 if (pipe->fourcc == 0) {
1742 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1749 static int parse_plane(struct plane_arg *plane, const char *p)
1753 plane->plane_id = strtoul(p, &end, 10);
1758 plane->crtc_id = strtoul(p, &end, 10);
1763 plane->w = strtoul(p, &end, 10);
1768 plane->h = strtoul(p, &end, 10);
1770 if (*end == '+' || *end == '-') {
1771 plane->x = strtol(end, &end, 10);
1772 if (*end != '+' && *end != '-')
1774 plane->y = strtol(end, &end, 10);
1776 plane->has_position = true;
1781 plane->scale = strtod(p, &end);
1782 if (plane->scale <= 0.0)
1789 strncpy(plane->format_str, end + 1, 4);
1790 plane->format_str[4] = '\0';
1792 strcpy(plane->format_str, "XR24");
1795 plane->fourcc = util_format_fourcc(plane->format_str);
1796 if (plane->fourcc == 0) {
1797 fprintf(stderr, "unknown format %s\n", plane->format_str);
1804 static int parse_property(struct property_arg *p, const char *arg)
1806 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1810 p->name[DRM_PROP_NAME_LEN] = '\0';
1815 static void parse_fill_patterns(char *arg)
1817 char *fill = strtok(arg, ",");
1820 primary_fill = util_pattern_enum(fill);
1821 fill = strtok(NULL, ",");
1824 secondary_fill = util_pattern_enum(fill);
1827 static void usage(char *name)
1829 fprintf(stderr, "usage: %s [-acDdefMPpsCvw]\n", name);
1831 fprintf(stderr, "\n Query options:\n\n");
1832 fprintf(stderr, "\t-c\tlist connectors\n");
1833 fprintf(stderr, "\t-e\tlist encoders\n");
1834 fprintf(stderr, "\t-f\tlist framebuffers\n");
1835 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1837 fprintf(stderr, "\n Test options:\n\n");
1838 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1839 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1840 fprintf(stderr, "\t-C\ttest hw cursor\n");
1841 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1842 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1843 fprintf(stderr, "\t-a \tuse atomic API\n");
1844 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
1846 fprintf(stderr, "\n Generic options:\n\n");
1847 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1848 fprintf(stderr, "\t-M module\tuse the given driver\n");
1849 fprintf(stderr, "\t-D device\tuse the given device\n");
1851 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1855 static int page_flipping_supported(void)
1857 /*FIXME: generic ioctl needed? */
1861 struct drm_i915_getparam gp;
1863 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1866 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1868 fprintf(stderr, "drm_i915_getparam: %m\n");
1876 static int cursor_supported(void)
1878 /*FIXME: generic ioctl needed? */
1882 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1884 drmModeConnector *connector;
1889 for (i = 0; i < pipe->num_cons; i++) {
1890 id = strtoul(pipe->cons[i], &endp, 10);
1891 if (endp == pipe->cons[i]) {
1892 connector = get_connector_by_name(dev, pipe->cons[i]);
1894 fprintf(stderr, "no connector named '%s'\n",
1899 id = connector->connector_id;
1902 pipe->con_ids[i] = id;
1908 static char optstr[] = "acdD:efF:M:P:ps:Cvw:";
1910 int main(int argc, char **argv)
1915 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1916 int drop_master = 0;
1918 int test_cursor = 0;
1920 char *device = NULL;
1921 char *module = NULL;
1923 unsigned int count = 0, plane_count = 0;
1924 unsigned int prop_count = 0;
1925 struct pipe_arg *pipe_args = NULL;
1926 struct plane_arg *plane_args = NULL;
1927 struct property_arg *prop_args = NULL;
1928 unsigned int args = 0;
1931 memset(&dev, 0, sizeof dev);
1934 while ((c = getopt(argc, argv, optstr)) != -1) {
1958 parse_fill_patterns(optarg);
1962 /* Preserve the default behaviour of dumping all information. */
1966 plane_args = realloc(plane_args,
1967 (plane_count + 1) * sizeof *plane_args);
1968 if (plane_args == NULL) {
1969 fprintf(stderr, "memory allocation failed\n");
1972 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1974 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1984 pipe_args = realloc(pipe_args,
1985 (count + 1) * sizeof *pipe_args);
1986 if (pipe_args == NULL) {
1987 fprintf(stderr, "memory allocation failed\n");
1990 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1992 if (parse_connector(&pipe_args[count], optarg) < 0)
2004 prop_args = realloc(prop_args,
2005 (prop_count + 1) * sizeof *prop_args);
2006 if (prop_args == NULL) {
2007 fprintf(stderr, "memory allocation failed\n");
2010 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
2012 if (parse_property(&prop_args[prop_count], optarg) < 0)
2023 if (!args || (args == 1 && use_atomic))
2024 encoders = connectors = crtcs = planes = framebuffers = 1;
2026 dev.fd = util_open(device, module);
2030 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
2031 if (ret && use_atomic) {
2032 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
2037 dev.use_atomic = use_atomic;
2039 if (test_vsync && !page_flipping_supported()) {
2040 fprintf(stderr, "page flipping not supported by drm.\n");
2044 if (test_vsync && !count) {
2045 fprintf(stderr, "page flipping requires at least one -s option.\n");
2049 if (test_cursor && !cursor_supported()) {
2050 fprintf(stderr, "hw cursor not supported by drm.\n");
2054 dev.resources = get_resources(&dev);
2055 if (!dev.resources) {
2060 for (i = 0; i < count; i++) {
2061 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
2062 free_resources(dev.resources);
2068 #define dump_resource(dev, res) if (res) dump_##res(dev)
2070 dump_resource(&dev, encoders);
2071 dump_resource(&dev, connectors);
2072 dump_resource(&dev, crtcs);
2073 dump_resource(&dev, planes);
2074 dump_resource(&dev, framebuffers);
2076 for (i = 0; i < prop_count; ++i)
2077 set_property(&dev, &prop_args[i]);
2079 if (dev.use_atomic) {
2080 dev.req = drmModeAtomicAlloc();
2082 if (count && plane_count) {
2085 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2086 if (ret || cap == 0) {
2087 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2091 atomic_set_mode(&dev, pipe_args, count);
2092 atomic_set_planes(&dev, plane_args, plane_count, false);
2094 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2096 fprintf(stderr, "Atomic Commit failed [1]\n");
2100 gettimeofday(&pipe_args->start, NULL);
2101 pipe_args->swap_count = 0;
2103 while (test_vsync) {
2104 drmModeAtomicFree(dev.req);
2105 dev.req = drmModeAtomicAlloc();
2106 atomic_set_planes(&dev, plane_args, plane_count, true);
2108 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2110 fprintf(stderr, "Atomic Commit failed [2]\n");
2114 pipe_args->swap_count++;
2115 if (pipe_args->swap_count == 60) {
2119 gettimeofday(&end, NULL);
2120 t = end.tv_sec + end.tv_usec * 1e-6 -
2121 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
2122 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
2123 pipe_args->swap_count = 0;
2124 pipe_args->start = end;
2129 drmDropMaster(dev.fd);
2133 drmModeAtomicFree(dev.req);
2134 dev.req = drmModeAtomicAlloc();
2136 atomic_clear_mode(&dev, pipe_args, count);
2137 atomic_clear_planes(&dev, plane_args, plane_count);
2138 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2140 fprintf(stderr, "Atomic Commit failed\n");
2144 atomic_clear_FB(&dev, plane_args, plane_count);
2147 drmModeAtomicFree(dev.req);
2149 if (count || plane_count) {
2152 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2153 if (ret || cap == 0) {
2154 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2159 set_mode(&dev, pipe_args, count);
2162 set_planes(&dev, plane_args, plane_count);
2165 set_cursors(&dev, pipe_args, count);
2168 test_page_flip(&dev, pipe_args, count);
2171 drmDropMaster(dev.fd);
2176 clear_cursors(&dev);
2179 clear_planes(&dev, plane_args, plane_count);
2186 free_resources(dev.resources);