OSDN Git Service

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