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, int index)
219 printf(" #%i %s %.2f %d %d %d %d %d %d %d %d %d",
234 mode_flag_str(mode->flags);
236 mode_type_str(mode->type);
240 static void dump_blob(struct device *dev, uint32_t blob_id)
243 unsigned char *blob_data;
244 drmModePropertyBlobPtr blob;
246 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
252 blob_data = blob->data;
254 for (i = 0; i < blob->length; i++) {
257 printf("%.2hhx", blob_data[i]);
261 drmModeFreePropertyBlob(blob);
264 static const char *modifier_to_string(uint64_t modifier)
267 case DRM_FORMAT_MOD_INVALID:
269 case DRM_FORMAT_MOD_LINEAR:
271 case I915_FORMAT_MOD_X_TILED:
273 case I915_FORMAT_MOD_Y_TILED:
275 case I915_FORMAT_MOD_Yf_TILED:
277 case I915_FORMAT_MOD_Y_TILED_CCS:
278 return "Y_TILED_CCS";
279 case I915_FORMAT_MOD_Yf_TILED_CCS:
280 return "Yf_TILED_CCS";
281 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
282 return "SAMSUNG_64_32_TILE";
283 case DRM_FORMAT_MOD_VIVANTE_TILED:
284 return "VIVANTE_TILED";
285 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
286 return "VIVANTE_SUPER_TILED";
287 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
288 return "VIVANTE_SPLIT_TILED";
289 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
290 return "VIVANTE_SPLIT_SUPER_TILED";
291 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
292 return "NVIDIA_TEGRA_TILED";
293 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
294 return "NVIDIA_16BX2_BLOCK(0)";
295 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
296 return "NVIDIA_16BX2_BLOCK(1)";
297 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
298 return "NVIDIA_16BX2_BLOCK(2)";
299 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
300 return "NVIDIA_16BX2_BLOCK(3)";
301 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
302 return "NVIDIA_16BX2_BLOCK(4)";
303 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
304 return "NVIDIA_16BX2_BLOCK(5)";
305 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
306 return "MOD_BROADCOM_VC4_T_TILED";
307 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
308 return "QCOM_COMPRESSED";
310 return "(UNKNOWN MODIFIER)";
314 static void dump_in_formats(struct device *dev, uint32_t blob_id)
317 drmModePropertyBlobPtr blob;
318 struct drm_format_modifier_blob *header;
320 struct drm_format_modifier *modifiers;
322 printf("\t\tin_formats blob decoded:\n");
323 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
330 formats = (uint32_t *) ((char *) header + header->formats_offset);
331 modifiers = (struct drm_format_modifier *)
332 ((char *) header + header->modifiers_offset);
334 for (i = 0; i < header->count_formats; i++) {
336 dump_fourcc(formats[i]);
338 for (j = 0; j < header->count_modifiers; j++) {
339 uint64_t mask = 1ULL << i;
340 if (modifiers[j].formats & mask)
341 printf(" %s", modifier_to_string(modifiers[j].modifier));
346 drmModeFreePropertyBlob(blob);
349 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
350 uint32_t prop_id, uint64_t value)
353 printf("\t%d", prop_id);
359 printf(" %s:\n", prop->name);
361 printf("\t\tflags:");
362 if (prop->flags & DRM_MODE_PROP_PENDING)
364 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
365 printf(" immutable");
366 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
367 printf(" signed range");
368 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
370 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
372 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
374 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
376 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
380 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
381 printf("\t\tvalues:");
382 for (i = 0; i < prop->count_values; i++)
383 printf(" %"PRId64, U642I64(prop->values[i]));
387 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
388 printf("\t\tvalues:");
389 for (i = 0; i < prop->count_values; i++)
390 printf(" %"PRIu64, prop->values[i]);
394 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
395 printf("\t\tenums:");
396 for (i = 0; i < prop->count_enums; i++)
397 printf(" %s=%llu", prop->enums[i].name,
398 prop->enums[i].value);
400 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
401 printf("\t\tvalues:");
402 for (i = 0; i < prop->count_enums; i++)
403 printf(" %s=0x%llx", prop->enums[i].name,
404 (1LL << prop->enums[i].value));
407 assert(prop->count_enums == 0);
410 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
411 printf("\t\tblobs:\n");
412 for (i = 0; i < prop->count_blobs; i++)
413 dump_blob(dev, prop->blob_ids[i]);
416 assert(prop->count_blobs == 0);
419 printf("\t\tvalue:");
420 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
421 dump_blob(dev, value);
422 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
423 printf(" %"PRId64"\n", value);
425 printf(" %"PRIu64"\n", value);
427 if (strcmp(prop->name, "IN_FORMATS") == 0)
428 dump_in_formats(dev, value);
431 static void dump_connectors(struct device *dev)
435 printf("Connectors:\n");
436 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
437 for (i = 0; i < dev->resources->res->count_connectors; i++) {
438 struct connector *_connector = &dev->resources->connectors[i];
439 drmModeConnector *connector = _connector->connector;
443 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
444 connector->connector_id,
445 connector->encoder_id,
446 util_lookup_connector_status_name(connector->connection),
448 connector->mmWidth, connector->mmHeight,
449 connector->count_modes);
451 for (j = 0; j < connector->count_encoders; j++)
452 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
455 if (connector->count_modes) {
457 printf("\tindex name refresh (Hz) hdisp hss hse htot vdisp "
459 for (j = 0; j < connector->count_modes; j++)
460 dump_mode(&connector->modes[j], j);
463 if (_connector->props) {
465 for (j = 0; j < (int)_connector->props->count_props; j++)
466 dump_prop(dev, _connector->props_info[j],
467 _connector->props->props[j],
468 _connector->props->prop_values[j]);
474 static void dump_crtcs(struct device *dev)
480 printf("id\tfb\tpos\tsize\n");
481 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
482 struct crtc *_crtc = &dev->resources->crtcs[i];
483 drmModeCrtc *crtc = _crtc->crtc;
487 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
491 crtc->width, crtc->height);
492 dump_mode(&crtc->mode, 0);
496 for (j = 0; j < _crtc->props->count_props; j++)
497 dump_prop(dev, _crtc->props_info[j],
498 _crtc->props->props[j],
499 _crtc->props->prop_values[j]);
501 printf(" no properties found\n");
507 static void dump_framebuffers(struct device *dev)
512 printf("Frame buffers:\n");
513 printf("id\tsize\tpitch\n");
514 for (i = 0; i < dev->resources->res->count_fbs; i++) {
515 fb = dev->resources->fbs[i].fb;
519 printf("%u\t(%ux%u)\t%u\n",
521 fb->width, fb->height,
527 static void dump_planes(struct device *dev)
532 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
534 if (!dev->resources->plane_res)
537 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
538 struct plane *plane = &dev->resources->planes[i];
539 drmModePlane *ovr = plane->plane;
543 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
544 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
545 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
546 ovr->gamma_size, ovr->possible_crtcs);
548 if (!ovr->count_formats)
552 for (j = 0; j < ovr->count_formats; j++)
553 dump_fourcc(ovr->formats[j]);
558 for (j = 0; j < plane->props->count_props; j++)
559 dump_prop(dev, plane->props_info[j],
560 plane->props->props[j],
561 plane->props->prop_values[j]);
563 printf(" no properties found\n");
571 static void free_resources(struct resources *res)
578 #define free_resource(_res, __res, type, Type) \
580 if (!(_res)->type##s) \
582 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
583 if (!(_res)->type##s[i].type) \
585 drmModeFree##Type((_res)->type##s[i].type); \
587 free((_res)->type##s); \
590 #define free_properties(_res, __res, type) \
592 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
593 drmModeFreeObjectProperties(res->type##s[i].props); \
594 free(res->type##s[i].props_info); \
599 free_properties(res, res, crtc);
601 free_resource(res, res, crtc, Crtc);
602 free_resource(res, res, encoder, Encoder);
604 for (i = 0; i < res->res->count_connectors; i++)
605 free(res->connectors[i].name);
607 free_resource(res, res, connector, Connector);
608 free_resource(res, res, fb, FB);
610 drmModeFreeResources(res->res);
613 if (res->plane_res) {
614 free_properties(res, plane_res, plane);
616 free_resource(res, plane_res, plane, Plane);
618 drmModeFreePlaneResources(res->plane_res);
624 static struct resources *get_resources(struct device *dev)
626 struct resources *res;
629 res = calloc(1, sizeof(*res));
633 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
635 res->res = drmModeGetResources(dev->fd);
637 fprintf(stderr, "drmModeGetResources failed: %s\n",
642 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
643 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
644 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
645 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
647 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
650 #define get_resource(_res, __res, type, Type) \
652 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
653 (_res)->type##s[i].type = \
654 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
655 if (!(_res)->type##s[i].type) \
656 fprintf(stderr, "could not get %s %i: %s\n", \
657 #type, (_res)->__res->type##s[i], \
662 get_resource(res, res, crtc, Crtc);
663 get_resource(res, res, encoder, Encoder);
664 get_resource(res, res, connector, Connector);
665 get_resource(res, res, fb, FB);
667 /* Set the name of all connectors based on the type name and the per-type ID. */
668 for (i = 0; i < res->res->count_connectors; i++) {
669 struct connector *connector = &res->connectors[i];
670 drmModeConnector *conn = connector->connector;
673 num = asprintf(&connector->name, "%s-%u",
674 util_lookup_connector_type_name(conn->connector_type),
675 conn->connector_type_id);
680 #define get_properties(_res, __res, type, Type) \
682 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
683 struct type *obj = &res->type##s[i]; \
686 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
687 DRM_MODE_OBJECT_##Type); \
690 "could not get %s %i properties: %s\n", \
691 #type, obj->type->type##_id, \
695 obj->props_info = calloc(obj->props->count_props, \
696 sizeof(*obj->props_info)); \
697 if (!obj->props_info) \
699 for (j = 0; j < obj->props->count_props; ++j) \
700 obj->props_info[j] = \
701 drmModeGetProperty(dev->fd, obj->props->props[j]); \
705 get_properties(res, res, crtc, CRTC);
706 get_properties(res, res, connector, CONNECTOR);
708 for (i = 0; i < res->res->count_crtcs; ++i)
709 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
711 res->plane_res = drmModeGetPlaneResources(dev->fd);
712 if (!res->plane_res) {
713 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
718 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
722 get_resource(res, plane_res, plane, Plane);
723 get_properties(res, plane_res, plane, PLANE);
732 static int get_crtc_index(struct device *dev, uint32_t id)
736 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
737 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
738 if (crtc && crtc->crtc_id == id)
745 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
747 struct connector *connector;
750 for (i = 0; i < dev->resources->res->count_connectors; i++) {
751 connector = &dev->resources->connectors[i];
753 if (strcmp(connector->name, name) == 0)
754 return connector->connector;
760 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
762 drmModeConnector *connector;
765 for (i = 0; i < dev->resources->res->count_connectors; i++) {
766 connector = dev->resources->connectors[i].connector;
767 if (connector && connector->connector_id == id)
774 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
776 drmModeEncoder *encoder;
779 for (i = 0; i < dev->resources->res->count_encoders; i++) {
780 encoder = dev->resources->encoders[i].encoder;
781 if (encoder && encoder->encoder_id == id)
788 /* -----------------------------------------------------------------------------
793 * Mode setting with the kernel interfaces is a bit of a chore.
794 * First you have to find the connector in question and make sure the
795 * requested mode is available.
796 * Then you need to find the encoder attached to that connector so you
797 * can bind it with a free crtc.
802 unsigned int num_cons;
808 drmModeModeInfo *mode;
810 unsigned int fb_id[2], current_fb_id;
811 struct timeval start;
817 uint32_t plane_id; /* the id of plane to use */
818 uint32_t crtc_id; /* the id of CRTC to bind to */
824 unsigned int old_fb_id;
827 char format_str[5]; /* need to leave room for terminating \0 */
831 static drmModeModeInfo *
832 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
833 const float vrefresh)
835 drmModeConnector *connector;
836 drmModeModeInfo *mode;
839 connector = get_connector_by_id(dev, con_id);
840 if (!connector || !connector->count_modes)
844 if (mode_str[0] == '#') {
845 int index = atoi(mode_str + 1);
847 if (index >= connector->count_modes || index < 0)
849 return &connector->modes[index];
853 for (i = 0; i < connector->count_modes; i++) {
854 mode = &connector->modes[i];
855 if (!strcmp(mode->name, mode_str)) {
856 /* If the vertical refresh frequency is not specified
857 * then return the first mode that match with the name.
858 * Else, return the mode that match the name and
859 * the specified vertical refresh frequency.
863 else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005)
871 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
873 uint32_t possible_crtcs = ~0;
874 uint32_t active_crtcs = 0;
875 unsigned int crtc_idx;
879 for (i = 0; i < pipe->num_cons; ++i) {
880 uint32_t crtcs_for_connector = 0;
881 drmModeConnector *connector;
882 drmModeEncoder *encoder;
885 connector = get_connector_by_id(dev, pipe->con_ids[i]);
889 for (j = 0; j < connector->count_encoders; ++j) {
890 encoder = get_encoder_by_id(dev, connector->encoders[j]);
894 crtcs_for_connector |= encoder->possible_crtcs;
896 idx = get_crtc_index(dev, encoder->crtc_id);
898 active_crtcs |= 1 << idx;
901 possible_crtcs &= crtcs_for_connector;
907 /* Return the first possible and active CRTC if one exists, or the first
908 * possible CRTC otherwise.
910 if (possible_crtcs & active_crtcs)
911 crtc_idx = ffs(possible_crtcs & active_crtcs);
913 crtc_idx = ffs(possible_crtcs);
915 return &dev->resources->crtcs[crtc_idx - 1];
918 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
920 drmModeModeInfo *mode = NULL;
925 for (i = 0; i < (int)pipe->num_cons; i++) {
926 mode = connector_find_mode(dev, pipe->con_ids[i],
927 pipe->mode_str, pipe->vrefresh);
931 "failed to find mode "
932 "\"%s-%.2fHz\" for connector %s\n",
933 pipe->mode_str, pipe->vrefresh, pipe->cons[i]);
936 "failed to find mode \"%s\" for connector %s\n",
937 pipe->mode_str, pipe->cons[i]);
942 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
943 * locate a CRTC that can be attached to all the connectors.
945 if (pipe->crtc_id != (uint32_t)-1) {
946 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
947 struct crtc *crtc = &dev->resources->crtcs[i];
949 if (pipe->crtc_id == crtc->crtc->crtc_id) {
955 pipe->crtc = pipe_find_crtc(dev, pipe);
959 fprintf(stderr, "failed to find CRTC for pipe\n");
964 pipe->crtc->mode = mode;
969 /* -----------------------------------------------------------------------------
973 struct property_arg {
976 char name[DRM_PROP_NAME_LEN+1];
982 static bool set_property(struct device *dev, struct property_arg *p)
984 drmModeObjectProperties *props = NULL;
985 drmModePropertyRes **props_info = NULL;
986 const char *obj_type;
993 #define find_object(_res, __res, type, Type) \
995 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
996 struct type *obj = &(_res)->type##s[i]; \
997 if (obj->type->type##_id != p->obj_id) \
999 p->obj_type = DRM_MODE_OBJECT_##Type; \
1001 props = obj->props; \
1002 props_info = obj->props_info; \
1006 find_object(dev->resources, res, crtc, CRTC);
1007 if (p->obj_type == 0)
1008 find_object(dev->resources, res, connector, CONNECTOR);
1009 if (p->obj_type == 0)
1010 find_object(dev->resources, plane_res, plane, PLANE);
1011 if (p->obj_type == 0) {
1012 fprintf(stderr, "Object %i not found, can't set property\n",
1018 fprintf(stderr, "%s %i has no properties\n",
1019 obj_type, p->obj_id);
1023 for (i = 0; i < (int)props->count_props; ++i) {
1026 if (strcmp(props_info[i]->name, p->name) == 0)
1030 if (i == (int)props->count_props) {
1032 fprintf(stderr, "%s %i has no %s property\n",
1033 obj_type, p->obj_id, p->name);
1037 p->prop_id = props->props[i];
1039 if (!dev->use_atomic)
1040 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
1041 p->prop_id, p->value);
1043 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
1046 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
1047 obj_type, p->obj_id, p->name, p->value, strerror(errno));
1052 /* -------------------------------------------------------------------------- */
1055 page_flip_handler(int fd, unsigned int frame,
1056 unsigned int sec, unsigned int usec, void *data)
1058 struct pipe_arg *pipe;
1059 unsigned int new_fb_id;
1064 if (pipe->current_fb_id == pipe->fb_id[0])
1065 new_fb_id = pipe->fb_id[1];
1067 new_fb_id = pipe->fb_id[0];
1069 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
1070 DRM_MODE_PAGE_FLIP_EVENT, pipe);
1071 pipe->current_fb_id = new_fb_id;
1073 if (pipe->swap_count == 60) {
1074 gettimeofday(&end, NULL);
1075 t = end.tv_sec + end.tv_usec * 1e-6 -
1076 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
1077 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
1078 pipe->swap_count = 0;
1083 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
1087 for (i = 0; i < ovr->count_formats; ++i) {
1088 if (ovr->formats[i] == fmt)
1095 static void add_property(struct device *dev, uint32_t obj_id,
1096 const char *name, uint64_t value)
1098 struct property_arg p;
1101 strcpy(p.name, name);
1104 set_property(dev, &p);
1107 static bool add_property_optional(struct device *dev, uint32_t obj_id,
1108 const char *name, uint64_t value)
1110 struct property_arg p;
1113 strcpy(p.name, name);
1117 return set_property(dev, &p);
1120 static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
1122 unsigned blob_id = 0;
1123 /* TODO: support 1024-sized LUTs, when the use-case arises */
1124 struct drm_color_lut gamma_lut[256];
1127 if (fourcc == DRM_FORMAT_C8) {
1128 /* TODO: Add C8 support for more patterns */
1129 util_smpte_c8_gamma(256, gamma_lut);
1130 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
1132 for (i = 0; i < 256; i++) {
1134 gamma_lut[i].green =
1135 gamma_lut[i].blue = i << 8;
1139 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
1140 add_property_optional(dev, crtc_id, "CTM", 0);
1141 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
1142 uint16_t r[256], g[256], b[256];
1144 for (i = 0; i < 256; i++) {
1145 r[i] = gamma_lut[i].red;
1146 g[i] = gamma_lut[i].green;
1147 b[i] = gamma_lut[i].blue;
1150 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
1152 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
1156 static int atomic_set_plane(struct device *dev, struct plane_arg *p,
1157 int pattern, bool update)
1159 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1160 struct bo *plane_bo;
1161 int crtc_x, crtc_y, crtc_w, crtc_h;
1162 struct crtc *crtc = NULL;
1164 unsigned int old_fb_id;
1166 /* Find an unused plane which can be connected to our CRTC. Find the
1167 * CRTC index first, then iterate over available planes.
1169 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1170 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1171 crtc = &dev->resources->crtcs[i];
1177 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1182 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
1183 p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
1185 plane_bo = p->old_bo;
1189 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h,
1190 handles, pitches, offsets, pattern);
1192 if (plane_bo == NULL)
1195 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1196 handles, pitches, offsets, &p->fb_id, 0)) {
1197 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1204 old_fb_id = p->fb_id;
1205 p->old_fb_id = old_fb_id;
1207 crtc_w = p->w * p->scale;
1208 crtc_h = p->h * p->scale;
1209 if (!p->has_position) {
1210 /* Default to the middle of the screen */
1211 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1212 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1218 add_property(dev, p->plane_id, "FB_ID", p->fb_id);
1219 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
1220 add_property(dev, p->plane_id, "SRC_X", 0);
1221 add_property(dev, p->plane_id, "SRC_Y", 0);
1222 add_property(dev, p->plane_id, "SRC_W", p->w << 16);
1223 add_property(dev, p->plane_id, "SRC_H", p->h << 16);
1224 add_property(dev, p->plane_id, "CRTC_X", crtc_x);
1225 add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
1226 add_property(dev, p->plane_id, "CRTC_W", crtc_w);
1227 add_property(dev, p->plane_id, "CRTC_H", crtc_h);
1232 static int set_plane(struct device *dev, struct plane_arg *p)
1235 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1237 struct bo *plane_bo;
1238 uint32_t plane_flags = 0;
1239 int crtc_x, crtc_y, crtc_w, crtc_h;
1240 struct crtc *crtc = NULL;
1244 /* Find an unused plane which can be connected to our CRTC. Find the
1245 * CRTC index first, then iterate over available planes.
1247 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
1248 if (p->crtc_id == dev->resources->res->crtcs[i]) {
1249 crtc = &dev->resources->crtcs[i];
1256 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
1260 plane_id = p->plane_id;
1262 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
1263 ovr = dev->resources->planes[i].plane;
1267 if (plane_id && plane_id != ovr->plane_id)
1270 if (!format_support(ovr, p->fourcc))
1273 if ((ovr->possible_crtcs & (1 << pipe)) &&
1274 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) {
1275 plane_id = ovr->plane_id;
1280 if (i == dev->resources->plane_res->count_planes) {
1281 fprintf(stderr, "no unused plane available for CRTC %u\n",
1282 crtc->crtc->crtc_id);
1286 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1287 p->w, p->h, p->format_str, plane_id);
1289 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1290 pitches, offsets, secondary_fill);
1291 if (plane_bo == NULL)
1296 /* just use single plane format for now.. */
1297 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1298 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1299 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1303 crtc_w = p->w * p->scale;
1304 crtc_h = p->h * p->scale;
1305 if (!p->has_position) {
1306 /* Default to the middle of the screen */
1307 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1308 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1314 /* note src coords (last 4 args) are in Q16 format */
1315 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1316 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1317 0, 0, p->w << 16, p->h << 16)) {
1318 fprintf(stderr, "failed to enable plane: %s\n",
1323 ovr->crtc_id = crtc->crtc->crtc_id;
1328 static void atomic_set_planes(struct device *dev, struct plane_arg *p,
1329 unsigned int count, bool update)
1331 unsigned int i, pattern = primary_fill;
1334 for (i = 0; i < count; i++) {
1336 pattern = secondary_fill;
1338 set_gamma(dev, p[i].crtc_id, p[i].fourcc);
1340 if (atomic_set_plane(dev, &p[i], pattern, update))
1345 static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1349 for (i = 0; i < count; i++) {
1350 add_property(dev, p[i].plane_id, "FB_ID", 0);
1351 add_property(dev, p[i].plane_id, "CRTC_ID", 0);
1352 add_property(dev, p[i].plane_id, "SRC_X", 0);
1353 add_property(dev, p[i].plane_id, "SRC_Y", 0);
1354 add_property(dev, p[i].plane_id, "SRC_W", 0);
1355 add_property(dev, p[i].plane_id, "SRC_H", 0);
1356 add_property(dev, p[i].plane_id, "CRTC_X", 0);
1357 add_property(dev, p[i].plane_id, "CRTC_Y", 0);
1358 add_property(dev, p[i].plane_id, "CRTC_W", 0);
1359 add_property(dev, p[i].plane_id, "CRTC_H", 0);
1363 static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
1367 for (i = 0; i < count; i++) {
1369 drmModeRmFB(dev->fd, p[i].fb_id);
1372 if (p[i].old_fb_id) {
1373 drmModeRmFB(dev->fd, p[i].old_fb_id);
1377 bo_destroy(p[i].bo);
1381 bo_destroy(p[i].old_bo);
1388 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1392 for (i = 0; i < count; i++) {
1394 drmModeRmFB(dev->fd, p[i].fb_id);
1396 bo_destroy(p[i].bo);
1400 static void atomic_set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1406 for (i = 0; i < count; i++) {
1407 struct pipe_arg *pipe = &pipes[i];
1409 ret = pipe_find_crtc_and_mode(dev, pipe);
1414 for (i = 0; i < count; i++) {
1415 struct pipe_arg *pipe = &pipes[i];
1418 if (pipe->mode == NULL)
1421 printf("setting mode %s-%.2fHz on connectors ",
1422 pipe->mode->name, mode_vrefresh(pipe->mode));
1423 for (j = 0; j < pipe->num_cons; ++j) {
1424 printf("%s, ", pipe->cons[j]);
1425 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc->crtc->crtc_id);
1427 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1429 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
1430 add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", blob_id);
1431 add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 1);
1435 static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1440 for (i = 0; i < count; i++) {
1441 struct pipe_arg *pipe = &pipes[i];
1443 if (pipe->mode == NULL)
1446 for (j = 0; j < pipe->num_cons; ++j)
1447 add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
1449 add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", 0);
1450 add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 0);
1454 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1456 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1463 dev->mode.width = 0;
1464 dev->mode.height = 0;
1465 dev->mode.fb_id = 0;
1467 for (i = 0; i < count; i++) {
1468 struct pipe_arg *pipe = &pipes[i];
1470 ret = pipe_find_crtc_and_mode(dev, pipe);
1474 dev->mode.width += pipe->mode->hdisplay;
1475 if (dev->mode.height < pipe->mode->vdisplay)
1476 dev->mode.height = pipe->mode->vdisplay;
1479 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1480 dev->mode.height, handles, pitches, offsets,
1487 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1488 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1490 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1491 dev->mode.width, dev->mode.height, strerror(errno));
1495 dev->mode.fb_id = fb_id;
1498 for (i = 0; i < count; i++) {
1499 struct pipe_arg *pipe = &pipes[i];
1501 if (pipe->mode == NULL)
1504 printf("setting mode %s-%.2fHz@%s on connectors ",
1505 pipe->mode->name, mode_vrefresh(pipe->mode),
1507 for (j = 0; j < pipe->num_cons; ++j)
1508 printf("%s, ", pipe->cons[j]);
1509 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1511 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1512 x, 0, pipe->con_ids, pipe->num_cons,
1515 /* XXX: Actually check if this is needed */
1516 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1518 x += pipe->mode->hdisplay;
1521 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1525 set_gamma(dev, pipe->crtc->crtc->crtc_id, pipe->fourcc);
1529 static void clear_mode(struct device *dev)
1531 if (dev->mode.fb_id)
1532 drmModeRmFB(dev->fd, dev->mode.fb_id);
1534 bo_destroy(dev->mode.bo);
1537 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1541 /* set up planes/overlays */
1542 for (i = 0; i < count; i++)
1543 if (set_plane(dev, &p[i]))
1547 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1549 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1554 /* maybe make cursor width/height configurable some day */
1558 /* create cursor bo.. just using PATTERN_PLAIN as it has
1561 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1562 offsets, UTIL_PATTERN_PLAIN);
1566 dev->mode.cursor_bo = bo;
1568 for (i = 0; i < count; i++) {
1569 struct pipe_arg *pipe = &pipes[i];
1570 ret = cursor_init(dev->fd, handles[0],
1571 pipe->crtc->crtc->crtc_id,
1572 pipe->mode->hdisplay, pipe->mode->vdisplay,
1575 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1584 static void clear_cursors(struct device *dev)
1588 if (dev->mode.cursor_bo)
1589 bo_destroy(dev->mode.cursor_bo);
1592 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1594 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1595 unsigned int other_fb_id;
1596 struct bo *other_bo;
1597 drmEventContext evctx;
1601 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1602 dev->mode.height, handles, pitches, offsets,
1603 UTIL_PATTERN_PLAIN);
1604 if (other_bo == NULL)
1607 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1608 pipes[0].fourcc, handles, pitches, offsets,
1611 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1615 for (i = 0; i < count; i++) {
1616 struct pipe_arg *pipe = &pipes[i];
1618 if (pipe->mode == NULL)
1621 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1622 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1625 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1628 gettimeofday(&pipe->start, NULL);
1629 pipe->swap_count = 0;
1630 pipe->fb_id[0] = dev->mode.fb_id;
1631 pipe->fb_id[1] = other_fb_id;
1632 pipe->current_fb_id = other_fb_id;
1635 memset(&evctx, 0, sizeof evctx);
1636 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1637 evctx.vblank_handler = NULL;
1638 evctx.page_flip_handler = page_flip_handler;
1642 struct pollfd pfd[2];
1645 pfd[0].events = POLLIN;
1647 pfd[1].events = POLLIN;
1649 if (poll(pfd, 2, -1) < 0) {
1650 fprintf(stderr, "poll error\n");
1657 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1662 FD_SET(dev->fd, &fds);
1663 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1666 fprintf(stderr, "select timed out or error (ret %d)\n",
1669 } else if (FD_ISSET(0, &fds)) {
1674 drmHandleEvent(dev->fd, &evctx);
1678 drmModeRmFB(dev->fd, other_fb_id);
1680 bo_destroy(other_bo);
1683 #define min(a, b) ((a) < (b) ? (a) : (b))
1685 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1693 pipe->crtc_id = (uint32_t)-1;
1694 strcpy(pipe->format_str, "XR24");
1696 /* Count the number of connectors and allocate them. */
1698 for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1703 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1704 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1705 if (pipe->con_ids == NULL || pipe->cons == NULL)
1708 /* Parse the connectors. */
1709 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1710 endp = strpbrk(p, ",@:");
1714 pipe->cons[i] = strndup(p, endp - p);
1720 if (i != pipe->num_cons - 1)
1723 /* Parse the remaining parameters. */
1726 pipe->crtc_id = strtoul(arg, &endp, 10);
1733 /* Search for the vertical refresh or the format. */
1734 p = strpbrk(arg, "-@");
1736 p = arg + strlen(arg);
1737 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1738 strncpy(pipe->mode_str, arg, len);
1739 pipe->mode_str[len] = '\0';
1742 pipe->vrefresh = strtof(p + 1, &endp);
1747 strncpy(pipe->format_str, p + 1, 4);
1748 pipe->format_str[4] = '\0';
1751 pipe->fourcc = util_format_fourcc(pipe->format_str);
1752 if (pipe->fourcc == 0) {
1753 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1760 static int parse_plane(struct plane_arg *plane, const char *p)
1764 plane->plane_id = strtoul(p, &end, 10);
1769 plane->crtc_id = strtoul(p, &end, 10);
1774 plane->w = strtoul(p, &end, 10);
1779 plane->h = strtoul(p, &end, 10);
1781 if (*end == '+' || *end == '-') {
1782 plane->x = strtol(end, &end, 10);
1783 if (*end != '+' && *end != '-')
1785 plane->y = strtol(end, &end, 10);
1787 plane->has_position = true;
1792 plane->scale = strtod(p, &end);
1793 if (plane->scale <= 0.0)
1800 strncpy(plane->format_str, end + 1, 4);
1801 plane->format_str[4] = '\0';
1803 strcpy(plane->format_str, "XR24");
1806 plane->fourcc = util_format_fourcc(plane->format_str);
1807 if (plane->fourcc == 0) {
1808 fprintf(stderr, "unknown format %s\n", plane->format_str);
1815 static int parse_property(struct property_arg *p, const char *arg)
1817 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1821 p->name[DRM_PROP_NAME_LEN] = '\0';
1826 static void parse_fill_patterns(char *arg)
1828 char *fill = strtok(arg, ",");
1831 primary_fill = util_pattern_enum(fill);
1832 fill = strtok(NULL, ",");
1835 secondary_fill = util_pattern_enum(fill);
1838 static void usage(char *name)
1840 fprintf(stderr, "usage: %s [-acDdefMPpsCvw]\n", name);
1842 fprintf(stderr, "\n Query options:\n\n");
1843 fprintf(stderr, "\t-c\tlist connectors\n");
1844 fprintf(stderr, "\t-e\tlist encoders\n");
1845 fprintf(stderr, "\t-f\tlist framebuffers\n");
1846 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1848 fprintf(stderr, "\n Test options:\n\n");
1849 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1850 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:[#<mode index>]<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1851 fprintf(stderr, "\t-C\ttest hw cursor\n");
1852 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1853 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1854 fprintf(stderr, "\t-a \tuse atomic API\n");
1855 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
1857 fprintf(stderr, "\n Generic options:\n\n");
1858 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1859 fprintf(stderr, "\t-M module\tuse the given driver\n");
1860 fprintf(stderr, "\t-D device\tuse the given device\n");
1862 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1866 static int page_flipping_supported(void)
1868 /*FIXME: generic ioctl needed? */
1872 struct drm_i915_getparam gp;
1874 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1877 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1879 fprintf(stderr, "drm_i915_getparam: %m\n");
1887 static int cursor_supported(void)
1889 /*FIXME: generic ioctl needed? */
1893 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1895 drmModeConnector *connector;
1900 for (i = 0; i < pipe->num_cons; i++) {
1901 id = strtoul(pipe->cons[i], &endp, 10);
1902 if (endp == pipe->cons[i]) {
1903 connector = get_connector_by_name(dev, pipe->cons[i]);
1905 fprintf(stderr, "no connector named '%s'\n",
1910 id = connector->connector_id;
1913 pipe->con_ids[i] = id;
1919 static char optstr[] = "acdD:efF:M:P:ps:Cvw:";
1921 int main(int argc, char **argv)
1926 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1927 int drop_master = 0;
1929 int test_cursor = 0;
1931 char *device = NULL;
1932 char *module = NULL;
1934 unsigned int count = 0, plane_count = 0;
1935 unsigned int prop_count = 0;
1936 struct pipe_arg *pipe_args = NULL;
1937 struct plane_arg *plane_args = NULL;
1938 struct property_arg *prop_args = NULL;
1939 unsigned int args = 0;
1942 memset(&dev, 0, sizeof dev);
1945 while ((c = getopt(argc, argv, optstr)) != -1) {
1969 parse_fill_patterns(optarg);
1973 /* Preserve the default behaviour of dumping all information. */
1977 plane_args = realloc(plane_args,
1978 (plane_count + 1) * sizeof *plane_args);
1979 if (plane_args == NULL) {
1980 fprintf(stderr, "memory allocation failed\n");
1983 memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1985 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1995 pipe_args = realloc(pipe_args,
1996 (count + 1) * sizeof *pipe_args);
1997 if (pipe_args == NULL) {
1998 fprintf(stderr, "memory allocation failed\n");
2001 memset(&pipe_args[count], 0, sizeof(*pipe_args));
2003 if (parse_connector(&pipe_args[count], optarg) < 0)
2015 prop_args = realloc(prop_args,
2016 (prop_count + 1) * sizeof *prop_args);
2017 if (prop_args == NULL) {
2018 fprintf(stderr, "memory allocation failed\n");
2021 memset(&prop_args[prop_count], 0, sizeof(*prop_args));
2023 if (parse_property(&prop_args[prop_count], optarg) < 0)
2034 if (!args || (args == 1 && use_atomic))
2035 encoders = connectors = crtcs = planes = framebuffers = 1;
2037 dev.fd = util_open(device, module);
2041 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
2042 if (ret && use_atomic) {
2043 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
2048 dev.use_atomic = use_atomic;
2050 if (test_vsync && !page_flipping_supported()) {
2051 fprintf(stderr, "page flipping not supported by drm.\n");
2055 if (test_vsync && !count) {
2056 fprintf(stderr, "page flipping requires at least one -s option.\n");
2060 if (test_cursor && !cursor_supported()) {
2061 fprintf(stderr, "hw cursor not supported by drm.\n");
2065 dev.resources = get_resources(&dev);
2066 if (!dev.resources) {
2071 for (i = 0; i < count; i++) {
2072 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
2073 free_resources(dev.resources);
2079 #define dump_resource(dev, res) if (res) dump_##res(dev)
2081 dump_resource(&dev, encoders);
2082 dump_resource(&dev, connectors);
2083 dump_resource(&dev, crtcs);
2084 dump_resource(&dev, planes);
2085 dump_resource(&dev, framebuffers);
2087 for (i = 0; i < prop_count; ++i)
2088 set_property(&dev, &prop_args[i]);
2090 if (dev.use_atomic) {
2091 dev.req = drmModeAtomicAlloc();
2093 if (count && plane_count) {
2096 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2097 if (ret || cap == 0) {
2098 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2102 atomic_set_mode(&dev, pipe_args, count);
2103 atomic_set_planes(&dev, plane_args, plane_count, false);
2105 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2107 fprintf(stderr, "Atomic Commit failed [1]\n");
2111 gettimeofday(&pipe_args->start, NULL);
2112 pipe_args->swap_count = 0;
2114 while (test_vsync) {
2115 drmModeAtomicFree(dev.req);
2116 dev.req = drmModeAtomicAlloc();
2117 atomic_set_planes(&dev, plane_args, plane_count, true);
2119 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2121 fprintf(stderr, "Atomic Commit failed [2]\n");
2125 pipe_args->swap_count++;
2126 if (pipe_args->swap_count == 60) {
2130 gettimeofday(&end, NULL);
2131 t = end.tv_sec + end.tv_usec * 1e-6 -
2132 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
2133 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
2134 pipe_args->swap_count = 0;
2135 pipe_args->start = end;
2140 drmDropMaster(dev.fd);
2144 drmModeAtomicFree(dev.req);
2145 dev.req = drmModeAtomicAlloc();
2147 atomic_clear_mode(&dev, pipe_args, count);
2148 atomic_clear_planes(&dev, plane_args, plane_count);
2149 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
2151 fprintf(stderr, "Atomic Commit failed\n");
2155 atomic_clear_FB(&dev, plane_args, plane_count);
2158 drmModeAtomicFree(dev.req);
2160 if (count || plane_count) {
2163 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
2164 if (ret || cap == 0) {
2165 fprintf(stderr, "driver doesn't support the dumb buffer API\n");
2170 set_mode(&dev, pipe_args, count);
2173 set_planes(&dev, plane_args, plane_count);
2176 set_cursors(&dev, pipe_args, count);
2179 test_page_flip(&dev, pipe_args, count);
2182 drmDropMaster(dev.fd);
2187 clear_cursors(&dev);
2190 clear_planes(&dev, plane_args, plane_count);
2197 free_resources(dev.resources);