OSDN Git Service

modetest: fix some compiler warnings
[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 #include "config.h"
41
42 #include <assert.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdint.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/poll.h>
50 #include <sys/time.h>
51
52 #include "xf86drm.h"
53 #include "xf86drmMode.h"
54 #include "drm_fourcc.h"
55 #include "libkms.h"
56
57 #ifdef HAVE_CAIRO
58 #include <math.h>
59 #include <cairo.h>
60 #endif
61
62 drmModeRes *resources;
63 int fd, modes;
64
65 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
66
67 struct type_name {
68         int type;
69         char *name;
70 };
71
72 #define type_name_fn(res) \
73 char * res##_str(int type) {                    \
74         unsigned int i;                                 \
75         for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
76                 if (res##_names[i].type == type)        \
77                         return res##_names[i].name;     \
78         }                                               \
79         return "(invalid)";                             \
80 }
81
82 struct type_name encoder_type_names[] = {
83         { DRM_MODE_ENCODER_NONE, "none" },
84         { DRM_MODE_ENCODER_DAC, "DAC" },
85         { DRM_MODE_ENCODER_TMDS, "TMDS" },
86         { DRM_MODE_ENCODER_LVDS, "LVDS" },
87         { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
88 };
89
90 type_name_fn(encoder_type)
91
92 struct type_name connector_status_names[] = {
93         { DRM_MODE_CONNECTED, "connected" },
94         { DRM_MODE_DISCONNECTED, "disconnected" },
95         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
96 };
97
98 type_name_fn(connector_status)
99
100 struct type_name connector_type_names[] = {
101         { DRM_MODE_CONNECTOR_Unknown, "unknown" },
102         { DRM_MODE_CONNECTOR_VGA, "VGA" },
103         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
104         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
105         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
106         { DRM_MODE_CONNECTOR_Composite, "composite" },
107         { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
108         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
109         { DRM_MODE_CONNECTOR_Component, "component" },
110         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
111         { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
112         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
113         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
114         { DRM_MODE_CONNECTOR_TV, "TV" },
115         { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
116 };
117
118 type_name_fn(connector_type)
119
120 void dump_encoders(void)
121 {
122         drmModeEncoder *encoder;
123         int i;
124
125         printf("Encoders:\n");
126         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
127         for (i = 0; i < resources->count_encoders; i++) {
128                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
129
130                 if (!encoder) {
131                         fprintf(stderr, "could not get encoder %i: %s\n",
132                                 resources->encoders[i], strerror(errno));
133                         continue;
134                 }
135                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
136                        encoder->encoder_id,
137                        encoder->crtc_id,
138                        encoder_type_str(encoder->encoder_type),
139                        encoder->possible_crtcs,
140                        encoder->possible_clones);
141                 drmModeFreeEncoder(encoder);
142         }
143         printf("\n");
144 }
145
146 void dump_mode(drmModeModeInfo *mode)
147 {
148         printf("  %s %d %d %d %d %d %d %d %d %d\n",
149                mode->name,
150                mode->vrefresh,
151                mode->hdisplay,
152                mode->hsync_start,
153                mode->hsync_end,
154                mode->htotal,
155                mode->vdisplay,
156                mode->vsync_start,
157                mode->vsync_end,
158                mode->vtotal);
159 }
160
161 static void
162 dump_props(drmModeConnector *connector)
163 {
164         drmModePropertyPtr props;
165         int i;
166
167         for (i = 0; i < connector->count_props; i++) {
168                 props = drmModeGetProperty(fd, connector->props[i]);
169                 printf("\t%s, flags %d\n", props->name, props->flags);
170                 drmModeFreeProperty(props);
171         }
172 }
173
174 void dump_connectors(void)
175 {
176         drmModeConnector *connector;
177         int i, j;
178
179         printf("Connectors:\n");
180         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
181         for (i = 0; i < resources->count_connectors; i++) {
182                 connector = drmModeGetConnector(fd, resources->connectors[i]);
183
184                 if (!connector) {
185                         fprintf(stderr, "could not get connector %i: %s\n",
186                                 resources->connectors[i], strerror(errno));
187                         continue;
188                 }
189
190                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
191                        connector->connector_id,
192                        connector->encoder_id,
193                        connector_status_str(connector->connection),
194                        connector_type_str(connector->connector_type),
195                        connector->mmWidth, connector->mmHeight,
196                        connector->count_modes);
197
198                 for (j = 0; j < connector->count_encoders; j++)
199                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
200                 printf("\n");
201
202                 if (!connector->count_modes)
203                         continue;
204
205                 printf("  modes:\n");
206                 printf("  name refresh (Hz) hdisp hss hse htot vdisp "
207                        "vss vse vtot)\n");
208                 for (j = 0; j < connector->count_modes; j++)
209                         dump_mode(&connector->modes[j]);
210
211                 printf("  props:\n");
212                 dump_props(connector);
213
214                 drmModeFreeConnector(connector);
215         }
216         printf("\n");
217 }
218
219 void dump_crtcs(void)
220 {
221         drmModeCrtc *crtc;
222         int i;
223
224         printf("CRTCs:\n");
225         printf("id\tfb\tpos\tsize\n");
226         for (i = 0; i < resources->count_crtcs; i++) {
227                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
228
229                 if (!crtc) {
230                         fprintf(stderr, "could not get crtc %i: %s\n",
231                                 resources->crtcs[i], strerror(errno));
232                         continue;
233                 }
234                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
235                        crtc->crtc_id,
236                        crtc->buffer_id,
237                        crtc->x, crtc->y,
238                        crtc->width, crtc->height);
239                 dump_mode(&crtc->mode);
240
241                 drmModeFreeCrtc(crtc);
242         }
243         printf("\n");
244 }
245
246 void dump_framebuffers(void)
247 {
248         drmModeFB *fb;
249         int i;
250
251         printf("Frame buffers:\n");
252         printf("id\tsize\tpitch\n");
253         for (i = 0; i < resources->count_fbs; i++) {
254                 fb = drmModeGetFB(fd, resources->fbs[i]);
255
256                 if (!fb) {
257                         fprintf(stderr, "could not get fb %i: %s\n",
258                                 resources->fbs[i], strerror(errno));
259                         continue;
260                 }
261                 printf("%u\t(%ux%u)\t%u\n",
262                        fb->fb_id,
263                        fb->width, fb->height,
264                        fb->pitch);
265
266                 drmModeFreeFB(fb);
267         }
268         printf("\n");
269 }
270
271 static void dump_planes(void)
272 {
273         drmModePlaneRes *plane_resources;
274         drmModePlane *ovr;
275         unsigned int i, j;
276
277         plane_resources = drmModeGetPlaneResources(fd);
278         if (!plane_resources) {
279                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
280                         strerror(errno));
281                 return;
282         }
283
284         printf("Planes:\n");
285         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
286         for (i = 0; i < plane_resources->count_planes; i++) {
287                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
288                 if (!ovr) {
289                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
290                                 strerror(errno));
291                         continue;
292                 }
293
294                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
295                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
296                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
297                        ovr->gamma_size);
298
299                 if (!ovr->count_formats)
300                         continue;
301
302                 printf("  formats:");
303                 for (j = 0; j < ovr->count_formats; j++)
304                         printf(" %4.4s", (char *)&ovr->formats[j]);
305                 printf("\n");
306
307                 drmModeFreePlane(ovr);
308         }
309         printf("\n");
310
311         return;
312 }
313
314 /*
315  * Mode setting with the kernel interfaces is a bit of a chore.
316  * First you have to find the connector in question and make sure the
317  * requested mode is available.
318  * Then you need to find the encoder attached to that connector so you
319  * can bind it with a free crtc.
320  */
321 struct connector {
322         uint32_t id;
323         char mode_str[64];
324         drmModeModeInfo *mode;
325         drmModeEncoder *encoder;
326         int crtc;
327         int pipe;
328         unsigned int fb_id[2], current_fb_id;
329         struct timeval start;
330
331         int swap_count;
332 };
333
334 struct plane {
335         uint32_t con_id;  /* the id of connector to bind to */
336         uint32_t w, h;
337         unsigned int fb_id;
338         char format_str[5]; /* need to leave room for terminating \0 */
339 };
340
341 static void
342 connector_find_mode(struct connector *c)
343 {
344         drmModeConnector *connector;
345         int i, j;
346
347         /* First, find the connector & mode */
348         c->mode = NULL;
349         for (i = 0; i < resources->count_connectors; i++) {
350                 connector = drmModeGetConnector(fd, resources->connectors[i]);
351
352                 if (!connector) {
353                         fprintf(stderr, "could not get connector %i: %s\n",
354                                 resources->connectors[i], strerror(errno));
355                         drmModeFreeConnector(connector);
356                         continue;
357                 }
358
359                 if (!connector->count_modes) {
360                         drmModeFreeConnector(connector);
361                         continue;
362                 }
363
364                 if (connector->connector_id != c->id) {
365                         drmModeFreeConnector(connector);
366                         continue;
367                 }
368
369                 for (j = 0; j < connector->count_modes; j++) {
370                         c->mode = &connector->modes[j];
371                         if (!strcmp(c->mode->name, c->mode_str))
372                                 break;
373                 }
374
375                 /* Found it, break out */
376                 if (c->mode)
377                         break;
378
379                 drmModeFreeConnector(connector);
380         }
381
382         if (!c->mode) {
383                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
384                 return;
385         }
386
387         /* Now get the encoder */
388         for (i = 0; i < resources->count_encoders; i++) {
389                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
390
391                 if (!c->encoder) {
392                         fprintf(stderr, "could not get encoder %i: %s\n",
393                                 resources->encoders[i], strerror(errno));
394                         drmModeFreeEncoder(c->encoder);
395                         continue;
396                 }
397
398                 if (c->encoder->encoder_id  == connector->encoder_id)
399                         break;
400
401                 drmModeFreeEncoder(c->encoder);
402         }
403
404         if (c->crtc == -1)
405                 c->crtc = c->encoder->crtc_id;
406
407         /* and figure out which crtc index it is: */
408         for (i = 0; i < resources->count_crtcs; i++) {
409                 if (c->crtc == resources->crtcs[i]) {
410                         c->pipe = i;
411                         break;
412                 }
413         }
414
415 }
416
417 static struct kms_bo *
418 allocate_buffer(struct kms_driver *kms,
419                 int width, int height, int *stride)
420 {
421         struct kms_bo *bo;
422         unsigned bo_attribs[] = {
423                 KMS_WIDTH,   0,
424                 KMS_HEIGHT,  0,
425                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
426                 KMS_TERMINATE_PROP_LIST
427         };
428         int ret;
429
430         bo_attribs[1] = width;
431         bo_attribs[3] = height;
432
433         ret = kms_bo_create(kms, bo_attribs, &bo);
434         if (ret) {
435                 fprintf(stderr, "failed to alloc buffer: %s\n",
436                         strerror(-ret));
437                 return NULL;
438         }
439
440         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
441         if (ret) {
442                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
443                         strerror(-ret));
444                 kms_bo_destroy(&bo);
445                 return NULL;
446         }
447
448         return bo;
449 }
450
451 static void
452 make_pwetty(void *data, int width, int height, int stride)
453 {
454 #ifdef HAVE_CAIRO
455         cairo_surface_t *surface;
456         cairo_t *cr;
457         int x, y;
458
459         surface = cairo_image_surface_create_for_data(data,
460                                                       CAIRO_FORMAT_ARGB32,
461                                                       width, height,
462                                                       stride);
463         cr = cairo_create(surface);
464         cairo_surface_destroy(surface);
465
466         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
467         for (x = 0; x < width; x += 250)
468                 for (y = 0; y < height; y += 250) {
469                         char buf[64];
470
471                         cairo_move_to(cr, x, y - 20);
472                         cairo_line_to(cr, x, y + 20);
473                         cairo_move_to(cr, x - 20, y);
474                         cairo_line_to(cr, x + 20, y);
475                         cairo_new_sub_path(cr);
476                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
477                         cairo_set_line_width(cr, 4);
478                         cairo_set_source_rgb(cr, 0, 0, 0);
479                         cairo_stroke_preserve(cr);
480                         cairo_set_source_rgb(cr, 1, 1, 1);
481                         cairo_set_line_width(cr, 2);
482                         cairo_stroke(cr);
483
484                         snprintf(buf, sizeof buf, "%d, %d", x, y);
485                         cairo_move_to(cr, x + 20, y + 20);
486                         cairo_text_path(cr, buf);
487                         cairo_set_source_rgb(cr, 0, 0, 0);
488                         cairo_stroke_preserve(cr);
489                         cairo_set_source_rgb(cr, 1, 1, 1);
490                         cairo_fill(cr);
491                 }
492
493         cairo_destroy(cr);
494 #endif
495 }
496
497 static int
498 create_test_buffer(struct kms_driver *kms,
499                    int width, int height, int *stride_out,
500                    struct kms_bo **bo_out)
501 {
502         struct kms_bo *bo;
503         int ret, i, j, stride;
504         void *virtual;
505
506         bo = allocate_buffer(kms, width, height, &stride);
507         if (!bo)
508                 return -1;
509
510         ret = kms_bo_map(bo, &virtual);
511         if (ret) {
512                 fprintf(stderr, "failed to map buffer: %s\n",
513                         strerror(-ret));
514                 kms_bo_destroy(&bo);
515                 return -1;
516         }
517
518         /* paint the buffer with colored tiles */
519         for (j = 0; j < height; j++) {
520                 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
521                 for (i = 0; i < width; i++) {
522                         div_t d = div(i, width);
523                         fb_ptr[i] =
524                                 0x00130502 * (d.quot >> 6) +
525                                 0x000a1120 * (d.rem >> 6);
526                 }
527         }
528
529         make_pwetty(virtual, width, height, stride);
530
531         kms_bo_unmap(bo);
532
533         *bo_out = bo;
534         *stride_out = stride;
535         return 0;
536 }
537
538 static int
539 create_grey_buffer(struct kms_driver *kms,
540                    int width, int height, int *stride_out,
541                    struct kms_bo **bo_out)
542 {
543         struct kms_bo *bo;
544         int size, ret, stride;
545         void *virtual;
546
547         bo = allocate_buffer(kms, width, height, &stride);
548         if (!bo)
549                 return -1;
550
551         ret = kms_bo_map(bo, &virtual);
552         if (ret) {
553                 fprintf(stderr, "failed to map buffer: %s\n",
554                         strerror(-ret));
555                 kms_bo_destroy(&bo);
556                 return -1;
557         }
558
559         size = stride * height;
560         memset(virtual, 0x77, size);
561         kms_bo_unmap(bo);
562
563         *bo_out = bo;
564         *stride_out = stride;
565
566         return 0;
567 }
568
569 void
570 page_flip_handler(int fd, unsigned int frame,
571                   unsigned int sec, unsigned int usec, void *data)
572 {
573         struct connector *c;
574         unsigned int new_fb_id;
575         struct timeval end;
576         double t;
577
578         c = data;
579         if (c->current_fb_id == c->fb_id[0])
580                 new_fb_id = c->fb_id[1];
581         else
582                 new_fb_id = c->fb_id[0];
583                         
584         drmModePageFlip(fd, c->crtc, new_fb_id,
585                         DRM_MODE_PAGE_FLIP_EVENT, c);
586         c->current_fb_id = new_fb_id;
587         c->swap_count++;
588         if (c->swap_count == 60) {
589                 gettimeofday(&end, NULL);
590                 t = end.tv_sec + end.tv_usec * 1e-6 -
591                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
592                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
593                 c->swap_count = 0;
594                 c->start = end;
595         }
596 }
597
598 /* swap these for big endian.. */
599 #define RED   2
600 #define GREEN 1
601 #define BLUE  0
602
603 static void
604 fill420(unsigned char *y, unsigned char *u, unsigned char *v,
605                 int cs /*chroma pixel stride */,
606                 int n, int width, int height, int stride)
607 {
608         int i, j;
609
610         /* paint the buffer with colored tiles, in blocks of 2x2 */
611         for (j = 0; j < height; j+=2) {
612                 unsigned char *y1p = y + j * stride;
613                 unsigned char *y2p = y1p + stride;
614                 unsigned char *up = u + (j/2) * stride * cs / 2;
615                 unsigned char *vp = v + (j/2) * stride * cs / 2;
616
617                 for (i = 0; i < width; i+=2) {
618                         div_t d = div(n+i+j, width);
619                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
620                         unsigned char *rgbp = (unsigned char *)&rgb;
621                         unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
622
623                         *(y2p++) = *(y1p++) = y;
624                         *(y2p++) = *(y1p++) = y;
625
626                         *up = (rgbp[BLUE] - y) * 0.565 + 128;
627                         *vp = (rgbp[RED] - y) * 0.713 + 128;
628                         up += cs;
629                         vp += cs;
630                 }
631         }
632 }
633
634 static void
635 fill422(unsigned char *virtual, int n, int width, int height, int stride)
636 {
637         int i, j;
638         /* paint the buffer with colored tiles */
639         for (j = 0; j < height; j++) {
640                 uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride);
641                 for (i = 0; i < width; i++) {
642                         div_t d = div(n+i+j, width);
643                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
644                         unsigned char *rgbp = (unsigned char *)&rgb;
645                         unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
646
647                         *(ptr++) = y;
648                         *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
649                         *(ptr++) = y;
650                         *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
651                 }
652         }
653 }
654
655 static void
656 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
657 {
658         int i, j;
659         /* paint the buffer with colored tiles */
660         for (j = 0; j < height; j++) {
661                 uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride);
662                 for (i = 0; i < width; i++) {
663                         div_t d = div(n+i+j, width);
664                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
665                         unsigned char *rgbp = (unsigned char *)&rgb;
666
667                         *(ptr++) = 0x8000 |
668                                         (rgbp[RED] >> 3) << 10 |
669                                         (rgbp[GREEN] >> 3) << 5 |
670                                         (rgbp[BLUE] >> 3);
671                 }
672         }
673 }
674
675 static int
676 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
677 {
678         drmModePlaneRes *plane_resources;
679         drmModePlane *ovr;
680         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
681         uint32_t plane_id = 0;
682         struct kms_bo *plane_bo;
683         uint32_t plane_flags = 0, format;
684         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
685         unsigned int i;
686
687         /* find an unused plane which can be connected to our crtc */
688         plane_resources = drmModeGetPlaneResources(fd);
689         if (!plane_resources) {
690                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
691                         strerror(errno));
692                 return -1;
693         }
694
695         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
696                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
697                 if (!ovr) {
698                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
699                                 strerror(errno));
700                         return -1;
701                 }
702
703                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
704                         plane_id = ovr->plane_id;
705
706                 drmModeFreePlane(ovr);
707         }
708
709         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
710                         p->w, p->h, p->format_str);
711
712         if (!plane_id) {
713                 fprintf(stderr, "failed to find plane!\n");
714                 return -1;
715         }
716
717         if (!strcmp(p->format_str, "XR24")) {
718                 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
719                         return -1;
720                 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
721                 format = DRM_FORMAT_XRGB8888;
722         } else {
723                 void *virtual;
724
725                 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
726                  * YUV formats, we don't use all of it..  since 4bytes/pixel is
727                  * worst case, so live with it for now and just don't use all
728                  * the buffer:
729                  */
730                 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
731                 if (!plane_bo)
732                         return -1;
733
734                 ret = kms_bo_map(plane_bo, &virtual);
735                 if (ret) {
736                         fprintf(stderr, "failed to map buffer: %s\n",
737                                 strerror(-ret));
738                         kms_bo_destroy(&plane_bo);
739                         return -1;
740                 }
741
742                 /* just testing a limited # of formats to test single
743                  * and multi-planar path.. would be nice to add more..
744                  */
745                 if (!strcmp(p->format_str, "YUYV")) {
746                         pitches[0] = p->w * 2;
747                         offsets[0] = 0;
748                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
749
750                         fill422(virtual, 0, p->w, p->h, pitches[0]);
751
752                         format = DRM_FORMAT_YUYV;
753                 } else if (!strcmp(p->format_str, "NV12")) {
754                         pitches[0] = p->w;
755                         offsets[0] = 0;
756                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
757                         pitches[1] = p->w;
758                         offsets[1] = p->w * p->h;
759                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
760
761                         fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
762                                         2, 0, p->w, p->h, pitches[0]);
763
764                         format = DRM_FORMAT_NV12;
765                 } else if (!strcmp(p->format_str, "YV12")) {
766                         pitches[0] = p->w;
767                         offsets[0] = 0;
768                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
769                         pitches[1] = p->w / 2;
770                         offsets[1] = p->w * p->h;
771                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
772                         pitches[2] = p->w / 2;
773                         offsets[2] = offsets[1] + (p->w * p->h) / 4;
774                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[2]);
775
776                         fill420(virtual, virtual+offsets[1], virtual+offsets[2],
777                                         1, 0, p->w, p->h, pitches[0]);
778
779                         format = DRM_FORMAT_YVU420;
780                 } else if (!strcmp(p->format_str, "XR15")) {
781                         pitches[0] = p->w * 2;
782                         offsets[0] = 0;
783                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
784
785                         fill1555(virtual, 0, p->w, p->h, pitches[0]);
786
787                         format = DRM_FORMAT_XRGB1555;
788                 } else if (!strcmp(p->format_str, "AR15")) {
789                         pitches[0] = p->w * 2;
790                         offsets[0] = 0;
791                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
792
793                         fill1555(virtual, 0, p->w, p->h, pitches[0]);
794
795                         format = DRM_FORMAT_ARGB1555;
796                 } else {
797                         fprintf(stderr, "Unknown format: %s\n", p->format_str);
798                         return -1;
799                 }
800
801                 kms_bo_unmap(plane_bo);
802         }
803
804         /* just use single plane format for now.. */
805         if (drmModeAddFB2(fd, p->w, p->h, format,
806                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
807                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
808                 return -1;
809         }
810
811         /* ok, boring.. but for now put in middle of screen: */
812         crtc_x = c->mode->hdisplay / 3;
813         crtc_y = c->mode->vdisplay / 3;
814         crtc_w = crtc_x;
815         crtc_h = crtc_y;
816
817         /* note src coords (last 4 args) are in Q16 format */
818         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
819                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
820                             0, 0, p->w << 16, p->h << 16)) {
821                 fprintf(stderr, "failed to enable plane: %s\n",
822                         strerror(errno));
823                 return -1;
824         }
825
826         return 0;
827 }
828
829 static void
830 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
831                 int page_flip)
832 {
833         struct kms_driver *kms;
834         struct kms_bo *bo, *other_bo;
835         unsigned int fb_id, other_fb_id;
836         int i, j, ret, width, height, x, stride;
837         unsigned handle;
838         drmEventContext evctx;
839
840         width = 0;
841         height = 0;
842         for (i = 0; i < count; i++) {
843                 connector_find_mode(&c[i]);
844                 if (c[i].mode == NULL)
845                         continue;
846                 width += c[i].mode->hdisplay;
847                 if (height < c[i].mode->vdisplay)
848                         height = c[i].mode->vdisplay;
849         }
850
851         ret = kms_create(fd, &kms);
852         if (ret) {
853                 fprintf(stderr, "failed to create kms driver: %s\n",
854                         strerror(-ret));
855                 return;
856         }
857
858         if (create_test_buffer(kms, width, height, &stride, &bo))
859                 return;
860
861         kms_bo_get_prop(bo, KMS_HANDLE, &handle);
862         ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
863         if (ret) {
864                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
865                         width, height, strerror(errno));
866                 return;
867         }
868
869         x = 0;
870         for (i = 0; i < count; i++) {
871                 if (c[i].mode == NULL)
872                         continue;
873
874                 printf("setting mode %s on connector %d, crtc %d\n",
875                        c[i].mode_str, c[i].id, c[i].crtc);
876
877                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
878                                      &c[i].id, 1, c[i].mode);
879
880                 /* XXX: Actually check if this is needed */
881                 drmModeDirtyFB(fd, fb_id, NULL, 0);
882
883                 x += c[i].mode->hdisplay;
884
885                 if (ret) {
886                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
887                         return;
888                 }
889
890                 /* if we have a plane/overlay to show, set that up now: */
891                 for (j = 0; j < plane_count; j++)
892                         if (p[j].con_id == c[i].id)
893                                 if (set_plane(kms, &c[i], &p[j]))
894                                         return;
895         }
896
897         if (!page_flip)
898                 return;
899         
900         if (create_grey_buffer(kms, width, height, &stride, &other_bo))
901                 return;
902
903         kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
904         ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
905                            &other_fb_id);
906         if (ret) {
907                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
908                 return;
909         }
910
911         for (i = 0; i < count; i++) {
912                 if (c[i].mode == NULL)
913                         continue;
914
915                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
916                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
917                 if (ret) {
918                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
919                         return;
920                 }
921                 gettimeofday(&c[i].start, NULL);
922                 c[i].swap_count = 0;
923                 c[i].fb_id[0] = fb_id;
924                 c[i].fb_id[1] = other_fb_id;
925                 c[i].current_fb_id = other_fb_id;
926         }
927
928         memset(&evctx, 0, sizeof evctx);
929         evctx.version = DRM_EVENT_CONTEXT_VERSION;
930         evctx.vblank_handler = NULL;
931         evctx.page_flip_handler = page_flip_handler;
932         
933         while (1) {
934 #if 0
935                 struct pollfd pfd[2];
936
937                 pfd[0].fd = 0;
938                 pfd[0].events = POLLIN;
939                 pfd[1].fd = fd;
940                 pfd[1].events = POLLIN;
941
942                 if (poll(pfd, 2, -1) < 0) {
943                         fprintf(stderr, "poll error\n");
944                         break;
945                 }
946
947                 if (pfd[0].revents)
948                         break;
949 #else
950                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
951                 fd_set fds;
952                 int ret;
953
954                 FD_ZERO(&fds);
955                 FD_SET(0, &fds);
956                 FD_SET(fd, &fds);
957                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
958
959                 if (ret <= 0) {
960                         fprintf(stderr, "select timed out or error (ret %d)\n",
961                                 ret);
962                         continue;
963                 } else if (FD_ISSET(0, &fds)) {
964                         break;
965                 }
966 #endif
967
968                 drmHandleEvent(fd, &evctx);
969         }
970
971         kms_bo_destroy(&bo);
972         kms_bo_destroy(&other_bo);
973         kms_destroy(&kms);
974 }
975
976 extern char *optarg;
977 extern int optind, opterr, optopt;
978 static char optstr[] = "ecpmfs:P:v";
979
980 void usage(char *name)
981 {
982         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
983         fprintf(stderr, "\t-e\tlist encoders\n");
984         fprintf(stderr, "\t-c\tlist connectors\n");
985         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
986         fprintf(stderr, "\t-m\tlist modes\n");
987         fprintf(stderr, "\t-f\tlist framebuffers\n");
988         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
989         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
990         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
991         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
992         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
993         fprintf(stderr, "\n\tDefault is to dump all info.\n");
994         exit(0);
995 }
996
997 #define dump_resource(res) if (res) dump_##res()
998
999 static int page_flipping_supported(void)
1000 {
1001         /*FIXME: generic ioctl needed? */
1002         return 1;
1003 #if 0
1004         int ret, value;
1005         struct drm_i915_getparam gp;
1006
1007         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1008         gp.value = &value;
1009
1010         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1011         if (ret) {
1012                 fprintf(stderr, "drm_i915_getparam: %m\n");
1013                 return 0;
1014         }
1015
1016         return *gp.value;
1017 #endif
1018 }
1019
1020 int main(int argc, char **argv)
1021 {
1022         int c;
1023         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1024         int test_vsync = 0;
1025         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm" };
1026         unsigned int i;
1027         int count = 0, plane_count = 0;
1028         struct connector con_args[2];
1029         struct plane plane_args[2] = {0};
1030         
1031         opterr = 0;
1032         while ((c = getopt(argc, argv, optstr)) != -1) {
1033                 switch (c) {
1034                 case 'e':
1035                         encoders = 1;
1036                         break;
1037                 case 'c':
1038                         connectors = 1;
1039                         break;
1040                 case 'p':
1041                         crtcs = 1;
1042                         planes = 1;
1043                         break;
1044                 case 'm':
1045                         modes = 1;
1046                         break;
1047                 case 'f':
1048                         framebuffers = 1;
1049                         break;
1050                 case 'v':
1051                         test_vsync = 1;
1052                         break;
1053                 case 's':
1054                         con_args[count].crtc = -1;
1055                         if (sscanf(optarg, "%d:%64s",
1056                                    &con_args[count].id,
1057                                    con_args[count].mode_str) != 2 &&
1058                             sscanf(optarg, "%d@%d:%64s",
1059                                    &con_args[count].id,
1060                                    &con_args[count].crtc,
1061                                    con_args[count].mode_str) != 3)
1062                                 usage(argv[0]);
1063                         count++;                                      
1064                         break;
1065                 case 'P':
1066                         strcpy(plane_args[plane_count].format_str, "XR24");
1067                         if (sscanf(optarg, "%d:%dx%d@%4s",
1068                                         &plane_args[plane_count].con_id,
1069                                         &plane_args[plane_count].w,
1070                                         &plane_args[plane_count].h,
1071                                         plane_args[plane_count].format_str) != 4 &&
1072                                 sscanf(optarg, "%d:%dx%d",
1073                                         &plane_args[plane_count].con_id,
1074                                         &plane_args[plane_count].w,
1075                                         &plane_args[plane_count].h) != 3)
1076                                 usage(argv[0]);
1077                         plane_count++;
1078                         break;
1079                 default:
1080                         usage(argv[0]);
1081                         break;
1082                 }
1083         }
1084
1085         if (argc == 1)
1086                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1087
1088         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1089                 printf("trying to load module %s...", modules[i]);
1090                 fd = drmOpen(modules[i], NULL);
1091                 if (fd < 0) {
1092                         printf("failed.\n");
1093                 } else {
1094                         printf("success.\n");
1095                         break;
1096                 }
1097         }
1098
1099         if (test_vsync && !page_flipping_supported()) {
1100                 fprintf(stderr, "page flipping not supported by drm.\n");
1101                 return -1;
1102         }
1103
1104         if (i == ARRAY_SIZE(modules)) {
1105                 fprintf(stderr, "failed to load any modules, aborting.\n");
1106                 return -1;
1107         }
1108
1109         resources = drmModeGetResources(fd);
1110         if (!resources) {
1111                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1112                         strerror(errno));
1113                 drmClose(fd);
1114                 return 1;
1115         }
1116
1117         dump_resource(encoders);
1118         dump_resource(connectors);
1119         dump_resource(crtcs);
1120         dump_resource(planes);
1121         dump_resource(framebuffers);
1122
1123         if (count > 0) {
1124                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1125                 getchar();
1126         }
1127
1128         drmModeFreeResources(resources);
1129
1130         return 0;
1131 }