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 #define bit_name_fn(res) \
137 const char * res##_str(int type) { \
139 const char *sep = ""; \
140 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
141 if (type & (1 << i)) { \
142 printf("%s%s", sep, res##_names[i]); \
149 static const char *mode_type_names[] = {
159 static bit_name_fn(mode_type)
161 static const char *mode_flag_names[] = {
178 static bit_name_fn(mode_flag)
180 static void dump_fourcc(uint32_t fourcc)
189 static void dump_encoders(struct device *dev)
191 drmModeEncoder *encoder;
194 printf("Encoders:\n");
195 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
196 for (i = 0; i < dev->resources->res->count_encoders; i++) {
197 encoder = dev->resources->encoders[i].encoder;
201 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
204 util_lookup_encoder_type_name(encoder->encoder_type),
205 encoder->possible_crtcs,
206 encoder->possible_clones);
211 static void dump_mode(drmModeModeInfo *mode)
213 printf(" %s %d %d %d %d %d %d %d %d %d %d",
227 mode_flag_str(mode->flags);
229 mode_type_str(mode->type);
233 static void dump_blob(struct device *dev, uint32_t blob_id)
236 unsigned char *blob_data;
237 drmModePropertyBlobPtr blob;
239 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
245 blob_data = blob->data;
247 for (i = 0; i < blob->length; i++) {
250 printf("%.2hhx", blob_data[i]);
254 drmModeFreePropertyBlob(blob);
257 static const char *modifier_to_string(uint64_t modifier)
260 case DRM_FORMAT_MOD_INVALID:
262 case DRM_FORMAT_MOD_LINEAR:
264 case I915_FORMAT_MOD_X_TILED:
266 case I915_FORMAT_MOD_Y_TILED:
268 case I915_FORMAT_MOD_Yf_TILED:
270 case I915_FORMAT_MOD_Y_TILED_CCS:
271 return "Y_TILED_CCS";
272 case I915_FORMAT_MOD_Yf_TILED_CCS:
273 return "Yf_TILED_CCS";
274 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
275 return "SAMSUNG_64_32_TILE";
276 case DRM_FORMAT_MOD_VIVANTE_TILED:
277 return "VIVANTE_TILED";
278 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
279 return "VIVANTE_SUPER_TILED";
280 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
281 return "VIVANTE_SPLIT_TILED";
282 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
283 return "VIVANTE_SPLIT_SUPER_TILED";
284 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
285 return "NVIDIA_TEGRA_TILED";
286 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
287 return "NVIDIA_16BX2_BLOCK(0)";
288 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
289 return "NVIDIA_16BX2_BLOCK(1)";
290 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
291 return "NVIDIA_16BX2_BLOCK(2)";
292 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
293 return "NVIDIA_16BX2_BLOCK(3)";
294 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
295 return "NVIDIA_16BX2_BLOCK(4)";
296 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
297 return "NVIDIA_16BX2_BLOCK(5)";
298 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
299 return "MOD_BROADCOM_VC4_T_TILED";
300 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
301 return "QCOM_COMPRESSED";
303 return "(UNKNOWN MODIFIER)";
307 static void dump_in_formats(struct device *dev, uint32_t blob_id)
310 drmModePropertyBlobPtr blob;
311 struct drm_format_modifier_blob *header;
313 struct drm_format_modifier *modifiers;
315 printf("\t\tin_formats blob decoded:\n");
316 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
323 formats = (uint32_t *) ((char *) header + header->formats_offset);
324 modifiers = (struct drm_format_modifier *)
325 ((char *) header + header->modifiers_offset);
327 for (i = 0; i < header->count_formats; i++) {
329 dump_fourcc(formats[i]);
331 for (j = 0; j < header->count_modifiers; j++) {
332 uint64_t mask = 1ULL << i;
333 if (modifiers[j].formats & mask)
334 printf(" %s", modifier_to_string(modifiers[j].modifier));
339 drmModeFreePropertyBlob(blob);
342 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
343 uint32_t prop_id, uint64_t value)
346 printf("\t%d", prop_id);
352 printf(" %s:\n", prop->name);
354 printf("\t\tflags:");
355 if (prop->flags & DRM_MODE_PROP_PENDING)
357 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
358 printf(" immutable");
359 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
360 printf(" signed range");
361 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
363 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
365 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
367 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
369 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
373 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
374 printf("\t\tvalues:");
375 for (i = 0; i < prop->count_values; i++)
376 printf(" %"PRId64, U642I64(prop->values[i]));
380 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
381 printf("\t\tvalues:");
382 for (i = 0; i < prop->count_values; i++)
383 printf(" %"PRIu64, prop->values[i]);
387 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
388 printf("\t\tenums:");
389 for (i = 0; i < prop->count_enums; i++)
390 printf(" %s=%llu", prop->enums[i].name,
391 prop->enums[i].value);
393 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
394 printf("\t\tvalues:");
395 for (i = 0; i < prop->count_enums; i++)
396 printf(" %s=0x%llx", prop->enums[i].name,
397 (1LL << prop->enums[i].value));
400 assert(prop->count_enums == 0);
403 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
404 printf("\t\tblobs:\n");
405 for (i = 0; i < prop->count_blobs; i++)
406 dump_blob(dev, prop->blob_ids[i]);
409 assert(prop->count_blobs == 0);
412 printf("\t\tvalue:");
413 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
414 dump_blob(dev, value);
415 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
416 printf(" %"PRId64"\n", value);
418 printf(" %"PRIu64"\n", value);
420 if (strcmp(prop->name, "IN_FORMATS") == 0)
421 dump_in_formats(dev, value);
424 static void dump_connectors(struct device *dev)
428 printf("Connectors:\n");
429 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
430 for (i = 0; i < dev->resources->res->count_connectors; i++) {
431 struct connector *_connector = &dev->resources->connectors[i];
432 drmModeConnector *connector = _connector->connector;
436 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
437 connector->connector_id,
438 connector->encoder_id,
439 util_lookup_connector_status_name(connector->connection),
441 connector->mmWidth, connector->mmHeight,
442 connector->count_modes);
444 for (j = 0; j < connector->count_encoders; j++)
445 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
448 if (connector->count_modes) {
450 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
452 for (j = 0; j < connector->count_modes; j++)
453 dump_mode(&connector->modes[j]);
456 if (_connector->props) {
458 for (j = 0; j < (int)_connector->props->count_props; j++)
459 dump_prop(dev, _connector->props_info[j],
460 _connector->props->props[j],
461 _connector->props->prop_values[j]);
467 static void dump_crtcs(struct device *dev)
473 printf("id\tfb\tpos\tsize\n");
474 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
475 struct crtc *_crtc = &dev->resources->crtcs[i];
476 drmModeCrtc *crtc = _crtc->crtc;
480 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
484 crtc->width, crtc->height);
485 dump_mode(&crtc->mode);
489 for (j = 0; j < _crtc->props->count_props; j++)
490 dump_prop(dev, _crtc->props_info[j],
491 _crtc->props->props[j],
492 _crtc->props->prop_values[j]);
494 printf(" no properties found\n");
500 static void dump_framebuffers(struct device *dev)
505 printf("Frame buffers:\n");
506 printf("id\tsize\tpitch\n");
507 for (i = 0; i < dev->resources->res->count_fbs; i++) {
508 fb = dev->resources->fbs[i].fb;
512 printf("%u\t(%ux%u)\t%u\n",
514 fb->width, fb->height,
520 static void dump_planes(struct device *dev)
525 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
527 if (!dev->resources->plane_res)
530 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
531 struct plane *plane = &dev->resources->planes[i];
532 drmModePlane *ovr = plane->plane;
536 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
537 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
538 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
539 ovr->gamma_size, ovr->possible_crtcs);
541 if (!ovr->count_formats)
545 for (j = 0; j < ovr->count_formats; j++)
546 dump_fourcc(ovr->formats[j]);
551 for (j = 0; j < plane->props->count_props; j++)
552 dump_prop(dev, plane->props_info[j],
553 plane->props->props[j],
554 plane->props->prop_values[j]);
556 printf(" no properties found\n");
564 static void free_resources(struct resources *res)
571 #define free_resource(_res, __res, type, Type) \
573 if (!(_res)->type##s) \
575 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
576 if (!(_res)->type##s[i].type) \
578 drmModeFree##Type((_res)->type##s[i].type); \
580 free((_res)->type##s); \
583 #define free_properties(_res, __res, type) \
585 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
586 drmModeFreeObjectProperties(res->type##s[i].props); \
587 free(res->type##s[i].props_info); \
592 free_properties(res, res, crtc);
594 free_resource(res, res, crtc, Crtc);
595 free_resource(res, res, encoder, Encoder);
597 for (i = 0; i < res->res->count_connectors; i++)
598 free(res->connectors[i].name);
600 free_resource(res, res, connector, Connector);
601 free_resource(res, res, fb, FB);
603 drmModeFreeResources(res->res);
606 if (res->plane_res) {
607 free_properties(res, plane_res, plane);
609 free_resource(res, plane_res, plane, Plane);
611 drmModeFreePlaneResources(res->plane_res);
617 static struct resources *get_resources(struct device *dev)
619 struct resources *res;
622 res = calloc(1, sizeof(*res));
626 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
628 res->res = drmModeGetResources(dev->fd);
630 fprintf(stderr, "drmModeGetResources failed: %s\n",
635 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
636 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
637 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
638 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
640 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
643 #define get_resource(_res, __res, type, Type) \
645 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
646 (_res)->type##s[i].type = \
647 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
648 if (!(_res)->type##s[i].type) \
649 fprintf(stderr, "could not get %s %i: %s\n", \
650 #type, (_res)->__res->type##s[i], \
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 /* Set the name of all connectors based on the type name and the per-type ID. */
661 for (i = 0; i < res->res->count_connectors; i++) {
662 struct connector *connector = &res->connectors[i];
663 drmModeConnector *conn = connector->connector;
666 num = asprintf(&connector->name, "%s-%u",
667 util_lookup_connector_type_name(conn->connector_type),
668 conn->connector_type_id);
673 #define get_properties(_res, __res, type, Type) \
675 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
676 struct type *obj = &res->type##s[i]; \
679 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
680 DRM_MODE_OBJECT_##Type); \
683 "could not get %s %i properties: %s\n", \
684 #type, obj->type->type##_id, \
688 obj->props_info = calloc(obj->props->count_props, \
689 sizeof(*obj->props_info)); \
690 if (!obj->props_info) \
692 for (j = 0; j < obj->props->count_props; ++j) \
693 obj->props_info[j] = \
694 drmModeGetProperty(dev->fd, obj->props->props[j]); \
698 get_properties(res, res, crtc, CRTC);
699 get_properties(res, res, connector, CONNECTOR);
701 for (i = 0; i < res->res->count_crtcs; ++i)
702 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
704 res->plane_res = drmModeGetPlaneResources(dev->fd);
705 if (!res->plane_res) {
706 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
711 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
715 get_resource(res, plane_res, plane, Plane);
716 get_properties(res, plane_res, plane, PLANE);
725 static int get_crtc_index(struct device *dev, uint32_t id)
729 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
730 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
731 if (crtc && crtc->crtc_id == id)
738 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
740 struct connector *connector;
743 for (i = 0; i < dev->resources->res->count_connectors; i++) {
744 connector = &dev->resources->connectors[i];
746 if (strcmp(connector->name, name) == 0)
747 return connector->connector;
753 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
755 drmModeConnector *connector;
758 for (i = 0; i < dev->resources->res->count_connectors; i++) {
759 connector = dev->resources->connectors[i].connector;
760 if (connector && connector->connector_id == id)
767 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
769 drmModeEncoder *encoder;
772 for (i = 0; i < dev->resources->res->count_encoders; i++) {
773 encoder = dev->resources->encoders[i].encoder;
774 if (encoder && encoder->encoder_id == id)
781 /* -----------------------------------------------------------------------------
786 * Mode setting with the kernel interfaces is a bit of a chore.
787 * First you have to find the connector in question and make sure the
788 * requested mode is available.
789 * Then you need to find the encoder attached to that connector so you
790 * can bind it with a free crtc.
795 unsigned int num_cons;
801 drmModeModeInfo *mode;
803 unsigned int fb_id[2], current_fb_id;
804 struct timeval start;
810 uint32_t plane_id; /* the id of plane to use */
811 uint32_t crtc_id; /* the id of CRTC to bind to */
817 unsigned int old_fb_id;
820 char format_str[5]; /* need to leave room for terminating \0 */
824 static drmModeModeInfo *
825 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
826 const float vrefresh)
828 drmModeConnector *connector;
829 drmModeModeInfo *mode;
833 connector = get_connector_by_id(dev, con_id);
834 if (!connector || !connector->count_modes)
837 for (i = 0; i < connector->count_modes; i++) {
838 mode = &connector->modes[i];
839 if (!strcmp(mode->name, mode_str)) {
840 /* If the vertical refresh frequency is not specified then return the
841 * first mode that match with the name. Else, return the mode that match
842 * the name and the specified vertical refresh frequency.
844 mode_vrefresh = mode->clock * 1000.00
845 / (mode->htotal * mode->vtotal);
848 else if (fabs(mode_vrefresh - vrefresh) < 0.005)
856 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
858 uint32_t possible_crtcs = ~0;
859 uint32_t active_crtcs = 0;
860 unsigned int crtc_idx;
864 for (i = 0; i < pipe->num_cons; ++i) {
865 uint32_t crtcs_for_connector = 0;
866 drmModeConnector *connector;
867 drmModeEncoder *encoder;
870 connector = get_connector_by_id(dev, pipe->con_ids[i]);
874 for (j = 0; j < connector->count_encoders; ++j) {
875 encoder = get_encoder_by_id(dev, connector->encoders[j]);
879 crtcs_for_connector |= encoder->possible_crtcs;
881 idx = get_crtc_index(dev, encoder->crtc_id);
883 active_crtcs |= 1 << idx;
886 possible_crtcs &= crtcs_for_connector;
892 /* Return the first possible and active CRTC if one exists, or the first
893 * possible CRTC otherwise.
895 if (possible_crtcs & active_crtcs)
896 crtc_idx = ffs(possible_crtcs & active_crtcs);
898 crtc_idx = ffs(possible_crtcs);
900 return &dev->resources->crtcs[crtc_idx - 1];
903 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
905 drmModeModeInfo *mode = NULL;
910 for (i = 0; i < (int)pipe->num_cons; i++) {
911 mode = connector_find_mode(dev, pipe->con_ids[i],
912 pipe->mode_str, pipe->vrefresh);
915 "failed to find mode \"%s\" for connector %s\n",
916 pipe->mode_str, pipe->cons[i]);
921 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
922 * locate a CRTC that can be attached to all the connectors.
924 if (pipe->crtc_id != (uint32_t)-1) {
925 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
926 struct crtc *crtc = &dev->resources->crtcs[i];
928 if (pipe->crtc_id == crtc->crtc->crtc_id) {
934 pipe->crtc = pipe_find_crtc(dev, pipe);
938 fprintf(stderr, "failed to find CRTC for pipe\n");
943 pipe->crtc->mode = mode;
948 /* -----------------------------------------------------------------------------
952 struct property_arg {
955 char name[DRM_PROP_NAME_LEN+1];
961 static bool set_property(struct device *dev, struct property_arg *p)
963 drmModeObjectProperties *props = NULL;
964 drmModePropertyRes **props_info = NULL;
965 const char *obj_type;
972 #define find_object(_res, __res, type, Type) \
974 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
975 struct type *obj = &(_res)->type##s[i]; \
976 if (obj->type->type##_id != p->obj_id) \
978 p->obj_type = DRM_MODE_OBJECT_##Type; \
980 props = obj->props; \
981 props_info = obj->props_info; \
985 find_object(dev->resources, res, crtc, CRTC);
986 if (p->obj_type == 0)
987 find_object(dev->resources, res, connector, CONNECTOR);
988 if (p->obj_type == 0)
989 find_object(dev->resources, plane_res, plane, PLANE);
990 if (p->obj_type == 0) {
991 fprintf(stderr, "Object %i not found, can't set property\n",
997 fprintf(stderr, "%s %i has no properties\n",
998 obj_type, p->obj_id);
1002 for (i = 0; i < (int)props->count_props; ++i) {
1005 if (strcmp(props_info[i]->name, p->name) == 0)
1009 if (i == (int)props->count_props) {
1011 fprintf(stderr, "%s %i has no %s property\n",
1012 obj_type, p->obj_id, p->name);
1016 p->prop_id = props->props[i];
1018 if (!dev->use_atomic)
1019 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1020 p->prop_id, p->value);
1022 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
1025 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1026 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1031 /* -------------------------------------------------------------------------- */
1034 page_flip_handler(int fd, unsigned int frame,
1035 unsigned int sec, unsigned int usec, void *data)
1037 struct pipe_arg *pipe;
1038 unsigned int new_fb_id;
1043 if (pipe->current_fb_id == pipe->fb_id[0])
1044 new_fb_id = pipe->fb_id[1];
1046 new_fb_id = pipe->fb_id[0];
1048 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
1049 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1050 pipe->current_fb_id = new_fb_id;
1052 if (pipe->swap_count == 60) {
1053 gettimeofday(&end, NULL);
1054 t = end.tv_sec + end.tv_usec * 1e-6 -
1055 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1056 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1057 pipe->swap_count = 0;
1062 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1066 for (i = 0; i < ovr->count_formats; ++i) {
1067 if (ovr->formats[i] == fmt)
1074 static void add_property(struct device *dev, uint32_t obj_id,
1075 const char *name, uint64_t value)
1077 struct property_arg p;
1080 strcpy(p.name, name);
1083 set_property(dev, &p);
1086 static bool add_property_optional(struct device *dev, uint32_t obj_id,
1087 const char *name, uint64_t value)
1089 struct property_arg p;
1092 strcpy(p.name, name);
1096 return set_property(dev, &p);
1099 static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
1101 unsigned blob_id = 0;
1102 /* TODO: support 1024-sized LUTs, when the use-case arises */
1103 struct drm_color_lut gamma_lut[256];
1106 if (fourcc == DRM_FORMAT_C8) {
1107 /* TODO: Add C8 support for more patterns */
1108 util_smpte_c8_gamma(256, gamma_lut);
1109 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
1111 for (i = 0; i < 256; i++) {
1113 gamma_lut[i].green =
1114 gamma_lut[i].blue = i << 8;
1118 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
1119 add_property_optional(dev, crtc_id, "CTM", 0);
1120 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
1121 uint16_t r[256], g[256], b[256];
1123 for (i = 0; i < 256; i++) {
1124 r[i] = gamma_lut[i].red;
1125 g[i] = gamma_lut[i].green;
1126 b[i] = gamma_lut[i].blue;
1129 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
1131 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
1135 static int atomic_set_plane(struct device *dev, struct plane_arg *p,
1136 int pattern, bool update)
1138 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1139 struct bo *plane_bo;
1140 int crtc_x, crtc_y, crtc_w, crtc_h;
1141 struct crtc *crtc = NULL;
1143 unsigned int old_fb_id;
1145 /* Find an unused plane which can be connected to our CRTC. Find the
1146 * CRTC index first, then iterate over available planes.
1148 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1149 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1150 crtc = &dev->resources->crtcs[i];
1156 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1161 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
1162 p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
1164 plane_bo = p->old_bo;
1168 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h,
1169 handles, pitches, offsets, pattern);
1171 if (plane_bo == NULL)
1174 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1175 handles, pitches, offsets, &p->fb_id, 0)) {
1176 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1183 old_fb_id = p->fb_id;
1184 p->old_fb_id = old_fb_id;
1186 crtc_w = p->w * p->scale;
1187 crtc_h = p->h * p->scale;
1188 if (!p->has_position) {
1189 /* Default to the middle of the screen */
1190 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1191 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1197 add_property(dev, p->plane_id, "FB_ID", p->fb_id);
1198 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
1199 add_property(dev, p->plane_id, "SRC_X", 0);
1200 add_property(dev, p->plane_id, "SRC_Y", 0);
1201 add_property(dev, p->plane_id, "SRC_W", p->w << 16);
1202 add_property(dev, p->plane_id, "SRC_H", p->h << 16);
1203 add_property(dev, p->plane_id, "CRTC_X", crtc_x);
1204 add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
1205 add_property(dev, p->plane_id, "CRTC_W", crtc_w);
1206 add_property(dev, p->plane_id, "CRTC_H", crtc_h);
1211 static int set_plane(struct device *dev, struct plane_arg *p)
1214 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1216 struct bo *plane_bo;
1217 uint32_t plane_flags = 0;
1218 int crtc_x, crtc_y, crtc_w, crtc_h;
1219 struct crtc *crtc = NULL;
1223 /* Find an unused plane which can be connected to our CRTC. Find the
1224 * CRTC index first, then iterate over available planes.
1226 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1227 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1228 crtc = &dev->resources->crtcs[i];
1235 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1239 plane_id = p->plane_id;
1241 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
1242 ovr = dev->resources->planes[i].plane;
1246 if (plane_id && plane_id != ovr->plane_id)
1249 if (!format_support(ovr, p->fourcc))
1252 if ((ovr->possible_crtcs & (1 << pipe)) &&
1253 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1254 plane_id = ovr->plane_id;
1259 if (i == dev->resources->plane_res->count_planes) {
1260 fprintf(stderr, "no unused plane available for CRTC %u\n",
1261 crtc->crtc->crtc_id);
1265 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1266 p->w, p->h, p->format_str, plane_id);
1268 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1269 pitches, offsets, secondary_fill);
1270 if (plane_bo == NULL)
1275 /* just use single plane format for now.. */
1276 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1277 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1278 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1282 crtc_w = p->w * p->scale;
1283 crtc_h = p->h * p->scale;
1284 if (!p->has_position) {
1285 /* Default to the middle of the screen */
1286 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1287 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1293 /* note src coords (last 4 args) are in Q16 format */
1294 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1295 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1296 0, 0, p->w << 16, p->h << 16)) {
1297 fprintf(stderr, "failed to enable plane: %s\n",
1302 ovr->crtc_id = crtc->crtc->crtc_id;
1307 static void atomic_set_planes(struct device *dev, struct plane_arg *p,
1308 unsigned int count, bool update)
1310 unsigned int i, pattern = primary_fill;
1313 for (i = 0; i < count; i++) {
1315 pattern = secondary_fill;
1317 set_gamma(dev, p[i].crtc_id, p[i].fourcc);
1319 if (atomic_set_plane(dev, &p[i], pattern, update))
1324 static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1328 for (i = 0; i < count; i++) {
1329 add_property(dev, p[i].plane_id, "FB_ID", 0);
1330 add_property(dev, p[i].plane_id, "CRTC_ID", 0);
1331 add_property(dev, p[i].plane_id, "SRC_X", 0);
1332 add_property(dev, p[i].plane_id, "SRC_Y", 0);
1333 add_property(dev, p[i].plane_id, "SRC_W", 0);
1334 add_property(dev, p[i].plane_id, "SRC_H", 0);
1335 add_property(dev, p[i].plane_id, "CRTC_X", 0);
1336 add_property(dev, p[i].plane_id, "CRTC_Y", 0);
1337 add_property(dev, p[i].plane_id, "CRTC_W", 0);
1338 add_property(dev, p[i].plane_id, "CRTC_H", 0);
1342 static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
1346 for (i = 0; i < count; i++) {
1348 drmModeRmFB(dev->fd, p[i].fb_id);
1351 if (p[i].old_fb_id) {
1352 drmModeRmFB(dev->fd, p[i].old_fb_id);
1356 bo_destroy(p[i].bo);
1360 bo_destroy(p[i].old_bo);
1367 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1371 for (i = 0; i < count; i++) {
1373 drmModeRmFB(dev->fd, p[i].fb_id);
1375 bo_destroy(p[i].bo);
1379 static void atomic_set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1385 for (i = 0; i < count; i++) {
1386 struct pipe_arg *pipe = &pipes[i];
1388 ret = pipe_find_crtc_and_mode(dev, pipe);
1393 for (i = 0; i < count; i++) {
1394 struct pipe_arg *pipe = &pipes[i];
1397 if (pipe->mode == NULL)
1400 printf("setting mode %s-%.2fHz on connectors ",
1401 pipe->mode_str, pipe->vrefresh);
1402 for (j = 0; j < pipe->num_cons; ++j) {
1403 printf("%s, ", pipe->cons[j]);
1404 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc->crtc->crtc_id);
1406 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1408 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
1409 add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", blob_id);
1410 add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 1);
1414 static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1419 for (i = 0; i < count; i++) {
1420 struct pipe_arg *pipe = &pipes[i];
1422 if (pipe->mode == NULL)
1425 for (j = 0; j < pipe->num_cons; ++j)
1426 add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
1428 add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", 0);
1429 add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 0);
1433 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1435 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1442 dev->mode.width = 0;
1443 dev->mode.height = 0;
1444 dev->mode.fb_id = 0;
1446 for (i = 0; i < count; i++) {
1447 struct pipe_arg *pipe = &pipes[i];
1449 ret = pipe_find_crtc_and_mode(dev, pipe);
1453 dev->mode.width += pipe->mode->hdisplay;
1454 if (dev->mode.height < pipe->mode->vdisplay)
1455 dev->mode.height = pipe->mode->vdisplay;
1458 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1459 dev->mode.height, handles, pitches, offsets,
1466 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1467 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1469 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1470 dev->mode.width, dev->mode.height, strerror(errno));
1474 dev->mode.fb_id = fb_id;
1477 for (i = 0; i < count; i++) {
1478 struct pipe_arg *pipe = &pipes[i];
1480 if (pipe->mode == NULL)
1483 printf("setting mode %s-%.2fHz@%s on connectors ",
1484 pipe->mode_str, pipe->vrefresh, pipe->format_str);
1485 for (j = 0; j < pipe->num_cons; ++j)
1486 printf("%s, ", pipe->cons[j]);
1487 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1489 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1490 x, 0, pipe->con_ids, pipe->num_cons,
1493 /* XXX: Actually check if this is needed */
1494 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1496 x += pipe->mode->hdisplay;
1499 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1503 set_gamma(dev, pipe->crtc->crtc->crtc_id, pipe->fourcc);
1507 static void clear_mode(struct device *dev)
1509 if (dev->mode.fb_id)
1510 drmModeRmFB(dev->fd, dev->mode.fb_id);
1512 bo_destroy(dev->mode.bo);
1515 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1519 /* set up planes/overlays */
1520 for (i = 0; i < count; i++)
1521 if (set_plane(dev, &p[i]))
1525 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1527 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1532 /* maybe make cursor width/height configurable some day */
1536 /* create cursor bo.. just using PATTERN_PLAIN as it has
1539 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1540 offsets, UTIL_PATTERN_PLAIN);
1544 dev->mode.cursor_bo = bo;
1546 for (i = 0; i < count; i++) {
1547 struct pipe_arg *pipe = &pipes[i];
1548 ret = cursor_init(dev->fd, handles[0],
1549 pipe->crtc->crtc->crtc_id,
1550 pipe->mode->hdisplay, pipe->mode->vdisplay,
1553 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1562 static void clear_cursors(struct device *dev)
1566 if (dev->mode.cursor_bo)
1567 bo_destroy(dev->mode.cursor_bo);
1570 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1572 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1573 unsigned int other_fb_id;
1574 struct bo *other_bo;
1575 drmEventContext evctx;
1579 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1580 dev->mode.height, handles, pitches, offsets,
1581 UTIL_PATTERN_PLAIN);
1582 if (other_bo == NULL)
1585 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1586 pipes[0].fourcc, handles, pitches, offsets,
1589 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1593 for (i = 0; i < count; i++) {
1594 struct pipe_arg *pipe = &pipes[i];
1596 if (pipe->mode == NULL)
1599 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1600 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1603 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1606 gettimeofday(&pipe->start, NULL);
1607 pipe->swap_count = 0;
1608 pipe->fb_id[0] = dev->mode.fb_id;
1609 pipe->fb_id[1] = other_fb_id;
1610 pipe->current_fb_id = other_fb_id;
1613 memset(&evctx, 0, sizeof evctx);
1614 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1615 evctx.vblank_handler = NULL;
1616 evctx.page_flip_handler = page_flip_handler;
1620 struct pollfd pfd[2];
1623 pfd[0].events = POLLIN;
1625 pfd[1].events = POLLIN;
1627 if (poll(pfd, 2, -1) < 0) {
1628 fprintf(stderr, "poll error\n");
1635 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1640 FD_SET(dev->fd, &fds);
1641 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1644 fprintf(stderr, "select timed out or error (ret %d)\n",
1647 } else if (FD_ISSET(0, &fds)) {
1652 drmHandleEvent(dev->fd, &evctx);
1656 drmModeRmFB(dev->fd, other_fb_id);
1658 bo_destroy(other_bo);
1661 #define min(a, b) ((a) < (b) ? (a) : (b))
1663 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1671 pipe->crtc_id = (uint32_t)-1;
1672 strcpy(pipe->format_str, "XR24");
1674 /* Count the number of connectors and allocate them. */
1676 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1681 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1682 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1683 if (pipe->con_ids == NULL || pipe->cons == NULL)
1686 /* Parse the connectors. */
1687 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1688 endp = strpbrk(p, ",@:");
1692 pipe->cons[i] = strndup(p, endp - p);
1698 if (i != pipe->num_cons - 1)
1701 /* Parse the remaining parameters. */
1704 pipe->crtc_id = strtoul(arg, &endp, 10);
1711 /* Search for the vertical refresh or the format. */
1712 p = strpbrk(arg, "-@");
1714 p = arg + strlen(arg);
1715 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1716 strncpy(pipe->mode_str, arg, len);
1717 pipe->mode_str[len] = '\0';
1720 pipe->vrefresh = strtof(p + 1, &endp);
1725 strncpy(pipe->format_str, p + 1, 4);
1726 pipe->format_str[4] = '\0';
1729 pipe->fourcc = util_format_fourcc(pipe->format_str);
1730 if (pipe->fourcc == 0) {
1731 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1738 static int parse_plane(struct plane_arg *plane, const char *p)
1742 plane->plane_id = strtoul(p, &end, 10);
1747 plane->crtc_id = strtoul(p, &end, 10);
1752 plane->w = strtoul(p, &end, 10);
1757 plane->h = strtoul(p, &end, 10);
1759 if (*end == '+' || *end == '-') {
1760 plane->x = strtol(end, &end, 10);
1761 if (*end != '+' && *end != '-')
1763 plane->y = strtol(end, &end, 10);
1765 plane->has_position = true;
1770 plane->scale = strtod(p, &end);
1771 if (plane->scale <= 0.0)
1778 strncpy(plane->format_str, end + 1, 4);
1779 plane->format_str[4] = '\0';
1781 strcpy(plane->format_str, "XR24");
1784 plane->fourcc = util_format_fourcc(plane->format_str);
1785 if (plane->fourcc == 0) {
1786 fprintf(stderr, "unknown format %s\n", plane->format_str);
1793 static int parse_property(struct property_arg *p, const char *arg)
1795 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1799 p->name[DRM_PROP_NAME_LEN] = '\0';
1804 static void parse_fill_patterns(char *arg)
1806 char *fill = strtok(arg, ",");
1809 primary_fill = util_pattern_enum(fill);
1810 fill = strtok(NULL, ",");
1813 secondary_fill = util_pattern_enum(fill);
1816 static void usage(char *name)
1818 fprintf(stderr, "usage: %s [-acDdefMPpsCvw]\n", name);
1820 fprintf(stderr, "\n Query options:\n\n");
1821 fprintf(stderr, "\t-c\tlist connectors\n");
1822 fprintf(stderr, "\t-e\tlist encoders\n");
1823 fprintf(stderr, "\t-f\tlist framebuffers\n");
1824 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1826 fprintf(stderr, "\n Test options:\n\n");
1827 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1828 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1829 fprintf(stderr, "\t-C\ttest hw cursor\n");
1830 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1831 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1832 fprintf(stderr, "\t-a \tuse atomic API\n");
1833 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
1835 fprintf(stderr, "\n Generic options:\n\n");
1836 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1837 fprintf(stderr, "\t-M module\tuse the given driver\n");
1838 fprintf(stderr, "\t-D device\tuse the given device\n");
1840 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1844 static int page_flipping_supported(void)
1846 /*FIXME: generic ioctl needed? */
1850 struct drm_i915_getparam gp;
1852 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1855 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1857 fprintf(stderr, "drm_i915_getparam: %m\n");
1865 static int cursor_supported(void)
1867 /*FIXME: generic ioctl needed? */
1871 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1873 drmModeConnector *connector;
1878 for (i = 0; i < pipe->num_cons; i++) {
1879 id = strtoul(pipe->cons[i], &endp, 10);
1880 if (endp == pipe->cons[i]) {
1881 connector = get_connector_by_name(dev, pipe->cons[i]);
1883 fprintf(stderr, "no connector named '%s'\n",
1888 id = connector->connector_id;
1891 pipe->con_ids[i] = id;
1897 static char optstr[] = "acdD:efF:M:P:ps:Cvw:";
1899 int main(int argc, char **argv)
1904 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1905 int drop_master = 0;
1907 int test_cursor = 0;
1909 char *device = NULL;
1910 char *module = NULL;
1912 unsigned int count = 0, plane_count = 0;
1913 unsigned int prop_count = 0;
1914 struct pipe_arg *pipe_args = NULL;
1915 struct plane_arg *plane_args = NULL;
1916 struct property_arg *prop_args = NULL;
1917 unsigned int args = 0;
1920 memset(&dev, 0, sizeof dev);
1923 while ((c = getopt(argc, argv, optstr)) != -1) {
1947 parse_fill_patterns(optarg);
1951 /* Preserve the default behaviour of dumping all information. */
1955 plane_args = realloc(plane_args,
1956 (plane_count + 1) * sizeof *plane_args);
1957 if (plane_args == NULL) {
1958 fprintf(stderr, "memory allocation failed\n");
1961 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1963 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1973 pipe_args = realloc(pipe_args,
1974 (count + 1) * sizeof *pipe_args);
1975 if (pipe_args == NULL) {
1976 fprintf(stderr, "memory allocation failed\n");
1979 memset(&pipe_args[count], 0, sizeof(*pipe_args));
1981 if (parse_connector(&pipe_args[count], optarg) < 0)
1993 prop_args = realloc(prop_args,
1994 (prop_count + 1) * sizeof *prop_args);
1995 if (prop_args == NULL) {
1996 fprintf(stderr, "memory allocation failed\n");
1999 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
2001 if (parse_property(&prop_args[prop_count], optarg) < 0)
2012 if (!args || (args == 1 && use_atomic))
2013 encoders = connectors = crtcs = planes = framebuffers = 1;
2015 dev.fd = util_open(device, module);
2019 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
2020 if (ret && use_atomic) {
2021 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
2026 dev.use_atomic = use_atomic;
2028 if (test_vsync && !page_flipping_supported()) {
2029 fprintf(stderr, "page flipping not supported by drm.\n");
2033 if (test_vsync && !count) {
2034 fprintf(stderr, "page flipping requires at least one -s option.\n");
2038 if (test_cursor && !cursor_supported()) {
2039 fprintf(stderr, "hw cursor not supported by drm.\n");
2043 dev.resources = get_resources(&dev);
2044 if (!dev.resources) {
2049 for (i = 0; i < count; i++) {
2050 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
2051 free_resources(dev.resources);
2057 #define dump_resource(dev, res) if (res) dump_##res(dev)
2059 dump_resource(&dev, encoders);
2060 dump_resource(&dev, connectors);
2061 dump_resource(&dev, crtcs);
2062 dump_resource(&dev, planes);
2063 dump_resource(&dev, framebuffers);
2065 for (i = 0; i < prop_count; ++i)
2066 set_property(&dev, &prop_args[i]);
2068 if (dev.use_atomic) {
2069 dev.req = drmModeAtomicAlloc();
2071 if (count && plane_count) {
2074 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2075 if (ret || cap == 0) {
2076 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2080 atomic_set_mode(&dev, pipe_args, count);
2081 atomic_set_planes(&dev, plane_args, plane_count, false);
2083 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2085 fprintf(stderr, "Atomic Commit failed [1]\n");
2089 gettimeofday(&pipe_args->start, NULL);
2090 pipe_args->swap_count = 0;
2092 while (test_vsync) {
2093 drmModeAtomicFree(dev.req);
2094 dev.req = drmModeAtomicAlloc();
2095 atomic_set_planes(&dev, plane_args, plane_count, true);
2097 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2099 fprintf(stderr, "Atomic Commit failed [2]\n");
2103 pipe_args->swap_count++;
2104 if (pipe_args->swap_count == 60) {
2108 gettimeofday(&end, NULL);
2109 t = end.tv_sec + end.tv_usec * 1e-6 -
2110 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
2111 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
2112 pipe_args->swap_count = 0;
2113 pipe_args->start = end;
2118 drmDropMaster(dev.fd);
2122 drmModeAtomicFree(dev.req);
2123 dev.req = drmModeAtomicAlloc();
2125 atomic_clear_mode(&dev, pipe_args, count);
2126 atomic_clear_planes(&dev, plane_args, plane_count);
2127 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2129 fprintf(stderr, "Atomic Commit failed\n");
2133 atomic_clear_FB(&dev, plane_args, plane_count);
2136 drmModeAtomicFree(dev.req);
2138 if (count || plane_count) {
2141 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2142 if (ret || cap == 0) {
2143 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2148 set_mode(&dev, pipe_args, count);
2151 set_planes(&dev, plane_args, plane_count);
2154 set_cursors(&dev, pipe_args, count);
2157 test_page_flip(&dev, pipe_args, count);
2160 drmDropMaster(dev.fd);
2165 clear_cursors(&dev);
2168 clear_planes(&dev, plane_args, plane_count);
2175 free_resources(dev.resources);