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