OSDN Git Service

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