bdb74aef6733c2a136c80b6e2911e66c7371ff9b
[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         /* RGB */
641         { DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
642         { DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
643         { DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
644 };
646 unsigned int format_fourcc(const char *name)
648         unsigned int i;
649         for (i = 0; i < ARRAY_SIZE(format_info); i++) {
650                 if (!strcmp(format_info[i].name, name))
651                         return format_info[i].format;
652         }
653         return 0;
656 /* -----------------------------------------------------------------------------
657  * Test patterns
658  */
660 enum fill_pattern {
661         PATTERN_TILES = 0,
662         PATTERN_PLAIN = 1,
663         PATTERN_SMPTE = 2,
664 };
666 struct color_rgb24 {
667         unsigned int value:24;
668 } __attribute__((__packed__));
670 struct color_yuv {
671         unsigned char y;
672         unsigned char u;
673         unsigned char v;
674 };
676 #define MAKE_YUV_601_Y(r, g, b) \
677         ((( 66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) + 16)
678 #define MAKE_YUV_601_U(r, g, b) \
679         (((-38 * (r) -  74 * (g) + 112 * (b) + 128) >> 8) + 128)
680 #define MAKE_YUV_601_V(r, g, b) \
681         (((112 * (r) -  94 * (g) -  18 * (b) + 128) >> 8) + 128)
683 #define MAKE_YUV_601(r, g, b) \
684         { .y = MAKE_YUV_601_Y(r, g, b), \
685           .u = MAKE_YUV_601_U(r, g, b), \
686           .v = MAKE_YUV_601_V(r, g, b) }
688 #define MAKE_RGBA(rgb, r, g, b, a) \
689         ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
690          (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
691          (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
692          (((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
694 static void
695 fill_smpte_yuv_planar(const struct yuv_info *yuv,
696                       unsigned char *y_mem, unsigned char *u_mem,
697                       unsigned char *v_mem, unsigned int width,
698                       unsigned int height, unsigned int stride)
700         const struct color_yuv colors_top[] = {
701                 MAKE_YUV_601(191, 192, 192),    /* grey */
702                 MAKE_YUV_601(192, 192, 0),      /* yellow */
703                 MAKE_YUV_601(0, 192, 192),      /* cyan */
704                 MAKE_YUV_601(0, 192, 0),        /* green */
705                 MAKE_YUV_601(192, 0, 192),      /* magenta */
706                 MAKE_YUV_601(192, 0, 0),        /* red */
707                 MAKE_YUV_601(0, 0, 192),        /* blue */
708         };
709         const struct color_yuv colors_middle[] = {
710                 MAKE_YUV_601(0, 0, 192),        /* blue */
711                 MAKE_YUV_601(19, 19, 19),       /* black */
712                 MAKE_YUV_601(192, 0, 192),      /* magenta */
713                 MAKE_YUV_601(19, 19, 19),       /* black */
714                 MAKE_YUV_601(0, 192, 192),      /* cyan */
715                 MAKE_YUV_601(19, 19, 19),       /* black */
716                 MAKE_YUV_601(192, 192, 192),    /* grey */
717         };
718         const struct color_yuv colors_bottom[] = {
719                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
720                 MAKE_YUV_601(255, 255, 255),    /* super white */
721                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
722                 MAKE_YUV_601(19, 19, 19),       /* black */
723                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
724                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
725                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
726                 MAKE_YUV_601(19, 19, 19),       /* black */
727         };
728         unsigned int cs = yuv->chroma_stride;
729         unsigned int xsub = yuv->xsub;
730         unsigned int ysub = yuv->ysub;
731         unsigned int x;
732         unsigned int y;
734         /* Luma */
735         for (y = 0; y < height * 6 / 9; ++y) {
736                 for (x = 0; x < width; ++x)
737                         y_mem[x] = colors_top[x * 7 / width].y;
738                 y_mem += stride;
739         }
741         for (; y < height * 7 / 9; ++y) {
742                 for (x = 0; x < width; ++x)
743                         y_mem[x] = colors_middle[x * 7 / width].y;
744                 y_mem += stride;
745         }
747         for (; y < height; ++y) {
748                 for (x = 0; x < width * 5 / 7; ++x)
749                         y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
750                 for (; x < width * 6 / 7; ++x)
751                         y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
752                                                  / (width / 7) + 4].y;
753                 for (; x < width; ++x)
754                         y_mem[x] = colors_bottom[7].y;
755                 y_mem += stride;
756         }
758         /* Chroma */
759         for (y = 0; y < height / ysub * 6 / 9; ++y) {
760                 for (x = 0; x < width; x += xsub) {
761                         u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
762                         v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
763                 }
764                 u_mem += stride * cs / xsub;
765                 v_mem += stride * cs / xsub;
766         }
768         for (; y < height / ysub * 7 / 9; ++y) {
769                 for (x = 0; x < width; x += xsub) {
770                         u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
771                         v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
772                 }
773                 u_mem += stride * cs / xsub;
774                 v_mem += stride * cs / xsub;
775         }
777         for (; y < height / ysub; ++y) {
778                 for (x = 0; x < width * 5 / 7; x += xsub) {
779                         u_mem[x*cs/xsub] =
780                                 colors_bottom[x * 4 / (width * 5 / 7)].u;
781                         v_mem[x*cs/xsub] =
782                                 colors_bottom[x * 4 / (width * 5 / 7)].v;
783                 }
784                 for (; x < width * 6 / 7; x += xsub) {
785                         u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
786                                                          3 / (width / 7) + 4].u;
787                         v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
788                                                          3 / (width / 7) + 4].v;
789                 }
790                 for (; x < width; x += xsub) {
791                         u_mem[x*cs/xsub] = colors_bottom[7].u;
792                         v_mem[x*cs/xsub] = colors_bottom[7].v;
793                 }
794                 u_mem += stride * cs / xsub;
795                 v_mem += stride * cs / xsub;
796         }
799 static void
800 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
801                       unsigned int width, unsigned int height,
802                       unsigned int stride)
804         const struct color_yuv colors_top[] = {
805                 MAKE_YUV_601(191, 192, 192),    /* grey */
806                 MAKE_YUV_601(192, 192, 0),      /* yellow */
807                 MAKE_YUV_601(0, 192, 192),      /* cyan */
808                 MAKE_YUV_601(0, 192, 0),        /* green */
809                 MAKE_YUV_601(192, 0, 192),      /* magenta */
810                 MAKE_YUV_601(192, 0, 0),        /* red */
811                 MAKE_YUV_601(0, 0, 192),        /* blue */
812         };
813         const struct color_yuv colors_middle[] = {
814                 MAKE_YUV_601(0, 0, 192),        /* blue */
815                 MAKE_YUV_601(19, 19, 19),       /* black */
816                 MAKE_YUV_601(192, 0, 192),      /* magenta */
817                 MAKE_YUV_601(19, 19, 19),       /* black */
818                 MAKE_YUV_601(0, 192, 192),      /* cyan */
819                 MAKE_YUV_601(19, 19, 19),       /* black */
820                 MAKE_YUV_601(192, 192, 192),    /* grey */
821         };
822         const struct color_yuv colors_bottom[] = {
823                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
824                 MAKE_YUV_601(255, 255, 255),    /* super white */
825                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
826                 MAKE_YUV_601(19, 19, 19),       /* black */
827                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
828                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
829                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
830                 MAKE_YUV_601(19, 19, 19),       /* black */
831         };
832         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
833         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
834         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
835         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
836         unsigned int x;
837         unsigned int y;
839         /* Luma */
840         for (y = 0; y < height * 6 / 9; ++y) {
841                 for (x = 0; x < width; ++x)
842                         y_mem[2*x] = colors_top[x * 7 / width].y;
843                 y_mem += stride * 2;
844         }
846         for (; y < height * 7 / 9; ++y) {
847                 for (x = 0; x < width; ++x)
848                         y_mem[2*x] = colors_middle[x * 7 / width].y;
849                 y_mem += stride * 2;
850         }
852         for (; y < height; ++y) {
853                 for (x = 0; x < width * 5 / 7; ++x)
854                         y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
855                 for (; x < width * 6 / 7; ++x)
856                         y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
857                                                    / (width / 7) + 4].y;
858                 for (; x < width; ++x)
859                         y_mem[2*x] = colors_bottom[7].y;
860                 y_mem += stride * 2;
861         }
863         /* Chroma */
864         for (y = 0; y < height * 6 / 9; ++y) {
865                 for (x = 0; x < width; x += 2) {
866                         c_mem[2*x+u] = colors_top[x * 7 / width].u;
867                         c_mem[2*x+v] = colors_top[x * 7 / width].v;
868                 }
869                 c_mem += stride * 2;
870         }
872         for (; y < height * 7 / 9; ++y) {
873                 for (x = 0; x < width; x += 2) {
874                         c_mem[2*x+u] = colors_middle[x * 7 / width].u;
875                         c_mem[2*x+v] = colors_middle[x * 7 / width].v;
876                 }
877                 c_mem += stride * 2;
878         }
880         for (; y < height; ++y) {
881                 for (x = 0; x < width * 5 / 7; x += 2) {
882                         c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
883                         c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
884                 }
885                 for (; x < width * 6 / 7; x += 2) {
886                         c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
887                                                      3 / (width / 7) + 4].u;
888                         c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
889                                                      3 / (width / 7) + 4].v;
890                 }
891                 for (; x < width; x += 2) {
892                         c_mem[2*x+u] = colors_bottom[7].u;
893                         c_mem[2*x+v] = colors_bottom[7].v;
894                 }
895                 c_mem += stride * 2;
896         }
899 static void
900 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
901                  unsigned int width, unsigned int height, unsigned int stride)
903         const uint16_t colors_top[] = {
904                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
905                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
906                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
907                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
908                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
909                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
910                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
911         };
912         const uint16_t colors_middle[] = {
913                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
914                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
915                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
916                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
917                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
918                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
919                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
920         };
921         const uint16_t colors_bottom[] = {
922                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
923                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
924                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
925                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
926                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
927                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
928                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
929                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
930         };
931         unsigned int x;
932         unsigned int y;
934         for (y = 0; y < height * 6 / 9; ++y) {
935                 for (x = 0; x < width; ++x)
936                         ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
937                 mem += stride;
938         }
940         for (; y < height * 7 / 9; ++y) {
941                 for (x = 0; x < width; ++x)
942                         ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
943                 mem += stride;
944         }
946         for (; y < height; ++y) {
947                 for (x = 0; x < width * 5 / 7; ++x)
948                         ((uint16_t *)mem)[x] =
949                                 colors_bottom[x * 4 / (width * 5 / 7)];
950                 for (; x < width * 6 / 7; ++x)
951                         ((uint16_t *)mem)[x] =
952                                 colors_bottom[(x - width * 5 / 7) * 3
953                                               / (width / 7) + 4];
954                 for (; x < width; ++x)
955                         ((uint16_t *)mem)[x] = colors_bottom[7];
956                 mem += stride;
957         }
960 static void
961 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
962                  unsigned int width, unsigned int height, unsigned int stride)
964         const uint32_t colors_top[] = {
965                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
966                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
967                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
968                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
969                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
970                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
971                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
972         };
973         const uint32_t colors_middle[] = {
974                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
975                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
976                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
977                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
978                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
979                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
980                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
981         };
982         const uint32_t colors_bottom[] = {
983                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
984                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
985                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
986                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
987                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
988                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
989                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
990                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
991         };
992         unsigned int x;
993         unsigned int y;
995         for (y = 0; y < height * 6 / 9; ++y) {
996                 for (x = 0; x < width; ++x)
997                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
998                 mem += stride;
999         }
1001         for (; y < height * 7 / 9; ++y) {
1002                 for (x = 0; x < width; ++x)
1003                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
1004                 mem += stride;
1005         }
1007         for (; y < height; ++y) {
1008                 for (x = 0; x < width * 5 / 7; ++x)
1009                         ((uint32_t *)mem)[x] =
1010                                 colors_bottom[x * 4 / (width * 5 / 7)];
1011                 for (; x < width * 6 / 7; ++x)
1012                         ((uint32_t *)mem)[x] =
1013                                 colors_bottom[(x - width * 5 / 7) * 3
1014                                               / (width / 7) + 4];
1015                 for (; x < width; ++x)
1016                         ((uint32_t *)mem)[x] = colors_bottom[7];
1017                 mem += stride;
1018         }
1021 static void
1022 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
1023            unsigned int height, unsigned int stride)
1025         unsigned char *u, *v;
1027         switch (info->format) {
1028         case DRM_FORMAT_UYVY:
1029         case DRM_FORMAT_VYUY:
1030         case DRM_FORMAT_YUYV:
1031         case DRM_FORMAT_YVYU:
1032                 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
1033                                              height, stride);
1035         case DRM_FORMAT_NV12:
1036         case DRM_FORMAT_NV21:
1037         case DRM_FORMAT_NV16:
1038         case DRM_FORMAT_NV61:
1039                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1040                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1041                 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
1042                                              width, height, stride);
1044         case DRM_FORMAT_YVU420:
1045                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
1046                                              planes[2], width, height, stride);
1048         case DRM_FORMAT_ARGB1555:
1049         case DRM_FORMAT_XRGB1555:
1050                 return fill_smpte_rgb16(&info->rgb, planes[0],
1051                                         width, height, stride);
1052         case DRM_FORMAT_XRGB8888:
1053                 return fill_smpte_rgb32(&info->rgb, planes[0],
1054                                         width, height, stride);
1055         }
1058 /* swap these for big endian.. */
1059 #define RED   2
1060 #define GREEN 1
1061 #define BLUE  0
1063 static void
1064 make_pwetty(void *data, int width, int height, int stride)
1066 #ifdef HAVE_CAIRO
1067         cairo_surface_t *surface;
1068         cairo_t *cr;
1069         int x, y;
1071         surface = cairo_image_surface_create_for_data(data,
1072                                                       CAIRO_FORMAT_ARGB32,
1073                                                       width, height,
1074                                                       stride);
1075         cr = cairo_create(surface);
1076         cairo_surface_destroy(surface);
1078         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1079         for (x = 0; x < width; x += 250)
1080                 for (y = 0; y < height; y += 250) {
1081                         char buf[64];
1083                         cairo_move_to(cr, x, y - 20);
1084                         cairo_line_to(cr, x, y + 20);
1085                         cairo_move_to(cr, x - 20, y);
1086                         cairo_line_to(cr, x + 20, y);
1087                         cairo_new_sub_path(cr);
1088                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
1089                         cairo_set_line_width(cr, 4);
1090                         cairo_set_source_rgb(cr, 0, 0, 0);
1091                         cairo_stroke_preserve(cr);
1092                         cairo_set_source_rgb(cr, 1, 1, 1);
1093                         cairo_set_line_width(cr, 2);
1094                         cairo_stroke(cr);
1096                         snprintf(buf, sizeof buf, "%d, %d", x, y);
1097                         cairo_move_to(cr, x + 20, y + 20);
1098                         cairo_text_path(cr, buf);
1099                         cairo_set_source_rgb(cr, 0, 0, 0);
1100                         cairo_stroke_preserve(cr);
1101                         cairo_set_source_rgb(cr, 1, 1, 1);
1102                         cairo_fill(cr);
1103                 }
1105         cairo_destroy(cr);
1106 #endif
1109 static void
1110 fill_tiles_yuv_planar(const struct yuv_info *yuv,
1111                       unsigned char *y_mem, unsigned char *u_mem,
1112                       unsigned char *v_mem, unsigned int width,
1113                       unsigned int height, unsigned int stride)
1115         unsigned int cs = yuv->chroma_stride;
1116         unsigned int xsub = yuv->xsub;
1117         unsigned int ysub = yuv->ysub;
1118         unsigned int x;
1119         unsigned int y;
1121         for (y = 0; y < height; ++y) {
1122                 for (x = 0; x < width; ++x) {
1123                         div_t d = div(x+y, width);
1124                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1125                                        + 0x000a1120 * (d.rem >> 6);
1126                         struct color_yuv color =
1127                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1128                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1130                         y_mem[x] = color.y;
1131                         u_mem[x/xsub*cs] = color.u;
1132                         v_mem[x/xsub*cs] = color.v;
1133                 }
1135                 y_mem += stride;
1136                 if ((y + 1) % ysub == 0) {
1137                         u_mem += stride * cs / xsub;
1138                         v_mem += stride * cs / xsub;
1139                 }
1140         }
1143 static void
1144 fill_tiles_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
1145                       unsigned int width, unsigned int height,
1146                       unsigned int stride)
1148         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
1149         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
1150         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
1151         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
1152         unsigned int x;
1153         unsigned int y;
1155         for (y = 0; y < height; ++y) {
1156                 for (x = 0; x < width; x += 2) {
1157                         div_t d = div(x+y, width);
1158                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1159                                        + 0x000a1120 * (d.rem >> 6);
1160                         struct color_yuv color =
1161                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1162                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1164                         y_mem[2*x] = color.y;
1165                         c_mem[2*x+u] = color.u;
1166                         y_mem[2*x+2] = color.y;
1167                         c_mem[2*x+v] = color.v;
1168                 }
1170                 y_mem += stride;
1171                 c_mem += stride;
1172         }
1175 static void
1176 fill_tiles_rgb16(const struct rgb_info *rgb, unsigned char *mem,
1177                  unsigned int width, unsigned int height, unsigned int stride)
1179         unsigned int i, j;
1181         for (j = 0; j < height; j++) {
1182                 uint16_t *ptr = (uint16_t*)((char*)mem + j * stride);
1183                 for (i = 0; i < width; i++) {
1184                         div_t d = div(i+j, width);
1185                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
1186                         unsigned char *rgbp = (unsigned char *)&rgb;
1188                         *(ptr++) = 0x8000 |
1189                                         (rgbp[RED] >> 3) << 10 |
1190                                         (rgbp[GREEN] >> 3) << 5 |
1191                                         (rgbp[BLUE] >> 3);
1192                 }
1193         }
1196 static void
1197 fill_tiles_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1198                  unsigned int width, unsigned int height, unsigned int stride)
1200         unsigned int i, j;
1202         for (j = 0; j < height; j++) {
1203                 uint32_t *ptr = (uint32_t*)((char*)mem + j * stride);
1204                 for (i = 0; i < width; i++) {
1205                         div_t d = div(i, width);
1206                         ptr[i] =
1207                                 0x00130502 * (d.quot >> 6) +
1208                                 0x000a1120 * (d.rem >> 6);
1209                 }
1210         }
1212         make_pwetty(mem, width, height, stride);
1215 static void
1216 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
1217            unsigned int height, unsigned int stride)
1219         unsigned char *u, *v;
1221         switch (info->format) {
1222         case DRM_FORMAT_UYVY:
1223         case DRM_FORMAT_VYUY:
1224         case DRM_FORMAT_YUYV:
1225         case DRM_FORMAT_YVYU:
1226                 return fill_tiles_yuv_packed(&info->yuv, planes[0],
1227                                              width, height, stride);
1229         case DRM_FORMAT_NV12:
1230         case DRM_FORMAT_NV21:
1231         case DRM_FORMAT_NV16:
1232         case DRM_FORMAT_NV61:
1233                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1234                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1235                 return fill_tiles_yuv_planar(&info->yuv, planes[0], u, v,
1236                                              width, height, stride);
1238         case DRM_FORMAT_YVU420:
1239                 return fill_tiles_yuv_planar(&info->yuv, planes[0], planes[1],
1240                                              planes[2], width, height, stride);
1242         case DRM_FORMAT_ARGB1555:
1243         case DRM_FORMAT_XRGB1555:
1244                 return fill_tiles_rgb16(&info->rgb, planes[0],
1245                                         width, height, stride);
1246         case DRM_FORMAT_XRGB8888:
1247                 return fill_tiles_rgb32(&info->rgb, planes[0],
1248                                         width, height, stride);
1249         }
1252 static void
1253 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
1254            unsigned int height, unsigned int stride)
1256         memset(planes[0], 0x77, stride * height);
1259 /*
1260  * fill_pattern - Fill a buffer with a test pattern
1261  * @format: Pixel format
1262  * @pattern: Test pattern
1263  * @buffer: Buffer memory
1264  * @width: Width in pixels
1265  * @height: Height in pixels
1266  * @stride: Line stride (pitch) in bytes
1267  *
1268  * Fill the buffer with the test pattern specified by the pattern parameter.
1269  * Supported formats vary depending on the selected pattern.
1270  */
1271 static void
1272 fill_pattern(unsigned int format, enum fill_pattern pattern,
1273              void *planes[3],
1274              unsigned int width, unsigned int height, unsigned int stride)
1276         const struct format_info *info = NULL;
1277         unsigned int i;
1279         for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
1280                 if (format_info[i].format == format) {
1281                         info = &format_info[i];
1282                         break;
1283                 }
1284         }
1286         if (info == NULL)
1287                 return;
1289         switch (pattern) {
1290         case PATTERN_TILES:
1291                 return fill_tiles(info, planes, width, height, stride);
1293         case PATTERN_SMPTE:
1294                 return fill_smpte(info, planes, width, height, stride);
1296         case PATTERN_PLAIN:
1297                 return fill_plain(info, planes, width, height, stride);
1299         default:
1300                 printf("Error: unsupported test pattern %u.\n", pattern);
1301                 break;
1302         }
1305 /* -----------------------------------------------------------------------------
1306  * Buffers management
1307  */
1309 static struct kms_bo *
1310 allocate_buffer(struct kms_driver *kms,
1311                 int width, int height, int *stride)
1313         struct kms_bo *bo;
1314         unsigned bo_attribs[] = {
1315                 KMS_WIDTH,   0,
1316                 KMS_HEIGHT,  0,
1317                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
1318                 KMS_TERMINATE_PROP_LIST
1319         };
1320         int ret;
1322         bo_attribs[1] = width;
1323         bo_attribs[3] = height;
1325         ret = kms_bo_create(kms, bo_attribs, &bo);
1326         if (ret) {
1327                 fprintf(stderr, "failed to alloc buffer: %s\n",
1328                         strerror(-ret));
1329                 return NULL;
1330         }
1332         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
1333         if (ret) {
1334                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
1335                         strerror(-ret));
1336                 kms_bo_destroy(&bo);
1337                 return NULL;
1338         }
1340         return bo;
1343 static struct kms_bo *
1344 create_test_buffer(struct kms_driver *kms, unsigned int format,
1345                    int width, int height, int handles[4],
1346                    int pitches[4], int offsets[4], enum fill_pattern pattern)
1348         struct kms_bo *bo;
1349         int ret, stride;
1350         void *planes[3];
1351         void *virtual;
1353         bo = allocate_buffer(kms, width, height, &pitches[0]);
1354         if (!bo)
1355                 return NULL;
1357         ret = kms_bo_map(bo, &virtual);
1358         if (ret) {
1359                 fprintf(stderr, "failed to map buffer: %s\n",
1360                         strerror(-ret));
1361                 kms_bo_destroy(&bo);
1362                 return NULL;
1363         }
1365         /* just testing a limited # of formats to test single
1366          * and multi-planar path.. would be nice to add more..
1367          */
1368         switch (format) {
1369         case DRM_FORMAT_UYVY:
1370         case DRM_FORMAT_VYUY:
1371         case DRM_FORMAT_YUYV:
1372         case DRM_FORMAT_YVYU:
1373                 pitches[0] = width * 2;
1374                 offsets[0] = 0;
1375                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1377                 planes[0] = virtual;
1378                 break;
1380         case DRM_FORMAT_NV12:
1381         case DRM_FORMAT_NV21:
1382         case DRM_FORMAT_NV16:
1383         case DRM_FORMAT_NV61:
1384                 pitches[0] = width;
1385                 offsets[0] = 0;
1386                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1387                 pitches[1] = width;
1388                 offsets[1] = width * height;
1389                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1391                 planes[0] = virtual;
1392                 planes[1] = virtual + offsets[1];
1393                 break;
1395         case DRM_FORMAT_YVU420:
1396                 pitches[0] = width;
1397                 offsets[0] = 0;
1398                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1399                 pitches[1] = width / 2;
1400                 offsets[1] = width * height;
1401                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1402                 pitches[2] = width / 2;
1403                 offsets[2] = offsets[1] + (width * height) / 4;
1404                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
1406                 planes[0] = virtual;
1407                 planes[1] = virtual + offsets[1];
1408                 planes[2] = virtual + offsets[2];
1409                 break;
1411         case DRM_FORMAT_RGB565:
1412         case DRM_FORMAT_ARGB1555:
1413         case DRM_FORMAT_XRGB1555:
1414                 pitches[0] = width * 2;
1415                 offsets[0] = 0;
1416                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1418                 planes[0] = virtual;
1419                 break;
1421         case DRM_FORMAT_RGB888:
1422                 pitches[0] = width * 3;
1423                 offsets[0] = 0;
1424                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1426                 planes[0] = virtual;
1427                 break;
1429         case DRM_FORMAT_XRGB8888:
1430                 pitches[0] = width * 4;
1431                 offsets[0] = 0;
1432                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1434                 planes[0] = virtual;
1435                 break;
1436         }
1438         fill_pattern(format, pattern, planes, width, height, pitches[0]);
1439         kms_bo_unmap(bo);
1441         return bo;
1444 /* -------------------------------------------------------------------------- */
1446 void
1447 page_flip_handler(int fd, unsigned int frame,
1448                   unsigned int sec, unsigned int usec, void *data)
1450         struct connector *c;
1451         unsigned int new_fb_id;
1452         struct timeval end;
1453         double t;
1455         c = data;
1456         if (c->current_fb_id == c->fb_id[0])
1457                 new_fb_id = c->fb_id[1];
1458         else
1459                 new_fb_id = c->fb_id[0];
1461         drmModePageFlip(fd, c->crtc, new_fb_id,
1462                         DRM_MODE_PAGE_FLIP_EVENT, c);
1463         c->current_fb_id = new_fb_id;
1464         c->swap_count++;
1465         if (c->swap_count == 60) {
1466                 gettimeofday(&end, NULL);
1467                 t = end.tv_sec + end.tv_usec * 1e-6 -
1468                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
1469                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
1470                 c->swap_count = 0;
1471                 c->start = end;
1472         }
1475 static int
1476 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
1478         drmModePlaneRes *plane_resources;
1479         drmModePlane *ovr;
1480         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1481         uint32_t plane_id = 0;
1482         struct kms_bo *plane_bo;
1483         uint32_t plane_flags = 0, format;
1484         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
1485         unsigned int i;
1487         format = format_fourcc(p->format_str);
1488         if (format == 0) {
1489                 fprintf(stderr, "Unknown format: %s\n", p->format_str);
1490                 return -1;
1491         }
1493         /* find an unused plane which can be connected to our crtc */
1494         plane_resources = drmModeGetPlaneResources(fd);
1495         if (!plane_resources) {
1496                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
1497                         strerror(errno));
1498                 return -1;
1499         }
1501         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
1502                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
1503                 if (!ovr) {
1504                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
1505                                 strerror(errno));
1506                         return -1;
1507                 }
1509                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
1510                         plane_id = ovr->plane_id;
1512                 drmModeFreePlane(ovr);
1513         }
1515         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
1516                         p->w, p->h, p->format_str);
1518         if (!plane_id) {
1519                 fprintf(stderr, "failed to find plane!\n");
1520                 return -1;
1521         }
1523         plane_bo = create_test_buffer(kms, format, p->w, p->h, handles,
1524                                       pitches, offsets, PATTERN_TILES);
1525         if (plane_bo == NULL)
1526                 return -1;
1528         /* just use single plane format for now.. */
1529         if (drmModeAddFB2(fd, p->w, p->h, format,
1530                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
1531                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1532                 return -1;
1533         }
1535         /* ok, boring.. but for now put in middle of screen: */
1536         crtc_x = c->mode->hdisplay / 3;
1537         crtc_y = c->mode->vdisplay / 3;
1538         crtc_w = crtc_x;
1539         crtc_h = crtc_y;
1541         /* note src coords (last 4 args) are in Q16 format */
1542         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
1543                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1544                             0, 0, p->w << 16, p->h << 16)) {
1545                 fprintf(stderr, "failed to enable plane: %s\n",
1546                         strerror(errno));
1547                 return -1;
1548         }
1550         return 0;
1553 static void
1554 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
1555                 int page_flip)
1557         struct kms_driver *kms;
1558         struct kms_bo *bo, *other_bo;
1559         unsigned int fb_id, other_fb_id;
1560         int i, j, ret, width, height, x;
1561         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1562         drmEventContext evctx;
1564         width = 0;
1565         height = 0;
1566         for (i = 0; i < count; i++) {
1567                 connector_find_mode(&c[i]);
1568                 if (c[i].mode == NULL)
1569                         continue;
1570                 width += c[i].mode->hdisplay;
1571                 if (height < c[i].mode->vdisplay)
1572                         height = c[i].mode->vdisplay;
1573         }
1575         ret = kms_create(fd, &kms);
1576         if (ret) {
1577                 fprintf(stderr, "failed to create kms driver: %s\n",
1578                         strerror(-ret));
1579                 return;
1580         }
1582         bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1583                                 pitches, offsets, PATTERN_SMPTE);
1584         if (bo == NULL)
1585                 return;
1587         ret = drmModeAddFB(fd, width, height, 24, 32, pitches[0], handles[0], &fb_id);
1588         if (ret) {
1589                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1590                         width, height, strerror(errno));
1591                 return;
1592         }
1594         x = 0;
1595         for (i = 0; i < count; i++) {
1596                 if (c[i].mode == NULL)
1597                         continue;
1599                 printf("setting mode %s on connector %d, crtc %d\n",
1600                        c[i].mode_str, c[i].id, c[i].crtc);
1602                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1603                                      &c[i].id, 1, c[i].mode);
1605                 /* XXX: Actually check if this is needed */
1606                 drmModeDirtyFB(fd, fb_id, NULL, 0);
1608                 x += c[i].mode->hdisplay;
1610                 if (ret) {
1611                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1612                         return;
1613                 }
1615                 /* if we have a plane/overlay to show, set that up now: */
1616                 for (j = 0; j < plane_count; j++)
1617                         if (p[j].con_id == c[i].id)
1618                                 if (set_plane(kms, &c[i], &p[j]))
1619                                         return;
1620         }
1622         if (!page_flip)
1623                 return;
1624         
1625         other_bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1626                                       pitches, offsets, PATTERN_PLAIN);
1627         if (other_bo == NULL)
1628                 return;
1630         ret = drmModeAddFB(fd, width, height, 32, 32, pitches[0], handles[0],
1631                            &other_fb_id);
1632         if (ret) {
1633                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1634                 return;
1635         }
1637         for (i = 0; i < count; i++) {
1638                 if (c[i].mode == NULL)
1639                         continue;
1641                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1642                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1643                 if (ret) {
1644                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1645                         return;
1646                 }
1647                 gettimeofday(&c[i].start, NULL);
1648                 c[i].swap_count = 0;
1649                 c[i].fb_id[0] = fb_id;
1650                 c[i].fb_id[1] = other_fb_id;
1651                 c[i].current_fb_id = other_fb_id;
1652         }
1654         memset(&evctx, 0, sizeof evctx);
1655         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1656         evctx.vblank_handler = NULL;
1657         evctx.page_flip_handler = page_flip_handler;
1658         
1659         while (1) {
1660 #if 0
1661                 struct pollfd pfd[2];
1663                 pfd[0].fd = 0;
1664                 pfd[0].events = POLLIN;
1665                 pfd[1].fd = fd;
1666                 pfd[1].events = POLLIN;
1668                 if (poll(pfd, 2, -1) < 0) {
1669                         fprintf(stderr, "poll error\n");
1670                         break;
1671                 }
1673                 if (pfd[0].revents)
1674                         break;
1675 #else
1676                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1677                 fd_set fds;
1678                 int ret;
1680                 FD_ZERO(&fds);
1681                 FD_SET(0, &fds);
1682                 FD_SET(fd, &fds);
1683                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1685                 if (ret <= 0) {
1686                         fprintf(stderr, "select timed out or error (ret %d)\n",
1687                                 ret);
1688                         continue;
1689                 } else if (FD_ISSET(0, &fds)) {
1690                         break;
1691                 }
1692 #endif
1694                 drmHandleEvent(fd, &evctx);
1695         }
1697         kms_bo_destroy(&bo);
1698         kms_bo_destroy(&other_bo);
1699         kms_destroy(&kms);
1702 extern char *optarg;
1703 extern int optind, opterr, optopt;
1704 static char optstr[] = "ecpmfs:P:v";
1706 void usage(char *name)
1708         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1709         fprintf(stderr, "\t-e\tlist encoders\n");
1710         fprintf(stderr, "\t-c\tlist connectors\n");
1711         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1712         fprintf(stderr, "\t-m\tlist modes\n");
1713         fprintf(stderr, "\t-f\tlist framebuffers\n");
1714         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1715         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1716         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1717         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1718         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1719         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1720         exit(0);
1723 #define dump_resource(res) if (res) dump_##res()
1725 static int page_flipping_supported(void)
1727         /*FIXME: generic ioctl needed? */
1728         return 1;
1729 #if 0
1730         int ret, value;
1731         struct drm_i915_getparam gp;
1733         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1734         gp.value = &value;
1736         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1737         if (ret) {
1738                 fprintf(stderr, "drm_i915_getparam: %m\n");
1739                 return 0;
1740         }
1742         return *gp.value;
1743 #endif
1746 int main(int argc, char **argv)
1748         int c;
1749         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1750         int test_vsync = 0;
1751         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1752         unsigned int i;
1753         int count = 0, plane_count = 0;
1754         struct connector con_args[2];
1755         struct plane plane_args[2] = {0};
1756         
1757         opterr = 0;
1758         while ((c = getopt(argc, argv, optstr)) != -1) {
1759                 switch (c) {
1760                 case 'e':
1761                         encoders = 1;
1762                         break;
1763                 case 'c':
1764                         connectors = 1;
1765                         break;
1766                 case 'p':
1767                         crtcs = 1;
1768                         planes = 1;
1769                         break;
1770                 case 'm':
1771                         modes = 1;
1772                         break;
1773                 case 'f':
1774                         framebuffers = 1;
1775                         break;
1776                 case 'v':
1777                         test_vsync = 1;
1778                         break;
1779                 case 's':
1780                         con_args[count].crtc = -1;
1781                         if (sscanf(optarg, "%d:%64s",
1782                                    &con_args[count].id,
1783                                    con_args[count].mode_str) != 2 &&
1784                             sscanf(optarg, "%d@%d:%64s",
1785                                    &con_args[count].id,
1786                                    &con_args[count].crtc,
1787                                    con_args[count].mode_str) != 3)
1788                                 usage(argv[0]);
1789                         count++;                                      
1790                         break;
1791                 case 'P':
1792                         strcpy(plane_args[plane_count].format_str, "XR24");
1793                         if (sscanf(optarg, "%d:%dx%d@%4s",
1794                                         &plane_args[plane_count].con_id,
1795                                         &plane_args[plane_count].w,
1796                                         &plane_args[plane_count].h,
1797                                         plane_args[plane_count].format_str) != 4 &&
1798                                 sscanf(optarg, "%d:%dx%d",
1799                                         &plane_args[plane_count].con_id,
1800                                         &plane_args[plane_count].w,
1801                                         &plane_args[plane_count].h) != 3)
1802                                 usage(argv[0]);
1803                         plane_count++;
1804                         break;
1805                 default:
1806                         usage(argv[0]);
1807                         break;
1808                 }
1809         }
1811         if (argc == 1)
1812                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1814         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1815                 printf("trying to load module %s...", modules[i]);
1816                 fd = drmOpen(modules[i], NULL);
1817                 if (fd < 0) {
1818                         printf("failed.\n");
1819                 } else {
1820                         printf("success.\n");
1821                         break;
1822                 }
1823         }
1825         if (test_vsync && !page_flipping_supported()) {
1826                 fprintf(stderr, "page flipping not supported by drm.\n");
1827                 return -1;
1828         }
1830         if (i == ARRAY_SIZE(modules)) {
1831                 fprintf(stderr, "failed to load any modules, aborting.\n");
1832                 return -1;
1833         }
1835         resources = drmModeGetResources(fd);
1836         if (!resources) {
1837                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1838                         strerror(errno));
1839                 drmClose(fd);
1840                 return 1;
1841         }
1843         dump_resource(encoders);
1844         dump_resource(connectors);
1845         dump_resource(crtcs);
1846         dump_resource(planes);
1847         dump_resource(framebuffers);
1849         if (count > 0) {
1850                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1851                 getchar();
1852         }
1854         drmModeFreeResources(resources);
1856         return 0;