modetest: Add test pattern support for missing RGB 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 <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 #define bit_name_fn(res)                                        \
122 char * res##_str(int type) {                                    \
123         int i;                                                  \
124         const char *sep = "";                                   \
125         for (i = 0; i < ARRAY_SIZE(res##_names); i++) {         \
126                 if (type & (1 << i)) {                          \
127                         printf("%s%s", sep, res##_names[i]);    \
128                         sep = ", ";                             \
129                 }                                               \
130         }                                                       \
133 static const char *mode_type_names[] = {
134         "builtin",
135         "clock_c",
136         "crtc_c",
137         "preferred",
138         "default",
139         "userdef",
140         "driver",
141 };
143 bit_name_fn(mode_type)
145 static const char *mode_flag_names[] = {
146         "phsync",
147         "nhsync",
148         "pvsync",
149         "nvsync",
150         "interlace",
151         "dblscan",
152         "csync",
153         "pcsync",
154         "ncsync",
155         "hskew",
156         "bcast",
157         "pixmux",
158         "dblclk",
159         "clkdiv2"
160 };
162 bit_name_fn(mode_flag)
164 void dump_encoders(void)
166         drmModeEncoder *encoder;
167         int i;
169         printf("Encoders:\n");
170         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
171         for (i = 0; i < resources->count_encoders; i++) {
172                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
174                 if (!encoder) {
175                         fprintf(stderr, "could not get encoder %i: %s\n",
176                                 resources->encoders[i], strerror(errno));
177                         continue;
178                 }
179                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
180                        encoder->encoder_id,
181                        encoder->crtc_id,
182                        encoder_type_str(encoder->encoder_type),
183                        encoder->possible_crtcs,
184                        encoder->possible_clones);
185                 drmModeFreeEncoder(encoder);
186         }
187         printf("\n");
190 void dump_mode(drmModeModeInfo *mode)
192         printf("  %s %d %d %d %d %d %d %d %d %d",
193                mode->name,
194                mode->vrefresh,
195                mode->hdisplay,
196                mode->hsync_start,
197                mode->hsync_end,
198                mode->htotal,
199                mode->vdisplay,
200                mode->vsync_start,
201                mode->vsync_end,
202                mode->vtotal);
204         printf(" flags: ");
205         mode_flag_str(mode->flags);
206         printf("; type: ");
207         mode_type_str(mode->type);
208         printf("\n");
211 static void
212 dump_blob(uint32_t blob_id)
214         uint32_t i;
215         unsigned char *blob_data;
216         drmModePropertyBlobPtr blob;
218         blob = drmModeGetPropertyBlob(fd, blob_id);
219         if (!blob)
220                 return;
222         blob_data = blob->data;
224         for (i = 0; i < blob->length; i++) {
225                 if (i % 16 == 0)
226                         printf("\n\t\t\t");
227                 printf("%.2hhx", blob_data[i]);
228         }
229         printf("\n");
231         drmModeFreePropertyBlob(blob);
234 static void
235 dump_prop(uint32_t prop_id, uint64_t value)
237         int i;
238         drmModePropertyPtr prop;
240         prop = drmModeGetProperty(fd, prop_id);
242         printf("\t%d", prop_id);
243         if (!prop) {
244                 printf("\n");
245                 return;
246         }
248         printf(" %s:\n", prop->name);
250         printf("\t\tflags:");
251         if (prop->flags & DRM_MODE_PROP_PENDING)
252                 printf(" pending");
253         if (prop->flags & DRM_MODE_PROP_RANGE)
254                 printf(" range");
255         if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
256                 printf(" immutable");
257         if (prop->flags & DRM_MODE_PROP_ENUM)
258                 printf(" enum");
259         if (prop->flags & DRM_MODE_PROP_BITMASK)
260                 printf(" bitmask");
261         if (prop->flags & DRM_MODE_PROP_BLOB)
262                 printf(" blob");
263         printf("\n");
265         if (prop->flags & DRM_MODE_PROP_RANGE) {
266                 printf("\t\tvalues:");
267                 for (i = 0; i < prop->count_values; i++)
268                         printf(" %"PRIu64, prop->values[i]);
269                 printf("\n");
270         }
272         if (prop->flags & DRM_MODE_PROP_ENUM) {
273                 printf("\t\tenums:");
274                 for (i = 0; i < prop->count_enums; i++)
275                         printf(" %s=%llu", prop->enums[i].name,
276                                prop->enums[i].value);
277                 printf("\n");
278         } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
279                 printf("\t\tvalues:");
280                 for (i = 0; i < prop->count_enums; i++)
281                         printf(" %s=0x%llx", prop->enums[i].name,
282                                (1LL << prop->enums[i].value));
283                 printf("\n");
284         } else {
285                 assert(prop->count_enums == 0);
286         }
288         if (prop->flags & DRM_MODE_PROP_BLOB) {
289                 printf("\t\tblobs:\n");
290                 for (i = 0; i < prop->count_blobs; i++)
291                         dump_blob(prop->blob_ids[i]);
292                 printf("\n");
293         } else {
294                 assert(prop->count_blobs == 0);
295         }
297         printf("\t\tvalue:");
298         if (prop->flags & DRM_MODE_PROP_BLOB)
299                 dump_blob(value);
300         else
301                 printf(" %"PRIu64"\n", value);
303         drmModeFreeProperty(prop);
306 void dump_connectors(void)
308         drmModeConnector *connector;
309         int i, j;
311         printf("Connectors:\n");
312         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
313         for (i = 0; i < resources->count_connectors; i++) {
314                 connector = drmModeGetConnector(fd, resources->connectors[i]);
316                 if (!connector) {
317                         fprintf(stderr, "could not get connector %i: %s\n",
318                                 resources->connectors[i], strerror(errno));
319                         continue;
320                 }
322                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
323                        connector->connector_id,
324                        connector->encoder_id,
325                        connector_status_str(connector->connection),
326                        connector_type_str(connector->connector_type),
327                        connector->mmWidth, connector->mmHeight,
328                        connector->count_modes);
330                 for (j = 0; j < connector->count_encoders; j++)
331                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
332                 printf("\n");
334                 if (connector->count_modes) {
335                         printf("  modes:\n");
336                         printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
337                                "vss vse vtot)\n");
338                         for (j = 0; j < connector->count_modes; j++)
339                                 dump_mode(&connector->modes[j]);
341                         printf("  props:\n");
342                         for (j = 0; j < connector->count_props; j++)
343                                 dump_prop(connector->props[j],
344                                           connector->prop_values[j]);
345                 }
347                 drmModeFreeConnector(connector);
348         }
349         printf("\n");
352 void dump_crtcs(void)
354         drmModeCrtc *crtc;
355         drmModeObjectPropertiesPtr props;
356         int i;
357         uint32_t j;
359         printf("CRTCs:\n");
360         printf("id\tfb\tpos\tsize\n");
361         for (i = 0; i < resources->count_crtcs; i++) {
362                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
364                 if (!crtc) {
365                         fprintf(stderr, "could not get crtc %i: %s\n",
366                                 resources->crtcs[i], strerror(errno));
367                         continue;
368                 }
369                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
370                        crtc->crtc_id,
371                        crtc->buffer_id,
372                        crtc->x, crtc->y,
373                        crtc->width, crtc->height);
374                 dump_mode(&crtc->mode);
376                 printf("  props:\n");
377                 props = drmModeObjectGetProperties(fd, crtc->crtc_id,
378                                                    DRM_MODE_OBJECT_CRTC);
379                 if (props) {
380                         for (j = 0; j < props->count_props; j++)
381                                 dump_prop(props->props[j],
382                                           props->prop_values[j]);
383                         drmModeFreeObjectProperties(props);
384                 } else {
385                         printf("\tcould not get crtc properties: %s\n",
386                                strerror(errno));
387                 }
389                 drmModeFreeCrtc(crtc);
390         }
391         printf("\n");
394 void dump_framebuffers(void)
396         drmModeFB *fb;
397         int i;
399         printf("Frame buffers:\n");
400         printf("id\tsize\tpitch\n");
401         for (i = 0; i < resources->count_fbs; i++) {
402                 fb = drmModeGetFB(fd, resources->fbs[i]);
404                 if (!fb) {
405                         fprintf(stderr, "could not get fb %i: %s\n",
406                                 resources->fbs[i], strerror(errno));
407                         continue;
408                 }
409                 printf("%u\t(%ux%u)\t%u\n",
410                        fb->fb_id,
411                        fb->width, fb->height,
412                        fb->pitch);
414                 drmModeFreeFB(fb);
415         }
416         printf("\n");
419 static void dump_planes(void)
421         drmModeObjectPropertiesPtr props;
422         drmModePlaneRes *plane_resources;
423         drmModePlane *ovr;
424         unsigned int i, j;
426         plane_resources = drmModeGetPlaneResources(fd);
427         if (!plane_resources) {
428                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
429                         strerror(errno));
430                 return;
431         }
433         printf("Planes:\n");
434         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
435         for (i = 0; i < plane_resources->count_planes; i++) {
436                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
437                 if (!ovr) {
438                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
439                                 strerror(errno));
440                         continue;
441                 }
443                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
444                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
445                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
446                        ovr->gamma_size);
448                 if (!ovr->count_formats)
449                         continue;
451                 printf("  formats:");
452                 for (j = 0; j < ovr->count_formats; j++)
453                         printf(" %4.4s", (char *)&ovr->formats[j]);
454                 printf("\n");
456                 printf("  props:\n");
457                 props = drmModeObjectGetProperties(fd, ovr->plane_id,
458                                                    DRM_MODE_OBJECT_PLANE);
459                 if (props) {
460                         for (j = 0; j < props->count_props; j++)
461                                 dump_prop(props->props[j],
462                                           props->prop_values[j]);
463                         drmModeFreeObjectProperties(props);
464                 } else {
465                         printf("\tcould not get plane properties: %s\n",
466                                strerror(errno));
467                 }
469                 drmModeFreePlane(ovr);
470         }
471         printf("\n");
473         drmModeFreePlaneResources(plane_resources);
474         return;
477 /* -----------------------------------------------------------------------------
478  * Connectors and planes
479  */
481 /*
482  * Mode setting with the kernel interfaces is a bit of a chore.
483  * First you have to find the connector in question and make sure the
484  * requested mode is available.
485  * Then you need to find the encoder attached to that connector so you
486  * can bind it with a free crtc.
487  */
488 struct connector {
489         uint32_t id;
490         char mode_str[64];
491         drmModeModeInfo *mode;
492         drmModeEncoder *encoder;
493         int crtc;
494         int pipe;
495         unsigned int fb_id[2], current_fb_id;
496         struct timeval start;
498         int swap_count;
499 };
501 struct plane {
502         uint32_t con_id;  /* the id of connector to bind to */
503         uint32_t w, h;
504         unsigned int fb_id;
505         char format_str[5]; /* need to leave room for terminating \0 */
506 };
508 static void
509 connector_find_mode(struct connector *c)
511         drmModeConnector *connector;
512         int i, j;
514         /* First, find the connector & mode */
515         c->mode = NULL;
516         for (i = 0; i < resources->count_connectors; i++) {
517                 connector = drmModeGetConnector(fd, resources->connectors[i]);
519                 if (!connector) {
520                         fprintf(stderr, "could not get connector %i: %s\n",
521                                 resources->connectors[i], strerror(errno));
522                         drmModeFreeConnector(connector);
523                         continue;
524                 }
526                 if (!connector->count_modes) {
527                         drmModeFreeConnector(connector);
528                         continue;
529                 }
531                 if (connector->connector_id != c->id) {
532                         drmModeFreeConnector(connector);
533                         continue;
534                 }
536                 for (j = 0; j < connector->count_modes; j++) {
537                         c->mode = &connector->modes[j];
538                         if (!strcmp(c->mode->name, c->mode_str))
539                                 break;
540                 }
542                 /* Found it, break out */
543                 if (c->mode)
544                         break;
546                 drmModeFreeConnector(connector);
547         }
549         if (!c->mode) {
550                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
551                 return;
552         }
554         /* Now get the encoder */
555         for (i = 0; i < resources->count_encoders; i++) {
556                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
558                 if (!c->encoder) {
559                         fprintf(stderr, "could not get encoder %i: %s\n",
560                                 resources->encoders[i], strerror(errno));
561                         drmModeFreeEncoder(c->encoder);
562                         continue;
563                 }
565                 if (c->encoder->encoder_id  == connector->encoder_id)
566                         break;
568                 drmModeFreeEncoder(c->encoder);
569         }
571         if (c->crtc == -1)
572                 c->crtc = c->encoder->crtc_id;
574         /* and figure out which crtc index it is: */
575         for (i = 0; i < resources->count_crtcs; i++) {
576                 if (c->crtc == resources->crtcs[i]) {
577                         c->pipe = i;
578                         break;
579                 }
580         }
584 /* -----------------------------------------------------------------------------
585  * Formats
586  */
588 struct color_component {
589         unsigned int length;
590         unsigned int offset;
591 };
593 struct rgb_info {
594         struct color_component red;
595         struct color_component green;
596         struct color_component blue;
597         struct color_component alpha;
598 };
600 enum yuv_order {
601         YUV_YCbCr = 1,
602         YUV_YCrCb = 2,
603         YUV_YC = 4,
604         YUV_CY = 8,
605 };
607 struct yuv_info {
608         enum yuv_order order;
609         unsigned int xsub;
610         unsigned int ysub;
611         unsigned int chroma_stride;
612 };
614 struct format_info {
615         unsigned int format;
616         const char *name;
617         const struct rgb_info rgb;
618         const struct yuv_info yuv;
619 };
621 #define MAKE_RGB_INFO(rl, ro, bl, bo, gl, go, al, ao) \
622         .rgb = { { (rl), (ro) }, { (bl), (bo) }, { (gl), (go) }, { (al), (ao) } }
624 #define MAKE_YUV_INFO(order, xsub, ysub, chroma_stride) \
625         .yuv = { (order), (xsub), (ysub), (chroma_stride) }
627 static const struct format_info format_info[] = {
628         /* YUV packed */
629         { DRM_FORMAT_UYVY, "UYVY", MAKE_YUV_INFO(YUV_YCbCr | YUV_CY, 2, 2, 2) },
630         { DRM_FORMAT_VYUY, "VYUY", MAKE_YUV_INFO(YUV_YCrCb | YUV_CY, 2, 2, 2) },
631         { DRM_FORMAT_YUYV, "YUYV", MAKE_YUV_INFO(YUV_YCbCr | YUV_YC, 2, 2, 2) },
632         { DRM_FORMAT_YVYU, "YVYU", MAKE_YUV_INFO(YUV_YCrCb | YUV_YC, 2, 2, 2) },
633         /* YUV semi-planar */
634         { DRM_FORMAT_NV12, "NV12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 2) },
635         { DRM_FORMAT_NV21, "NV21", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 2) },
636         { DRM_FORMAT_NV16, "NV16", MAKE_YUV_INFO(YUV_YCbCr, 2, 1, 2) },
637         { DRM_FORMAT_NV61, "NV61", MAKE_YUV_INFO(YUV_YCrCb, 2, 1, 2) },
638         /* YUV planar */
639         { DRM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
640         /* RGB16 */
641         { DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
642         { DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
643         { DRM_FORMAT_RGB565, "RG16", MAKE_RGB_INFO(5, 11, 6, 5, 5, 0, 0, 0) },
644         /* RGB24 */
645         { DRM_FORMAT_BGR888, "BG24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
646         { DRM_FORMAT_RGB888, "RG24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
647         /* RGB32 */
648         { DRM_FORMAT_ARGB8888, "AR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 8, 24) },
649         { DRM_FORMAT_BGRA8888, "BA24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 8, 0) },
650         { DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
651         { DRM_FORMAT_BGRX8888, "BX24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 0, 0) },
652 };
654 unsigned int format_fourcc(const char *name)
656         unsigned int i;
657         for (i = 0; i < ARRAY_SIZE(format_info); i++) {
658                 if (!strcmp(format_info[i].name, name))
659                         return format_info[i].format;
660         }
661         return 0;
664 /* -----------------------------------------------------------------------------
665  * Test patterns
666  */
668 enum fill_pattern {
669         PATTERN_TILES = 0,
670         PATTERN_PLAIN = 1,
671         PATTERN_SMPTE = 2,
672 };
674 struct color_rgb24 {
675         unsigned int value:24;
676 } __attribute__((__packed__));
678 struct color_yuv {
679         unsigned char y;
680         unsigned char u;
681         unsigned char v;
682 };
684 #define MAKE_YUV_601_Y(r, g, b) \
685         ((( 66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) + 16)
686 #define MAKE_YUV_601_U(r, g, b) \
687         (((-38 * (r) -  74 * (g) + 112 * (b) + 128) >> 8) + 128)
688 #define MAKE_YUV_601_V(r, g, b) \
689         (((112 * (r) -  94 * (g) -  18 * (b) + 128) >> 8) + 128)
691 #define MAKE_YUV_601(r, g, b) \
692         { .y = MAKE_YUV_601_Y(r, g, b), \
693           .u = MAKE_YUV_601_U(r, g, b), \
694           .v = MAKE_YUV_601_V(r, g, b) }
696 #define MAKE_RGBA(rgb, r, g, b, a) \
697         ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
698          (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
699          (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
700          (((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
702 #define MAKE_RGB24(rgb, r, g, b) \
703         { .value = MAKE_RGBA(rgb, r, g, b, 0) }
705 static void
706 fill_smpte_yuv_planar(const struct yuv_info *yuv,
707                       unsigned char *y_mem, unsigned char *u_mem,
708                       unsigned char *v_mem, unsigned int width,
709                       unsigned int height, unsigned int stride)
711         const struct color_yuv colors_top[] = {
712                 MAKE_YUV_601(191, 192, 192),    /* grey */
713                 MAKE_YUV_601(192, 192, 0),      /* yellow */
714                 MAKE_YUV_601(0, 192, 192),      /* cyan */
715                 MAKE_YUV_601(0, 192, 0),        /* green */
716                 MAKE_YUV_601(192, 0, 192),      /* magenta */
717                 MAKE_YUV_601(192, 0, 0),        /* red */
718                 MAKE_YUV_601(0, 0, 192),        /* blue */
719         };
720         const struct color_yuv colors_middle[] = {
721                 MAKE_YUV_601(0, 0, 192),        /* blue */
722                 MAKE_YUV_601(19, 19, 19),       /* black */
723                 MAKE_YUV_601(192, 0, 192),      /* magenta */
724                 MAKE_YUV_601(19, 19, 19),       /* black */
725                 MAKE_YUV_601(0, 192, 192),      /* cyan */
726                 MAKE_YUV_601(19, 19, 19),       /* black */
727                 MAKE_YUV_601(192, 192, 192),    /* grey */
728         };
729         const struct color_yuv colors_bottom[] = {
730                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
731                 MAKE_YUV_601(255, 255, 255),    /* super white */
732                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
733                 MAKE_YUV_601(19, 19, 19),       /* black */
734                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
735                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
736                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
737                 MAKE_YUV_601(19, 19, 19),       /* black */
738         };
739         unsigned int cs = yuv->chroma_stride;
740         unsigned int xsub = yuv->xsub;
741         unsigned int ysub = yuv->ysub;
742         unsigned int x;
743         unsigned int y;
745         /* Luma */
746         for (y = 0; y < height * 6 / 9; ++y) {
747                 for (x = 0; x < width; ++x)
748                         y_mem[x] = colors_top[x * 7 / width].y;
749                 y_mem += stride;
750         }
752         for (; y < height * 7 / 9; ++y) {
753                 for (x = 0; x < width; ++x)
754                         y_mem[x] = colors_middle[x * 7 / width].y;
755                 y_mem += stride;
756         }
758         for (; y < height; ++y) {
759                 for (x = 0; x < width * 5 / 7; ++x)
760                         y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
761                 for (; x < width * 6 / 7; ++x)
762                         y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
763                                                  / (width / 7) + 4].y;
764                 for (; x < width; ++x)
765                         y_mem[x] = colors_bottom[7].y;
766                 y_mem += stride;
767         }
769         /* Chroma */
770         for (y = 0; y < height / ysub * 6 / 9; ++y) {
771                 for (x = 0; x < width; x += xsub) {
772                         u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
773                         v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
774                 }
775                 u_mem += stride * cs / xsub;
776                 v_mem += stride * cs / xsub;
777         }
779         for (; y < height / ysub * 7 / 9; ++y) {
780                 for (x = 0; x < width; x += xsub) {
781                         u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
782                         v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
783                 }
784                 u_mem += stride * cs / xsub;
785                 v_mem += stride * cs / xsub;
786         }
788         for (; y < height / ysub; ++y) {
789                 for (x = 0; x < width * 5 / 7; x += xsub) {
790                         u_mem[x*cs/xsub] =
791                                 colors_bottom[x * 4 / (width * 5 / 7)].u;
792                         v_mem[x*cs/xsub] =
793                                 colors_bottom[x * 4 / (width * 5 / 7)].v;
794                 }
795                 for (; x < width * 6 / 7; x += xsub) {
796                         u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
797                                                          3 / (width / 7) + 4].u;
798                         v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
799                                                          3 / (width / 7) + 4].v;
800                 }
801                 for (; x < width; x += xsub) {
802                         u_mem[x*cs/xsub] = colors_bottom[7].u;
803                         v_mem[x*cs/xsub] = colors_bottom[7].v;
804                 }
805                 u_mem += stride * cs / xsub;
806                 v_mem += stride * cs / xsub;
807         }
810 static void
811 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
812                       unsigned int width, unsigned int height,
813                       unsigned int stride)
815         const struct color_yuv colors_top[] = {
816                 MAKE_YUV_601(191, 192, 192),    /* grey */
817                 MAKE_YUV_601(192, 192, 0),      /* yellow */
818                 MAKE_YUV_601(0, 192, 192),      /* cyan */
819                 MAKE_YUV_601(0, 192, 0),        /* green */
820                 MAKE_YUV_601(192, 0, 192),      /* magenta */
821                 MAKE_YUV_601(192, 0, 0),        /* red */
822                 MAKE_YUV_601(0, 0, 192),        /* blue */
823         };
824         const struct color_yuv colors_middle[] = {
825                 MAKE_YUV_601(0, 0, 192),        /* blue */
826                 MAKE_YUV_601(19, 19, 19),       /* black */
827                 MAKE_YUV_601(192, 0, 192),      /* magenta */
828                 MAKE_YUV_601(19, 19, 19),       /* black */
829                 MAKE_YUV_601(0, 192, 192),      /* cyan */
830                 MAKE_YUV_601(19, 19, 19),       /* black */
831                 MAKE_YUV_601(192, 192, 192),    /* grey */
832         };
833         const struct color_yuv colors_bottom[] = {
834                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
835                 MAKE_YUV_601(255, 255, 255),    /* super white */
836                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
837                 MAKE_YUV_601(19, 19, 19),       /* black */
838                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
839                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
840                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
841                 MAKE_YUV_601(19, 19, 19),       /* black */
842         };
843         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
844         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
845         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
846         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
847         unsigned int x;
848         unsigned int y;
850         /* Luma */
851         for (y = 0; y < height * 6 / 9; ++y) {
852                 for (x = 0; x < width; ++x)
853                         y_mem[2*x] = colors_top[x * 7 / width].y;
854                 y_mem += stride * 2;
855         }
857         for (; y < height * 7 / 9; ++y) {
858                 for (x = 0; x < width; ++x)
859                         y_mem[2*x] = colors_middle[x * 7 / width].y;
860                 y_mem += stride * 2;
861         }
863         for (; y < height; ++y) {
864                 for (x = 0; x < width * 5 / 7; ++x)
865                         y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
866                 for (; x < width * 6 / 7; ++x)
867                         y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
868                                                    / (width / 7) + 4].y;
869                 for (; x < width; ++x)
870                         y_mem[2*x] = colors_bottom[7].y;
871                 y_mem += stride * 2;
872         }
874         /* Chroma */
875         for (y = 0; y < height * 6 / 9; ++y) {
876                 for (x = 0; x < width; x += 2) {
877                         c_mem[2*x+u] = colors_top[x * 7 / width].u;
878                         c_mem[2*x+v] = colors_top[x * 7 / width].v;
879                 }
880                 c_mem += stride * 2;
881         }
883         for (; y < height * 7 / 9; ++y) {
884                 for (x = 0; x < width; x += 2) {
885                         c_mem[2*x+u] = colors_middle[x * 7 / width].u;
886                         c_mem[2*x+v] = colors_middle[x * 7 / width].v;
887                 }
888                 c_mem += stride * 2;
889         }
891         for (; y < height; ++y) {
892                 for (x = 0; x < width * 5 / 7; x += 2) {
893                         c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
894                         c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
895                 }
896                 for (; x < width * 6 / 7; x += 2) {
897                         c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
898                                                      3 / (width / 7) + 4].u;
899                         c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
900                                                      3 / (width / 7) + 4].v;
901                 }
902                 for (; x < width; x += 2) {
903                         c_mem[2*x+u] = colors_bottom[7].u;
904                         c_mem[2*x+v] = colors_bottom[7].v;
905                 }
906                 c_mem += stride * 2;
907         }
910 static void
911 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
912                  unsigned int width, unsigned int height, unsigned int stride)
914         const uint16_t colors_top[] = {
915                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
916                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
917                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
918                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
919                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
920                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
921                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
922         };
923         const uint16_t colors_middle[] = {
924                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
925                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
926                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
927                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
928                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
929                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
930                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
931         };
932         const uint16_t colors_bottom[] = {
933                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
934                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
935                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
936                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
937                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
938                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
939                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
940                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
941         };
942         unsigned int x;
943         unsigned int y;
945         for (y = 0; y < height * 6 / 9; ++y) {
946                 for (x = 0; x < width; ++x)
947                         ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
948                 mem += stride;
949         }
951         for (; y < height * 7 / 9; ++y) {
952                 for (x = 0; x < width; ++x)
953                         ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
954                 mem += stride;
955         }
957         for (; y < height; ++y) {
958                 for (x = 0; x < width * 5 / 7; ++x)
959                         ((uint16_t *)mem)[x] =
960                                 colors_bottom[x * 4 / (width * 5 / 7)];
961                 for (; x < width * 6 / 7; ++x)
962                         ((uint16_t *)mem)[x] =
963                                 colors_bottom[(x - width * 5 / 7) * 3
964                                               / (width / 7) + 4];
965                 for (; x < width; ++x)
966                         ((uint16_t *)mem)[x] = colors_bottom[7];
967                 mem += stride;
968         }
971 static void
972 fill_smpte_rgb24(const struct rgb_info *rgb, void *mem,
973                  unsigned int width, unsigned int height, unsigned int stride)
975         const struct color_rgb24 colors_top[] = {
976                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
977                 MAKE_RGB24(rgb, 192, 192, 0),   /* yellow */
978                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
979                 MAKE_RGB24(rgb, 0, 192, 0),     /* green */
980                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
981                 MAKE_RGB24(rgb, 192, 0, 0),     /* red */
982                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
983         };
984         const struct color_rgb24 colors_middle[] = {
985                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
986                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
987                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
988                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
989                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
990                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
991                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
992         };
993         const struct color_rgb24 colors_bottom[] = {
994                 MAKE_RGB24(rgb, 0, 33, 76),     /* in-phase */
995                 MAKE_RGB24(rgb, 255, 255, 255), /* super white */
996                 MAKE_RGB24(rgb, 50, 0, 106),    /* quadrature */
997                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
998                 MAKE_RGB24(rgb, 9, 9, 9),       /* 3.5% */
999                 MAKE_RGB24(rgb, 19, 19, 19),    /* 7.5% */
1000                 MAKE_RGB24(rgb, 29, 29, 29),    /* 11.5% */
1001                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
1002         };
1003         unsigned int x;
1004         unsigned int y;
1006         for (y = 0; y < height * 6 / 9; ++y) {
1007                 for (x = 0; x < width; ++x)
1008                         ((struct color_rgb24 *)mem)[x] =
1009                                 colors_top[x * 7 / width];
1010                 mem += stride;
1011         }
1013         for (; y < height * 7 / 9; ++y) {
1014                 for (x = 0; x < width; ++x)
1015                         ((struct color_rgb24 *)mem)[x] =
1016                                 colors_middle[x * 7 / width];
1017                 mem += stride;
1018         }
1020         for (; y < height; ++y) {
1021                 for (x = 0; x < width * 5 / 7; ++x)
1022                         ((struct color_rgb24 *)mem)[x] =
1023                                 colors_bottom[x * 4 / (width * 5 / 7)];
1024                 for (; x < width * 6 / 7; ++x)
1025                         ((struct color_rgb24 *)mem)[x] =
1026                                 colors_bottom[(x - width * 5 / 7) * 3
1027                                               / (width / 7) + 4];
1028                 for (; x < width; ++x)
1029                         ((struct color_rgb24 *)mem)[x] = colors_bottom[7];
1030                 mem += stride;
1031         }
1034 static void
1035 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1036                  unsigned int width, unsigned int height, unsigned int stride)
1038         const uint32_t colors_top[] = {
1039                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
1040                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
1041                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
1042                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
1043                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
1044                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
1045                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
1046         };
1047         const uint32_t colors_middle[] = {
1048                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
1049                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1050                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
1051                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1052                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
1053                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1054                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
1055         };
1056         const uint32_t colors_bottom[] = {
1057                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
1058                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
1059                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
1060                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1061                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
1062                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
1063                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
1064                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1065         };
1066         unsigned int x;
1067         unsigned int y;
1069         for (y = 0; y < height * 6 / 9; ++y) {
1070                 for (x = 0; x < width; ++x)
1071                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
1072                 mem += stride;
1073         }
1075         for (; y < height * 7 / 9; ++y) {
1076                 for (x = 0; x < width; ++x)
1077                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
1078                 mem += stride;
1079         }
1081         for (; y < height; ++y) {
1082                 for (x = 0; x < width * 5 / 7; ++x)
1083                         ((uint32_t *)mem)[x] =
1084                                 colors_bottom[x * 4 / (width * 5 / 7)];
1085                 for (; x < width * 6 / 7; ++x)
1086                         ((uint32_t *)mem)[x] =
1087                                 colors_bottom[(x - width * 5 / 7) * 3
1088                                               / (width / 7) + 4];
1089                 for (; x < width; ++x)
1090                         ((uint32_t *)mem)[x] = colors_bottom[7];
1091                 mem += stride;
1092         }
1095 static void
1096 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
1097            unsigned int height, unsigned int stride)
1099         unsigned char *u, *v;
1101         switch (info->format) {
1102         case DRM_FORMAT_UYVY:
1103         case DRM_FORMAT_VYUY:
1104         case DRM_FORMAT_YUYV:
1105         case DRM_FORMAT_YVYU:
1106                 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
1107                                              height, stride);
1109         case DRM_FORMAT_NV12:
1110         case DRM_FORMAT_NV21:
1111         case DRM_FORMAT_NV16:
1112         case DRM_FORMAT_NV61:
1113                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1114                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1115                 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
1116                                              width, height, stride);
1118         case DRM_FORMAT_YVU420:
1119                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
1120                                              planes[2], width, height, stride);
1122         case DRM_FORMAT_RGB565:
1123         case DRM_FORMAT_ARGB1555:
1124         case DRM_FORMAT_XRGB1555:
1125                 return fill_smpte_rgb16(&info->rgb, planes[0],
1126                                         width, height, stride);
1127         case DRM_FORMAT_BGR888:
1128         case DRM_FORMAT_RGB888:
1129                 return fill_smpte_rgb24(&info->rgb, planes[0],
1130                                         width, height, stride);
1131         case DRM_FORMAT_ARGB8888:
1132         case DRM_FORMAT_BGRA8888:
1133         case DRM_FORMAT_XRGB8888:
1134         case DRM_FORMAT_BGRX8888:
1135                 return fill_smpte_rgb32(&info->rgb, planes[0],
1136                                         width, height, stride);
1137         }
1140 /* swap these for big endian.. */
1141 #define RED   2
1142 #define GREEN 1
1143 #define BLUE  0
1145 static void
1146 make_pwetty(void *data, int width, int height, int stride)
1148 #ifdef HAVE_CAIRO
1149         cairo_surface_t *surface;
1150         cairo_t *cr;
1151         int x, y;
1153         surface = cairo_image_surface_create_for_data(data,
1154                                                       CAIRO_FORMAT_ARGB32,
1155                                                       width, height,
1156                                                       stride);
1157         cr = cairo_create(surface);
1158         cairo_surface_destroy(surface);
1160         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1161         for (x = 0; x < width; x += 250)
1162                 for (y = 0; y < height; y += 250) {
1163                         char buf[64];
1165                         cairo_move_to(cr, x, y - 20);
1166                         cairo_line_to(cr, x, y + 20);
1167                         cairo_move_to(cr, x - 20, y);
1168                         cairo_line_to(cr, x + 20, y);
1169                         cairo_new_sub_path(cr);
1170                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
1171                         cairo_set_line_width(cr, 4);
1172                         cairo_set_source_rgb(cr, 0, 0, 0);
1173                         cairo_stroke_preserve(cr);
1174                         cairo_set_source_rgb(cr, 1, 1, 1);
1175                         cairo_set_line_width(cr, 2);
1176                         cairo_stroke(cr);
1178                         snprintf(buf, sizeof buf, "%d, %d", x, y);
1179                         cairo_move_to(cr, x + 20, y + 20);
1180                         cairo_text_path(cr, buf);
1181                         cairo_set_source_rgb(cr, 0, 0, 0);
1182                         cairo_stroke_preserve(cr);
1183                         cairo_set_source_rgb(cr, 1, 1, 1);
1184                         cairo_fill(cr);
1185                 }
1187         cairo_destroy(cr);
1188 #endif
1191 static void
1192 fill_tiles_yuv_planar(const struct yuv_info *yuv,
1193                       unsigned char *y_mem, unsigned char *u_mem,
1194                       unsigned char *v_mem, unsigned int width,
1195                       unsigned int height, unsigned int stride)
1197         unsigned int cs = yuv->chroma_stride;
1198         unsigned int xsub = yuv->xsub;
1199         unsigned int ysub = yuv->ysub;
1200         unsigned int x;
1201         unsigned int y;
1203         for (y = 0; y < height; ++y) {
1204                 for (x = 0; x < width; ++x) {
1205                         div_t d = div(x+y, width);
1206                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1207                                        + 0x000a1120 * (d.rem >> 6);
1208                         struct color_yuv color =
1209                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1210                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1212                         y_mem[x] = color.y;
1213                         u_mem[x/xsub*cs] = color.u;
1214                         v_mem[x/xsub*cs] = color.v;
1215                 }
1217                 y_mem += stride;
1218                 if ((y + 1) % ysub == 0) {
1219                         u_mem += stride * cs / xsub;
1220                         v_mem += stride * cs / xsub;
1221                 }
1222         }
1225 static void
1226 fill_tiles_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
1227                       unsigned int width, unsigned int height,
1228                       unsigned int stride)
1230         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
1231         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
1232         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
1233         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
1234         unsigned int x;
1235         unsigned int y;
1237         for (y = 0; y < height; ++y) {
1238                 for (x = 0; x < width; x += 2) {
1239                         div_t d = div(x+y, width);
1240                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1241                                        + 0x000a1120 * (d.rem >> 6);
1242                         struct color_yuv color =
1243                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1244                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1246                         y_mem[2*x] = color.y;
1247                         c_mem[2*x+u] = color.u;
1248                         y_mem[2*x+2] = color.y;
1249                         c_mem[2*x+v] = color.v;
1250                 }
1252                 y_mem += stride;
1253                 c_mem += stride;
1254         }
1257 static void
1258 fill_tiles_rgb16(const struct rgb_info *rgb, unsigned char *mem,
1259                  unsigned int width, unsigned int height, unsigned int stride)
1261         unsigned int x, y;
1263         for (y = 0; y < height; ++y) {
1264                 for (x = 0; x < width; ++x) {
1265                         div_t d = div(x+y, width);
1266                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1267                                        + 0x000a1120 * (d.rem >> 6);
1268                         uint16_t color =
1269                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
1270                                           (rgb32 >> 8) & 0xff, rgb32 & 0xff,
1271                                           255);
1273                         ((uint16_t *)mem)[x] = color;
1274                 }
1275                 mem += stride;
1276         }
1279 static void
1280 fill_tiles_rgb24(const struct rgb_info *rgb, unsigned char *mem,
1281                  unsigned int width, unsigned int height, unsigned int stride)
1283         unsigned int x, y;
1285         for (y = 0; y < height; ++y) {
1286                 for (x = 0; x < width; ++x) {
1287                         div_t d = div(x+y, width);
1288                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1289                                        + 0x000a1120 * (d.rem >> 6);
1290                         struct color_rgb24 color =
1291                                 MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
1292                                            (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1294                         ((struct color_rgb24 *)mem)[x] = color;
1295                 }
1296                 mem += stride;
1297         }
1300 static void
1301 fill_tiles_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1302                  unsigned int width, unsigned int height, unsigned int stride)
1304         unsigned char *mem_base = mem;
1305         unsigned int x, y;
1307         for (y = 0; y < height; ++y) {
1308                 for (x = 0; x < width; ++x) {
1309                         div_t d = div(x+y, width);
1310                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1311                                        + 0x000a1120 * (d.rem >> 6);
1312                         uint32_t color =
1313                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
1314                                           (rgb32 >> 8) & 0xff, rgb32 & 0xff,
1315                                           255);
1317                         ((uint32_t *)mem)[x] = color;
1318                 }
1319                 mem += stride;
1320         }
1322         make_pwetty(mem_base, width, height, stride);
1325 static void
1326 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
1327            unsigned int height, unsigned int stride)
1329         unsigned char *u, *v;
1331         switch (info->format) {
1332         case DRM_FORMAT_UYVY:
1333         case DRM_FORMAT_VYUY:
1334         case DRM_FORMAT_YUYV:
1335         case DRM_FORMAT_YVYU:
1336                 return fill_tiles_yuv_packed(&info->yuv, planes[0],
1337                                              width, height, stride);
1339         case DRM_FORMAT_NV12:
1340         case DRM_FORMAT_NV21:
1341         case DRM_FORMAT_NV16:
1342         case DRM_FORMAT_NV61:
1343                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1344                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1345                 return fill_tiles_yuv_planar(&info->yuv, planes[0], u, v,
1346                                              width, height, stride);
1348         case DRM_FORMAT_YVU420:
1349                 return fill_tiles_yuv_planar(&info->yuv, planes[0], planes[1],
1350                                              planes[2], width, height, stride);
1352         case DRM_FORMAT_RGB565:
1353         case DRM_FORMAT_ARGB1555:
1354         case DRM_FORMAT_XRGB1555:
1355                 return fill_tiles_rgb16(&info->rgb, planes[0],
1356                                         width, height, stride);
1357         case DRM_FORMAT_BGR888:
1358         case DRM_FORMAT_RGB888:
1359                 return fill_tiles_rgb24(&info->rgb, planes[0],
1360                                         width, height, stride);
1361         case DRM_FORMAT_ARGB8888:
1362         case DRM_FORMAT_BGRA8888:
1363         case DRM_FORMAT_XRGB8888:
1364         case DRM_FORMAT_BGRX8888:
1365                 return fill_tiles_rgb32(&info->rgb, planes[0],
1366                                         width, height, stride);
1367         }
1370 static void
1371 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
1372            unsigned int height, unsigned int stride)
1374         memset(planes[0], 0x77, stride * height);
1377 /*
1378  * fill_pattern - Fill a buffer with a test pattern
1379  * @format: Pixel format
1380  * @pattern: Test pattern
1381  * @buffer: Buffer memory
1382  * @width: Width in pixels
1383  * @height: Height in pixels
1384  * @stride: Line stride (pitch) in bytes
1385  *
1386  * Fill the buffer with the test pattern specified by the pattern parameter.
1387  * Supported formats vary depending on the selected pattern.
1388  */
1389 static void
1390 fill_pattern(unsigned int format, enum fill_pattern pattern,
1391              void *planes[3],
1392              unsigned int width, unsigned int height, unsigned int stride)
1394         const struct format_info *info = NULL;
1395         unsigned int i;
1397         for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
1398                 if (format_info[i].format == format) {
1399                         info = &format_info[i];
1400                         break;
1401                 }
1402         }
1404         if (info == NULL)
1405                 return;
1407         switch (pattern) {
1408         case PATTERN_TILES:
1409                 return fill_tiles(info, planes, width, height, stride);
1411         case PATTERN_SMPTE:
1412                 return fill_smpte(info, planes, width, height, stride);
1414         case PATTERN_PLAIN:
1415                 return fill_plain(info, planes, width, height, stride);
1417         default:
1418                 printf("Error: unsupported test pattern %u.\n", pattern);
1419                 break;
1420         }
1423 /* -----------------------------------------------------------------------------
1424  * Buffers management
1425  */
1427 static struct kms_bo *
1428 allocate_buffer(struct kms_driver *kms,
1429                 int width, int height, int *stride)
1431         struct kms_bo *bo;
1432         unsigned bo_attribs[] = {
1433                 KMS_WIDTH,   0,
1434                 KMS_HEIGHT,  0,
1435                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
1436                 KMS_TERMINATE_PROP_LIST
1437         };
1438         int ret;
1440         bo_attribs[1] = width;
1441         bo_attribs[3] = height;
1443         ret = kms_bo_create(kms, bo_attribs, &bo);
1444         if (ret) {
1445                 fprintf(stderr, "failed to alloc buffer: %s\n",
1446                         strerror(-ret));
1447                 return NULL;
1448         }
1450         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
1451         if (ret) {
1452                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
1453                         strerror(-ret));
1454                 kms_bo_destroy(&bo);
1455                 return NULL;
1456         }
1458         return bo;
1461 static struct kms_bo *
1462 create_test_buffer(struct kms_driver *kms, unsigned int format,
1463                    int width, int height, int handles[4],
1464                    int pitches[4], int offsets[4], enum fill_pattern pattern)
1466         struct kms_bo *bo;
1467         int ret, stride;
1468         void *planes[3];
1469         void *virtual;
1471         bo = allocate_buffer(kms, width, height, &pitches[0]);
1472         if (!bo)
1473                 return NULL;
1475         ret = kms_bo_map(bo, &virtual);
1476         if (ret) {
1477                 fprintf(stderr, "failed to map buffer: %s\n",
1478                         strerror(-ret));
1479                 kms_bo_destroy(&bo);
1480                 return NULL;
1481         }
1483         /* just testing a limited # of formats to test single
1484          * and multi-planar path.. would be nice to add more..
1485          */
1486         switch (format) {
1487         case DRM_FORMAT_UYVY:
1488         case DRM_FORMAT_VYUY:
1489         case DRM_FORMAT_YUYV:
1490         case DRM_FORMAT_YVYU:
1491                 pitches[0] = width * 2;
1492                 offsets[0] = 0;
1493                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1495                 planes[0] = virtual;
1496                 break;
1498         case DRM_FORMAT_NV12:
1499         case DRM_FORMAT_NV21:
1500         case DRM_FORMAT_NV16:
1501         case DRM_FORMAT_NV61:
1502                 pitches[0] = width;
1503                 offsets[0] = 0;
1504                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1505                 pitches[1] = width;
1506                 offsets[1] = width * height;
1507                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1509                 planes[0] = virtual;
1510                 planes[1] = virtual + offsets[1];
1511                 break;
1513         case DRM_FORMAT_YVU420:
1514                 pitches[0] = width;
1515                 offsets[0] = 0;
1516                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1517                 pitches[1] = width / 2;
1518                 offsets[1] = width * height;
1519                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1520                 pitches[2] = width / 2;
1521                 offsets[2] = offsets[1] + (width * height) / 4;
1522                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
1524                 planes[0] = virtual;
1525                 planes[1] = virtual + offsets[1];
1526                 planes[2] = virtual + offsets[2];
1527                 break;
1529         case DRM_FORMAT_RGB565:
1530         case DRM_FORMAT_ARGB1555:
1531         case DRM_FORMAT_XRGB1555:
1532                 pitches[0] = width * 2;
1533                 offsets[0] = 0;
1534                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1536                 planes[0] = virtual;
1537                 break;
1539         case DRM_FORMAT_BGR888:
1540         case DRM_FORMAT_RGB888:
1541                 pitches[0] = width * 3;
1542                 offsets[0] = 0;
1543                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1545                 planes[0] = virtual;
1546                 break;
1548         case DRM_FORMAT_ARGB8888:
1549         case DRM_FORMAT_BGRA8888:
1550         case DRM_FORMAT_XRGB8888:
1551         case DRM_FORMAT_BGRX8888:
1552                 pitches[0] = width * 4;
1553                 offsets[0] = 0;
1554                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1556                 planes[0] = virtual;
1557                 break;
1558         }
1560         fill_pattern(format, pattern, planes, width, height, pitches[0]);
1561         kms_bo_unmap(bo);
1563         return bo;
1566 /* -------------------------------------------------------------------------- */
1568 void
1569 page_flip_handler(int fd, unsigned int frame,
1570                   unsigned int sec, unsigned int usec, void *data)
1572         struct connector *c;
1573         unsigned int new_fb_id;
1574         struct timeval end;
1575         double t;
1577         c = data;
1578         if (c->current_fb_id == c->fb_id[0])
1579                 new_fb_id = c->fb_id[1];
1580         else
1581                 new_fb_id = c->fb_id[0];
1583         drmModePageFlip(fd, c->crtc, new_fb_id,
1584                         DRM_MODE_PAGE_FLIP_EVENT, c);
1585         c->current_fb_id = new_fb_id;
1586         c->swap_count++;
1587         if (c->swap_count == 60) {
1588                 gettimeofday(&end, NULL);
1589                 t = end.tv_sec + end.tv_usec * 1e-6 -
1590                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
1591                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
1592                 c->swap_count = 0;
1593                 c->start = end;
1594         }
1597 static int
1598 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
1600         drmModePlaneRes *plane_resources;
1601         drmModePlane *ovr;
1602         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1603         uint32_t plane_id = 0;
1604         struct kms_bo *plane_bo;
1605         uint32_t plane_flags = 0, format;
1606         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
1607         unsigned int i;
1609         format = format_fourcc(p->format_str);
1610         if (format == 0) {
1611                 fprintf(stderr, "Unknown format: %s\n", p->format_str);
1612                 return -1;
1613         }
1615         /* find an unused plane which can be connected to our crtc */
1616         plane_resources = drmModeGetPlaneResources(fd);
1617         if (!plane_resources) {
1618                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
1619                         strerror(errno));
1620                 return -1;
1621         }
1623         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
1624                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
1625                 if (!ovr) {
1626                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
1627                                 strerror(errno));
1628                         return -1;
1629                 }
1631                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
1632                         plane_id = ovr->plane_id;
1634                 drmModeFreePlane(ovr);
1635         }
1637         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
1638                         p->w, p->h, p->format_str);
1640         if (!plane_id) {
1641                 fprintf(stderr, "failed to find plane!\n");
1642                 return -1;
1643         }
1645         plane_bo = create_test_buffer(kms, format, p->w, p->h, handles,
1646                                       pitches, offsets, PATTERN_TILES);
1647         if (plane_bo == NULL)
1648                 return -1;
1650         /* just use single plane format for now.. */
1651         if (drmModeAddFB2(fd, p->w, p->h, format,
1652                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
1653                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1654                 return -1;
1655         }
1657         /* ok, boring.. but for now put in middle of screen: */
1658         crtc_x = c->mode->hdisplay / 3;
1659         crtc_y = c->mode->vdisplay / 3;
1660         crtc_w = crtc_x;
1661         crtc_h = crtc_y;
1663         /* note src coords (last 4 args) are in Q16 format */
1664         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
1665                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1666                             0, 0, p->w << 16, p->h << 16)) {
1667                 fprintf(stderr, "failed to enable plane: %s\n",
1668                         strerror(errno));
1669                 return -1;
1670         }
1672         return 0;
1675 static void
1676 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
1677                 int page_flip)
1679         struct kms_driver *kms;
1680         struct kms_bo *bo, *other_bo;
1681         unsigned int fb_id, other_fb_id;
1682         int i, j, ret, width, height, x;
1683         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1684         drmEventContext evctx;
1686         width = 0;
1687         height = 0;
1688         for (i = 0; i < count; i++) {
1689                 connector_find_mode(&c[i]);
1690                 if (c[i].mode == NULL)
1691                         continue;
1692                 width += c[i].mode->hdisplay;
1693                 if (height < c[i].mode->vdisplay)
1694                         height = c[i].mode->vdisplay;
1695         }
1697         ret = kms_create(fd, &kms);
1698         if (ret) {
1699                 fprintf(stderr, "failed to create kms driver: %s\n",
1700                         strerror(-ret));
1701                 return;
1702         }
1704         bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1705                                 pitches, offsets, PATTERN_SMPTE);
1706         if (bo == NULL)
1707                 return;
1709         ret = drmModeAddFB(fd, width, height, 24, 32, pitches[0], handles[0], &fb_id);
1710         if (ret) {
1711                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1712                         width, height, strerror(errno));
1713                 return;
1714         }
1716         x = 0;
1717         for (i = 0; i < count; i++) {
1718                 if (c[i].mode == NULL)
1719                         continue;
1721                 printf("setting mode %s on connector %d, crtc %d\n",
1722                        c[i].mode_str, c[i].id, c[i].crtc);
1724                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1725                                      &c[i].id, 1, c[i].mode);
1727                 /* XXX: Actually check if this is needed */
1728                 drmModeDirtyFB(fd, fb_id, NULL, 0);
1730                 x += c[i].mode->hdisplay;
1732                 if (ret) {
1733                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1734                         return;
1735                 }
1737                 /* if we have a plane/overlay to show, set that up now: */
1738                 for (j = 0; j < plane_count; j++)
1739                         if (p[j].con_id == c[i].id)
1740                                 if (set_plane(kms, &c[i], &p[j]))
1741                                         return;
1742         }
1744         if (!page_flip)
1745                 return;
1746         
1747         other_bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1748                                       pitches, offsets, PATTERN_PLAIN);
1749         if (other_bo == NULL)
1750                 return;
1752         ret = drmModeAddFB(fd, width, height, 32, 32, pitches[0], handles[0],
1753                            &other_fb_id);
1754         if (ret) {
1755                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1756                 return;
1757         }
1759         for (i = 0; i < count; i++) {
1760                 if (c[i].mode == NULL)
1761                         continue;
1763                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1764                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1765                 if (ret) {
1766                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1767                         return;
1768                 }
1769                 gettimeofday(&c[i].start, NULL);
1770                 c[i].swap_count = 0;
1771                 c[i].fb_id[0] = fb_id;
1772                 c[i].fb_id[1] = other_fb_id;
1773                 c[i].current_fb_id = other_fb_id;
1774         }
1776         memset(&evctx, 0, sizeof evctx);
1777         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1778         evctx.vblank_handler = NULL;
1779         evctx.page_flip_handler = page_flip_handler;
1780         
1781         while (1) {
1782 #if 0
1783                 struct pollfd pfd[2];
1785                 pfd[0].fd = 0;
1786                 pfd[0].events = POLLIN;
1787                 pfd[1].fd = fd;
1788                 pfd[1].events = POLLIN;
1790                 if (poll(pfd, 2, -1) < 0) {
1791                         fprintf(stderr, "poll error\n");
1792                         break;
1793                 }
1795                 if (pfd[0].revents)
1796                         break;
1797 #else
1798                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1799                 fd_set fds;
1800                 int ret;
1802                 FD_ZERO(&fds);
1803                 FD_SET(0, &fds);
1804                 FD_SET(fd, &fds);
1805                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1807                 if (ret <= 0) {
1808                         fprintf(stderr, "select timed out or error (ret %d)\n",
1809                                 ret);
1810                         continue;
1811                 } else if (FD_ISSET(0, &fds)) {
1812                         break;
1813                 }
1814 #endif
1816                 drmHandleEvent(fd, &evctx);
1817         }
1819         kms_bo_destroy(&bo);
1820         kms_bo_destroy(&other_bo);
1821         kms_destroy(&kms);
1824 extern char *optarg;
1825 extern int optind, opterr, optopt;
1826 static char optstr[] = "ecpmfs:P:v";
1828 void usage(char *name)
1830         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1831         fprintf(stderr, "\t-e\tlist encoders\n");
1832         fprintf(stderr, "\t-c\tlist connectors\n");
1833         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1834         fprintf(stderr, "\t-m\tlist modes\n");
1835         fprintf(stderr, "\t-f\tlist framebuffers\n");
1836         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1837         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1838         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1839         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1840         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1841         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1842         exit(0);
1845 #define dump_resource(res) if (res) dump_##res()
1847 static int page_flipping_supported(void)
1849         /*FIXME: generic ioctl needed? */
1850         return 1;
1851 #if 0
1852         int ret, value;
1853         struct drm_i915_getparam gp;
1855         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1856         gp.value = &value;
1858         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1859         if (ret) {
1860                 fprintf(stderr, "drm_i915_getparam: %m\n");
1861                 return 0;
1862         }
1864         return *gp.value;
1865 #endif
1868 int main(int argc, char **argv)
1870         int c;
1871         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1872         int test_vsync = 0;
1873         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1874         unsigned int i;
1875         int count = 0, plane_count = 0;
1876         struct connector con_args[2];
1877         struct plane plane_args[2] = {0};
1878         
1879         opterr = 0;
1880         while ((c = getopt(argc, argv, optstr)) != -1) {
1881                 switch (c) {
1882                 case 'e':
1883                         encoders = 1;
1884                         break;
1885                 case 'c':
1886                         connectors = 1;
1887                         break;
1888                 case 'p':
1889                         crtcs = 1;
1890                         planes = 1;
1891                         break;
1892                 case 'm':
1893                         modes = 1;
1894                         break;
1895                 case 'f':
1896                         framebuffers = 1;
1897                         break;
1898                 case 'v':
1899                         test_vsync = 1;
1900                         break;
1901                 case 's':
1902                         con_args[count].crtc = -1;
1903                         if (sscanf(optarg, "%d:%64s",
1904                                    &con_args[count].id,
1905                                    con_args[count].mode_str) != 2 &&
1906                             sscanf(optarg, "%d@%d:%64s",
1907                                    &con_args[count].id,
1908                                    &con_args[count].crtc,
1909                                    con_args[count].mode_str) != 3)
1910                                 usage(argv[0]);
1911                         count++;                                      
1912                         break;
1913                 case 'P':
1914                         strcpy(plane_args[plane_count].format_str, "XR24");
1915                         if (sscanf(optarg, "%d:%dx%d@%4s",
1916                                         &plane_args[plane_count].con_id,
1917                                         &plane_args[plane_count].w,
1918                                         &plane_args[plane_count].h,
1919                                         plane_args[plane_count].format_str) != 4 &&
1920                                 sscanf(optarg, "%d:%dx%d",
1921                                         &plane_args[plane_count].con_id,
1922                                         &plane_args[plane_count].w,
1923                                         &plane_args[plane_count].h) != 3)
1924                                 usage(argv[0]);
1925                         plane_count++;
1926                         break;
1927                 default:
1928                         usage(argv[0]);
1929                         break;
1930                 }
1931         }
1933         if (argc == 1)
1934                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1936         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1937                 printf("trying to load module %s...", modules[i]);
1938                 fd = drmOpen(modules[i], NULL);
1939                 if (fd < 0) {
1940                         printf("failed.\n");
1941                 } else {
1942                         printf("success.\n");
1943                         break;
1944                 }
1945         }
1947         if (test_vsync && !page_flipping_supported()) {
1948                 fprintf(stderr, "page flipping not supported by drm.\n");
1949                 return -1;
1950         }
1952         if (i == ARRAY_SIZE(modules)) {
1953                 fprintf(stderr, "failed to load any modules, aborting.\n");
1954                 return -1;
1955         }
1957         resources = drmModeGetResources(fd);
1958         if (!resources) {
1959                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1960                         strerror(errno));
1961                 drmClose(fd);
1962                 return 1;
1963         }
1965         dump_resource(encoders);
1966         dump_resource(connectors);
1967         dump_resource(crtcs);
1968         dump_resource(planes);
1969         dump_resource(framebuffers);
1971         if (count > 0) {
1972                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1973                 getchar();
1974         }
1976         drmModeFreeResources(resources);
1978         return 0;