modetest: add AR15/XR15 (RGB-1555) formats
[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 void
656 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
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;
667                         *(ptr++) = 0x8000 |
668                                         (rgbp[RED] >> 3) << 10 |
669                                         (rgbp[GREEN] >> 3) << 5 |
670                                         (rgbp[BLUE] >> 3);
671                 }
672         }
675 static int
676 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
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 i, ret, crtc_x, crtc_y, crtc_w, crtc_h;
686         /* find an unused plane which can be connected to our crtc */
687         plane_resources = drmModeGetPlaneResources(fd);
688         if (!plane_resources) {
689                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
690                         strerror(errno));
691                 return -1;
692         }
694         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
695                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
696                 if (!ovr) {
697                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
698                                 strerror(errno));
699                         return -1;
700                 }
702                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
703                         plane_id = ovr->plane_id;
705                 drmModeFreePlane(ovr);
706         }
708         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
709                         p->w, p->h, p->format_str);
711         if (!plane_id) {
712                 fprintf(stderr, "failed to find plane!\n");
713                 return -1;
714         }
716         if (!strcmp(p->format_str, "XR24")) {
717                 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
718                         return -1;
719                 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
720                 format = DRM_FORMAT_XRGB8888;
721         } else {
722                 void *virtual;
724                 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
725                  * YUV formats, we don't use all of it..  since 4bytes/pixel is
726                  * worst case, so live with it for now and just don't use all
727                  * the buffer:
728                  */
729                 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
730                 if (!plane_bo)
731                         return -1;
733                 ret = kms_bo_map(plane_bo, &virtual);
734                 if (ret) {
735                         fprintf(stderr, "failed to map buffer: %s\n",
736                                 strerror(-ret));
737                         kms_bo_destroy(&plane_bo);
738                         return -1;
739                 }
741                 /* just testing a limited # of formats to test single
742                  * and multi-planar path.. would be nice to add more..
743                  */
744                 if (!strcmp(p->format_str, "YUYV")) {
745                         pitches[0] = p->w * 2;
746                         offsets[0] = 0;
747                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
749                         fill422(virtual, 0, p->w, p->h, pitches[0]);
751                         format = DRM_FORMAT_YUYV;
752                 } else if (!strcmp(p->format_str, "NV12")) {
753                         pitches[0] = p->w;
754                         offsets[0] = 0;
755                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
756                         pitches[1] = p->w;
757                         offsets[1] = p->w * p->h;
758                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
760                         fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
761                                         2, 0, p->w, p->h, pitches[0]);
763                         format = DRM_FORMAT_NV12;
764                 } else if (!strcmp(p->format_str, "YV12")) {
765                         pitches[0] = p->w;
766                         offsets[0] = 0;
767                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
768                         pitches[1] = p->w / 2;
769                         offsets[1] = p->w * p->h;
770                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
771                         pitches[2] = p->w / 2;
772                         offsets[2] = offsets[1] + (p->w * p->h) / 4;
773                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
775                         fill420(virtual, virtual+offsets[1], virtual+offsets[2],
776                                         1, 0, p->w, p->h, pitches[0]);
778                         format = DRM_FORMAT_YVU420;
779                 } else if (!strcmp(p->format_str, "XR15")) {
780                         pitches[0] = p->w * 2;
781                         offsets[0] = 0;
782                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
784                         fill1555(virtual, 0, p->w, p->h, pitches[0]);
786                         format = DRM_FORMAT_XRGB1555;
787                 } else if (!strcmp(p->format_str, "AR15")) {
788                         pitches[0] = p->w * 2;
789                         offsets[0] = 0;
790                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
792                         fill1555(virtual, 0, p->w, p->h, pitches[0]);
794                         format = DRM_FORMAT_ARGB1555;
795                 } else {
796                         fprintf(stderr, "Unknown format: %s\n", p->format_str);
797                         return -1;
798                 }
800                 kms_bo_unmap(plane_bo);
801         }
803         /* just use single plane format for now.. */
804         if (drmModeAddFB2(fd, p->w, p->h, format,
805                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
806                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
807                 return -1;
808         }
810         /* ok, boring.. but for now put in middle of screen: */
811         crtc_x = c->mode->hdisplay / 3;
812         crtc_y = c->mode->vdisplay / 3;
813         crtc_w = crtc_x;
814         crtc_h = crtc_y;
816         /* note src coords (last 4 args) are in Q16 format */
817         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
818                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
819                             0, 0, p->w << 16, p->h << 16)) {
820                 fprintf(stderr, "failed to enable plane: %s\n",
821                         strerror(errno));
822                 return -1;
823         }
825         return 0;
828 static void
829 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
830                 int page_flip)
832         struct kms_driver *kms;
833         struct kms_bo *bo, *other_bo;
834         unsigned int fb_id, other_fb_id;
835         int i, j, ret, width, height, x, stride;
836         unsigned handle;
837         drmEventContext evctx;
839         width = 0;
840         height = 0;
841         for (i = 0; i < count; i++) {
842                 connector_find_mode(&c[i]);
843                 if (c[i].mode == NULL)
844                         continue;
845                 width += c[i].mode->hdisplay;
846                 if (height < c[i].mode->vdisplay)
847                         height = c[i].mode->vdisplay;
848         }
850         ret = kms_create(fd, &kms);
851         if (ret) {
852                 fprintf(stderr, "failed to create kms driver: %s\n",
853                         strerror(-ret));
854                 return;
855         }
857         if (create_test_buffer(kms, width, height, &stride, &bo))
858                 return;
860         kms_bo_get_prop(bo, KMS_HANDLE, &handle);
861         ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
862         if (ret) {
863                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
864                         width, height, strerror(errno));
865                 return;
866         }
868         x = 0;
869         for (i = 0; i < count; i++) {
870                 if (c[i].mode == NULL)
871                         continue;
873                 printf("setting mode %s on connector %d, crtc %d\n",
874                        c[i].mode_str, c[i].id, c[i].crtc);
876                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
877                                      &c[i].id, 1, c[i].mode);
879                 /* XXX: Actually check if this is needed */
880                 drmModeDirtyFB(fd, fb_id, NULL, 0);
882                 x += c[i].mode->hdisplay;
884                 if (ret) {
885                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
886                         return;
887                 }
889                 /* if we have a plane/overlay to show, set that up now: */
890                 for (j = 0; j < plane_count; j++)
891                         if (p[j].con_id == c[i].id)
892                                 if (set_plane(kms, &c[i], &p[j]))
893                                         return;
894         }
896         if (!page_flip)
897                 return;
898         
899         if (create_grey_buffer(kms, width, height, &stride, &other_bo))
900                 return;
902         kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
903         ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
904                            &other_fb_id);
905         if (ret) {
906                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
907                 return;
908         }
910         for (i = 0; i < count; i++) {
911                 if (c[i].mode == NULL)
912                         continue;
914                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
915                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
916                 if (ret) {
917                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
918                         return;
919                 }
920                 gettimeofday(&c[i].start, NULL);
921                 c[i].swap_count = 0;
922                 c[i].fb_id[0] = fb_id;
923                 c[i].fb_id[1] = other_fb_id;
924                 c[i].current_fb_id = other_fb_id;
925         }
927         memset(&evctx, 0, sizeof evctx);
928         evctx.version = DRM_EVENT_CONTEXT_VERSION;
929         evctx.vblank_handler = NULL;
930         evctx.page_flip_handler = page_flip_handler;
931         
932         while (1) {
933 #if 0
934                 struct pollfd pfd[2];
936                 pfd[0].fd = 0;
937                 pfd[0].events = POLLIN;
938                 pfd[1].fd = fd;
939                 pfd[1].events = POLLIN;
941                 if (poll(pfd, 2, -1) < 0) {
942                         fprintf(stderr, "poll error\n");
943                         break;
944                 }
946                 if (pfd[0].revents)
947                         break;
948 #else
949                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
950                 fd_set fds;
951                 int ret;
953                 FD_ZERO(&fds);
954                 FD_SET(0, &fds);
955                 FD_SET(fd, &fds);
956                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
958                 if (ret <= 0) {
959                         fprintf(stderr, "select timed out or error (ret %d)\n",
960                                 ret);
961                         continue;
962                 } else if (FD_ISSET(0, &fds)) {
963                         break;
964                 }
965 #endif
967                 drmHandleEvent(fd, &evctx);
968         }
970         kms_bo_destroy(&bo);
971         kms_bo_destroy(&other_bo);
972         kms_destroy(&kms);
975 extern char *optarg;
976 extern int optind, opterr, optopt;
977 static char optstr[] = "ecpmfs:P:v";
979 void usage(char *name)
981         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
982         fprintf(stderr, "\t-e\tlist encoders\n");
983         fprintf(stderr, "\t-c\tlist connectors\n");
984         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
985         fprintf(stderr, "\t-m\tlist modes\n");
986         fprintf(stderr, "\t-f\tlist framebuffers\n");
987         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
988         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
989         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
990         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
991         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
992         fprintf(stderr, "\n\tDefault is to dump all info.\n");
993         exit(0);
996 #define dump_resource(res) if (res) dump_##res()
998 static int page_flipping_supported(int fd)
1000         /*FIXME: generic ioctl needed? */
1001         return 1;
1002 #if 0
1003         int ret, value;
1004         struct drm_i915_getparam gp;
1006         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1007         gp.value = &value;
1009         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1010         if (ret) {
1011                 fprintf(stderr, "drm_i915_getparam: %m\n");
1012                 return 0;
1013         }
1015         return *gp.value;
1016 #endif
1019 int main(int argc, char **argv)
1021         int c;
1022         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1023         int test_vsync = 0;
1024         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm" };
1025         char *modeset = NULL;
1026         int i, count = 0, plane_count = 0;
1027         struct connector con_args[2];
1028         struct plane plane_args[2] = {0};
1029         
1030         opterr = 0;
1031         while ((c = getopt(argc, argv, optstr)) != -1) {
1032                 switch (c) {
1033                 case 'e':
1034                         encoders = 1;
1035                         break;
1036                 case 'c':
1037                         connectors = 1;
1038                         break;
1039                 case 'p':
1040                         crtcs = 1;
1041                         planes = 1;
1042                         break;
1043                 case 'm':
1044                         modes = 1;
1045                         break;
1046                 case 'f':
1047                         framebuffers = 1;
1048                         break;
1049                 case 'v':
1050                         test_vsync = 1;
1051                         break;
1052                 case 's':
1053                         modeset = strdup(optarg);
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         }
1085         if (argc == 1)
1086                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
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         }
1099         if (test_vsync && !page_flipping_supported(fd)) {
1100                 fprintf(stderr, "page flipping not supported by drm.\n");
1101                 return -1;
1102         }
1104         if (i == ARRAY_SIZE(modules)) {
1105                 fprintf(stderr, "failed to load any modules, aborting.\n");
1106                 return -1;
1107         }
1109         resources = drmModeGetResources(fd);
1110         if (!resources) {
1111                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1112                         strerror(errno));
1113                 drmClose(fd);
1114                 return 1;
1115         }
1117         dump_resource(encoders);
1118         dump_resource(connectors);
1119         dump_resource(crtcs);
1120         dump_resource(planes);
1121         dump_resource(framebuffers);
1123         if (count > 0) {
1124                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1125                 getchar();
1126         }
1128         drmModeFreeResources(resources);
1130         return 0;