]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - tests/modetest/modetest.c
modetest: Move connector and plane parsing to separate functions
[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         unsigned int fourcc;
507 };
509 static void
510 connector_find_mode(struct connector *c)
512         drmModeConnector *connector;
513         int i, j;
515         /* First, find the connector & mode */
516         c->mode = NULL;
517         for (i = 0; i < resources->count_connectors; i++) {
518                 connector = drmModeGetConnector(fd, resources->connectors[i]);
520                 if (!connector) {
521                         fprintf(stderr, "could not get connector %i: %s\n",
522                                 resources->connectors[i], strerror(errno));
523                         drmModeFreeConnector(connector);
524                         continue;
525                 }
527                 if (!connector->count_modes) {
528                         drmModeFreeConnector(connector);
529                         continue;
530                 }
532                 if (connector->connector_id != c->id) {
533                         drmModeFreeConnector(connector);
534                         continue;
535                 }
537                 for (j = 0; j < connector->count_modes; j++) {
538                         c->mode = &connector->modes[j];
539                         if (!strcmp(c->mode->name, c->mode_str))
540                                 break;
541                 }
543                 /* Found it, break out */
544                 if (c->mode)
545                         break;
547                 drmModeFreeConnector(connector);
548         }
550         if (!c->mode) {
551                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
552                 return;
553         }
555         /* Now get the encoder */
556         for (i = 0; i < resources->count_encoders; i++) {
557                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
559                 if (!c->encoder) {
560                         fprintf(stderr, "could not get encoder %i: %s\n",
561                                 resources->encoders[i], strerror(errno));
562                         drmModeFreeEncoder(c->encoder);
563                         continue;
564                 }
566                 if (c->encoder->encoder_id  == connector->encoder_id)
567                         break;
569                 drmModeFreeEncoder(c->encoder);
570         }
572         if (c->crtc == -1)
573                 c->crtc = c->encoder->crtc_id;
575         /* and figure out which crtc index it is: */
576         for (i = 0; i < resources->count_crtcs; i++) {
577                 if (c->crtc == resources->crtcs[i]) {
578                         c->pipe = i;
579                         break;
580                 }
581         }
585 /* -----------------------------------------------------------------------------
586  * Formats
587  */
589 struct color_component {
590         unsigned int length;
591         unsigned int offset;
592 };
594 struct rgb_info {
595         struct color_component red;
596         struct color_component green;
597         struct color_component blue;
598         struct color_component alpha;
599 };
601 enum yuv_order {
602         YUV_YCbCr = 1,
603         YUV_YCrCb = 2,
604         YUV_YC = 4,
605         YUV_CY = 8,
606 };
608 struct yuv_info {
609         enum yuv_order order;
610         unsigned int xsub;
611         unsigned int ysub;
612         unsigned int chroma_stride;
613 };
615 struct format_info {
616         unsigned int format;
617         const char *name;
618         const struct rgb_info rgb;
619         const struct yuv_info yuv;
620 };
622 #define MAKE_RGB_INFO(rl, ro, bl, bo, gl, go, al, ao) \
623         .rgb = { { (rl), (ro) }, { (bl), (bo) }, { (gl), (go) }, { (al), (ao) } }
625 #define MAKE_YUV_INFO(order, xsub, ysub, chroma_stride) \
626         .yuv = { (order), (xsub), (ysub), (chroma_stride) }
628 static const struct format_info format_info[] = {
629         /* YUV packed */
630         { DRM_FORMAT_UYVY, "UYVY", MAKE_YUV_INFO(YUV_YCbCr | YUV_CY, 2, 2, 2) },
631         { DRM_FORMAT_VYUY, "VYUY", MAKE_YUV_INFO(YUV_YCrCb | YUV_CY, 2, 2, 2) },
632         { DRM_FORMAT_YUYV, "YUYV", MAKE_YUV_INFO(YUV_YCbCr | YUV_YC, 2, 2, 2) },
633         { DRM_FORMAT_YVYU, "YVYU", MAKE_YUV_INFO(YUV_YCrCb | YUV_YC, 2, 2, 2) },
634         /* YUV semi-planar */
635         { DRM_FORMAT_NV12, "NV12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 2) },
636         { DRM_FORMAT_NV21, "NV21", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 2) },
637         { DRM_FORMAT_NV16, "NV16", MAKE_YUV_INFO(YUV_YCbCr, 2, 1, 2) },
638         { DRM_FORMAT_NV61, "NV61", MAKE_YUV_INFO(YUV_YCrCb, 2, 1, 2) },
639         /* YUV planar */
640         { DRM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
641         /* RGB16 */
642         { DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
643         { DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
644         { DRM_FORMAT_RGB565, "RG16", MAKE_RGB_INFO(5, 11, 6, 5, 5, 0, 0, 0) },
645         /* RGB24 */
646         { DRM_FORMAT_BGR888, "BG24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
647         { DRM_FORMAT_RGB888, "RG24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
648         /* RGB32 */
649         { DRM_FORMAT_ARGB8888, "AR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 8, 24) },
650         { DRM_FORMAT_BGRA8888, "BA24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 8, 0) },
651         { DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
652         { DRM_FORMAT_BGRX8888, "BX24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 0, 0) },
653 };
655 unsigned int format_fourcc(const char *name)
657         unsigned int i;
658         for (i = 0; i < ARRAY_SIZE(format_info); i++) {
659                 if (!strcmp(format_info[i].name, name))
660                         return format_info[i].format;
661         }
662         return 0;
665 /* -----------------------------------------------------------------------------
666  * Test patterns
667  */
669 enum fill_pattern {
670         PATTERN_TILES = 0,
671         PATTERN_PLAIN = 1,
672         PATTERN_SMPTE = 2,
673 };
675 struct color_rgb24 {
676         unsigned int value:24;
677 } __attribute__((__packed__));
679 struct color_yuv {
680         unsigned char y;
681         unsigned char u;
682         unsigned char v;
683 };
685 #define MAKE_YUV_601_Y(r, g, b) \
686         ((( 66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) + 16)
687 #define MAKE_YUV_601_U(r, g, b) \
688         (((-38 * (r) -  74 * (g) + 112 * (b) + 128) >> 8) + 128)
689 #define MAKE_YUV_601_V(r, g, b) \
690         (((112 * (r) -  94 * (g) -  18 * (b) + 128) >> 8) + 128)
692 #define MAKE_YUV_601(r, g, b) \
693         { .y = MAKE_YUV_601_Y(r, g, b), \
694           .u = MAKE_YUV_601_U(r, g, b), \
695           .v = MAKE_YUV_601_V(r, g, b) }
697 #define MAKE_RGBA(rgb, r, g, b, a) \
698         ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
699          (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
700          (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
701          (((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
703 #define MAKE_RGB24(rgb, r, g, b) \
704         { .value = MAKE_RGBA(rgb, r, g, b, 0) }
706 static void
707 fill_smpte_yuv_planar(const struct yuv_info *yuv,
708                       unsigned char *y_mem, unsigned char *u_mem,
709                       unsigned char *v_mem, unsigned int width,
710                       unsigned int height, unsigned int stride)
712         const struct color_yuv colors_top[] = {
713                 MAKE_YUV_601(191, 192, 192),    /* grey */
714                 MAKE_YUV_601(192, 192, 0),      /* yellow */
715                 MAKE_YUV_601(0, 192, 192),      /* cyan */
716                 MAKE_YUV_601(0, 192, 0),        /* green */
717                 MAKE_YUV_601(192, 0, 192),      /* magenta */
718                 MAKE_YUV_601(192, 0, 0),        /* red */
719                 MAKE_YUV_601(0, 0, 192),        /* blue */
720         };
721         const struct color_yuv colors_middle[] = {
722                 MAKE_YUV_601(0, 0, 192),        /* blue */
723                 MAKE_YUV_601(19, 19, 19),       /* black */
724                 MAKE_YUV_601(192, 0, 192),      /* magenta */
725                 MAKE_YUV_601(19, 19, 19),       /* black */
726                 MAKE_YUV_601(0, 192, 192),      /* cyan */
727                 MAKE_YUV_601(19, 19, 19),       /* black */
728                 MAKE_YUV_601(192, 192, 192),    /* grey */
729         };
730         const struct color_yuv colors_bottom[] = {
731                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
732                 MAKE_YUV_601(255, 255, 255),    /* super white */
733                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
734                 MAKE_YUV_601(19, 19, 19),       /* black */
735                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
736                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
737                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
738                 MAKE_YUV_601(19, 19, 19),       /* black */
739         };
740         unsigned int cs = yuv->chroma_stride;
741         unsigned int xsub = yuv->xsub;
742         unsigned int ysub = yuv->ysub;
743         unsigned int x;
744         unsigned int y;
746         /* Luma */
747         for (y = 0; y < height * 6 / 9; ++y) {
748                 for (x = 0; x < width; ++x)
749                         y_mem[x] = colors_top[x * 7 / width].y;
750                 y_mem += stride;
751         }
753         for (; y < height * 7 / 9; ++y) {
754                 for (x = 0; x < width; ++x)
755                         y_mem[x] = colors_middle[x * 7 / width].y;
756                 y_mem += stride;
757         }
759         for (; y < height; ++y) {
760                 for (x = 0; x < width * 5 / 7; ++x)
761                         y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
762                 for (; x < width * 6 / 7; ++x)
763                         y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
764                                                  / (width / 7) + 4].y;
765                 for (; x < width; ++x)
766                         y_mem[x] = colors_bottom[7].y;
767                 y_mem += stride;
768         }
770         /* Chroma */
771         for (y = 0; y < height / ysub * 6 / 9; ++y) {
772                 for (x = 0; x < width; x += xsub) {
773                         u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
774                         v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
775                 }
776                 u_mem += stride * cs / xsub;
777                 v_mem += stride * cs / xsub;
778         }
780         for (; y < height / ysub * 7 / 9; ++y) {
781                 for (x = 0; x < width; x += xsub) {
782                         u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
783                         v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
784                 }
785                 u_mem += stride * cs / xsub;
786                 v_mem += stride * cs / xsub;
787         }
789         for (; y < height / ysub; ++y) {
790                 for (x = 0; x < width * 5 / 7; x += xsub) {
791                         u_mem[x*cs/xsub] =
792                                 colors_bottom[x * 4 / (width * 5 / 7)].u;
793                         v_mem[x*cs/xsub] =
794                                 colors_bottom[x * 4 / (width * 5 / 7)].v;
795                 }
796                 for (; x < width * 6 / 7; x += xsub) {
797                         u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
798                                                          3 / (width / 7) + 4].u;
799                         v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
800                                                          3 / (width / 7) + 4].v;
801                 }
802                 for (; x < width; x += xsub) {
803                         u_mem[x*cs/xsub] = colors_bottom[7].u;
804                         v_mem[x*cs/xsub] = colors_bottom[7].v;
805                 }
806                 u_mem += stride * cs / xsub;
807                 v_mem += stride * cs / xsub;
808         }
811 static void
812 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
813                       unsigned int width, unsigned int height,
814                       unsigned int stride)
816         const struct color_yuv colors_top[] = {
817                 MAKE_YUV_601(191, 192, 192),    /* grey */
818                 MAKE_YUV_601(192, 192, 0),      /* yellow */
819                 MAKE_YUV_601(0, 192, 192),      /* cyan */
820                 MAKE_YUV_601(0, 192, 0),        /* green */
821                 MAKE_YUV_601(192, 0, 192),      /* magenta */
822                 MAKE_YUV_601(192, 0, 0),        /* red */
823                 MAKE_YUV_601(0, 0, 192),        /* blue */
824         };
825         const struct color_yuv colors_middle[] = {
826                 MAKE_YUV_601(0, 0, 192),        /* blue */
827                 MAKE_YUV_601(19, 19, 19),       /* black */
828                 MAKE_YUV_601(192, 0, 192),      /* magenta */
829                 MAKE_YUV_601(19, 19, 19),       /* black */
830                 MAKE_YUV_601(0, 192, 192),      /* cyan */
831                 MAKE_YUV_601(19, 19, 19),       /* black */
832                 MAKE_YUV_601(192, 192, 192),    /* grey */
833         };
834         const struct color_yuv colors_bottom[] = {
835                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
836                 MAKE_YUV_601(255, 255, 255),    /* super white */
837                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
838                 MAKE_YUV_601(19, 19, 19),       /* black */
839                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
840                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
841                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
842                 MAKE_YUV_601(19, 19, 19),       /* black */
843         };
844         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
845         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
846         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
847         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
848         unsigned int x;
849         unsigned int y;
851         /* Luma */
852         for (y = 0; y < height * 6 / 9; ++y) {
853                 for (x = 0; x < width; ++x)
854                         y_mem[2*x] = colors_top[x * 7 / width].y;
855                 y_mem += stride * 2;
856         }
858         for (; y < height * 7 / 9; ++y) {
859                 for (x = 0; x < width; ++x)
860                         y_mem[2*x] = colors_middle[x * 7 / width].y;
861                 y_mem += stride * 2;
862         }
864         for (; y < height; ++y) {
865                 for (x = 0; x < width * 5 / 7; ++x)
866                         y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
867                 for (; x < width * 6 / 7; ++x)
868                         y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
869                                                    / (width / 7) + 4].y;
870                 for (; x < width; ++x)
871                         y_mem[2*x] = colors_bottom[7].y;
872                 y_mem += stride * 2;
873         }
875         /* Chroma */
876         for (y = 0; y < height * 6 / 9; ++y) {
877                 for (x = 0; x < width; x += 2) {
878                         c_mem[2*x+u] = colors_top[x * 7 / width].u;
879                         c_mem[2*x+v] = colors_top[x * 7 / width].v;
880                 }
881                 c_mem += stride * 2;
882         }
884         for (; y < height * 7 / 9; ++y) {
885                 for (x = 0; x < width; x += 2) {
886                         c_mem[2*x+u] = colors_middle[x * 7 / width].u;
887                         c_mem[2*x+v] = colors_middle[x * 7 / width].v;
888                 }
889                 c_mem += stride * 2;
890         }
892         for (; y < height; ++y) {
893                 for (x = 0; x < width * 5 / 7; x += 2) {
894                         c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
895                         c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
896                 }
897                 for (; x < width * 6 / 7; x += 2) {
898                         c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
899                                                      3 / (width / 7) + 4].u;
900                         c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
901                                                      3 / (width / 7) + 4].v;
902                 }
903                 for (; x < width; x += 2) {
904                         c_mem[2*x+u] = colors_bottom[7].u;
905                         c_mem[2*x+v] = colors_bottom[7].v;
906                 }
907                 c_mem += stride * 2;
908         }
911 static void
912 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
913                  unsigned int width, unsigned int height, unsigned int stride)
915         const uint16_t colors_top[] = {
916                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
917                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
918                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
919                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
920                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
921                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
922                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
923         };
924         const uint16_t colors_middle[] = {
925                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
926                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
927                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
928                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
929                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
930                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
931                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
932         };
933         const uint16_t colors_bottom[] = {
934                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
935                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
936                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
937                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
938                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
939                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
940                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
941                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
942         };
943         unsigned int x;
944         unsigned int y;
946         for (y = 0; y < height * 6 / 9; ++y) {
947                 for (x = 0; x < width; ++x)
948                         ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
949                 mem += stride;
950         }
952         for (; y < height * 7 / 9; ++y) {
953                 for (x = 0; x < width; ++x)
954                         ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
955                 mem += stride;
956         }
958         for (; y < height; ++y) {
959                 for (x = 0; x < width * 5 / 7; ++x)
960                         ((uint16_t *)mem)[x] =
961                                 colors_bottom[x * 4 / (width * 5 / 7)];
962                 for (; x < width * 6 / 7; ++x)
963                         ((uint16_t *)mem)[x] =
964                                 colors_bottom[(x - width * 5 / 7) * 3
965                                               / (width / 7) + 4];
966                 for (; x < width; ++x)
967                         ((uint16_t *)mem)[x] = colors_bottom[7];
968                 mem += stride;
969         }
972 static void
973 fill_smpte_rgb24(const struct rgb_info *rgb, void *mem,
974                  unsigned int width, unsigned int height, unsigned int stride)
976         const struct color_rgb24 colors_top[] = {
977                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
978                 MAKE_RGB24(rgb, 192, 192, 0),   /* yellow */
979                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
980                 MAKE_RGB24(rgb, 0, 192, 0),     /* green */
981                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
982                 MAKE_RGB24(rgb, 192, 0, 0),     /* red */
983                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
984         };
985         const struct color_rgb24 colors_middle[] = {
986                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
987                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
988                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
989                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
990                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
991                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
992                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
993         };
994         const struct color_rgb24 colors_bottom[] = {
995                 MAKE_RGB24(rgb, 0, 33, 76),     /* in-phase */
996                 MAKE_RGB24(rgb, 255, 255, 255), /* super white */
997                 MAKE_RGB24(rgb, 50, 0, 106),    /* quadrature */
998                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
999                 MAKE_RGB24(rgb, 9, 9, 9),       /* 3.5% */
1000                 MAKE_RGB24(rgb, 19, 19, 19),    /* 7.5% */
1001                 MAKE_RGB24(rgb, 29, 29, 29),    /* 11.5% */
1002                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
1003         };
1004         unsigned int x;
1005         unsigned int y;
1007         for (y = 0; y < height * 6 / 9; ++y) {
1008                 for (x = 0; x < width; ++x)
1009                         ((struct color_rgb24 *)mem)[x] =
1010                                 colors_top[x * 7 / width];
1011                 mem += stride;
1012         }
1014         for (; y < height * 7 / 9; ++y) {
1015                 for (x = 0; x < width; ++x)
1016                         ((struct color_rgb24 *)mem)[x] =
1017                                 colors_middle[x * 7 / width];
1018                 mem += stride;
1019         }
1021         for (; y < height; ++y) {
1022                 for (x = 0; x < width * 5 / 7; ++x)
1023                         ((struct color_rgb24 *)mem)[x] =
1024                                 colors_bottom[x * 4 / (width * 5 / 7)];
1025                 for (; x < width * 6 / 7; ++x)
1026                         ((struct color_rgb24 *)mem)[x] =
1027                                 colors_bottom[(x - width * 5 / 7) * 3
1028                                               / (width / 7) + 4];
1029                 for (; x < width; ++x)
1030                         ((struct color_rgb24 *)mem)[x] = colors_bottom[7];
1031                 mem += stride;
1032         }
1035 static void
1036 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1037                  unsigned int width, unsigned int height, unsigned int stride)
1039         const uint32_t colors_top[] = {
1040                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
1041                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
1042                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
1043                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
1044                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
1045                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
1046                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
1047         };
1048         const uint32_t colors_middle[] = {
1049                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
1050                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1051                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
1052                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1053                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
1054                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1055                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
1056         };
1057         const uint32_t colors_bottom[] = {
1058                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
1059                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
1060                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
1061                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1062                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
1063                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
1064                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
1065                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
1066         };
1067         unsigned int x;
1068         unsigned int y;
1070         for (y = 0; y < height * 6 / 9; ++y) {
1071                 for (x = 0; x < width; ++x)
1072                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
1073                 mem += stride;
1074         }
1076         for (; y < height * 7 / 9; ++y) {
1077                 for (x = 0; x < width; ++x)
1078                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
1079                 mem += stride;
1080         }
1082         for (; y < height; ++y) {
1083                 for (x = 0; x < width * 5 / 7; ++x)
1084                         ((uint32_t *)mem)[x] =
1085                                 colors_bottom[x * 4 / (width * 5 / 7)];
1086                 for (; x < width * 6 / 7; ++x)
1087                         ((uint32_t *)mem)[x] =
1088                                 colors_bottom[(x - width * 5 / 7) * 3
1089                                               / (width / 7) + 4];
1090                 for (; x < width; ++x)
1091                         ((uint32_t *)mem)[x] = colors_bottom[7];
1092                 mem += stride;
1093         }
1096 static void
1097 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
1098            unsigned int height, unsigned int stride)
1100         unsigned char *u, *v;
1102         switch (info->format) {
1103         case DRM_FORMAT_UYVY:
1104         case DRM_FORMAT_VYUY:
1105         case DRM_FORMAT_YUYV:
1106         case DRM_FORMAT_YVYU:
1107                 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
1108                                              height, stride);
1110         case DRM_FORMAT_NV12:
1111         case DRM_FORMAT_NV21:
1112         case DRM_FORMAT_NV16:
1113         case DRM_FORMAT_NV61:
1114                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1115                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1116                 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
1117                                              width, height, stride);
1119         case DRM_FORMAT_YVU420:
1120                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
1121                                              planes[2], width, height, stride);
1123         case DRM_FORMAT_RGB565:
1124         case DRM_FORMAT_ARGB1555:
1125         case DRM_FORMAT_XRGB1555:
1126                 return fill_smpte_rgb16(&info->rgb, planes[0],
1127                                         width, height, stride);
1128         case DRM_FORMAT_BGR888:
1129         case DRM_FORMAT_RGB888:
1130                 return fill_smpte_rgb24(&info->rgb, planes[0],
1131                                         width, height, stride);
1132         case DRM_FORMAT_ARGB8888:
1133         case DRM_FORMAT_BGRA8888:
1134         case DRM_FORMAT_XRGB8888:
1135         case DRM_FORMAT_BGRX8888:
1136                 return fill_smpte_rgb32(&info->rgb, planes[0],
1137                                         width, height, stride);
1138         }
1141 /* swap these for big endian.. */
1142 #define RED   2
1143 #define GREEN 1
1144 #define BLUE  0
1146 static void
1147 make_pwetty(void *data, int width, int height, int stride)
1149 #ifdef HAVE_CAIRO
1150         cairo_surface_t *surface;
1151         cairo_t *cr;
1152         int x, y;
1154         surface = cairo_image_surface_create_for_data(data,
1155                                                       CAIRO_FORMAT_ARGB32,
1156                                                       width, height,
1157                                                       stride);
1158         cr = cairo_create(surface);
1159         cairo_surface_destroy(surface);
1161         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1162         for (x = 0; x < width; x += 250)
1163                 for (y = 0; y < height; y += 250) {
1164                         char buf[64];
1166                         cairo_move_to(cr, x, y - 20);
1167                         cairo_line_to(cr, x, y + 20);
1168                         cairo_move_to(cr, x - 20, y);
1169                         cairo_line_to(cr, x + 20, y);
1170                         cairo_new_sub_path(cr);
1171                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
1172                         cairo_set_line_width(cr, 4);
1173                         cairo_set_source_rgb(cr, 0, 0, 0);
1174                         cairo_stroke_preserve(cr);
1175                         cairo_set_source_rgb(cr, 1, 1, 1);
1176                         cairo_set_line_width(cr, 2);
1177                         cairo_stroke(cr);
1179                         snprintf(buf, sizeof buf, "%d, %d", x, y);
1180                         cairo_move_to(cr, x + 20, y + 20);
1181                         cairo_text_path(cr, buf);
1182                         cairo_set_source_rgb(cr, 0, 0, 0);
1183                         cairo_stroke_preserve(cr);
1184                         cairo_set_source_rgb(cr, 1, 1, 1);
1185                         cairo_fill(cr);
1186                 }
1188         cairo_destroy(cr);
1189 #endif
1192 static void
1193 fill_tiles_yuv_planar(const struct yuv_info *yuv,
1194                       unsigned char *y_mem, unsigned char *u_mem,
1195                       unsigned char *v_mem, unsigned int width,
1196                       unsigned int height, unsigned int stride)
1198         unsigned int cs = yuv->chroma_stride;
1199         unsigned int xsub = yuv->xsub;
1200         unsigned int ysub = yuv->ysub;
1201         unsigned int x;
1202         unsigned int y;
1204         for (y = 0; y < height; ++y) {
1205                 for (x = 0; x < width; ++x) {
1206                         div_t d = div(x+y, width);
1207                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1208                                        + 0x000a1120 * (d.rem >> 6);
1209                         struct color_yuv color =
1210                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1211                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1213                         y_mem[x] = color.y;
1214                         u_mem[x/xsub*cs] = color.u;
1215                         v_mem[x/xsub*cs] = color.v;
1216                 }
1218                 y_mem += stride;
1219                 if ((y + 1) % ysub == 0) {
1220                         u_mem += stride * cs / xsub;
1221                         v_mem += stride * cs / xsub;
1222                 }
1223         }
1226 static void
1227 fill_tiles_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
1228                       unsigned int width, unsigned int height,
1229                       unsigned int stride)
1231         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
1232         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
1233         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
1234         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
1235         unsigned int x;
1236         unsigned int y;
1238         for (y = 0; y < height; ++y) {
1239                 for (x = 0; x < width; x += 2) {
1240                         div_t d = div(x+y, width);
1241                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1242                                        + 0x000a1120 * (d.rem >> 6);
1243                         struct color_yuv color =
1244                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1245                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1247                         y_mem[2*x] = color.y;
1248                         c_mem[2*x+u] = color.u;
1249                         y_mem[2*x+2] = color.y;
1250                         c_mem[2*x+v] = color.v;
1251                 }
1253                 y_mem += stride;
1254                 c_mem += stride;
1255         }
1258 static void
1259 fill_tiles_rgb16(const struct rgb_info *rgb, unsigned char *mem,
1260                  unsigned int width, unsigned int height, unsigned int stride)
1262         unsigned int x, y;
1264         for (y = 0; y < height; ++y) {
1265                 for (x = 0; x < width; ++x) {
1266                         div_t d = div(x+y, width);
1267                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1268                                        + 0x000a1120 * (d.rem >> 6);
1269                         uint16_t color =
1270                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
1271                                           (rgb32 >> 8) & 0xff, rgb32 & 0xff,
1272                                           255);
1274                         ((uint16_t *)mem)[x] = color;
1275                 }
1276                 mem += stride;
1277         }
1280 static void
1281 fill_tiles_rgb24(const struct rgb_info *rgb, unsigned char *mem,
1282                  unsigned int width, unsigned int height, unsigned int stride)
1284         unsigned int x, y;
1286         for (y = 0; y < height; ++y) {
1287                 for (x = 0; x < width; ++x) {
1288                         div_t d = div(x+y, width);
1289                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1290                                        + 0x000a1120 * (d.rem >> 6);
1291                         struct color_rgb24 color =
1292                                 MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
1293                                            (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1295                         ((struct color_rgb24 *)mem)[x] = color;
1296                 }
1297                 mem += stride;
1298         }
1301 static void
1302 fill_tiles_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1303                  unsigned int width, unsigned int height, unsigned int stride)
1305         unsigned char *mem_base = mem;
1306         unsigned int x, y;
1308         for (y = 0; y < height; ++y) {
1309                 for (x = 0; x < width; ++x) {
1310                         div_t d = div(x+y, width);
1311                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1312                                        + 0x000a1120 * (d.rem >> 6);
1313                         uint32_t color =
1314                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
1315                                           (rgb32 >> 8) & 0xff, rgb32 & 0xff,
1316                                           255);
1318                         ((uint32_t *)mem)[x] = color;
1319                 }
1320                 mem += stride;
1321         }
1323         make_pwetty(mem_base, width, height, stride);
1326 static void
1327 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
1328            unsigned int height, unsigned int stride)
1330         unsigned char *u, *v;
1332         switch (info->format) {
1333         case DRM_FORMAT_UYVY:
1334         case DRM_FORMAT_VYUY:
1335         case DRM_FORMAT_YUYV:
1336         case DRM_FORMAT_YVYU:
1337                 return fill_tiles_yuv_packed(&info->yuv, planes[0],
1338                                              width, height, stride);
1340         case DRM_FORMAT_NV12:
1341         case DRM_FORMAT_NV21:
1342         case DRM_FORMAT_NV16:
1343         case DRM_FORMAT_NV61:
1344                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1345                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1346                 return fill_tiles_yuv_planar(&info->yuv, planes[0], u, v,
1347                                              width, height, stride);
1349         case DRM_FORMAT_YVU420:
1350                 return fill_tiles_yuv_planar(&info->yuv, planes[0], planes[1],
1351                                              planes[2], width, height, stride);
1353         case DRM_FORMAT_RGB565:
1354         case DRM_FORMAT_ARGB1555:
1355         case DRM_FORMAT_XRGB1555:
1356                 return fill_tiles_rgb16(&info->rgb, planes[0],
1357                                         width, height, stride);
1358         case DRM_FORMAT_BGR888:
1359         case DRM_FORMAT_RGB888:
1360                 return fill_tiles_rgb24(&info->rgb, planes[0],
1361                                         width, height, stride);
1362         case DRM_FORMAT_ARGB8888:
1363         case DRM_FORMAT_BGRA8888:
1364         case DRM_FORMAT_XRGB8888:
1365         case DRM_FORMAT_BGRX8888:
1366                 return fill_tiles_rgb32(&info->rgb, planes[0],
1367                                         width, height, stride);
1368         }
1371 static void
1372 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
1373            unsigned int height, unsigned int stride)
1375         memset(planes[0], 0x77, stride * height);
1378 /*
1379  * fill_pattern - Fill a buffer with a test pattern
1380  * @format: Pixel format
1381  * @pattern: Test pattern
1382  * @buffer: Buffer memory
1383  * @width: Width in pixels
1384  * @height: Height in pixels
1385  * @stride: Line stride (pitch) in bytes
1386  *
1387  * Fill the buffer with the test pattern specified by the pattern parameter.
1388  * Supported formats vary depending on the selected pattern.
1389  */
1390 static void
1391 fill_pattern(unsigned int format, enum fill_pattern pattern,
1392              void *planes[3],
1393              unsigned int width, unsigned int height, unsigned int stride)
1395         const struct format_info *info = NULL;
1396         unsigned int i;
1398         for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
1399                 if (format_info[i].format == format) {
1400                         info = &format_info[i];
1401                         break;
1402                 }
1403         }
1405         if (info == NULL)
1406                 return;
1408         switch (pattern) {
1409         case PATTERN_TILES:
1410                 return fill_tiles(info, planes, width, height, stride);
1412         case PATTERN_SMPTE:
1413                 return fill_smpte(info, planes, width, height, stride);
1415         case PATTERN_PLAIN:
1416                 return fill_plain(info, planes, width, height, stride);
1418         default:
1419                 printf("Error: unsupported test pattern %u.\n", pattern);
1420                 break;
1421         }
1424 /* -----------------------------------------------------------------------------
1425  * Buffers management
1426  */
1428 static struct kms_bo *
1429 allocate_buffer(struct kms_driver *kms,
1430                 int width, int height, int *stride)
1432         struct kms_bo *bo;
1433         unsigned bo_attribs[] = {
1434                 KMS_WIDTH,   0,
1435                 KMS_HEIGHT,  0,
1436                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
1437                 KMS_TERMINATE_PROP_LIST
1438         };
1439         int ret;
1441         bo_attribs[1] = width;
1442         bo_attribs[3] = height;
1444         ret = kms_bo_create(kms, bo_attribs, &bo);
1445         if (ret) {
1446                 fprintf(stderr, "failed to alloc buffer: %s\n",
1447                         strerror(-ret));
1448                 return NULL;
1449         }
1451         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
1452         if (ret) {
1453                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
1454                         strerror(-ret));
1455                 kms_bo_destroy(&bo);
1456                 return NULL;
1457         }
1459         return bo;
1462 static struct kms_bo *
1463 create_test_buffer(struct kms_driver *kms, unsigned int format,
1464                    int width, int height, int handles[4],
1465                    int pitches[4], int offsets[4], enum fill_pattern pattern)
1467         struct kms_bo *bo;
1468         int ret, stride;
1469         void *planes[3];
1470         void *virtual;
1472         bo = allocate_buffer(kms, width, height, &pitches[0]);
1473         if (!bo)
1474                 return NULL;
1476         ret = kms_bo_map(bo, &virtual);
1477         if (ret) {
1478                 fprintf(stderr, "failed to map buffer: %s\n",
1479                         strerror(-ret));
1480                 kms_bo_destroy(&bo);
1481                 return NULL;
1482         }
1484         /* just testing a limited # of formats to test single
1485          * and multi-planar path.. would be nice to add more..
1486          */
1487         switch (format) {
1488         case DRM_FORMAT_UYVY:
1489         case DRM_FORMAT_VYUY:
1490         case DRM_FORMAT_YUYV:
1491         case DRM_FORMAT_YVYU:
1492                 pitches[0] = width * 2;
1493                 offsets[0] = 0;
1494                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1496                 planes[0] = virtual;
1497                 break;
1499         case DRM_FORMAT_NV12:
1500         case DRM_FORMAT_NV21:
1501         case DRM_FORMAT_NV16:
1502         case DRM_FORMAT_NV61:
1503                 pitches[0] = width;
1504                 offsets[0] = 0;
1505                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1506                 pitches[1] = width;
1507                 offsets[1] = width * height;
1508                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1510                 planes[0] = virtual;
1511                 planes[1] = virtual + offsets[1];
1512                 break;
1514         case DRM_FORMAT_YVU420:
1515                 pitches[0] = width;
1516                 offsets[0] = 0;
1517                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1518                 pitches[1] = width / 2;
1519                 offsets[1] = width * height;
1520                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1521                 pitches[2] = width / 2;
1522                 offsets[2] = offsets[1] + (width * height) / 4;
1523                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
1525                 planes[0] = virtual;
1526                 planes[1] = virtual + offsets[1];
1527                 planes[2] = virtual + offsets[2];
1528                 break;
1530         case DRM_FORMAT_RGB565:
1531         case DRM_FORMAT_ARGB1555:
1532         case DRM_FORMAT_XRGB1555:
1533                 pitches[0] = width * 2;
1534                 offsets[0] = 0;
1535                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1537                 planes[0] = virtual;
1538                 break;
1540         case DRM_FORMAT_BGR888:
1541         case DRM_FORMAT_RGB888:
1542                 pitches[0] = width * 3;
1543                 offsets[0] = 0;
1544                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1546                 planes[0] = virtual;
1547                 break;
1549         case DRM_FORMAT_ARGB8888:
1550         case DRM_FORMAT_BGRA8888:
1551         case DRM_FORMAT_XRGB8888:
1552         case DRM_FORMAT_BGRX8888:
1553                 pitches[0] = width * 4;
1554                 offsets[0] = 0;
1555                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1557                 planes[0] = virtual;
1558                 break;
1559         }
1561         fill_pattern(format, pattern, planes, width, height, pitches[0]);
1562         kms_bo_unmap(bo);
1564         return bo;
1567 /* -------------------------------------------------------------------------- */
1569 void
1570 page_flip_handler(int fd, unsigned int frame,
1571                   unsigned int sec, unsigned int usec, void *data)
1573         struct connector *c;
1574         unsigned int new_fb_id;
1575         struct timeval end;
1576         double t;
1578         c = data;
1579         if (c->current_fb_id == c->fb_id[0])
1580                 new_fb_id = c->fb_id[1];
1581         else
1582                 new_fb_id = c->fb_id[0];
1584         drmModePageFlip(fd, c->crtc, new_fb_id,
1585                         DRM_MODE_PAGE_FLIP_EVENT, c);
1586         c->current_fb_id = new_fb_id;
1587         c->swap_count++;
1588         if (c->swap_count == 60) {
1589                 gettimeofday(&end, NULL);
1590                 t = end.tv_sec + end.tv_usec * 1e-6 -
1591                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
1592                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
1593                 c->swap_count = 0;
1594                 c->start = end;
1595         }
1598 static int
1599 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
1601         drmModePlaneRes *plane_resources;
1602         drmModePlane *ovr;
1603         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1604         uint32_t plane_id = 0;
1605         struct kms_bo *plane_bo;
1606         uint32_t plane_flags = 0;
1607         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
1608         unsigned int i;
1610         /* find an unused plane which can be connected to our crtc */
1611         plane_resources = drmModeGetPlaneResources(fd);
1612         if (!plane_resources) {
1613                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
1614                         strerror(errno));
1615                 return -1;
1616         }
1618         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
1619                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
1620                 if (!ovr) {
1621                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
1622                                 strerror(errno));
1623                         return -1;
1624                 }
1626                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
1627                         plane_id = ovr->plane_id;
1629                 drmModeFreePlane(ovr);
1630         }
1632         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
1633                         p->w, p->h, p->format_str);
1635         if (!plane_id) {
1636                 fprintf(stderr, "failed to find plane!\n");
1637                 return -1;
1638         }
1640         plane_bo = create_test_buffer(kms, p->fourcc, p->w, p->h, handles,
1641                                       pitches, offsets, PATTERN_TILES);
1642         if (plane_bo == NULL)
1643                 return -1;
1645         /* just use single plane format for now.. */
1646         if (drmModeAddFB2(fd, p->w, p->h, p->fourcc,
1647                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
1648                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1649                 return -1;
1650         }
1652         /* ok, boring.. but for now put in middle of screen: */
1653         crtc_x = c->mode->hdisplay / 3;
1654         crtc_y = c->mode->vdisplay / 3;
1655         crtc_w = crtc_x;
1656         crtc_h = crtc_y;
1658         /* note src coords (last 4 args) are in Q16 format */
1659         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
1660                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1661                             0, 0, p->w << 16, p->h << 16)) {
1662                 fprintf(stderr, "failed to enable plane: %s\n",
1663                         strerror(errno));
1664                 return -1;
1665         }
1667         return 0;
1670 static void
1671 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
1672                 int page_flip)
1674         struct kms_driver *kms;
1675         struct kms_bo *bo, *other_bo;
1676         unsigned int fb_id, other_fb_id;
1677         int i, j, ret, width, height, x;
1678         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1679         drmEventContext evctx;
1681         width = 0;
1682         height = 0;
1683         for (i = 0; i < count; i++) {
1684                 connector_find_mode(&c[i]);
1685                 if (c[i].mode == NULL)
1686                         continue;
1687                 width += c[i].mode->hdisplay;
1688                 if (height < c[i].mode->vdisplay)
1689                         height = c[i].mode->vdisplay;
1690         }
1692         ret = kms_create(fd, &kms);
1693         if (ret) {
1694                 fprintf(stderr, "failed to create kms driver: %s\n",
1695                         strerror(-ret));
1696                 return;
1697         }
1699         bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1700                                 pitches, offsets, PATTERN_SMPTE);
1701         if (bo == NULL)
1702                 return;
1704         ret = drmModeAddFB(fd, width, height, 24, 32, pitches[0], handles[0], &fb_id);
1705         if (ret) {
1706                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1707                         width, height, strerror(errno));
1708                 return;
1709         }
1711         x = 0;
1712         for (i = 0; i < count; i++) {
1713                 if (c[i].mode == NULL)
1714                         continue;
1716                 printf("setting mode %s on connector %d, crtc %d\n",
1717                        c[i].mode_str, c[i].id, c[i].crtc);
1719                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1720                                      &c[i].id, 1, c[i].mode);
1722                 /* XXX: Actually check if this is needed */
1723                 drmModeDirtyFB(fd, fb_id, NULL, 0);
1725                 x += c[i].mode->hdisplay;
1727                 if (ret) {
1728                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1729                         return;
1730                 }
1732                 /* if we have a plane/overlay to show, set that up now: */
1733                 for (j = 0; j < plane_count; j++)
1734                         if (p[j].con_id == c[i].id)
1735                                 if (set_plane(kms, &c[i], &p[j]))
1736                                         return;
1737         }
1739         if (!page_flip)
1740                 return;
1741         
1742         other_bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1743                                       pitches, offsets, PATTERN_PLAIN);
1744         if (other_bo == NULL)
1745                 return;
1747         ret = drmModeAddFB(fd, width, height, 32, 32, pitches[0], handles[0],
1748                            &other_fb_id);
1749         if (ret) {
1750                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1751                 return;
1752         }
1754         for (i = 0; i < count; i++) {
1755                 if (c[i].mode == NULL)
1756                         continue;
1758                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1759                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1760                 if (ret) {
1761                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1762                         return;
1763                 }
1764                 gettimeofday(&c[i].start, NULL);
1765                 c[i].swap_count = 0;
1766                 c[i].fb_id[0] = fb_id;
1767                 c[i].fb_id[1] = other_fb_id;
1768                 c[i].current_fb_id = other_fb_id;
1769         }
1771         memset(&evctx, 0, sizeof evctx);
1772         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1773         evctx.vblank_handler = NULL;
1774         evctx.page_flip_handler = page_flip_handler;
1775         
1776         while (1) {
1777 #if 0
1778                 struct pollfd pfd[2];
1780                 pfd[0].fd = 0;
1781                 pfd[0].events = POLLIN;
1782                 pfd[1].fd = fd;
1783                 pfd[1].events = POLLIN;
1785                 if (poll(pfd, 2, -1) < 0) {
1786                         fprintf(stderr, "poll error\n");
1787                         break;
1788                 }
1790                 if (pfd[0].revents)
1791                         break;
1792 #else
1793                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1794                 fd_set fds;
1795                 int ret;
1797                 FD_ZERO(&fds);
1798                 FD_SET(0, &fds);
1799                 FD_SET(fd, &fds);
1800                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1802                 if (ret <= 0) {
1803                         fprintf(stderr, "select timed out or error (ret %d)\n",
1804                                 ret);
1805                         continue;
1806                 } else if (FD_ISSET(0, &fds)) {
1807                         break;
1808                 }
1809 #endif
1811                 drmHandleEvent(fd, &evctx);
1812         }
1814         kms_bo_destroy(&bo);
1815         kms_bo_destroy(&other_bo);
1816         kms_destroy(&kms);
1819 extern char *optarg;
1820 extern int optind, opterr, optopt;
1821 static char optstr[] = "ecpmfs:P:v";
1823 static int parse_connector(struct connector *c, const char *arg)
1825         c->crtc = -1;
1827         if (sscanf(arg, "%d:%64s", &c->id, &c->mode_str) == 2)
1828                 return 0;
1830         if (sscanf(arg, "%d@%d:%64s", &c->id, &c->crtc, &c->mode_str) == 3)
1831                 return 0;
1833         return -1;
1836 static int parse_plane(struct plane *p, const char *arg)
1838         strcpy(p->format_str, "XR24");
1840         if (sscanf(arg, "%d:%dx%d@%4s", &p->con_id, &p->w, &p->h, &p->format_str) != 4 &&
1841             sscanf(arg, "%d:%dx%d", &p->con_id, &p->w, &p->h) != 3)
1842                 return -1;
1844         p->fourcc = format_fourcc(p->format_str);
1845         if (p->fourcc == 0) {
1846                 fprintf(stderr, "unknown format %s\n", p->format_str);
1847                 return -1;
1848         }
1850         return 0;
1853 void usage(char *name)
1855         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1856         fprintf(stderr, "\t-e\tlist encoders\n");
1857         fprintf(stderr, "\t-c\tlist connectors\n");
1858         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1859         fprintf(stderr, "\t-m\tlist modes\n");
1860         fprintf(stderr, "\t-f\tlist framebuffers\n");
1861         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1862         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1863         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1864         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1865         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1866         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1867         exit(0);
1870 #define dump_resource(res) if (res) dump_##res()
1872 static int page_flipping_supported(void)
1874         /*FIXME: generic ioctl needed? */
1875         return 1;
1876 #if 0
1877         int ret, value;
1878         struct drm_i915_getparam gp;
1880         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1881         gp.value = &value;
1883         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1884         if (ret) {
1885                 fprintf(stderr, "drm_i915_getparam: %m\n");
1886                 return 0;
1887         }
1889         return *gp.value;
1890 #endif
1893 int main(int argc, char **argv)
1895         int c;
1896         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1897         int test_vsync = 0;
1898         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1899         unsigned int i;
1900         int count = 0, plane_count = 0;
1901         struct connector con_args[2];
1902         struct plane plane_args[2] = {0};
1903         
1904         opterr = 0;
1905         while ((c = getopt(argc, argv, optstr)) != -1) {
1906                 switch (c) {
1907                 case 'e':
1908                         encoders = 1;
1909                         break;
1910                 case 'c':
1911                         connectors = 1;
1912                         break;
1913                 case 'p':
1914                         crtcs = 1;
1915                         planes = 1;
1916                         break;
1917                 case 'm':
1918                         modes = 1;
1919                         break;
1920                 case 'f':
1921                         framebuffers = 1;
1922                         break;
1923                 case 'v':
1924                         test_vsync = 1;
1925                         break;
1926                 case 's':
1927                         if (parse_connector(&con_args[count], optarg) < 0)
1928                                 usage(argv[0]);
1929                         count++;                                      
1930                         break;
1931                 case 'P':
1932                         if (parse_plane(&plane_args[plane_count], optarg) < 0)
1933                                 usage(argv[0]);
1934                         plane_count++;
1935                         break;
1936                 default:
1937                         usage(argv[0]);
1938                         break;
1939                 }
1940         }
1942         if (argc == 1)
1943                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1945         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1946                 printf("trying to load module %s...", modules[i]);
1947                 fd = drmOpen(modules[i], NULL);
1948                 if (fd < 0) {
1949                         printf("failed.\n");
1950                 } else {
1951                         printf("success.\n");
1952                         break;
1953                 }
1954         }
1956         if (test_vsync && !page_flipping_supported()) {
1957                 fprintf(stderr, "page flipping not supported by drm.\n");
1958                 return -1;
1959         }
1961         if (i == ARRAY_SIZE(modules)) {
1962                 fprintf(stderr, "failed to load any modules, aborting.\n");
1963                 return -1;
1964         }
1966         resources = drmModeGetResources(fd);
1967         if (!resources) {
1968                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1969                         strerror(errno));
1970                 drmClose(fd);
1971                 return 1;
1972         }
1974         dump_resource(encoders);
1975         dump_resource(connectors);
1976         dump_resource(crtcs);
1977         dump_resource(planes);
1978         dump_resource(framebuffers);
1980         if (count > 0) {
1981                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1982                 getchar();
1983         }
1985         drmModeFreeResources(resources);
1987         return 0;