OSDN Git Service

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