modetest: add YUV and multi-planar support
[glsdk/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  */
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"
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>
52 #include "xf86drm.h"
53 #include "xf86drmMode.h"
54 #include "drm_fourcc.h"
55 #include "libkms.h"
57 #ifdef HAVE_CAIRO
58 #include <math.h>
59 #include <cairo.h>
60 #endif
62 drmModeRes *resources;
63 int fd, modes;
65 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
67 struct type_name {
68         int type;
69         char *name;
70 };
72 #define type_name_fn(res) \
73 char * res##_str(int type) {                    \
74         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 }
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 };
90 type_name_fn(encoder_type)
92 struct type_name connector_status_names[] = {
93         { DRM_MODE_CONNECTED, "connected" },
94         { DRM_MODE_DISCONNECTED, "disconnected" },
95         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
96 };
98 type_name_fn(connector_status)
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 };
118 type_name_fn(connector_type)
120 void dump_encoders(void)
122         drmModeEncoder *encoder;
123         int i;
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]);
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");
146 void dump_mode(drmModeModeInfo *mode)
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);
161 static void
162 dump_props(drmModeConnector *connector)
164         drmModePropertyPtr props;
165         int i;
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         }
174 void dump_connectors(void)
176         drmModeConnector *connector;
177         int i, j;
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]);
184                 if (!connector) {
185                         fprintf(stderr, "could not get connector %i: %s\n",
186                                 resources->connectors[i], strerror(errno));
187                         continue;
188                 }
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);
198                 for (j = 0; j < connector->count_encoders; j++)
199                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
200                 printf("\n");
202                 if (!connector->count_modes)
203                         continue;
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]);
211                 printf("  props:\n");
212                 dump_props(connector);
214                 drmModeFreeConnector(connector);
215         }
216         printf("\n");
219 void dump_crtcs(void)
221         drmModeCrtc *crtc;
222         int i;
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]);
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);
241                 drmModeFreeCrtc(crtc);
242         }
243         printf("\n");
246 void dump_framebuffers(void)
248         drmModeFB *fb;
249         int i;
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]);
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);
266                 drmModeFreeFB(fb);
267         }
268         printf("\n");
271 static void dump_planes(void)
273         drmModePlaneRes *plane_resources;
274         drmModePlane *ovr;
275         int i, j;
277         plane_resources = drmModeGetPlaneResources(fd);
278         if (!plane_resources) {
279                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
280                         strerror(errno));
281                 return;
282         }
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                 }
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);
299                 if (!ovr->count_formats)
300                         continue;
302                 printf("  formats:");
303                 for (j = 0; j < ovr->count_formats; j++)
304                         printf(" %4.4s", (char *)&ovr->formats[j]);
305                 printf("\n");
307                 drmModeFreePlane(ovr);
308         }
309         printf("\n");
311         return;
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;
331         int swap_count;
332 };
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 };
341 static void
342 connector_find_mode(struct connector *c)
344         drmModeConnector *connector;
345         int i, j;
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]);
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                 }
359                 if (!connector->count_modes) {
360                         drmModeFreeConnector(connector);
361                         continue;
362                 }
364                 if (connector->connector_id != c->id) {
365                         drmModeFreeConnector(connector);
366                         continue;
367                 }
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                 }
375                 /* Found it, break out */
376                 if (c->mode)
377                         break;
379                 drmModeFreeConnector(connector);
380         }
382         if (!c->mode) {
383                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
384                 return;
385         }
387         /* Now get the encoder */
388         for (i = 0; i < resources->count_encoders; i++) {
389                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
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                 }
398                 if (c->encoder->encoder_id  == connector->encoder_id)
399                         break;
401                 drmModeFreeEncoder(c->encoder);
402         }
404         if (c->crtc == -1)
405                 c->crtc = c->encoder->crtc_id;
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         }
417 static struct kms_bo *
418 allocate_buffer(struct kms_driver *kms,
419                 int width, int height, int *stride)
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;
430         bo_attribs[1] = width;
431         bo_attribs[3] = height;
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         }
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         }
448         return bo;
451 static void
452 make_pwetty(void *data, int width, int height, int stride)
454 #ifdef HAVE_CAIRO
455         cairo_surface_t *surface;
456         cairo_t *cr;
457         int x, y;
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);
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];
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);
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                 }
493         cairo_destroy(cr);
494 #endif
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)
502         struct kms_bo *bo;
503         int ret, i, j, stride;
504         void *virtual;
506         bo = allocate_buffer(kms, width, height, &stride);
507         if (!bo)
508                 return -1;
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         }
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         }
529         make_pwetty(virtual, width, height, stride);
531         kms_bo_unmap(bo);
533         *bo_out = bo;
534         *stride_out = stride;
535         return 0;
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)
543         struct kms_bo *bo;
544         int size, ret, stride;
545         void *virtual;
547         bo = allocate_buffer(kms, width, height, &stride);
548         if (!bo)
549                 return -1;
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         }
559         size = stride * height;
560         memset(virtual, 0x77, size);
561         kms_bo_unmap(bo);
563         *bo_out = bo;
564         *stride_out = stride;
566         return 0;
569 void
570 page_flip_handler(int fd, unsigned int frame,
571                   unsigned int sec, unsigned int usec, void *data)
573         struct connector *c;
574         unsigned int new_fb_id;
575         struct timeval end;
576         double t;
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         }
598 /* swap these for big endian.. */
599 #define RED   2
600 #define GREEN 1
601 #define BLUE  0
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)
608         int i, j;
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;
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]);
623                         *(y2p++) = *(y1p++) = y;
624                         *(y2p++) = *(y1p++) = y;
626                         *up = (rgbp[BLUE] - y) * 0.565 + 128;
627                         *vp = (rgbp[RED] - y) * 0.713 + 128;
628                         up += cs;
629                         vp += cs;
630                 }
631         }
634 static void
635 fill422(unsigned char *virtual, int n, int width, int height, int stride)
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]);
647                         *(ptr++) = y;
648                         *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
649                         *(ptr++) = y;
650                         *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
651                 }
652         }
655 static int
656 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
658         drmModePlaneRes *plane_resources;
659         drmModePlane *ovr;
660         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
661         uint32_t plane_id = 0;
662         struct kms_bo *plane_bo;
663         uint32_t plane_flags = 0, format;
664         int i, ret, crtc_x, crtc_y, crtc_w, crtc_h;
666         /* find an unused plane which can be connected to our crtc */
667         plane_resources = drmModeGetPlaneResources(fd);
668         if (!plane_resources) {
669                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
670                         strerror(errno));
671                 return -1;
672         }
674         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
675                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
676                 if (!ovr) {
677                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
678                                 strerror(errno));
679                         return -1;
680                 }
682                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
683                         plane_id = ovr->plane_id;
685                 drmModeFreePlane(ovr);
686         }
688         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
689                         p->w, p->h, p->format_str);
691         if (!plane_id) {
692                 fprintf(stderr, "failed to find plane!\n");
693                 return -1;
694         }
696         if (!strcmp(p->format_str, "XR24")) {
697                 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
698                         return -1;
699                 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
700                 format = DRM_FORMAT_XRGB8888;
701         } else {
702                 void *virtual;
704                 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
705                  * YUV formats, we don't use all of it..  since 4bytes/pixel is
706                  * worst case, so live with it for now and just don't use all
707                  * the buffer:
708                  */
709                 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
710                 if (!plane_bo)
711                         return -1;
713                 ret = kms_bo_map(plane_bo, &virtual);
714                 if (ret) {
715                         fprintf(stderr, "failed to map buffer: %s\n",
716                                 strerror(-ret));
717                         kms_bo_destroy(&plane_bo);
718                         return -1;
719                 }
721                 /* just testing a limited # of formats to test single
722                  * and multi-planar path.. would be nice to add more..
723                  */
724                 if (!strcmp(p->format_str, "YUYV")) {
725                         pitches[0] = p->w * 2;
726                         offsets[0] = 0;
727                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
729                         fill422(virtual, 0, p->w, p->h, pitches[0]);
731                         format = DRM_FORMAT_YUYV;
732                 } else if (!strcmp(p->format_str, "NV12")) {
733                         pitches[0] = p->w;
734                         offsets[0] = 0;
735                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
736                         pitches[1] = p->w;
737                         offsets[1] = p->w * p->h;
738                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
740                         fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
741                                         2, 0, p->w, p->h, pitches[0]);
743                         format = DRM_FORMAT_NV12;
744                 } else if (!strcmp(p->format_str, "YV12")) {
745                         pitches[0] = p->w;
746                         offsets[0] = 0;
747                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
748                         pitches[1] = p->w / 2;
749                         offsets[1] = p->w * p->h;
750                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
751                         pitches[2] = p->w / 2;
752                         offsets[2] = offsets[1] + (p->w * p->h) / 4;
753                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
755                         fill420(virtual, virtual+offsets[1], virtual+offsets[2],
756                                         1, 0, p->w, p->h, pitches[0]);
758                         format = DRM_FORMAT_YVU420;
759                 } else {
760                         fprintf(stderr, "Unknown format: %s\n", p->format_str);
761                         return -1;
762                 }
764                 kms_bo_unmap(plane_bo);
765         }
767         /* just use single plane format for now.. */
768         if (drmModeAddFB2(fd, p->w, p->h, format,
769                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
770                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
771                 return -1;
772         }
774         /* ok, boring.. but for now put in middle of screen: */
775         crtc_x = c->mode->hdisplay / 3;
776         crtc_y = c->mode->vdisplay / 3;
777         crtc_w = crtc_x;
778         crtc_h = crtc_y;
780         /* note src coords (last 4 args) are in Q16 format */
781         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
782                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
783                             0, 0, p->w << 16, p->h << 16)) {
784                 fprintf(stderr, "failed to enable plane: %s\n",
785                         strerror(errno));
786                 return -1;
787         }
789         return 0;
792 static void
793 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
794                 int page_flip)
796         struct kms_driver *kms;
797         struct kms_bo *bo, *other_bo;
798         unsigned int fb_id, other_fb_id;
799         int i, j, ret, width, height, x, stride;
800         unsigned handle;
801         drmEventContext evctx;
803         width = 0;
804         height = 0;
805         for (i = 0; i < count; i++) {
806                 connector_find_mode(&c[i]);
807                 if (c[i].mode == NULL)
808                         continue;
809                 width += c[i].mode->hdisplay;
810                 if (height < c[i].mode->vdisplay)
811                         height = c[i].mode->vdisplay;
812         }
814         ret = kms_create(fd, &kms);
815         if (ret) {
816                 fprintf(stderr, "failed to create kms driver: %s\n",
817                         strerror(-ret));
818                 return;
819         }
821         if (create_test_buffer(kms, width, height, &stride, &bo))
822                 return;
824         kms_bo_get_prop(bo, KMS_HANDLE, &handle);
825         ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
826         if (ret) {
827                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
828                         width, height, strerror(errno));
829                 return;
830         }
832         x = 0;
833         for (i = 0; i < count; i++) {
834                 if (c[i].mode == NULL)
835                         continue;
837                 printf("setting mode %s on connector %d, crtc %d\n",
838                        c[i].mode_str, c[i].id, c[i].crtc);
840                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
841                                      &c[i].id, 1, c[i].mode);
843                 /* XXX: Actually check if this is needed */
844                 drmModeDirtyFB(fd, fb_id, NULL, 0);
846                 x += c[i].mode->hdisplay;
848                 if (ret) {
849                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
850                         return;
851                 }
853                 /* if we have a plane/overlay to show, set that up now: */
854                 for (j = 0; j < plane_count; j++)
855                         if (p[j].con_id == c[i].id)
856                                 if (set_plane(kms, &c[i], &p[j]))
857                                         return;
858         }
860         if (!page_flip)
861                 return;
862         
863         if (create_grey_buffer(kms, width, height, &stride, &other_bo))
864                 return;
866         kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
867         ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
868                            &other_fb_id);
869         if (ret) {
870                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
871                 return;
872         }
874         for (i = 0; i < count; i++) {
875                 if (c[i].mode == NULL)
876                         continue;
878                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
879                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
880                 if (ret) {
881                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
882                         return;
883                 }
884                 gettimeofday(&c[i].start, NULL);
885                 c[i].swap_count = 0;
886                 c[i].fb_id[0] = fb_id;
887                 c[i].fb_id[1] = other_fb_id;
888                 c[i].current_fb_id = other_fb_id;
889         }
891         memset(&evctx, 0, sizeof evctx);
892         evctx.version = DRM_EVENT_CONTEXT_VERSION;
893         evctx.vblank_handler = NULL;
894         evctx.page_flip_handler = page_flip_handler;
895         
896         while (1) {
897 #if 0
898                 struct pollfd pfd[2];
900                 pfd[0].fd = 0;
901                 pfd[0].events = POLLIN;
902                 pfd[1].fd = fd;
903                 pfd[1].events = POLLIN;
905                 if (poll(pfd, 2, -1) < 0) {
906                         fprintf(stderr, "poll error\n");
907                         break;
908                 }
910                 if (pfd[0].revents)
911                         break;
912 #else
913                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
914                 fd_set fds;
915                 int ret;
917                 FD_ZERO(&fds);
918                 FD_SET(0, &fds);
919                 FD_SET(fd, &fds);
920                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
922                 if (ret <= 0) {
923                         fprintf(stderr, "select timed out or error (ret %d)\n",
924                                 ret);
925                         continue;
926                 } else if (FD_ISSET(0, &fds)) {
927                         break;
928                 }
929 #endif
931                 drmHandleEvent(fd, &evctx);
932         }
934         kms_bo_destroy(&bo);
935         kms_bo_destroy(&other_bo);
936         kms_destroy(&kms);
939 extern char *optarg;
940 extern int optind, opterr, optopt;
941 static char optstr[] = "ecpmfs:P:v";
943 void usage(char *name)
945         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
946         fprintf(stderr, "\t-e\tlist encoders\n");
947         fprintf(stderr, "\t-c\tlist connectors\n");
948         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
949         fprintf(stderr, "\t-m\tlist modes\n");
950         fprintf(stderr, "\t-f\tlist framebuffers\n");
951         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
952         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
953         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
954         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
955         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
956         fprintf(stderr, "\n\tDefault is to dump all info.\n");
957         exit(0);
960 #define dump_resource(res) if (res) dump_##res()
962 static int page_flipping_supported(int fd)
964         /*FIXME: generic ioctl needed? */
965         return 1;
966 #if 0
967         int ret, value;
968         struct drm_i915_getparam gp;
970         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
971         gp.value = &value;
973         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
974         if (ret) {
975                 fprintf(stderr, "drm_i915_getparam: %m\n");
976                 return 0;
977         }
979         return *gp.value;
980 #endif
983 int main(int argc, char **argv)
985         int c;
986         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
987         int test_vsync = 0;
988         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm" };
989         char *modeset = NULL;
990         int i, count = 0, plane_count = 0;
991         struct connector con_args[2];
992         struct plane plane_args[2] = {0};
993         
994         opterr = 0;
995         while ((c = getopt(argc, argv, optstr)) != -1) {
996                 switch (c) {
997                 case 'e':
998                         encoders = 1;
999                         break;
1000                 case 'c':
1001                         connectors = 1;
1002                         break;
1003                 case 'p':
1004                         crtcs = 1;
1005                         planes = 1;
1006                         break;
1007                 case 'm':
1008                         modes = 1;
1009                         break;
1010                 case 'f':
1011                         framebuffers = 1;
1012                         break;
1013                 case 'v':
1014                         test_vsync = 1;
1015                         break;
1016                 case 's':
1017                         modeset = strdup(optarg);
1018                         con_args[count].crtc = -1;
1019                         if (sscanf(optarg, "%d:%64s",
1020                                    &con_args[count].id,
1021                                    con_args[count].mode_str) != 2 &&
1022                             sscanf(optarg, "%d@%d:%64s",
1023                                    &con_args[count].id,
1024                                    &con_args[count].crtc,
1025                                    con_args[count].mode_str) != 3)
1026                                 usage(argv[0]);
1027                         count++;                                      
1028                         break;
1029                 case 'P':
1030                         strcpy(plane_args[plane_count].format_str, "XR24");
1031                         if (sscanf(optarg, "%d:%dx%d@%4s",
1032                                         &plane_args[plane_count].con_id,
1033                                         &plane_args[plane_count].w,
1034                                         &plane_args[plane_count].h,
1035                                         plane_args[plane_count].format_str) != 4 &&
1036                                 sscanf(optarg, "%d:%dx%d",
1037                                         &plane_args[plane_count].con_id,
1038                                         &plane_args[plane_count].w,
1039                                         &plane_args[plane_count].h) != 3)
1040                                 usage(argv[0]);
1041                         plane_count++;
1042                         break;
1043                 default:
1044                         usage(argv[0]);
1045                         break;
1046                 }
1047         }
1049         if (argc == 1)
1050                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1052         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1053                 printf("trying to load module %s...", modules[i]);
1054                 fd = drmOpen(modules[i], NULL);
1055                 if (fd < 0) {
1056                         printf("failed.\n");
1057                 } else {
1058                         printf("success.\n");
1059                         break;
1060                 }
1061         }
1063         if (test_vsync && !page_flipping_supported(fd)) {
1064                 fprintf(stderr, "page flipping not supported by drm.\n");
1065                 return -1;
1066         }
1068         if (i == ARRAY_SIZE(modules)) {
1069                 fprintf(stderr, "failed to load any modules, aborting.\n");
1070                 return -1;
1071         }
1073         resources = drmModeGetResources(fd);
1074         if (!resources) {
1075                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1076                         strerror(errno));
1077                 drmClose(fd);
1078                 return 1;
1079         }
1081         dump_resource(encoders);
1082         dump_resource(connectors);
1083         dump_resource(crtcs);
1084         dump_resource(planes);
1085         dump_resource(framebuffers);
1087         if (count > 0) {
1088                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1089                 getchar();
1090         }
1092         drmModeFreeResources(resources);
1094         return 0;