]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - tests/modetest/modetest.c
f1dfe981a1ee0f4875358907ea294e6f3c4797dd
[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         /* YUV planar */
637         { DRM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
638         /* RGB */
639         { DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
640         { DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
641         { DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
642 };
644 unsigned int format_fourcc(const char *name)
646         unsigned int i;
647         for (i = 0; i < ARRAY_SIZE(format_info); i++) {
648                 if (!strcmp(format_info[i].name, name))
649                         return format_info[i].format;
650         }
651         return 0;
654 /* -----------------------------------------------------------------------------
655  * Test patterns
656  */
658 enum fill_pattern {
659         PATTERN_TILES = 0,
660         PATTERN_PLAIN = 1,
661         PATTERN_SMPTE = 2,
662 };
664 struct color_rgb24 {
665         unsigned int value:24;
666 } __attribute__((__packed__));
668 struct color_yuv {
669         unsigned char y;
670         unsigned char u;
671         unsigned char v;
672 };
674 #define MAKE_YUV_601_Y(r, g, b) \
675         ((( 66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) + 16)
676 #define MAKE_YUV_601_U(r, g, b) \
677         (((-38 * (r) -  74 * (g) + 112 * (b) + 128) >> 8) + 128)
678 #define MAKE_YUV_601_V(r, g, b) \
679         (((112 * (r) -  94 * (g) -  18 * (b) + 128) >> 8) + 128)
681 #define MAKE_YUV_601(r, g, b) \
682         { .y = MAKE_YUV_601_Y(r, g, b), \
683           .u = MAKE_YUV_601_U(r, g, b), \
684           .v = MAKE_YUV_601_V(r, g, b) }
686 #define MAKE_RGBA(rgb, r, g, b, a) \
687         ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
688          (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
689          (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
690          (((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
692 static void
693 fill_smpte_yuv_planar(const struct yuv_info *yuv,
694                       unsigned char *y_mem, unsigned char *u_mem,
695                       unsigned char *v_mem, unsigned int width,
696                       unsigned int height, unsigned int stride)
698         const struct color_yuv colors_top[] = {
699                 MAKE_YUV_601(191, 192, 192),    /* grey */
700                 MAKE_YUV_601(192, 192, 0),      /* yellow */
701                 MAKE_YUV_601(0, 192, 192),      /* cyan */
702                 MAKE_YUV_601(0, 192, 0),        /* green */
703                 MAKE_YUV_601(192, 0, 192),      /* magenta */
704                 MAKE_YUV_601(192, 0, 0),        /* red */
705                 MAKE_YUV_601(0, 0, 192),        /* blue */
706         };
707         const struct color_yuv colors_middle[] = {
708                 MAKE_YUV_601(0, 0, 192),        /* blue */
709                 MAKE_YUV_601(19, 19, 19),       /* black */
710                 MAKE_YUV_601(192, 0, 192),      /* magenta */
711                 MAKE_YUV_601(19, 19, 19),       /* black */
712                 MAKE_YUV_601(0, 192, 192),      /* cyan */
713                 MAKE_YUV_601(19, 19, 19),       /* black */
714                 MAKE_YUV_601(192, 192, 192),    /* grey */
715         };
716         const struct color_yuv colors_bottom[] = {
717                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
718                 MAKE_YUV_601(255, 255, 255),    /* super white */
719                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
720                 MAKE_YUV_601(19, 19, 19),       /* black */
721                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
722                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
723                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
724                 MAKE_YUV_601(19, 19, 19),       /* black */
725         };
726         unsigned int cs = yuv->chroma_stride;
727         unsigned int xsub = yuv->xsub;
728         unsigned int ysub = yuv->ysub;
729         unsigned int x;
730         unsigned int y;
732         /* Luma */
733         for (y = 0; y < height * 6 / 9; ++y) {
734                 for (x = 0; x < width; ++x)
735                         y_mem[x] = colors_top[x * 7 / width].y;
736                 y_mem += stride;
737         }
739         for (; y < height * 7 / 9; ++y) {
740                 for (x = 0; x < width; ++x)
741                         y_mem[x] = colors_middle[x * 7 / width].y;
742                 y_mem += stride;
743         }
745         for (; y < height; ++y) {
746                 for (x = 0; x < width * 5 / 7; ++x)
747                         y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
748                 for (; x < width * 6 / 7; ++x)
749                         y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
750                                                  / (width / 7) + 4].y;
751                 for (; x < width; ++x)
752                         y_mem[x] = colors_bottom[7].y;
753                 y_mem += stride;
754         }
756         /* Chroma */
757         for (y = 0; y < height / ysub * 6 / 9; ++y) {
758                 for (x = 0; x < width; x += xsub) {
759                         u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
760                         v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
761                 }
762                 u_mem += stride * cs / xsub;
763                 v_mem += stride * cs / xsub;
764         }
766         for (; y < height / ysub * 7 / 9; ++y) {
767                 for (x = 0; x < width; x += xsub) {
768                         u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
769                         v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
770                 }
771                 u_mem += stride * cs / xsub;
772                 v_mem += stride * cs / xsub;
773         }
775         for (; y < height / ysub; ++y) {
776                 for (x = 0; x < width * 5 / 7; x += xsub) {
777                         u_mem[x*cs/xsub] =
778                                 colors_bottom[x * 4 / (width * 5 / 7)].u;
779                         v_mem[x*cs/xsub] =
780                                 colors_bottom[x * 4 / (width * 5 / 7)].v;
781                 }
782                 for (; x < width * 6 / 7; x += xsub) {
783                         u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
784                                                          3 / (width / 7) + 4].u;
785                         v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
786                                                          3 / (width / 7) + 4].v;
787                 }
788                 for (; x < width; x += xsub) {
789                         u_mem[x*cs/xsub] = colors_bottom[7].u;
790                         v_mem[x*cs/xsub] = colors_bottom[7].v;
791                 }
792                 u_mem += stride * cs / xsub;
793                 v_mem += stride * cs / xsub;
794         }
797 static void
798 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
799                       unsigned int width, unsigned int height,
800                       unsigned int stride)
802         const struct color_yuv colors_top[] = {
803                 MAKE_YUV_601(191, 192, 192),    /* grey */
804                 MAKE_YUV_601(192, 192, 0),      /* yellow */
805                 MAKE_YUV_601(0, 192, 192),      /* cyan */
806                 MAKE_YUV_601(0, 192, 0),        /* green */
807                 MAKE_YUV_601(192, 0, 192),      /* magenta */
808                 MAKE_YUV_601(192, 0, 0),        /* red */
809                 MAKE_YUV_601(0, 0, 192),        /* blue */
810         };
811         const struct color_yuv colors_middle[] = {
812                 MAKE_YUV_601(0, 0, 192),        /* blue */
813                 MAKE_YUV_601(19, 19, 19),       /* black */
814                 MAKE_YUV_601(192, 0, 192),      /* magenta */
815                 MAKE_YUV_601(19, 19, 19),       /* black */
816                 MAKE_YUV_601(0, 192, 192),      /* cyan */
817                 MAKE_YUV_601(19, 19, 19),       /* black */
818                 MAKE_YUV_601(192, 192, 192),    /* grey */
819         };
820         const struct color_yuv colors_bottom[] = {
821                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
822                 MAKE_YUV_601(255, 255, 255),    /* super white */
823                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
824                 MAKE_YUV_601(19, 19, 19),       /* black */
825                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
826                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
827                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
828                 MAKE_YUV_601(19, 19, 19),       /* black */
829         };
830         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
831         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
832         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
833         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
834         unsigned int x;
835         unsigned int y;
837         /* Luma */
838         for (y = 0; y < height * 6 / 9; ++y) {
839                 for (x = 0; x < width; ++x)
840                         y_mem[2*x] = colors_top[x * 7 / width].y;
841                 y_mem += stride * 2;
842         }
844         for (; y < height * 7 / 9; ++y) {
845                 for (x = 0; x < width; ++x)
846                         y_mem[2*x] = colors_middle[x * 7 / width].y;
847                 y_mem += stride * 2;
848         }
850         for (; y < height; ++y) {
851                 for (x = 0; x < width * 5 / 7; ++x)
852                         y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
853                 for (; x < width * 6 / 7; ++x)
854                         y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
855                                                    / (width / 7) + 4].y;
856                 for (; x < width; ++x)
857                         y_mem[2*x] = colors_bottom[7].y;
858                 y_mem += stride * 2;
859         }
861         /* Chroma */
862         for (y = 0; y < height * 6 / 9; ++y) {
863                 for (x = 0; x < width; x += 2) {
864                         c_mem[2*x+u] = colors_top[x * 7 / width].u;
865                         c_mem[2*x+v] = colors_top[x * 7 / width].v;
866                 }
867                 c_mem += stride * 2;
868         }
870         for (; y < height * 7 / 9; ++y) {
871                 for (x = 0; x < width; x += 2) {
872                         c_mem[2*x+u] = colors_middle[x * 7 / width].u;
873                         c_mem[2*x+v] = colors_middle[x * 7 / width].v;
874                 }
875                 c_mem += stride * 2;
876         }
878         for (; y < height; ++y) {
879                 for (x = 0; x < width * 5 / 7; x += 2) {
880                         c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
881                         c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
882                 }
883                 for (; x < width * 6 / 7; x += 2) {
884                         c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
885                                                      3 / (width / 7) + 4].u;
886                         c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
887                                                      3 / (width / 7) + 4].v;
888                 }
889                 for (; x < width; x += 2) {
890                         c_mem[2*x+u] = colors_bottom[7].u;
891                         c_mem[2*x+v] = colors_bottom[7].v;
892                 }
893                 c_mem += stride * 2;
894         }
897 static void
898 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
899                  unsigned int width, unsigned int height, unsigned int stride)
901         const uint16_t colors_top[] = {
902                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
903                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
904                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
905                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
906                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
907                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
908                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
909         };
910         const uint16_t colors_middle[] = {
911                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
912                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
913                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
914                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
915                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
916                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
917                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
918         };
919         const uint16_t colors_bottom[] = {
920                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
921                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
922                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
923                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
924                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
925                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
926                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
927                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
928         };
929         unsigned int x;
930         unsigned int y;
932         for (y = 0; y < height * 6 / 9; ++y) {
933                 for (x = 0; x < width; ++x)
934                         ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
935                 mem += stride;
936         }
938         for (; y < height * 7 / 9; ++y) {
939                 for (x = 0; x < width; ++x)
940                         ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
941                 mem += stride;
942         }
944         for (; y < height; ++y) {
945                 for (x = 0; x < width * 5 / 7; ++x)
946                         ((uint16_t *)mem)[x] =
947                                 colors_bottom[x * 4 / (width * 5 / 7)];
948                 for (; x < width * 6 / 7; ++x)
949                         ((uint16_t *)mem)[x] =
950                                 colors_bottom[(x - width * 5 / 7) * 3
951                                               / (width / 7) + 4];
952                 for (; x < width; ++x)
953                         ((uint16_t *)mem)[x] = colors_bottom[7];
954                 mem += stride;
955         }
958 static void
959 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
960                  unsigned int width, unsigned int height, unsigned int stride)
962         const uint32_t colors_top[] = {
963                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
964                 MAKE_RGBA(rgb, 192, 192, 0, 255),       /* yellow */
965                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
966                 MAKE_RGBA(rgb, 0, 192, 0, 255),         /* green */
967                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
968                 MAKE_RGBA(rgb, 192, 0, 0, 255),         /* red */
969                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
970         };
971         const uint32_t colors_middle[] = {
972                 MAKE_RGBA(rgb, 0, 0, 192, 255),         /* blue */
973                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
974                 MAKE_RGBA(rgb, 192, 0, 192, 255),       /* magenta */
975                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
976                 MAKE_RGBA(rgb, 0, 192, 192, 255),       /* cyan */
977                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
978                 MAKE_RGBA(rgb, 192, 192, 192, 255),     /* grey */
979         };
980         const uint32_t colors_bottom[] = {
981                 MAKE_RGBA(rgb, 0, 33, 76, 255),         /* in-phase */
982                 MAKE_RGBA(rgb, 255, 255, 255, 255),     /* super white */
983                 MAKE_RGBA(rgb, 50, 0, 106, 255),        /* quadrature */
984                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
985                 MAKE_RGBA(rgb, 9, 9, 9, 255),           /* 3.5% */
986                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* 7.5% */
987                 MAKE_RGBA(rgb, 29, 29, 29, 255),        /* 11.5% */
988                 MAKE_RGBA(rgb, 19, 19, 19, 255),        /* black */
989         };
990         unsigned int x;
991         unsigned int y;
993         for (y = 0; y < height * 6 / 9; ++y) {
994                 for (x = 0; x < width; ++x)
995                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
996                 mem += stride;
997         }
999         for (; y < height * 7 / 9; ++y) {
1000                 for (x = 0; x < width; ++x)
1001                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
1002                 mem += stride;
1003         }
1005         for (; y < height; ++y) {
1006                 for (x = 0; x < width * 5 / 7; ++x)
1007                         ((uint32_t *)mem)[x] =
1008                                 colors_bottom[x * 4 / (width * 5 / 7)];
1009                 for (; x < width * 6 / 7; ++x)
1010                         ((uint32_t *)mem)[x] =
1011                                 colors_bottom[(x - width * 5 / 7) * 3
1012                                               / (width / 7) + 4];
1013                 for (; x < width; ++x)
1014                         ((uint32_t *)mem)[x] = colors_bottom[7];
1015                 mem += stride;
1016         }
1019 static void
1020 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
1021            unsigned int height, unsigned int stride)
1023         unsigned char *u, *v;
1025         switch (info->format) {
1026         case DRM_FORMAT_UYVY:
1027         case DRM_FORMAT_VYUY:
1028         case DRM_FORMAT_YUYV:
1029         case DRM_FORMAT_YVYU:
1030                 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
1031                                              height, stride);
1033         case DRM_FORMAT_NV12:
1034         case DRM_FORMAT_NV21:
1035                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1036                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1037                 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
1038                                              width, height, stride);
1040         case DRM_FORMAT_YVU420:
1041                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
1042                                              planes[2], width, height, stride);
1044         case DRM_FORMAT_ARGB1555:
1045         case DRM_FORMAT_XRGB1555:
1046                 return fill_smpte_rgb16(&info->rgb, planes[0],
1047                                         width, height, stride);
1048         case DRM_FORMAT_XRGB8888:
1049                 return fill_smpte_rgb32(&info->rgb, planes[0],
1050                                         width, height, stride);
1051         }
1054 /* swap these for big endian.. */
1055 #define RED   2
1056 #define GREEN 1
1057 #define BLUE  0
1059 static void
1060 make_pwetty(void *data, int width, int height, int stride)
1062 #ifdef HAVE_CAIRO
1063         cairo_surface_t *surface;
1064         cairo_t *cr;
1065         int x, y;
1067         surface = cairo_image_surface_create_for_data(data,
1068                                                       CAIRO_FORMAT_ARGB32,
1069                                                       width, height,
1070                                                       stride);
1071         cr = cairo_create(surface);
1072         cairo_surface_destroy(surface);
1074         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1075         for (x = 0; x < width; x += 250)
1076                 for (y = 0; y < height; y += 250) {
1077                         char buf[64];
1079                         cairo_move_to(cr, x, y - 20);
1080                         cairo_line_to(cr, x, y + 20);
1081                         cairo_move_to(cr, x - 20, y);
1082                         cairo_line_to(cr, x + 20, y);
1083                         cairo_new_sub_path(cr);
1084                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
1085                         cairo_set_line_width(cr, 4);
1086                         cairo_set_source_rgb(cr, 0, 0, 0);
1087                         cairo_stroke_preserve(cr);
1088                         cairo_set_source_rgb(cr, 1, 1, 1);
1089                         cairo_set_line_width(cr, 2);
1090                         cairo_stroke(cr);
1092                         snprintf(buf, sizeof buf, "%d, %d", x, y);
1093                         cairo_move_to(cr, x + 20, y + 20);
1094                         cairo_text_path(cr, buf);
1095                         cairo_set_source_rgb(cr, 0, 0, 0);
1096                         cairo_stroke_preserve(cr);
1097                         cairo_set_source_rgb(cr, 1, 1, 1);
1098                         cairo_fill(cr);
1099                 }
1101         cairo_destroy(cr);
1102 #endif
1105 static void
1106 fill_tiles_yuv_planar(const struct yuv_info *yuv,
1107                       unsigned char *y_mem, unsigned char *u_mem,
1108                       unsigned char *v_mem, unsigned int width,
1109                       unsigned int height, unsigned int stride)
1111         unsigned int cs = yuv->chroma_stride;
1112         unsigned int i, j;
1114         for (j = 0; j < height; j+=2) {
1115                 unsigned char *y1p = y_mem + j * stride;
1116                 unsigned char *y2p = y1p + stride;
1117                 unsigned char *up = u_mem + (j/2) * stride * cs / 2;
1118                 unsigned char *vp = v_mem + (j/2) * stride * cs / 2;
1120                 for (i = 0; i < width; i+=2) {
1121                         div_t d = div(i+j, width);
1122                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
1123                         unsigned char *rgbp = (unsigned char *)&rgb;
1124                         unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
1126                         *(y2p++) = *(y1p++) = y;
1127                         *(y2p++) = *(y1p++) = y;
1129                         *up = (rgbp[BLUE] - y) * 0.565 + 128;
1130                         *vp = (rgbp[RED] - y) * 0.713 + 128;
1131                         up += cs;
1132                         vp += cs;
1133                 }
1134         }
1137 static void
1138 fill_tiles_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
1139                       unsigned int width, unsigned int height,
1140                       unsigned int stride)
1142         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
1143         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
1144         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
1145         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
1146         unsigned int x;
1147         unsigned int y;
1149         for (y = 0; y < height; ++y) {
1150                 for (x = 0; x < width; x += 2) {
1151                         div_t d = div(x+y, width);
1152                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
1153                                        + 0x000a1120 * (d.rem >> 6);
1154                         struct color_yuv color =
1155                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
1156                                              (rgb32 >> 8) & 0xff, rgb32 & 0xff);
1158                         y_mem[2*x] = color.y;
1159                         c_mem[2*x+u] = color.u;
1160                         y_mem[2*x+2] = color.y;
1161                         c_mem[2*x+v] = color.v;
1162                 }
1164                 y_mem += stride;
1165                 c_mem += stride;
1166         }
1169 static void
1170 fill_tiles_rgb16(const struct rgb_info *rgb, unsigned char *mem,
1171                  unsigned int width, unsigned int height, unsigned int stride)
1173         unsigned int i, j;
1175         for (j = 0; j < height; j++) {
1176                 uint16_t *ptr = (uint16_t*)((char*)mem + j * stride);
1177                 for (i = 0; i < width; i++) {
1178                         div_t d = div(i+j, width);
1179                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
1180                         unsigned char *rgbp = (unsigned char *)&rgb;
1182                         *(ptr++) = 0x8000 |
1183                                         (rgbp[RED] >> 3) << 10 |
1184                                         (rgbp[GREEN] >> 3) << 5 |
1185                                         (rgbp[BLUE] >> 3);
1186                 }
1187         }
1190 static void
1191 fill_tiles_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1192                  unsigned int width, unsigned int height, unsigned int stride)
1194         unsigned int i, j;
1196         for (j = 0; j < height; j++) {
1197                 uint32_t *ptr = (uint32_t*)((char*)mem + j * stride);
1198                 for (i = 0; i < width; i++) {
1199                         div_t d = div(i, width);
1200                         ptr[i] =
1201                                 0x00130502 * (d.quot >> 6) +
1202                                 0x000a1120 * (d.rem >> 6);
1203                 }
1204         }
1206         make_pwetty(mem, width, height, stride);
1209 static void
1210 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
1211            unsigned int height, unsigned int stride)
1213         unsigned char *u, *v;
1215         switch (info->format) {
1216         case DRM_FORMAT_UYVY:
1217         case DRM_FORMAT_VYUY:
1218         case DRM_FORMAT_YUYV:
1219         case DRM_FORMAT_YVYU:
1220                 return fill_tiles_yuv_packed(&info->yuv, planes[0],
1221                                              width, height, stride);
1223         case DRM_FORMAT_NV12:
1224         case DRM_FORMAT_NV21:
1225                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1226                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1227                 return fill_tiles_yuv_planar(&info->yuv, planes[0], u, v,
1228                                              width, height, stride);
1230         case DRM_FORMAT_YVU420:
1231                 return fill_tiles_yuv_planar(&info->yuv, planes[0], planes[1],
1232                                              planes[2], width, height, stride);
1234         case DRM_FORMAT_ARGB1555:
1235         case DRM_FORMAT_XRGB1555:
1236                 return fill_tiles_rgb16(&info->rgb, planes[0],
1237                                         width, height, stride);
1238         case DRM_FORMAT_XRGB8888:
1239                 return fill_tiles_rgb32(&info->rgb, planes[0],
1240                                         width, height, stride);
1241         }
1244 static void
1245 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
1246            unsigned int height, unsigned int stride)
1248         memset(planes[0], 0x77, stride * height);
1251 /*
1252  * fill_pattern - Fill a buffer with a test pattern
1253  * @format: Pixel format
1254  * @pattern: Test pattern
1255  * @buffer: Buffer memory
1256  * @width: Width in pixels
1257  * @height: Height in pixels
1258  * @stride: Line stride (pitch) in bytes
1259  *
1260  * Fill the buffer with the test pattern specified by the pattern parameter.
1261  * Supported formats vary depending on the selected pattern.
1262  */
1263 static void
1264 fill_pattern(unsigned int format, enum fill_pattern pattern,
1265              void *planes[3],
1266              unsigned int width, unsigned int height, unsigned int stride)
1268         const struct format_info *info = NULL;
1269         unsigned int i;
1271         for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
1272                 if (format_info[i].format == format) {
1273                         info = &format_info[i];
1274                         break;
1275                 }
1276         }
1278         if (info == NULL)
1279                 return;
1281         switch (pattern) {
1282         case PATTERN_TILES:
1283                 return fill_tiles(info, planes, width, height, stride);
1285         case PATTERN_SMPTE:
1286                 return fill_smpte(info, planes, width, height, stride);
1288         case PATTERN_PLAIN:
1289                 return fill_plain(info, planes, width, height, stride);
1291         default:
1292                 printf("Error: unsupported test pattern %u.\n", pattern);
1293                 break;
1294         }
1297 /* -----------------------------------------------------------------------------
1298  * Buffers management
1299  */
1301 static struct kms_bo *
1302 allocate_buffer(struct kms_driver *kms,
1303                 int width, int height, int *stride)
1305         struct kms_bo *bo;
1306         unsigned bo_attribs[] = {
1307                 KMS_WIDTH,   0,
1308                 KMS_HEIGHT,  0,
1309                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
1310                 KMS_TERMINATE_PROP_LIST
1311         };
1312         int ret;
1314         bo_attribs[1] = width;
1315         bo_attribs[3] = height;
1317         ret = kms_bo_create(kms, bo_attribs, &bo);
1318         if (ret) {
1319                 fprintf(stderr, "failed to alloc buffer: %s\n",
1320                         strerror(-ret));
1321                 return NULL;
1322         }
1324         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
1325         if (ret) {
1326                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
1327                         strerror(-ret));
1328                 kms_bo_destroy(&bo);
1329                 return NULL;
1330         }
1332         return bo;
1335 static struct kms_bo *
1336 create_test_buffer(struct kms_driver *kms, unsigned int format,
1337                    int width, int height, int handles[4],
1338                    int pitches[4], int offsets[4], enum fill_pattern pattern)
1340         struct kms_bo *bo;
1341         int ret, stride;
1342         void *planes[3];
1343         void *virtual;
1345         bo = allocate_buffer(kms, width, height, &pitches[0]);
1346         if (!bo)
1347                 return NULL;
1349         ret = kms_bo_map(bo, &virtual);
1350         if (ret) {
1351                 fprintf(stderr, "failed to map buffer: %s\n",
1352                         strerror(-ret));
1353                 kms_bo_destroy(&bo);
1354                 return NULL;
1355         }
1357         /* just testing a limited # of formats to test single
1358          * and multi-planar path.. would be nice to add more..
1359          */
1360         switch (format) {
1361         case DRM_FORMAT_UYVY:
1362         case DRM_FORMAT_VYUY:
1363         case DRM_FORMAT_YUYV:
1364         case DRM_FORMAT_YVYU:
1365                 pitches[0] = width * 2;
1366                 offsets[0] = 0;
1367                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1369                 planes[0] = virtual;
1370                 break;
1372         case DRM_FORMAT_NV12:
1373         case DRM_FORMAT_NV21:
1374                 pitches[0] = width;
1375                 offsets[0] = 0;
1376                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1377                 pitches[1] = width;
1378                 offsets[1] = width * height;
1379                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1381                 planes[0] = virtual;
1382                 planes[1] = virtual + offsets[1];
1383                 break;
1385         case DRM_FORMAT_YVU420:
1386                 pitches[0] = width;
1387                 offsets[0] = 0;
1388                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1389                 pitches[1] = width / 2;
1390                 offsets[1] = width * height;
1391                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1392                 pitches[2] = width / 2;
1393                 offsets[2] = offsets[1] + (width * height) / 4;
1394                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
1396                 planes[0] = virtual;
1397                 planes[1] = virtual + offsets[1];
1398                 planes[2] = virtual + offsets[2];
1399                 break;
1401         case DRM_FORMAT_RGB565:
1402         case DRM_FORMAT_ARGB1555:
1403         case DRM_FORMAT_XRGB1555:
1404                 pitches[0] = width * 2;
1405                 offsets[0] = 0;
1406                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1408                 planes[0] = virtual;
1409                 break;
1411         case DRM_FORMAT_RGB888:
1412                 pitches[0] = width * 3;
1413                 offsets[0] = 0;
1414                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1416                 planes[0] = virtual;
1417                 break;
1419         case DRM_FORMAT_XRGB8888:
1420                 pitches[0] = width * 4;
1421                 offsets[0] = 0;
1422                 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1424                 planes[0] = virtual;
1425                 break;
1426         }
1428         fill_pattern(format, pattern, planes, width, height, pitches[0]);
1429         kms_bo_unmap(bo);
1431         return bo;
1434 /* -------------------------------------------------------------------------- */
1436 void
1437 page_flip_handler(int fd, unsigned int frame,
1438                   unsigned int sec, unsigned int usec, void *data)
1440         struct connector *c;
1441         unsigned int new_fb_id;
1442         struct timeval end;
1443         double t;
1445         c = data;
1446         if (c->current_fb_id == c->fb_id[0])
1447                 new_fb_id = c->fb_id[1];
1448         else
1449                 new_fb_id = c->fb_id[0];
1451         drmModePageFlip(fd, c->crtc, new_fb_id,
1452                         DRM_MODE_PAGE_FLIP_EVENT, c);
1453         c->current_fb_id = new_fb_id;
1454         c->swap_count++;
1455         if (c->swap_count == 60) {
1456                 gettimeofday(&end, NULL);
1457                 t = end.tv_sec + end.tv_usec * 1e-6 -
1458                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
1459                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
1460                 c->swap_count = 0;
1461                 c->start = end;
1462         }
1465 static int
1466 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
1468         drmModePlaneRes *plane_resources;
1469         drmModePlane *ovr;
1470         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1471         uint32_t plane_id = 0;
1472         struct kms_bo *plane_bo;
1473         uint32_t plane_flags = 0, format;
1474         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
1475         unsigned int i;
1477         format = format_fourcc(p->format_str);
1478         if (format == 0) {
1479                 fprintf(stderr, "Unknown format: %s\n", p->format_str);
1480                 return -1;
1481         }
1483         /* find an unused plane which can be connected to our crtc */
1484         plane_resources = drmModeGetPlaneResources(fd);
1485         if (!plane_resources) {
1486                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
1487                         strerror(errno));
1488                 return -1;
1489         }
1491         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
1492                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
1493                 if (!ovr) {
1494                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
1495                                 strerror(errno));
1496                         return -1;
1497                 }
1499                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
1500                         plane_id = ovr->plane_id;
1502                 drmModeFreePlane(ovr);
1503         }
1505         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
1506                         p->w, p->h, p->format_str);
1508         if (!plane_id) {
1509                 fprintf(stderr, "failed to find plane!\n");
1510                 return -1;
1511         }
1513         plane_bo = create_test_buffer(kms, format, p->w, p->h, handles,
1514                                       pitches, offsets, PATTERN_TILES);
1515         if (plane_bo == NULL)
1516                 return -1;
1518         /* just use single plane format for now.. */
1519         if (drmModeAddFB2(fd, p->w, p->h, format,
1520                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
1521                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1522                 return -1;
1523         }
1525         /* ok, boring.. but for now put in middle of screen: */
1526         crtc_x = c->mode->hdisplay / 3;
1527         crtc_y = c->mode->vdisplay / 3;
1528         crtc_w = crtc_x;
1529         crtc_h = crtc_y;
1531         /* note src coords (last 4 args) are in Q16 format */
1532         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
1533                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1534                             0, 0, p->w << 16, p->h << 16)) {
1535                 fprintf(stderr, "failed to enable plane: %s\n",
1536                         strerror(errno));
1537                 return -1;
1538         }
1540         return 0;
1543 static void
1544 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
1545                 int page_flip)
1547         struct kms_driver *kms;
1548         struct kms_bo *bo, *other_bo;
1549         unsigned int fb_id, other_fb_id;
1550         int i, j, ret, width, height, x;
1551         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1552         drmEventContext evctx;
1554         width = 0;
1555         height = 0;
1556         for (i = 0; i < count; i++) {
1557                 connector_find_mode(&c[i]);
1558                 if (c[i].mode == NULL)
1559                         continue;
1560                 width += c[i].mode->hdisplay;
1561                 if (height < c[i].mode->vdisplay)
1562                         height = c[i].mode->vdisplay;
1563         }
1565         ret = kms_create(fd, &kms);
1566         if (ret) {
1567                 fprintf(stderr, "failed to create kms driver: %s\n",
1568                         strerror(-ret));
1569                 return;
1570         }
1572         bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1573                                 pitches, offsets, PATTERN_SMPTE);
1574         if (bo == NULL)
1575                 return;
1577         ret = drmModeAddFB(fd, width, height, 24, 32, pitches[0], handles[0], &fb_id);
1578         if (ret) {
1579                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1580                         width, height, strerror(errno));
1581                 return;
1582         }
1584         x = 0;
1585         for (i = 0; i < count; i++) {
1586                 if (c[i].mode == NULL)
1587                         continue;
1589                 printf("setting mode %s on connector %d, crtc %d\n",
1590                        c[i].mode_str, c[i].id, c[i].crtc);
1592                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1593                                      &c[i].id, 1, c[i].mode);
1595                 /* XXX: Actually check if this is needed */
1596                 drmModeDirtyFB(fd, fb_id, NULL, 0);
1598                 x += c[i].mode->hdisplay;
1600                 if (ret) {
1601                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1602                         return;
1603                 }
1605                 /* if we have a plane/overlay to show, set that up now: */
1606                 for (j = 0; j < plane_count; j++)
1607                         if (p[j].con_id == c[i].id)
1608                                 if (set_plane(kms, &c[i], &p[j]))
1609                                         return;
1610         }
1612         if (!page_flip)
1613                 return;
1614         
1615         other_bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1616                                       pitches, offsets, PATTERN_PLAIN);
1617         if (other_bo == NULL)
1618                 return;
1620         ret = drmModeAddFB(fd, width, height, 32, 32, pitches[0], handles[0],
1621                            &other_fb_id);
1622         if (ret) {
1623                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1624                 return;
1625         }
1627         for (i = 0; i < count; i++) {
1628                 if (c[i].mode == NULL)
1629                         continue;
1631                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1632                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1633                 if (ret) {
1634                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1635                         return;
1636                 }
1637                 gettimeofday(&c[i].start, NULL);
1638                 c[i].swap_count = 0;
1639                 c[i].fb_id[0] = fb_id;
1640                 c[i].fb_id[1] = other_fb_id;
1641                 c[i].current_fb_id = other_fb_id;
1642         }
1644         memset(&evctx, 0, sizeof evctx);
1645         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1646         evctx.vblank_handler = NULL;
1647         evctx.page_flip_handler = page_flip_handler;
1648         
1649         while (1) {
1650 #if 0
1651                 struct pollfd pfd[2];
1653                 pfd[0].fd = 0;
1654                 pfd[0].events = POLLIN;
1655                 pfd[1].fd = fd;
1656                 pfd[1].events = POLLIN;
1658                 if (poll(pfd, 2, -1) < 0) {
1659                         fprintf(stderr, "poll error\n");
1660                         break;
1661                 }
1663                 if (pfd[0].revents)
1664                         break;
1665 #else
1666                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1667                 fd_set fds;
1668                 int ret;
1670                 FD_ZERO(&fds);
1671                 FD_SET(0, &fds);
1672                 FD_SET(fd, &fds);
1673                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1675                 if (ret <= 0) {
1676                         fprintf(stderr, "select timed out or error (ret %d)\n",
1677                                 ret);
1678                         continue;
1679                 } else if (FD_ISSET(0, &fds)) {
1680                         break;
1681                 }
1682 #endif
1684                 drmHandleEvent(fd, &evctx);
1685         }
1687         kms_bo_destroy(&bo);
1688         kms_bo_destroy(&other_bo);
1689         kms_destroy(&kms);
1692 extern char *optarg;
1693 extern int optind, opterr, optopt;
1694 static char optstr[] = "ecpmfs:P:v";
1696 void usage(char *name)
1698         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1699         fprintf(stderr, "\t-e\tlist encoders\n");
1700         fprintf(stderr, "\t-c\tlist connectors\n");
1701         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1702         fprintf(stderr, "\t-m\tlist modes\n");
1703         fprintf(stderr, "\t-f\tlist framebuffers\n");
1704         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1705         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1706         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1707         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1708         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1709         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1710         exit(0);
1713 #define dump_resource(res) if (res) dump_##res()
1715 static int page_flipping_supported(void)
1717         /*FIXME: generic ioctl needed? */
1718         return 1;
1719 #if 0
1720         int ret, value;
1721         struct drm_i915_getparam gp;
1723         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1724         gp.value = &value;
1726         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1727         if (ret) {
1728                 fprintf(stderr, "drm_i915_getparam: %m\n");
1729                 return 0;
1730         }
1732         return *gp.value;
1733 #endif
1736 int main(int argc, char **argv)
1738         int c;
1739         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1740         int test_vsync = 0;
1741         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1742         unsigned int i;
1743         int count = 0, plane_count = 0;
1744         struct connector con_args[2];
1745         struct plane plane_args[2] = {0};
1746         
1747         opterr = 0;
1748         while ((c = getopt(argc, argv, optstr)) != -1) {
1749                 switch (c) {
1750                 case 'e':
1751                         encoders = 1;
1752                         break;
1753                 case 'c':
1754                         connectors = 1;
1755                         break;
1756                 case 'p':
1757                         crtcs = 1;
1758                         planes = 1;
1759                         break;
1760                 case 'm':
1761                         modes = 1;
1762                         break;
1763                 case 'f':
1764                         framebuffers = 1;
1765                         break;
1766                 case 'v':
1767                         test_vsync = 1;
1768                         break;
1769                 case 's':
1770                         con_args[count].crtc = -1;
1771                         if (sscanf(optarg, "%d:%64s",
1772                                    &con_args[count].id,
1773                                    con_args[count].mode_str) != 2 &&
1774                             sscanf(optarg, "%d@%d:%64s",
1775                                    &con_args[count].id,
1776                                    &con_args[count].crtc,
1777                                    con_args[count].mode_str) != 3)
1778                                 usage(argv[0]);
1779                         count++;                                      
1780                         break;
1781                 case 'P':
1782                         strcpy(plane_args[plane_count].format_str, "XR24");
1783                         if (sscanf(optarg, "%d:%dx%d@%4s",
1784                                         &plane_args[plane_count].con_id,
1785                                         &plane_args[plane_count].w,
1786                                         &plane_args[plane_count].h,
1787                                         plane_args[plane_count].format_str) != 4 &&
1788                                 sscanf(optarg, "%d:%dx%d",
1789                                         &plane_args[plane_count].con_id,
1790                                         &plane_args[plane_count].w,
1791                                         &plane_args[plane_count].h) != 3)
1792                                 usage(argv[0]);
1793                         plane_count++;
1794                         break;
1795                 default:
1796                         usage(argv[0]);
1797                         break;
1798                 }
1799         }
1801         if (argc == 1)
1802                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1804         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1805                 printf("trying to load module %s...", modules[i]);
1806                 fd = drmOpen(modules[i], NULL);
1807                 if (fd < 0) {
1808                         printf("failed.\n");
1809                 } else {
1810                         printf("success.\n");
1811                         break;
1812                 }
1813         }
1815         if (test_vsync && !page_flipping_supported()) {
1816                 fprintf(stderr, "page flipping not supported by drm.\n");
1817                 return -1;
1818         }
1820         if (i == ARRAY_SIZE(modules)) {
1821                 fprintf(stderr, "failed to load any modules, aborting.\n");
1822                 return -1;
1823         }
1825         resources = drmModeGetResources(fd);
1826         if (!resources) {
1827                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1828                         strerror(errno));
1829                 drmClose(fd);
1830                 return 1;
1831         }
1833         dump_resource(encoders);
1834         dump_resource(connectors);
1835         dump_resource(crtcs);
1836         dump_resource(planes);
1837         dump_resource(framebuffers);
1839         if (count > 0) {
1840                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1841                 getchar();
1842         }
1844         drmModeFreeResources(resources);
1846         return 0;