OSDN Git Service

21d543842c957633ebae3af56235f5979a547bd1
[android-x86/external-libdrm.git] / tests / modetest / modetest.c
1 /*
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>
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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
24  * IN THE SOFTWARE.
25  */
26
27 /*
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.
36  *
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.
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <assert.h>
46 #include <ctype.h>
47 #include <stdbool.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdint.h>
51 #include <inttypes.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include <strings.h>
55 #include <errno.h>
56 #include <poll.h>
57 #include <sys/time.h>
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
60 #endif
61
62 #include "xf86drm.h"
63 #include "xf86drmMode.h"
64 #include "drm_fourcc.h"
65
66 #include "util/common.h"
67 #include "util/format.h"
68 #include "util/kms.h"
69 #include "util/pattern.h"
70
71 #include "buffers.h"
72 #include "cursor.h"
73
74 struct crtc {
75         drmModeCrtc *crtc;
76         drmModeObjectProperties *props;
77         drmModePropertyRes **props_info;
78         drmModeModeInfo *mode;
79 };
80
81 struct encoder {
82         drmModeEncoder *encoder;
83 };
84
85 struct connector {
86         drmModeConnector *connector;
87         drmModeObjectProperties *props;
88         drmModePropertyRes **props_info;
89         char *name;
90 };
91
92 struct fb {
93         drmModeFB *fb;
94 };
95
96 struct plane {
97         drmModePlane *plane;
98         drmModeObjectProperties *props;
99         drmModePropertyRes **props_info;
100 };
101
102 struct resources {
103         drmModeRes *res;
104         drmModePlaneRes *plane_res;
105
106         struct crtc *crtcs;
107         struct encoder *encoders;
108         struct connector *connectors;
109         struct fb *fbs;
110         struct plane *planes;
111 };
112
113 struct device {
114         int fd;
115
116         struct resources *resources;
117
118         struct {
119                 unsigned int width;
120                 unsigned int height;
121
122                 unsigned int fb_id;
123                 struct bo *bo;
124                 struct bo *cursor_bo;
125         } mode;
126 };
127
128 static inline int64_t U642I64(uint64_t val)
129 {
130         return (int64_t)*((int64_t *)&val);
131 }
132
133 #define bit_name_fn(res)                                        \
134 const char * res##_str(int type) {                              \
135         unsigned int i;                                         \
136         const char *sep = "";                                   \
137         for (i = 0; i < ARRAY_SIZE(res##_names); i++) {         \
138                 if (type & (1 << i)) {                          \
139                         printf("%s%s", sep, res##_names[i]);    \
140                         sep = ", ";                             \
141                 }                                               \
142         }                                                       \
143         return NULL;                                            \
144 }
145
146 static const char *mode_type_names[] = {
147         "builtin",
148         "clock_c",
149         "crtc_c",
150         "preferred",
151         "default",
152         "userdef",
153         "driver",
154 };
155
156 static bit_name_fn(mode_type)
157
158 static const char *mode_flag_names[] = {
159         "phsync",
160         "nhsync",
161         "pvsync",
162         "nvsync",
163         "interlace",
164         "dblscan",
165         "csync",
166         "pcsync",
167         "ncsync",
168         "hskew",
169         "bcast",
170         "pixmux",
171         "dblclk",
172         "clkdiv2"
173 };
174
175 static bit_name_fn(mode_flag)
176
177 static void dump_encoders(struct device *dev)
178 {
179         drmModeEncoder *encoder;
180         int i;
181
182         printf("Encoders:\n");
183         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
184         for (i = 0; i < dev->resources->res->count_encoders; i++) {
185                 encoder = dev->resources->encoders[i].encoder;
186                 if (!encoder)
187                         continue;
188
189                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
190                        encoder->encoder_id,
191                        encoder->crtc_id,
192                        util_lookup_encoder_type_name(encoder->encoder_type),
193                        encoder->possible_crtcs,
194                        encoder->possible_clones);
195         }
196         printf("\n");
197 }
198
199 static void dump_mode(drmModeModeInfo *mode)
200 {
201         printf("  %s %d %d %d %d %d %d %d %d %d",
202                mode->name,
203                mode->vrefresh,
204                mode->hdisplay,
205                mode->hsync_start,
206                mode->hsync_end,
207                mode->htotal,
208                mode->vdisplay,
209                mode->vsync_start,
210                mode->vsync_end,
211                mode->vtotal);
212
213         printf(" flags: ");
214         mode_flag_str(mode->flags);
215         printf("; type: ");
216         mode_type_str(mode->type);
217         printf("\n");
218 }
219
220 static void dump_blob(struct device *dev, uint32_t blob_id)
221 {
222         uint32_t i;
223         unsigned char *blob_data;
224         drmModePropertyBlobPtr blob;
225
226         blob = drmModeGetPropertyBlob(dev->fd, blob_id);
227         if (!blob) {
228                 printf("\n");
229                 return;
230         }
231
232         blob_data = blob->data;
233
234         for (i = 0; i < blob->length; i++) {
235                 if (i % 16 == 0)
236                         printf("\n\t\t\t");
237                 printf("%.2hhx", blob_data[i]);
238         }
239         printf("\n");
240
241         drmModeFreePropertyBlob(blob);
242 }
243
244 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
245                       uint32_t prop_id, uint64_t value)
246 {
247         int i;
248         printf("\t%d", prop_id);
249         if (!prop) {
250                 printf("\n");
251                 return;
252         }
253
254         printf(" %s:\n", prop->name);
255
256         printf("\t\tflags:");
257         if (prop->flags & DRM_MODE_PROP_PENDING)
258                 printf(" pending");
259         if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
260                 printf(" immutable");
261         if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
262                 printf(" signed range");
263         if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
264                 printf(" range");
265         if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
266                 printf(" enum");
267         if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
268                 printf(" bitmask");
269         if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
270                 printf(" blob");
271         if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
272                 printf(" object");
273         printf("\n");
274
275         if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
276                 printf("\t\tvalues:");
277                 for (i = 0; i < prop->count_values; i++)
278                         printf(" %"PRId64, U642I64(prop->values[i]));
279                 printf("\n");
280         }
281
282         if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
283                 printf("\t\tvalues:");
284                 for (i = 0; i < prop->count_values; i++)
285                         printf(" %"PRIu64, prop->values[i]);
286                 printf("\n");
287         }
288
289         if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
290                 printf("\t\tenums:");
291                 for (i = 0; i < prop->count_enums; i++)
292                         printf(" %s=%llu", prop->enums[i].name,
293                                prop->enums[i].value);
294                 printf("\n");
295         } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
296                 printf("\t\tvalues:");
297                 for (i = 0; i < prop->count_enums; i++)
298                         printf(" %s=0x%llx", prop->enums[i].name,
299                                (1LL << prop->enums[i].value));
300                 printf("\n");
301         } else {
302                 assert(prop->count_enums == 0);
303         }
304
305         if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
306                 printf("\t\tblobs:\n");
307                 for (i = 0; i < prop->count_blobs; i++)
308                         dump_blob(dev, prop->blob_ids[i]);
309                 printf("\n");
310         } else {
311                 assert(prop->count_blobs == 0);
312         }
313
314         printf("\t\tvalue:");
315         if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
316                 dump_blob(dev, value);
317         else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
318                 printf(" %"PRId64"\n", value);
319         else
320                 printf(" %"PRIu64"\n", value);
321 }
322
323 static void dump_connectors(struct device *dev)
324 {
325         int i, j;
326
327         printf("Connectors:\n");
328         printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
329         for (i = 0; i < dev->resources->res->count_connectors; i++) {
330                 struct connector *_connector = &dev->resources->connectors[i];
331                 drmModeConnector *connector = _connector->connector;
332                 if (!connector)
333                         continue;
334
335                 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
336                        connector->connector_id,
337                        connector->encoder_id,
338                        util_lookup_connector_status_name(connector->connection),
339                        _connector->name,
340                        connector->mmWidth, connector->mmHeight,
341                        connector->count_modes);
342
343                 for (j = 0; j < connector->count_encoders; j++)
344                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
345                 printf("\n");
346
347                 if (connector->count_modes) {
348                         printf("  modes:\n");
349                         printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
350                                "vss vse vtot)\n");
351                         for (j = 0; j < connector->count_modes; j++)
352                                 dump_mode(&connector->modes[j]);
353                 }
354
355                 if (_connector->props) {
356                         printf("  props:\n");
357                         for (j = 0; j < (int)_connector->props->count_props; j++)
358                                 dump_prop(dev, _connector->props_info[j],
359                                           _connector->props->props[j],
360                                           _connector->props->prop_values[j]);
361                 }
362         }
363         printf("\n");
364 }
365
366 static void dump_crtcs(struct device *dev)
367 {
368         int i;
369         uint32_t j;
370
371         printf("CRTCs:\n");
372         printf("id\tfb\tpos\tsize\n");
373         for (i = 0; i < dev->resources->res->count_crtcs; i++) {
374                 struct crtc *_crtc = &dev->resources->crtcs[i];
375                 drmModeCrtc *crtc = _crtc->crtc;
376                 if (!crtc)
377                         continue;
378
379                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
380                        crtc->crtc_id,
381                        crtc->buffer_id,
382                        crtc->x, crtc->y,
383                        crtc->width, crtc->height);
384                 dump_mode(&crtc->mode);
385
386                 if (_crtc->props) {
387                         printf("  props:\n");
388                         for (j = 0; j < _crtc->props->count_props; j++)
389                                 dump_prop(dev, _crtc->props_info[j],
390                                           _crtc->props->props[j],
391                                           _crtc->props->prop_values[j]);
392                 } else {
393                         printf("  no properties found\n");
394                 }
395         }
396         printf("\n");
397 }
398
399 static void dump_framebuffers(struct device *dev)
400 {
401         drmModeFB *fb;
402         int i;
403
404         printf("Frame buffers:\n");
405         printf("id\tsize\tpitch\n");
406         for (i = 0; i < dev->resources->res->count_fbs; i++) {
407                 fb = dev->resources->fbs[i].fb;
408                 if (!fb)
409                         continue;
410
411                 printf("%u\t(%ux%u)\t%u\n",
412                        fb->fb_id,
413                        fb->width, fb->height,
414                        fb->pitch);
415         }
416         printf("\n");
417 }
418
419 static void dump_planes(struct device *dev)
420 {
421         unsigned int i, j;
422
423         printf("Planes:\n");
424         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
425
426         if (!dev->resources->plane_res)
427                 return;
428
429         for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
430                 struct plane *plane = &dev->resources->planes[i];
431                 drmModePlane *ovr = plane->plane;
432                 if (!ovr)
433                         continue;
434
435                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
436                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
437                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
438                        ovr->gamma_size, ovr->possible_crtcs);
439
440                 if (!ovr->count_formats)
441                         continue;
442
443                 printf("  formats:");
444                 for (j = 0; j < ovr->count_formats; j++)
445                         printf(" %4.4s", (char *)&ovr->formats[j]);
446                 printf("\n");
447
448                 if (plane->props) {
449                         printf("  props:\n");
450                         for (j = 0; j < plane->props->count_props; j++)
451                                 dump_prop(dev, plane->props_info[j],
452                                           plane->props->props[j],
453                                           plane->props->prop_values[j]);
454                 } else {
455                         printf("  no properties found\n");
456                 }
457         }
458         printf("\n");
459
460         return;
461 }
462
463 static void free_resources(struct resources *res)
464 {
465         int i;
466
467         if (!res)
468                 return;
469
470 #define free_resource(_res, __res, type, Type)                                  \
471         do {                                                                    \
472                 if (!(_res)->type##s)                                           \
473                         break;                                                  \
474                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
475                         if (!(_res)->type##s[i].type)                           \
476                                 break;                                          \
477                         drmModeFree##Type((_res)->type##s[i].type);             \
478                 }                                                               \
479                 free((_res)->type##s);                                          \
480         } while (0)
481
482 #define free_properties(_res, __res, type)                                      \
483         do {                                                                    \
484                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
485                         drmModeFreeObjectProperties(res->type##s[i].props);     \
486                         free(res->type##s[i].props_info);                       \
487                 }                                                               \
488         } while (0)
489
490         if (res->res) {
491                 free_properties(res, res, crtc);
492
493                 free_resource(res, res, crtc, Crtc);
494                 free_resource(res, res, encoder, Encoder);
495
496                 for (i = 0; i < res->res->count_connectors; i++)
497                         free(res->connectors[i].name);
498
499                 free_resource(res, res, connector, Connector);
500                 free_resource(res, res, fb, FB);
501
502                 drmModeFreeResources(res->res);
503         }
504
505         if (res->plane_res) {
506                 free_properties(res, plane_res, plane);
507
508                 free_resource(res, plane_res, plane, Plane);
509
510                 drmModeFreePlaneResources(res->plane_res);
511         }
512
513         free(res);
514 }
515
516 static struct resources *get_resources(struct device *dev)
517 {
518         struct resources *res;
519         int i;
520
521         res = calloc(1, sizeof(*res));
522         if (res == 0)
523                 return NULL;
524
525         drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
526
527         res->res = drmModeGetResources(dev->fd);
528         if (!res->res) {
529                 fprintf(stderr, "drmModeGetResources failed: %s\n",
530                         strerror(errno));
531                 goto error;
532         }
533
534         res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
535         res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
536         res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
537         res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
538
539         if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
540                 goto error;
541
542 #define get_resource(_res, __res, type, Type)                                   \
543         do {                                                                    \
544                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
545                         (_res)->type##s[i].type =                               \
546                                 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
547                         if (!(_res)->type##s[i].type)                           \
548                                 fprintf(stderr, "could not get %s %i: %s\n",    \
549                                         #type, (_res)->__res->type##s[i],       \
550                                         strerror(errno));                       \
551                 }                                                               \
552         } while (0)
553
554         get_resource(res, res, crtc, Crtc);
555         get_resource(res, res, encoder, Encoder);
556         get_resource(res, res, connector, Connector);
557         get_resource(res, res, fb, FB);
558
559         /* Set the name of all connectors based on the type name and the per-type ID. */
560         for (i = 0; i < res->res->count_connectors; i++) {
561                 struct connector *connector = &res->connectors[i];
562                 drmModeConnector *conn = connector->connector;
563
564                 asprintf(&connector->name, "%s-%u",
565                          util_lookup_connector_type_name(conn->connector_type),
566                          conn->connector_type_id);
567         }
568
569 #define get_properties(_res, __res, type, Type)                                 \
570         do {                                                                    \
571                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
572                         struct type *obj = &res->type##s[i];                    \
573                         unsigned int j;                                         \
574                         obj->props =                                            \
575                                 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
576                                                            DRM_MODE_OBJECT_##Type); \
577                         if (!obj->props) {                                      \
578                                 fprintf(stderr,                                 \
579                                         "could not get %s %i properties: %s\n", \
580                                         #type, obj->type->type##_id,            \
581                                         strerror(errno));                       \
582                                 continue;                                       \
583                         }                                                       \
584                         obj->props_info = calloc(obj->props->count_props,       \
585                                                  sizeof(*obj->props_info));     \
586                         if (!obj->props_info)                                   \
587                                 continue;                                       \
588                         for (j = 0; j < obj->props->count_props; ++j)           \
589                                 obj->props_info[j] =                            \
590                                         drmModeGetProperty(dev->fd, obj->props->props[j]); \
591                 }                                                               \
592         } while (0)
593
594         get_properties(res, res, crtc, CRTC);
595         get_properties(res, res, connector, CONNECTOR);
596
597         for (i = 0; i < res->res->count_crtcs; ++i)
598                 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
599
600         res->plane_res = drmModeGetPlaneResources(dev->fd);
601         if (!res->plane_res) {
602                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
603                         strerror(errno));
604                 return res;
605         }
606
607         res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
608         if (!res->planes)
609                 goto error;
610
611         get_resource(res, plane_res, plane, Plane);
612         get_properties(res, plane_res, plane, PLANE);
613
614         return res;
615
616 error:
617         free_resources(res);
618         return NULL;
619 }
620
621 static int get_crtc_index(struct device *dev, uint32_t id)
622 {
623         int i;
624
625         for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
626                 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
627                 if (crtc && crtc->crtc_id == id)
628                         return i;
629         }
630
631         return -1;
632 }
633
634 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
635 {
636         struct connector *connector;
637         int i;
638
639         for (i = 0; i < dev->resources->res->count_connectors; i++) {
640                 connector = &dev->resources->connectors[i];
641
642                 if (strcmp(connector->name, name) == 0)
643                         return connector->connector;
644         }
645
646         return NULL;
647 }
648
649 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
650 {
651         drmModeConnector *connector;
652         int i;
653
654         for (i = 0; i < dev->resources->res->count_connectors; i++) {
655                 connector = dev->resources->connectors[i].connector;
656                 if (connector && connector->connector_id == id)
657                         return connector;
658         }
659
660         return NULL;
661 }
662
663 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
664 {
665         drmModeEncoder *encoder;
666         int i;
667
668         for (i = 0; i < dev->resources->res->count_encoders; i++) {
669                 encoder = dev->resources->encoders[i].encoder;
670                 if (encoder && encoder->encoder_id == id)
671                         return encoder;
672         }
673
674         return NULL;
675 }
676
677 /* -----------------------------------------------------------------------------
678  * Pipes and planes
679  */
680
681 /*
682  * Mode setting with the kernel interfaces is a bit of a chore.
683  * First you have to find the connector in question and make sure the
684  * requested mode is available.
685  * Then you need to find the encoder attached to that connector so you
686  * can bind it with a free crtc.
687  */
688 struct pipe_arg {
689         const char **cons;
690         uint32_t *con_ids;
691         unsigned int num_cons;
692         uint32_t crtc_id;
693         char mode_str[64];
694         char format_str[5];
695         unsigned int vrefresh;
696         unsigned int fourcc;
697         drmModeModeInfo *mode;
698         struct crtc *crtc;
699         unsigned int fb_id[2], current_fb_id;
700         struct timeval start;
701
702         int swap_count;
703 };
704
705 struct plane_arg {
706         uint32_t crtc_id;  /* the id of CRTC to bind to */
707         bool has_position;
708         int32_t x, y;
709         uint32_t w, h;
710         double scale;
711         unsigned int fb_id;
712         struct bo *bo;
713         char format_str[5]; /* need to leave room for terminating \0 */
714         unsigned int fourcc;
715 };
716
717 static drmModeModeInfo *
718 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
719         const unsigned int vrefresh)
720 {
721         drmModeConnector *connector;
722         drmModeModeInfo *mode;
723         int i;
724
725         connector = get_connector_by_id(dev, con_id);
726         if (!connector || !connector->count_modes)
727                 return NULL;
728
729         for (i = 0; i < connector->count_modes; i++) {
730                 mode = &connector->modes[i];
731                 if (!strcmp(mode->name, mode_str)) {
732                         /* If the vertical refresh frequency is not specified then return the
733                          * first mode that match with the name. Else, return the mode that match
734                          * the name and the specified vertical refresh frequency.
735                          */
736                         if (vrefresh == 0)
737                                 return mode;
738                         else if (mode->vrefresh == vrefresh)
739                                 return mode;
740                 }
741         }
742
743         return NULL;
744 }
745
746 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
747 {
748         uint32_t possible_crtcs = ~0;
749         uint32_t active_crtcs = 0;
750         unsigned int crtc_idx;
751         unsigned int i;
752         int j;
753
754         for (i = 0; i < pipe->num_cons; ++i) {
755                 uint32_t crtcs_for_connector = 0;
756                 drmModeConnector *connector;
757                 drmModeEncoder *encoder;
758                 int idx;
759
760                 connector = get_connector_by_id(dev, pipe->con_ids[i]);
761                 if (!connector)
762                         return NULL;
763
764                 for (j = 0; j < connector->count_encoders; ++j) {
765                         encoder = get_encoder_by_id(dev, connector->encoders[j]);
766                         if (!encoder)
767                                 continue;
768
769                         crtcs_for_connector |= encoder->possible_crtcs;
770
771                         idx = get_crtc_index(dev, encoder->crtc_id);
772                         if (idx >= 0)
773                                 active_crtcs |= 1 << idx;
774                 }
775
776                 possible_crtcs &= crtcs_for_connector;
777         }
778
779         if (!possible_crtcs)
780                 return NULL;
781
782         /* Return the first possible and active CRTC if one exists, or the first
783          * possible CRTC otherwise.
784          */
785         if (possible_crtcs & active_crtcs)
786                 crtc_idx = ffs(possible_crtcs & active_crtcs);
787         else
788                 crtc_idx = ffs(possible_crtcs);
789
790         return &dev->resources->crtcs[crtc_idx - 1];
791 }
792
793 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
794 {
795         drmModeModeInfo *mode = NULL;
796         int i;
797
798         pipe->mode = NULL;
799
800         for (i = 0; i < (int)pipe->num_cons; i++) {
801                 mode = connector_find_mode(dev, pipe->con_ids[i],
802                                            pipe->mode_str, pipe->vrefresh);
803                 if (mode == NULL) {
804                         fprintf(stderr,
805                                 "failed to find mode \"%s\" for connector %s\n",
806                                 pipe->mode_str, pipe->cons[i]);
807                         return -EINVAL;
808                 }
809         }
810
811         /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
812          * locate a CRTC that can be attached to all the connectors.
813          */
814         if (pipe->crtc_id != (uint32_t)-1) {
815                 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
816                         struct crtc *crtc = &dev->resources->crtcs[i];
817
818                         if (pipe->crtc_id == crtc->crtc->crtc_id) {
819                                 pipe->crtc = crtc;
820                                 break;
821                         }
822                 }
823         } else {
824                 pipe->crtc = pipe_find_crtc(dev, pipe);
825         }
826
827         if (!pipe->crtc) {
828                 fprintf(stderr, "failed to find CRTC for pipe\n");
829                 return -EINVAL;
830         }
831
832         pipe->mode = mode;
833         pipe->crtc->mode = mode;
834
835         return 0;
836 }
837
838 /* -----------------------------------------------------------------------------
839  * Properties
840  */
841
842 struct property_arg {
843         uint32_t obj_id;
844         uint32_t obj_type;
845         char name[DRM_PROP_NAME_LEN+1];
846         uint32_t prop_id;
847         uint64_t value;
848 };
849
850 static void set_property(struct device *dev, struct property_arg *p)
851 {
852         drmModeObjectProperties *props = NULL;
853         drmModePropertyRes **props_info = NULL;
854         const char *obj_type;
855         int ret;
856         int i;
857
858         p->obj_type = 0;
859         p->prop_id = 0;
860
861 #define find_object(_res, __res, type, Type)                                    \
862         do {                                                                    \
863                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
864                         struct type *obj = &(_res)->type##s[i];                 \
865                         if (obj->type->type##_id != p->obj_id)                  \
866                                 continue;                                       \
867                         p->obj_type = DRM_MODE_OBJECT_##Type;                   \
868                         obj_type = #Type;                                       \
869                         props = obj->props;                                     \
870                         props_info = obj->props_info;                           \
871                 }                                                               \
872         } while(0)                                                              \
873
874         find_object(dev->resources, res, crtc, CRTC);
875         if (p->obj_type == 0)
876                 find_object(dev->resources, res, connector, CONNECTOR);
877         if (p->obj_type == 0)
878                 find_object(dev->resources, plane_res, plane, PLANE);
879         if (p->obj_type == 0) {
880                 fprintf(stderr, "Object %i not found, can't set property\n",
881                         p->obj_id);
882                         return;
883         }
884
885         if (!props) {
886                 fprintf(stderr, "%s %i has no properties\n",
887                         obj_type, p->obj_id);
888                 return;
889         }
890
891         for (i = 0; i < (int)props->count_props; ++i) {
892                 if (!props_info[i])
893                         continue;
894                 if (strcmp(props_info[i]->name, p->name) == 0)
895                         break;
896         }
897
898         if (i == (int)props->count_props) {
899                 fprintf(stderr, "%s %i has no %s property\n",
900                         obj_type, p->obj_id, p->name);
901                 return;
902         }
903
904         p->prop_id = props->props[i];
905
906         ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
907                                        p->prop_id, p->value);
908         if (ret < 0)
909                 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
910                         obj_type, p->obj_id, p->name, p->value, strerror(errno));
911 }
912
913 /* -------------------------------------------------------------------------- */
914
915 static void
916 page_flip_handler(int fd, unsigned int frame,
917                   unsigned int sec, unsigned int usec, void *data)
918 {
919         struct pipe_arg *pipe;
920         unsigned int new_fb_id;
921         struct timeval end;
922         double t;
923
924         pipe = data;
925         if (pipe->current_fb_id == pipe->fb_id[0])
926                 new_fb_id = pipe->fb_id[1];
927         else
928                 new_fb_id = pipe->fb_id[0];
929
930         drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
931                         DRM_MODE_PAGE_FLIP_EVENT, pipe);
932         pipe->current_fb_id = new_fb_id;
933         pipe->swap_count++;
934         if (pipe->swap_count == 60) {
935                 gettimeofday(&end, NULL);
936                 t = end.tv_sec + end.tv_usec * 1e-6 -
937                         (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
938                 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
939                 pipe->swap_count = 0;
940                 pipe->start = end;
941         }
942 }
943
944 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
945 {
946         unsigned int i;
947
948         for (i = 0; i < ovr->count_formats; ++i) {
949                 if (ovr->formats[i] == fmt)
950                         return true;
951         }
952
953         return false;
954 }
955
956 static int set_plane(struct device *dev, struct plane_arg *p)
957 {
958         drmModePlane *ovr;
959         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
960         uint32_t plane_id = 0;
961         struct bo *plane_bo;
962         uint32_t plane_flags = 0;
963         int crtc_x, crtc_y, crtc_w, crtc_h;
964         struct crtc *crtc = NULL;
965         unsigned int pipe;
966         unsigned int i;
967
968         /* Find an unused plane which can be connected to our CRTC. Find the
969          * CRTC index first, then iterate over available planes.
970          */
971         for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
972                 if (p->crtc_id == dev->resources->res->crtcs[i]) {
973                         crtc = &dev->resources->crtcs[i];
974                         pipe = i;
975                         break;
976                 }
977         }
978
979         if (!crtc) {
980                 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
981                 return -1;
982         }
983
984         for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
985                 ovr = dev->resources->planes[i].plane;
986                 if (!ovr || !format_support(ovr, p->fourcc))
987                         continue;
988
989                 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
990                         plane_id = ovr->plane_id;
991         }
992
993         if (!plane_id) {
994                 fprintf(stderr, "no unused plane available for CRTC %u\n",
995                         crtc->crtc->crtc_id);
996                 return -1;
997         }
998
999         fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1000                 p->w, p->h, p->format_str, plane_id);
1001
1002         plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1003                              pitches, offsets, UTIL_PATTERN_TILES);
1004         if (plane_bo == NULL)
1005                 return -1;
1006
1007         p->bo = plane_bo;
1008
1009         /* just use single plane format for now.. */
1010         if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1011                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
1012                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1013                 return -1;
1014         }
1015
1016         crtc_w = p->w * p->scale;
1017         crtc_h = p->h * p->scale;
1018         if (!p->has_position) {
1019                 /* Default to the middle of the screen */
1020                 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1021                 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1022         } else {
1023                 crtc_x = p->x;
1024                 crtc_y = p->y;
1025         }
1026
1027         /* note src coords (last 4 args) are in Q16 format */
1028         if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1029                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1030                             0, 0, p->w << 16, p->h << 16)) {
1031                 fprintf(stderr, "failed to enable plane: %s\n",
1032                         strerror(errno));
1033                 return -1;
1034         }
1035
1036         ovr->crtc_id = crtc->crtc->crtc_id;
1037
1038         return 0;
1039 }
1040
1041 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1042 {
1043         unsigned int i;
1044
1045         for (i = 0; i < count; i++) {
1046                 if (p[i].fb_id)
1047                         drmModeRmFB(dev->fd, p[i].fb_id);
1048                 if (p[i].bo)
1049                         bo_destroy(p[i].bo);
1050         }
1051 }
1052
1053
1054 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1055 {
1056         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1057         unsigned int fb_id;
1058         struct bo *bo;
1059         unsigned int i;
1060         unsigned int j;
1061         int ret, x;
1062
1063         dev->mode.width = 0;
1064         dev->mode.height = 0;
1065         dev->mode.fb_id = 0;
1066
1067         for (i = 0; i < count; i++) {
1068                 struct pipe_arg *pipe = &pipes[i];
1069
1070                 ret = pipe_find_crtc_and_mode(dev, pipe);
1071                 if (ret < 0)
1072                         continue;
1073
1074                 dev->mode.width += pipe->mode->hdisplay;
1075                 if (dev->mode.height < pipe->mode->vdisplay)
1076                         dev->mode.height = pipe->mode->vdisplay;
1077         }
1078
1079         bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1080                        dev->mode.height, handles, pitches, offsets,
1081                        UTIL_PATTERN_SMPTE);
1082         if (bo == NULL)
1083                 return;
1084
1085         dev->mode.bo = bo;
1086
1087         ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1088                             pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1089         if (ret) {
1090                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1091                         dev->mode.width, dev->mode.height, strerror(errno));
1092                 return;
1093         }
1094
1095         dev->mode.fb_id = fb_id;
1096
1097         x = 0;
1098         for (i = 0; i < count; i++) {
1099                 struct pipe_arg *pipe = &pipes[i];
1100
1101                 if (pipe->mode == NULL)
1102                         continue;
1103
1104                 printf("setting mode %s-%dHz@%s on connectors ",
1105                        pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1106                 for (j = 0; j < pipe->num_cons; ++j)
1107                         printf("%s, ", pipe->cons[j]);
1108                 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1109
1110                 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1111                                      x, 0, pipe->con_ids, pipe->num_cons,
1112                                      pipe->mode);
1113
1114                 /* XXX: Actually check if this is needed */
1115                 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1116
1117                 x += pipe->mode->hdisplay;
1118
1119                 if (ret) {
1120                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1121                         return;
1122                 }
1123         }
1124 }
1125
1126 static void clear_mode(struct device *dev)
1127 {
1128         if (dev->mode.fb_id)
1129                 drmModeRmFB(dev->fd, dev->mode.fb_id);
1130         if (dev->mode.bo)
1131                 bo_destroy(dev->mode.bo);
1132 }
1133
1134 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1135 {
1136         unsigned int i;
1137
1138         /* set up planes/overlays */
1139         for (i = 0; i < count; i++)
1140                 if (set_plane(dev, &p[i]))
1141                         return;
1142 }
1143
1144 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1145 {
1146         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1147         struct bo *bo;
1148         unsigned int i;
1149         int ret;
1150
1151         /* maybe make cursor width/height configurable some day */
1152         uint32_t cw = 64;
1153         uint32_t ch = 64;
1154
1155         /* create cursor bo.. just using PATTERN_PLAIN as it has
1156          * translucent alpha
1157          */
1158         bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1159                        offsets, UTIL_PATTERN_PLAIN);
1160         if (bo == NULL)
1161                 return;
1162
1163         dev->mode.cursor_bo = bo;
1164
1165         for (i = 0; i < count; i++) {
1166                 struct pipe_arg *pipe = &pipes[i];
1167                 ret = cursor_init(dev->fd, handles[0],
1168                                 pipe->crtc->crtc->crtc_id,
1169                                 pipe->mode->hdisplay, pipe->mode->vdisplay,
1170                                 cw, ch);
1171                 if (ret) {
1172                         fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1173                                         pipe->crtc_id);
1174                         return;
1175                 }
1176         }
1177
1178         cursor_start();
1179 }
1180
1181 static void clear_cursors(struct device *dev)
1182 {
1183         cursor_stop();
1184
1185         if (dev->mode.cursor_bo)
1186                 bo_destroy(dev->mode.cursor_bo);
1187 }
1188
1189 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1190 {
1191         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1192         unsigned int other_fb_id;
1193         struct bo *other_bo;
1194         drmEventContext evctx;
1195         unsigned int i;
1196         int ret;
1197
1198         other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1199                              dev->mode.height, handles, pitches, offsets,
1200                              UTIL_PATTERN_PLAIN);
1201         if (other_bo == NULL)
1202                 return;
1203
1204         ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1205                             pipes[0].fourcc, handles, pitches, offsets,
1206                             &other_fb_id, 0);
1207         if (ret) {
1208                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1209                 goto err;
1210         }
1211
1212         for (i = 0; i < count; i++) {
1213                 struct pipe_arg *pipe = &pipes[i];
1214
1215                 if (pipe->mode == NULL)
1216                         continue;
1217
1218                 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1219                                       other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1220                                       pipe);
1221                 if (ret) {
1222                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1223                         goto err_rmfb;
1224                 }
1225                 gettimeofday(&pipe->start, NULL);
1226                 pipe->swap_count = 0;
1227                 pipe->fb_id[0] = dev->mode.fb_id;
1228                 pipe->fb_id[1] = other_fb_id;
1229                 pipe->current_fb_id = other_fb_id;
1230         }
1231
1232         memset(&evctx, 0, sizeof evctx);
1233         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1234         evctx.vblank_handler = NULL;
1235         evctx.page_flip_handler = page_flip_handler;
1236
1237         while (1) {
1238 #if 0
1239                 struct pollfd pfd[2];
1240
1241                 pfd[0].fd = 0;
1242                 pfd[0].events = POLLIN;
1243                 pfd[1].fd = fd;
1244                 pfd[1].events = POLLIN;
1245
1246                 if (poll(pfd, 2, -1) < 0) {
1247                         fprintf(stderr, "poll error\n");
1248                         break;
1249                 }
1250
1251                 if (pfd[0].revents)
1252                         break;
1253 #else
1254                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1255                 fd_set fds;
1256
1257                 FD_ZERO(&fds);
1258                 FD_SET(0, &fds);
1259                 FD_SET(dev->fd, &fds);
1260                 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1261
1262                 if (ret <= 0) {
1263                         fprintf(stderr, "select timed out or error (ret %d)\n",
1264                                 ret);
1265                         continue;
1266                 } else if (FD_ISSET(0, &fds)) {
1267                         break;
1268                 }
1269 #endif
1270
1271                 drmHandleEvent(dev->fd, &evctx);
1272         }
1273
1274 err_rmfb:
1275         drmModeRmFB(dev->fd, other_fb_id);
1276 err:
1277         bo_destroy(other_bo);
1278 }
1279
1280 #define min(a, b)       ((a) < (b) ? (a) : (b))
1281
1282 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1283 {
1284         unsigned int len;
1285         unsigned int i;
1286         const char *p;
1287         char *endp;
1288
1289         pipe->vrefresh = 0;
1290         pipe->crtc_id = (uint32_t)-1;
1291         strcpy(pipe->format_str, "XR24");
1292
1293         /* Count the number of connectors and allocate them. */
1294         pipe->num_cons = 1;
1295         for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1296                 if (*p == ',')
1297                         pipe->num_cons++;
1298         }
1299
1300         pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1301         pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1302         if (pipe->con_ids == NULL || pipe->cons == NULL)
1303                 return -1;
1304
1305         /* Parse the connectors. */
1306         for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1307                 endp = strpbrk(p, ",@:");
1308                 if (!endp)
1309                         break;
1310
1311                 pipe->cons[i] = strndup(p, endp - p);
1312
1313                 if (*endp != ',')
1314                         break;
1315         }
1316
1317         if (i != pipe->num_cons - 1)
1318                 return -1;
1319
1320         /* Parse the remaining parameters. */
1321         if (*endp == '@') {
1322                 arg = endp + 1;
1323                 pipe->crtc_id = strtoul(arg, &endp, 10);
1324         }
1325         if (*endp != ':')
1326                 return -1;
1327
1328         arg = endp + 1;
1329
1330         /* Search for the vertical refresh or the format. */
1331         p = strpbrk(arg, "-@");
1332         if (p == NULL)
1333                 p = arg + strlen(arg);
1334         len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1335         strncpy(pipe->mode_str, arg, len);
1336         pipe->mode_str[len] = '\0';
1337
1338         if (*p == '-') {
1339                 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1340                 p = endp;
1341         }
1342
1343         if (*p == '@') {
1344                 strncpy(pipe->format_str, p + 1, 4);
1345                 pipe->format_str[4] = '\0';
1346         }
1347
1348         pipe->fourcc = util_format_fourcc(pipe->format_str);
1349         if (pipe->fourcc == 0)  {
1350                 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1351                 return -1;
1352         }
1353
1354         return 0;
1355 }
1356
1357 static int parse_plane(struct plane_arg *plane, const char *p)
1358 {
1359         char *end;
1360
1361         plane->crtc_id = strtoul(p, &end, 10);
1362         if (*end != ':')
1363                 return -EINVAL;
1364
1365         p = end + 1;
1366         plane->w = strtoul(p, &end, 10);
1367         if (*end != 'x')
1368                 return -EINVAL;
1369
1370         p = end + 1;
1371         plane->h = strtoul(p, &end, 10);
1372
1373         if (*end == '+' || *end == '-') {
1374                 plane->x = strtol(end, &end, 10);
1375                 if (*end != '+' && *end != '-')
1376                         return -EINVAL;
1377                 plane->y = strtol(end, &end, 10);
1378
1379                 plane->has_position = true;
1380         }
1381
1382         if (*end == '*') {
1383                 p = end + 1;
1384                 plane->scale = strtod(p, &end);
1385                 if (plane->scale <= 0.0)
1386                         return -EINVAL;
1387         } else {
1388                 plane->scale = 1.0;
1389         }
1390
1391         if (*end == '@') {
1392                 p = end + 1;
1393                 if (strlen(p) != 4)
1394                         return -EINVAL;
1395
1396                 strcpy(plane->format_str, p);
1397         } else {
1398                 strcpy(plane->format_str, "XR24");
1399         }
1400
1401         plane->fourcc = util_format_fourcc(plane->format_str);
1402         if (plane->fourcc == 0) {
1403                 fprintf(stderr, "unknown format %s\n", plane->format_str);
1404                 return -EINVAL;
1405         }
1406
1407         return 0;
1408 }
1409
1410 static int parse_property(struct property_arg *p, const char *arg)
1411 {
1412         if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1413                 return -1;
1414
1415         p->obj_type = 0;
1416         p->name[DRM_PROP_NAME_LEN] = '\0';
1417
1418         return 0;
1419 }
1420
1421 static void usage(char *name)
1422 {
1423         fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1424
1425         fprintf(stderr, "\n Query options:\n\n");
1426         fprintf(stderr, "\t-c\tlist connectors\n");
1427         fprintf(stderr, "\t-e\tlist encoders\n");
1428         fprintf(stderr, "\t-f\tlist framebuffers\n");
1429         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1430
1431         fprintf(stderr, "\n Test options:\n\n");
1432         fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1433         fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1434         fprintf(stderr, "\t-C\ttest hw cursor\n");
1435         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1436         fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1437
1438         fprintf(stderr, "\n Generic options:\n\n");
1439         fprintf(stderr, "\t-d\tdrop master after mode set\n");
1440         fprintf(stderr, "\t-M module\tuse the given driver\n");
1441         fprintf(stderr, "\t-D device\tuse the given device\n");
1442
1443         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1444         exit(0);
1445 }
1446
1447 static int page_flipping_supported(void)
1448 {
1449         /*FIXME: generic ioctl needed? */
1450         return 1;
1451 #if 0
1452         int ret, value;
1453         struct drm_i915_getparam gp;
1454
1455         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1456         gp.value = &value;
1457
1458         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1459         if (ret) {
1460                 fprintf(stderr, "drm_i915_getparam: %m\n");
1461                 return 0;
1462         }
1463
1464         return *gp.value;
1465 #endif
1466 }
1467
1468 static int cursor_supported(void)
1469 {
1470         /*FIXME: generic ioctl needed? */
1471         return 1;
1472 }
1473
1474 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1475 {
1476         drmModeConnector *connector;
1477         unsigned int i;
1478         uint32_t id;
1479         char *endp;
1480
1481         for (i = 0; i < pipe->num_cons; i++) {
1482                 id = strtoul(pipe->cons[i], &endp, 10);
1483                 if (endp == pipe->cons[i]) {
1484                         connector = get_connector_by_name(dev, pipe->cons[i]);
1485                         if (!connector) {
1486                                 fprintf(stderr, "no connector named '%s'\n",
1487                                         pipe->cons[i]);
1488                                 return -ENODEV;
1489                         }
1490
1491                         id = connector->connector_id;
1492                 }
1493
1494                 pipe->con_ids[i] = id;
1495         }
1496
1497         return 0;
1498 }
1499
1500 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1501
1502 int main(int argc, char **argv)
1503 {
1504         struct device dev;
1505
1506         int c;
1507         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1508         int drop_master = 0;
1509         int test_vsync = 0;
1510         int test_cursor = 0;
1511         char *device = NULL;
1512         char *module = NULL;
1513         unsigned int i;
1514         unsigned int count = 0, plane_count = 0;
1515         unsigned int prop_count = 0;
1516         struct pipe_arg *pipe_args = NULL;
1517         struct plane_arg *plane_args = NULL;
1518         struct property_arg *prop_args = NULL;
1519         unsigned int args = 0;
1520         int ret;
1521
1522         memset(&dev, 0, sizeof dev);
1523
1524         opterr = 0;
1525         while ((c = getopt(argc, argv, optstr)) != -1) {
1526                 args++;
1527
1528                 switch (c) {
1529                 case 'c':
1530                         connectors = 1;
1531                         break;
1532                 case 'D':
1533                         device = optarg;
1534                         args--;
1535                         break;
1536                 case 'd':
1537                         drop_master = 1;
1538                         break;
1539                 case 'e':
1540                         encoders = 1;
1541                         break;
1542                 case 'f':
1543                         framebuffers = 1;
1544                         break;
1545                 case 'M':
1546                         module = optarg;
1547                         /* Preserve the default behaviour of dumping all information. */
1548                         args--;
1549                         break;
1550                 case 'P':
1551                         plane_args = realloc(plane_args,
1552                                              (plane_count + 1) * sizeof *plane_args);
1553                         if (plane_args == NULL) {
1554                                 fprintf(stderr, "memory allocation failed\n");
1555                                 return 1;
1556                         }
1557                         memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1558
1559                         if (parse_plane(&plane_args[plane_count], optarg) < 0)
1560                                 usage(argv[0]);
1561
1562                         plane_count++;
1563                         break;
1564                 case 'p':
1565                         crtcs = 1;
1566                         planes = 1;
1567                         break;
1568                 case 's':
1569                         pipe_args = realloc(pipe_args,
1570                                             (count + 1) * sizeof *pipe_args);
1571                         if (pipe_args == NULL) {
1572                                 fprintf(stderr, "memory allocation failed\n");
1573                                 return 1;
1574                         }
1575                         memset(&pipe_args[count], 0, sizeof(*pipe_args));
1576
1577                         if (parse_connector(&pipe_args[count], optarg) < 0)
1578                                 usage(argv[0]);
1579
1580                         count++;
1581                         break;
1582                 case 'C':
1583                         test_cursor = 1;
1584                         break;
1585                 case 'v':
1586                         test_vsync = 1;
1587                         break;
1588                 case 'w':
1589                         prop_args = realloc(prop_args,
1590                                            (prop_count + 1) * sizeof *prop_args);
1591                         if (prop_args == NULL) {
1592                                 fprintf(stderr, "memory allocation failed\n");
1593                                 return 1;
1594                         }
1595                         memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1596
1597                         if (parse_property(&prop_args[prop_count], optarg) < 0)
1598                                 usage(argv[0]);
1599
1600                         prop_count++;
1601                         break;
1602                 default:
1603                         usage(argv[0]);
1604                         break;
1605                 }
1606         }
1607
1608         if (!args)
1609                 encoders = connectors = crtcs = planes = framebuffers = 1;
1610
1611         dev.fd = util_open(device, module);
1612         if (dev.fd < 0)
1613                 return -1;
1614
1615         if (test_vsync && !page_flipping_supported()) {
1616                 fprintf(stderr, "page flipping not supported by drm.\n");
1617                 return -1;
1618         }
1619
1620         if (test_vsync && !count) {
1621                 fprintf(stderr, "page flipping requires at least one -s option.\n");
1622                 return -1;
1623         }
1624
1625         if (test_cursor && !cursor_supported()) {
1626                 fprintf(stderr, "hw cursor not supported by drm.\n");
1627                 return -1;
1628         }
1629
1630         dev.resources = get_resources(&dev);
1631         if (!dev.resources) {
1632                 drmClose(dev.fd);
1633                 return 1;
1634         }
1635
1636         for (i = 0; i < count; i++) {
1637                 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1638                         free_resources(dev.resources);
1639                         drmClose(dev.fd);
1640                         return 1;
1641                 }
1642         }
1643
1644 #define dump_resource(dev, res) if (res) dump_##res(dev)
1645
1646         dump_resource(&dev, encoders);
1647         dump_resource(&dev, connectors);
1648         dump_resource(&dev, crtcs);
1649         dump_resource(&dev, planes);
1650         dump_resource(&dev, framebuffers);
1651
1652         for (i = 0; i < prop_count; ++i)
1653                 set_property(&dev, &prop_args[i]);
1654
1655         if (count || plane_count) {
1656                 uint64_t cap = 0;
1657
1658                 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1659                 if (ret || cap == 0) {
1660                         fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1661                         return 1;
1662                 }
1663
1664                 if (count)
1665                         set_mode(&dev, pipe_args, count);
1666
1667                 if (plane_count)
1668                         set_planes(&dev, plane_args, plane_count);
1669
1670                 if (test_cursor)
1671                         set_cursors(&dev, pipe_args, count);
1672
1673                 if (test_vsync)
1674                         test_page_flip(&dev, pipe_args, count);
1675
1676                 if (drop_master)
1677                         drmDropMaster(dev.fd);
1678
1679                 getchar();
1680
1681                 if (test_cursor)
1682                         clear_cursors(&dev);
1683
1684                 if (plane_count)
1685                         clear_planes(&dev, plane_args, plane_count);
1686
1687                 if (count)
1688                         clear_mode(&dev);
1689         }
1690
1691         free_resources(dev.resources);
1692
1693         return 0;
1694 }