df2b97767ba6d4c87853ce389f23eb0bf1460883
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 } \
131 }
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)
165 {
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");
188 }
190 void dump_mode(drmModeModeInfo *mode)
191 {
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");
209 }
211 static void
212 dump_blob(uint32_t blob_id)
213 {
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);
232 }
234 static void
235 dump_prop(uint32_t prop_id, uint64_t value)
236 {
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);
304 }
306 void dump_connectors(void)
307 {
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");
350 }
352 void dump_crtcs(void)
353 {
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");
392 }
394 void dump_framebuffers(void)
395 {
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");
417 }
419 static void dump_planes(void)
420 {
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;
475 }
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)
510 {
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 }
582 }
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 { DRM_FORMAT_YUYV, "YUYV", MAKE_YUV_INFO(YUV_YCbCr | YUV_YC, 2, 2, 2) },
629 { DRM_FORMAT_NV12, "NV12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 2) },
630 { DRM_FORMAT_NV21, "NV21", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 2) },
631 { DRM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
632 { DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
633 { DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
634 { DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
635 };
637 unsigned int format_fourcc(const char *name)
638 {
639 unsigned int i;
640 for (i = 0; i < ARRAY_SIZE(format_info); i++) {
641 if (!strcmp(format_info[i].name, name))
642 return format_info[i].format;
643 }
644 return 0;
645 }
647 /* -----------------------------------------------------------------------------
648 * Test patterns
649 */
651 enum fill_pattern {
652 PATTERN_TILES = 0,
653 PATTERN_PLAIN = 1,
654 PATTERN_SMPTE = 2,
655 };
657 struct color_rgb24 {
658 unsigned int value:24;
659 } __attribute__((__packed__));
661 struct color_yuv {
662 unsigned char y;
663 unsigned char u;
664 unsigned char v;
665 };
667 #define MAKE_YUV_601_Y(r, g, b) \
668 ((( 66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16)
669 #define MAKE_YUV_601_U(r, g, b) \
670 (((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
671 #define MAKE_YUV_601_V(r, g, b) \
672 (((112 * (r) - 94 * (g) - 18 * (b) + 128) >> 8) + 128)
674 #define MAKE_YUV_601(r, g, b) \
675 { .y = MAKE_YUV_601_Y(r, g, b), \
676 .u = MAKE_YUV_601_U(r, g, b), \
677 .v = MAKE_YUV_601_V(r, g, b) }
679 #define MAKE_RGBA(rgb, r, g, b, a) \
680 ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
681 (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
682 (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
683 (((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
685 static void
686 fill_smpte_yuv_planar(const struct yuv_info *yuv,
687 unsigned char *y_mem, unsigned char *u_mem,
688 unsigned char *v_mem, unsigned int width,
689 unsigned int height, unsigned int stride)
690 {
691 const struct color_yuv colors_top[] = {
692 MAKE_YUV_601(191, 192, 192), /* grey */
693 MAKE_YUV_601(192, 192, 0), /* yellow */
694 MAKE_YUV_601(0, 192, 192), /* cyan */
695 MAKE_YUV_601(0, 192, 0), /* green */
696 MAKE_YUV_601(192, 0, 192), /* magenta */
697 MAKE_YUV_601(192, 0, 0), /* red */
698 MAKE_YUV_601(0, 0, 192), /* blue */
699 };
700 const struct color_yuv colors_middle[] = {
701 MAKE_YUV_601(0, 0, 192), /* blue */
702 MAKE_YUV_601(19, 19, 19), /* black */
703 MAKE_YUV_601(192, 0, 192), /* magenta */
704 MAKE_YUV_601(19, 19, 19), /* black */
705 MAKE_YUV_601(0, 192, 192), /* cyan */
706 MAKE_YUV_601(19, 19, 19), /* black */
707 MAKE_YUV_601(192, 192, 192), /* grey */
708 };
709 const struct color_yuv colors_bottom[] = {
710 MAKE_YUV_601(0, 33, 76), /* in-phase */
711 MAKE_YUV_601(255, 255, 255), /* super white */
712 MAKE_YUV_601(50, 0, 106), /* quadrature */
713 MAKE_YUV_601(19, 19, 19), /* black */
714 MAKE_YUV_601(9, 9, 9), /* 3.5% */
715 MAKE_YUV_601(19, 19, 19), /* 7.5% */
716 MAKE_YUV_601(29, 29, 29), /* 11.5% */
717 MAKE_YUV_601(19, 19, 19), /* black */
718 };
719 unsigned int cs = yuv->chroma_stride;
720 unsigned int xsub = yuv->xsub;
721 unsigned int ysub = yuv->ysub;
722 unsigned int x;
723 unsigned int y;
725 /* Luma */
726 for (y = 0; y < height * 6 / 9; ++y) {
727 for (x = 0; x < width; ++x)
728 y_mem[x] = colors_top[x * 7 / width].y;
729 y_mem += stride;
730 }
732 for (; y < height * 7 / 9; ++y) {
733 for (x = 0; x < width; ++x)
734 y_mem[x] = colors_middle[x * 7 / width].y;
735 y_mem += stride;
736 }
738 for (; y < height; ++y) {
739 for (x = 0; x < width * 5 / 7; ++x)
740 y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
741 for (; x < width * 6 / 7; ++x)
742 y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
743 / (width / 7) + 4].y;
744 for (; x < width; ++x)
745 y_mem[x] = colors_bottom[7].y;
746 y_mem += stride;
747 }
749 /* Chroma */
750 for (y = 0; y < height / ysub * 6 / 9; ++y) {
751 for (x = 0; x < width; x += xsub) {
752 u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
753 v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
754 }
755 u_mem += stride * cs / xsub;
756 v_mem += stride * cs / xsub;
757 }
759 for (; y < height / ysub * 7 / 9; ++y) {
760 for (x = 0; x < width; x += xsub) {
761 u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
762 v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
763 }
764 u_mem += stride * cs / xsub;
765 v_mem += stride * cs / xsub;
766 }
768 for (; y < height / ysub; ++y) {
769 for (x = 0; x < width * 5 / 7; x += xsub) {
770 u_mem[x*cs/xsub] =
771 colors_bottom[x * 4 / (width * 5 / 7)].u;
772 v_mem[x*cs/xsub] =
773 colors_bottom[x * 4 / (width * 5 / 7)].v;
774 }
775 for (; x < width * 6 / 7; x += xsub) {
776 u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
777 3 / (width / 7) + 4].u;
778 v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
779 3 / (width / 7) + 4].v;
780 }
781 for (; x < width; x += xsub) {
782 u_mem[x*cs/xsub] = colors_bottom[7].u;
783 v_mem[x*cs/xsub] = colors_bottom[7].v;
784 }
785 u_mem += stride * cs / xsub;
786 v_mem += stride * cs / xsub;
787 }
788 }
790 static void
791 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
792 unsigned int width, unsigned int height,
793 unsigned int stride)
794 {
795 const struct color_yuv colors_top[] = {
796 MAKE_YUV_601(191, 192, 192), /* grey */
797 MAKE_YUV_601(192, 192, 0), /* yellow */
798 MAKE_YUV_601(0, 192, 192), /* cyan */
799 MAKE_YUV_601(0, 192, 0), /* green */
800 MAKE_YUV_601(192, 0, 192), /* magenta */
801 MAKE_YUV_601(192, 0, 0), /* red */
802 MAKE_YUV_601(0, 0, 192), /* blue */
803 };
804 const struct color_yuv colors_middle[] = {
805 MAKE_YUV_601(0, 0, 192), /* blue */
806 MAKE_YUV_601(19, 19, 19), /* black */
807 MAKE_YUV_601(192, 0, 192), /* magenta */
808 MAKE_YUV_601(19, 19, 19), /* black */
809 MAKE_YUV_601(0, 192, 192), /* cyan */
810 MAKE_YUV_601(19, 19, 19), /* black */
811 MAKE_YUV_601(192, 192, 192), /* grey */
812 };
813 const struct color_yuv colors_bottom[] = {
814 MAKE_YUV_601(0, 33, 76), /* in-phase */
815 MAKE_YUV_601(255, 255, 255), /* super white */
816 MAKE_YUV_601(50, 0, 106), /* quadrature */
817 MAKE_YUV_601(19, 19, 19), /* black */
818 MAKE_YUV_601(9, 9, 9), /* 3.5% */
819 MAKE_YUV_601(19, 19, 19), /* 7.5% */
820 MAKE_YUV_601(29, 29, 29), /* 11.5% */
821 MAKE_YUV_601(19, 19, 19), /* black */
822 };
823 unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
824 unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
825 unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
826 unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
827 unsigned int x;
828 unsigned int y;
830 /* Luma */
831 for (y = 0; y < height * 6 / 9; ++y) {
832 for (x = 0; x < width; ++x)
833 y_mem[2*x] = colors_top[x * 7 / width].y;
834 y_mem += stride * 2;
835 }
837 for (; y < height * 7 / 9; ++y) {
838 for (x = 0; x < width; ++x)
839 y_mem[2*x] = colors_middle[x * 7 / width].y;
840 y_mem += stride * 2;
841 }
843 for (; y < height; ++y) {
844 for (x = 0; x < width * 5 / 7; ++x)
845 y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
846 for (; x < width * 6 / 7; ++x)
847 y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
848 / (width / 7) + 4].y;
849 for (; x < width; ++x)
850 y_mem[2*x] = colors_bottom[7].y;
851 y_mem += stride * 2;
852 }
854 /* Chroma */
855 for (y = 0; y < height * 6 / 9; ++y) {
856 for (x = 0; x < width; x += 2) {
857 c_mem[2*x+u] = colors_top[x * 7 / width].u;
858 c_mem[2*x+v] = colors_top[x * 7 / width].v;
859 }
860 c_mem += stride * 2;
861 }
863 for (; y < height * 7 / 9; ++y) {
864 for (x = 0; x < width; x += 2) {
865 c_mem[2*x+u] = colors_middle[x * 7 / width].u;
866 c_mem[2*x+v] = colors_middle[x * 7 / width].v;
867 }
868 c_mem += stride * 2;
869 }
871 for (; y < height; ++y) {
872 for (x = 0; x < width * 5 / 7; x += 2) {
873 c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
874 c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
875 }
876 for (; x < width * 6 / 7; x += 2) {
877 c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
878 3 / (width / 7) + 4].u;
879 c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
880 3 / (width / 7) + 4].v;
881 }
882 for (; x < width; x += 2) {
883 c_mem[2*x+u] = colors_bottom[7].u;
884 c_mem[2*x+v] = colors_bottom[7].v;
885 }
886 c_mem += stride * 2;
887 }
888 }
890 static void
891 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
892 unsigned int width, unsigned int height, unsigned int stride)
893 {
894 const uint16_t colors_top[] = {
895 MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
896 MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
897 MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
898 MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
899 MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
900 MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
901 MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
902 };
903 const uint16_t colors_middle[] = {
904 MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
905 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
906 MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
907 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
908 MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
909 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
910 MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
911 };
912 const uint16_t colors_bottom[] = {
913 MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
914 MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
915 MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
916 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
917 MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
918 MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
919 MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
920 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
921 };
922 unsigned int x;
923 unsigned int y;
925 for (y = 0; y < height * 6 / 9; ++y) {
926 for (x = 0; x < width; ++x)
927 ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
928 mem += stride;
929 }
931 for (; y < height * 7 / 9; ++y) {
932 for (x = 0; x < width; ++x)
933 ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
934 mem += stride;
935 }
937 for (; y < height; ++y) {
938 for (x = 0; x < width * 5 / 7; ++x)
939 ((uint16_t *)mem)[x] =
940 colors_bottom[x * 4 / (width * 5 / 7)];
941 for (; x < width * 6 / 7; ++x)
942 ((uint16_t *)mem)[x] =
943 colors_bottom[(x - width * 5 / 7) * 3
944 / (width / 7) + 4];
945 for (; x < width; ++x)
946 ((uint16_t *)mem)[x] = colors_bottom[7];
947 mem += stride;
948 }
949 }
951 static void
952 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
953 unsigned int width, unsigned int height, unsigned int stride)
954 {
955 const uint32_t colors_top[] = {
956 MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
957 MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
958 MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
959 MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
960 MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
961 MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
962 MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
963 };
964 const uint32_t colors_middle[] = {
965 MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
966 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
967 MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
968 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
969 MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
970 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
971 MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
972 };
973 const uint32_t colors_bottom[] = {
974 MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
975 MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
976 MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
977 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
978 MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
979 MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
980 MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
981 MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
982 };
983 unsigned int x;
984 unsigned int y;
986 for (y = 0; y < height * 6 / 9; ++y) {
987 for (x = 0; x < width; ++x)
988 ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
989 mem += stride;
990 }
992 for (; y < height * 7 / 9; ++y) {
993 for (x = 0; x < width; ++x)
994 ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
995 mem += stride;
996 }
998 for (; y < height; ++y) {
999 for (x = 0; x < width * 5 / 7; ++x)
1000 ((uint32_t *)mem)[x] =
1001 colors_bottom[x * 4 / (width * 5 / 7)];
1002 for (; x < width * 6 / 7; ++x)
1003 ((uint32_t *)mem)[x] =
1004 colors_bottom[(x - width * 5 / 7) * 3
1005 / (width / 7) + 4];
1006 for (; x < width; ++x)
1007 ((uint32_t *)mem)[x] = colors_bottom[7];
1008 mem += stride;
1009 }
1010 }
1012 static void
1013 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
1014 unsigned int height, unsigned int stride)
1015 {
1016 unsigned char *u, *v;
1018 switch (info->format) {
1019 case DRM_FORMAT_YUYV:
1020 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
1021 height, stride);
1023 case DRM_FORMAT_NV12:
1024 case DRM_FORMAT_NV21:
1025 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1026 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1027 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
1028 width, height, stride);
1029 case DRM_FORMAT_YVU420:
1030 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
1031 planes[2], width, height, stride);
1033 case DRM_FORMAT_ARGB1555:
1034 case DRM_FORMAT_XRGB1555:
1035 return fill_smpte_rgb16(&info->rgb, planes[0],
1036 width, height, stride);
1037 case DRM_FORMAT_XRGB8888:
1038 return fill_smpte_rgb32(&info->rgb, planes[0],
1039 width, height, stride);
1040 }
1041 }
1043 /* swap these for big endian.. */
1044 #define RED 2
1045 #define GREEN 1
1046 #define BLUE 0
1048 static void
1049 make_pwetty(void *data, int width, int height, int stride)
1050 {
1051 #ifdef HAVE_CAIRO
1052 cairo_surface_t *surface;
1053 cairo_t *cr;
1054 int x, y;
1056 surface = cairo_image_surface_create_for_data(data,
1057 CAIRO_FORMAT_ARGB32,
1058 width, height,
1059 stride);
1060 cr = cairo_create(surface);
1061 cairo_surface_destroy(surface);
1063 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1064 for (x = 0; x < width; x += 250)
1065 for (y = 0; y < height; y += 250) {
1066 char buf[64];
1068 cairo_move_to(cr, x, y - 20);
1069 cairo_line_to(cr, x, y + 20);
1070 cairo_move_to(cr, x - 20, y);
1071 cairo_line_to(cr, x + 20, y);
1072 cairo_new_sub_path(cr);
1073 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
1074 cairo_set_line_width(cr, 4);
1075 cairo_set_source_rgb(cr, 0, 0, 0);
1076 cairo_stroke_preserve(cr);
1077 cairo_set_source_rgb(cr, 1, 1, 1);
1078 cairo_set_line_width(cr, 2);
1079 cairo_stroke(cr);
1081 snprintf(buf, sizeof buf, "%d, %d", x, y);
1082 cairo_move_to(cr, x + 20, y + 20);
1083 cairo_text_path(cr, buf);
1084 cairo_set_source_rgb(cr, 0, 0, 0);
1085 cairo_stroke_preserve(cr);
1086 cairo_set_source_rgb(cr, 1, 1, 1);
1087 cairo_fill(cr);
1088 }
1090 cairo_destroy(cr);
1091 #endif
1092 }
1094 static void
1095 fill_tiles_yuv_planar(const struct yuv_info *yuv,
1096 unsigned char *y_mem, unsigned char *u_mem,
1097 unsigned char *v_mem, unsigned int width,
1098 unsigned int height, unsigned int stride)
1099 {
1100 unsigned int cs = yuv->chroma_stride;
1101 unsigned int i, j;
1103 for (j = 0; j < height; j+=2) {
1104 unsigned char *y1p = y_mem + j * stride;
1105 unsigned char *y2p = y1p + stride;
1106 unsigned char *up = u_mem + (j/2) * stride * cs / 2;
1107 unsigned char *vp = v_mem + (j/2) * stride * cs / 2;
1109 for (i = 0; i < width; i+=2) {
1110 div_t d = div(i+j, width);
1111 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
1112 unsigned char *rgbp = (unsigned char *)&rgb;
1113 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
1115 *(y2p++) = *(y1p++) = y;
1116 *(y2p++) = *(y1p++) = y;
1118 *up = (rgbp[BLUE] - y) * 0.565 + 128;
1119 *vp = (rgbp[RED] - y) * 0.713 + 128;
1120 up += cs;
1121 vp += cs;
1122 }
1123 }
1124 }
1126 static void
1127 fill_tiles_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
1128 unsigned int width, unsigned int height,
1129 unsigned int stride)
1130 {
1131 unsigned int i, j;
1133 for (j = 0; j < height; j++) {
1134 uint8_t *ptr = (uint8_t*)((char*)mem + j * stride);
1135 for (i = 0; i < width; i++) {
1136 div_t d = div(i+j, width);
1137 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
1138 unsigned char *rgbp = (unsigned char *)&rgb;
1139 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
1141 *(ptr++) = y;
1142 *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
1143 *(ptr++) = y;
1144 *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
1145 }
1146 }
1147 }
1149 static void
1150 fill_tiles_rgb16(const struct rgb_info *rgb, unsigned char *mem,
1151 unsigned int width, unsigned int height, unsigned int stride)
1152 {
1153 unsigned int i, j;
1155 for (j = 0; j < height; j++) {
1156 uint16_t *ptr = (uint16_t*)((char*)mem + j * stride);
1157 for (i = 0; i < width; i++) {
1158 div_t d = div(i+j, width);
1159 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
1160 unsigned char *rgbp = (unsigned char *)&rgb;
1162 *(ptr++) = 0x8000 |
1163 (rgbp[RED] >> 3) << 10 |
1164 (rgbp[GREEN] >> 3) << 5 |
1165 (rgbp[BLUE] >> 3);
1166 }
1167 }
1168 }
1170 static void
1171 fill_tiles_rgb32(const struct rgb_info *rgb, unsigned char *mem,
1172 unsigned int width, unsigned int height, unsigned int stride)
1173 {
1174 unsigned int i, j;
1176 for (j = 0; j < height; j++) {
1177 uint32_t *ptr = (uint32_t*)((char*)mem + j * stride);
1178 for (i = 0; i < width; i++) {
1179 div_t d = div(i, width);
1180 ptr[i] =
1181 0x00130502 * (d.quot >> 6) +
1182 0x000a1120 * (d.rem >> 6);
1183 }
1184 }
1186 make_pwetty(mem, width, height, stride);
1187 }
1189 static void
1190 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
1191 unsigned int height, unsigned int stride)
1192 {
1193 unsigned char *u, *v;
1195 switch (info->format) {
1196 case DRM_FORMAT_YUYV:
1197 return fill_tiles_yuv_packed(&info->yuv, planes[0],
1198 width, height, stride);
1200 case DRM_FORMAT_NV12:
1201 case DRM_FORMAT_NV21:
1202 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
1203 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
1204 return fill_tiles_yuv_planar(&info->yuv, planes[0], u, v,
1205 width, height, stride);
1206 case DRM_FORMAT_YVU420:
1207 return fill_tiles_yuv_planar(&info->yuv, planes[0], planes[1],
1208 planes[2], width, height, stride);
1210 case DRM_FORMAT_ARGB1555:
1211 case DRM_FORMAT_XRGB1555:
1212 return fill_tiles_rgb16(&info->rgb, planes[0],
1213 width, height, stride);
1214 case DRM_FORMAT_XRGB8888:
1215 return fill_tiles_rgb32(&info->rgb, planes[0],
1216 width, height, stride);
1217 }
1218 }
1220 static void
1221 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
1222 unsigned int height, unsigned int stride)
1223 {
1224 memset(planes[0], 0x77, stride * height);
1225 }
1227 /*
1228 * fill_pattern - Fill a buffer with a test pattern
1229 * @format: Pixel format
1230 * @pattern: Test pattern
1231 * @buffer: Buffer memory
1232 * @width: Width in pixels
1233 * @height: Height in pixels
1234 * @stride: Line stride (pitch) in bytes
1235 *
1236 * Fill the buffer with the test pattern specified by the pattern parameter.
1237 * Supported formats vary depending on the selected pattern.
1238 */
1239 static void
1240 fill_pattern(unsigned int format, enum fill_pattern pattern,
1241 void *planes[3],
1242 unsigned int width, unsigned int height, unsigned int stride)
1243 {
1244 const struct format_info *info = NULL;
1245 unsigned int i;
1247 for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
1248 if (format_info[i].format == format) {
1249 info = &format_info[i];
1250 break;
1251 }
1252 }
1254 if (info == NULL)
1255 return;
1257 switch (pattern) {
1258 case PATTERN_TILES:
1259 return fill_tiles(info, planes, width, height, stride);
1261 case PATTERN_SMPTE:
1262 return fill_smpte(info, planes, width, height, stride);
1264 case PATTERN_PLAIN:
1265 return fill_plain(info, planes, width, height, stride);
1267 default:
1268 printf("Error: unsupported test pattern %u.\n", pattern);
1269 break;
1270 }
1271 }
1273 /* -----------------------------------------------------------------------------
1274 * Buffers management
1275 */
1277 static struct kms_bo *
1278 allocate_buffer(struct kms_driver *kms,
1279 int width, int height, int *stride)
1280 {
1281 struct kms_bo *bo;
1282 unsigned bo_attribs[] = {
1283 KMS_WIDTH, 0,
1284 KMS_HEIGHT, 0,
1285 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
1286 KMS_TERMINATE_PROP_LIST
1287 };
1288 int ret;
1290 bo_attribs[1] = width;
1291 bo_attribs[3] = height;
1293 ret = kms_bo_create(kms, bo_attribs, &bo);
1294 if (ret) {
1295 fprintf(stderr, "failed to alloc buffer: %s\n",
1296 strerror(-ret));
1297 return NULL;
1298 }
1300 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
1301 if (ret) {
1302 fprintf(stderr, "failed to retreive buffer stride: %s\n",
1303 strerror(-ret));
1304 kms_bo_destroy(&bo);
1305 return NULL;
1306 }
1308 return bo;
1309 }
1311 static struct kms_bo *
1312 create_test_buffer(struct kms_driver *kms, unsigned int format,
1313 int width, int height, int handles[4],
1314 int pitches[4], int offsets[4], enum fill_pattern pattern)
1315 {
1316 struct kms_bo *bo;
1317 int ret, stride;
1318 void *planes[3];
1319 void *virtual;
1321 bo = allocate_buffer(kms, width, height, &pitches[0]);
1322 if (!bo)
1323 return NULL;
1325 ret = kms_bo_map(bo, &virtual);
1326 if (ret) {
1327 fprintf(stderr, "failed to map buffer: %s\n",
1328 strerror(-ret));
1329 kms_bo_destroy(&bo);
1330 return NULL;
1331 }
1333 /* just testing a limited # of formats to test single
1334 * and multi-planar path.. would be nice to add more..
1335 */
1336 switch (format) {
1337 case DRM_FORMAT_YUYV:
1338 pitches[0] = width * 2;
1339 offsets[0] = 0;
1340 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1342 planes[0] = virtual;
1343 break;
1345 case DRM_FORMAT_NV12:
1346 case DRM_FORMAT_NV21:
1347 pitches[0] = width;
1348 offsets[0] = 0;
1349 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1350 pitches[1] = width;
1351 offsets[1] = width * height;
1352 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1354 planes[0] = virtual;
1355 planes[1] = virtual + offsets[1];
1356 break;
1358 case DRM_FORMAT_YVU420:
1359 pitches[0] = width;
1360 offsets[0] = 0;
1361 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1362 pitches[1] = width / 2;
1363 offsets[1] = width * height;
1364 kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
1365 pitches[2] = width / 2;
1366 offsets[2] = offsets[1] + (width * height) / 4;
1367 kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
1369 planes[0] = virtual;
1370 planes[1] = virtual + offsets[1];
1371 planes[2] = virtual + offsets[2];
1372 break;
1374 case DRM_FORMAT_RGB565:
1375 case DRM_FORMAT_ARGB1555:
1376 case DRM_FORMAT_XRGB1555:
1377 pitches[0] = width * 2;
1378 offsets[0] = 0;
1379 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1381 planes[0] = virtual;
1382 break;
1384 case DRM_FORMAT_RGB888:
1385 pitches[0] = width * 3;
1386 offsets[0] = 0;
1387 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1389 planes[0] = virtual;
1390 break;
1392 case DRM_FORMAT_XRGB8888:
1393 pitches[0] = width * 4;
1394 offsets[0] = 0;
1395 kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
1397 planes[0] = virtual;
1398 break;
1399 }
1401 fill_pattern(format, pattern, planes, width, height, pitches[0]);
1402 kms_bo_unmap(bo);
1404 return bo;
1405 }
1407 /* -------------------------------------------------------------------------- */
1409 void
1410 page_flip_handler(int fd, unsigned int frame,
1411 unsigned int sec, unsigned int usec, void *data)
1412 {
1413 struct connector *c;
1414 unsigned int new_fb_id;
1415 struct timeval end;
1416 double t;
1418 c = data;
1419 if (c->current_fb_id == c->fb_id[0])
1420 new_fb_id = c->fb_id[1];
1421 else
1422 new_fb_id = c->fb_id[0];
1424 drmModePageFlip(fd, c->crtc, new_fb_id,
1425 DRM_MODE_PAGE_FLIP_EVENT, c);
1426 c->current_fb_id = new_fb_id;
1427 c->swap_count++;
1428 if (c->swap_count == 60) {
1429 gettimeofday(&end, NULL);
1430 t = end.tv_sec + end.tv_usec * 1e-6 -
1431 (c->start.tv_sec + c->start.tv_usec * 1e-6);
1432 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
1433 c->swap_count = 0;
1434 c->start = end;
1435 }
1436 }
1438 static int
1439 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
1440 {
1441 drmModePlaneRes *plane_resources;
1442 drmModePlane *ovr;
1443 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1444 uint32_t plane_id = 0;
1445 struct kms_bo *plane_bo;
1446 uint32_t plane_flags = 0, format;
1447 int ret, crtc_x, crtc_y, crtc_w, crtc_h;
1448 unsigned int i;
1450 format = format_fourcc(p->format_str);
1451 if (format == 0) {
1452 fprintf(stderr, "Unknown format: %s\n", p->format_str);
1453 return -1;
1454 }
1456 /* find an unused plane which can be connected to our crtc */
1457 plane_resources = drmModeGetPlaneResources(fd);
1458 if (!plane_resources) {
1459 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
1460 strerror(errno));
1461 return -1;
1462 }
1464 for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
1465 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
1466 if (!ovr) {
1467 fprintf(stderr, "drmModeGetPlane failed: %s\n",
1468 strerror(errno));
1469 return -1;
1470 }
1472 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
1473 plane_id = ovr->plane_id;
1475 drmModeFreePlane(ovr);
1476 }
1478 fprintf(stderr, "testing %dx%d@%s overlay plane\n",
1479 p->w, p->h, p->format_str);
1481 if (!plane_id) {
1482 fprintf(stderr, "failed to find plane!\n");
1483 return -1;
1484 }
1486 plane_bo = create_test_buffer(kms, format, p->w, p->h, handles,
1487 pitches, offsets, PATTERN_TILES);
1488 if (plane_bo == NULL)
1489 return -1;
1491 /* just use single plane format for now.. */
1492 if (drmModeAddFB2(fd, p->w, p->h, format,
1493 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1494 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1495 return -1;
1496 }
1498 /* ok, boring.. but for now put in middle of screen: */
1499 crtc_x = c->mode->hdisplay / 3;
1500 crtc_y = c->mode->vdisplay / 3;
1501 crtc_w = crtc_x;
1502 crtc_h = crtc_y;
1504 /* note src coords (last 4 args) are in Q16 format */
1505 if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
1506 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1507 0, 0, p->w << 16, p->h << 16)) {
1508 fprintf(stderr, "failed to enable plane: %s\n",
1509 strerror(errno));
1510 return -1;
1511 }
1513 return 0;
1514 }
1516 static void
1517 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
1518 int page_flip)
1519 {
1520 struct kms_driver *kms;
1521 struct kms_bo *bo, *other_bo;
1522 unsigned int fb_id, other_fb_id;
1523 int i, j, ret, width, height, x;
1524 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1525 drmEventContext evctx;
1527 width = 0;
1528 height = 0;
1529 for (i = 0; i < count; i++) {
1530 connector_find_mode(&c[i]);
1531 if (c[i].mode == NULL)
1532 continue;
1533 width += c[i].mode->hdisplay;
1534 if (height < c[i].mode->vdisplay)
1535 height = c[i].mode->vdisplay;
1536 }
1538 ret = kms_create(fd, &kms);
1539 if (ret) {
1540 fprintf(stderr, "failed to create kms driver: %s\n",
1541 strerror(-ret));
1542 return;
1543 }
1545 bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1546 pitches, offsets, PATTERN_SMPTE);
1547 if (bo == NULL)
1548 return;
1550 ret = drmModeAddFB(fd, width, height, 24, 32, pitches[0], handles[0], &fb_id);
1551 if (ret) {
1552 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1553 width, height, strerror(errno));
1554 return;
1555 }
1557 x = 0;
1558 for (i = 0; i < count; i++) {
1559 if (c[i].mode == NULL)
1560 continue;
1562 printf("setting mode %s on connector %d, crtc %d\n",
1563 c[i].mode_str, c[i].id, c[i].crtc);
1565 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1566 &c[i].id, 1, c[i].mode);
1568 /* XXX: Actually check if this is needed */
1569 drmModeDirtyFB(fd, fb_id, NULL, 0);
1571 x += c[i].mode->hdisplay;
1573 if (ret) {
1574 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1575 return;
1576 }
1578 /* if we have a plane/overlay to show, set that up now: */
1579 for (j = 0; j < plane_count; j++)
1580 if (p[j].con_id == c[i].id)
1581 if (set_plane(kms, &c[i], &p[j]))
1582 return;
1583 }
1585 if (!page_flip)
1586 return;
1588 other_bo = create_test_buffer(kms, DRM_FORMAT_XRGB8888, width, height, handles,
1589 pitches, offsets, PATTERN_PLAIN);
1590 if (other_bo == NULL)
1591 return;
1593 ret = drmModeAddFB(fd, width, height, 32, 32, pitches[0], handles[0],
1594 &other_fb_id);
1595 if (ret) {
1596 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1597 return;
1598 }
1600 for (i = 0; i < count; i++) {
1601 if (c[i].mode == NULL)
1602 continue;
1604 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1605 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1606 if (ret) {
1607 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1608 return;
1609 }
1610 gettimeofday(&c[i].start, NULL);
1611 c[i].swap_count = 0;
1612 c[i].fb_id[0] = fb_id;
1613 c[i].fb_id[1] = other_fb_id;
1614 c[i].current_fb_id = other_fb_id;
1615 }
1617 memset(&evctx, 0, sizeof evctx);
1618 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1619 evctx.vblank_handler = NULL;
1620 evctx.page_flip_handler = page_flip_handler;
1622 while (1) {
1623 #if 0
1624 struct pollfd pfd[2];
1626 pfd[0].fd = 0;
1627 pfd[0].events = POLLIN;
1628 pfd[1].fd = fd;
1629 pfd[1].events = POLLIN;
1631 if (poll(pfd, 2, -1) < 0) {
1632 fprintf(stderr, "poll error\n");
1633 break;
1634 }
1636 if (pfd[0].revents)
1637 break;
1638 #else
1639 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1640 fd_set fds;
1641 int ret;
1643 FD_ZERO(&fds);
1644 FD_SET(0, &fds);
1645 FD_SET(fd, &fds);
1646 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1648 if (ret <= 0) {
1649 fprintf(stderr, "select timed out or error (ret %d)\n",
1650 ret);
1651 continue;
1652 } else if (FD_ISSET(0, &fds)) {
1653 break;
1654 }
1655 #endif
1657 drmHandleEvent(fd, &evctx);
1658 }
1660 kms_bo_destroy(&bo);
1661 kms_bo_destroy(&other_bo);
1662 kms_destroy(&kms);
1663 }
1665 extern char *optarg;
1666 extern int optind, opterr, optopt;
1667 static char optstr[] = "ecpmfs:P:v";
1669 void usage(char *name)
1670 {
1671 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1672 fprintf(stderr, "\t-e\tlist encoders\n");
1673 fprintf(stderr, "\t-c\tlist connectors\n");
1674 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1675 fprintf(stderr, "\t-m\tlist modes\n");
1676 fprintf(stderr, "\t-f\tlist framebuffers\n");
1677 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1678 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1679 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1680 fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1681 fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1682 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1683 exit(0);
1684 }
1686 #define dump_resource(res) if (res) dump_##res()
1688 static int page_flipping_supported(void)
1689 {
1690 /*FIXME: generic ioctl needed? */
1691 return 1;
1692 #if 0
1693 int ret, value;
1694 struct drm_i915_getparam gp;
1696 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1697 gp.value = &value;
1699 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1700 if (ret) {
1701 fprintf(stderr, "drm_i915_getparam: %m\n");
1702 return 0;
1703 }
1705 return *gp.value;
1706 #endif
1707 }
1709 int main(int argc, char **argv)
1710 {
1711 int c;
1712 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1713 int test_vsync = 0;
1714 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1715 unsigned int i;
1716 int count = 0, plane_count = 0;
1717 struct connector con_args[2];
1718 struct plane plane_args[2] = {0};
1720 opterr = 0;
1721 while ((c = getopt(argc, argv, optstr)) != -1) {
1722 switch (c) {
1723 case 'e':
1724 encoders = 1;
1725 break;
1726 case 'c':
1727 connectors = 1;
1728 break;
1729 case 'p':
1730 crtcs = 1;
1731 planes = 1;
1732 break;
1733 case 'm':
1734 modes = 1;
1735 break;
1736 case 'f':
1737 framebuffers = 1;
1738 break;
1739 case 'v':
1740 test_vsync = 1;
1741 break;
1742 case 's':
1743 con_args[count].crtc = -1;
1744 if (sscanf(optarg, "%d:%64s",
1745 &con_args[count].id,
1746 con_args[count].mode_str) != 2 &&
1747 sscanf(optarg, "%d@%d:%64s",
1748 &con_args[count].id,
1749 &con_args[count].crtc,
1750 con_args[count].mode_str) != 3)
1751 usage(argv[0]);
1752 count++;
1753 break;
1754 case 'P':
1755 strcpy(plane_args[plane_count].format_str, "XR24");
1756 if (sscanf(optarg, "%d:%dx%d@%4s",
1757 &plane_args[plane_count].con_id,
1758 &plane_args[plane_count].w,
1759 &plane_args[plane_count].h,
1760 plane_args[plane_count].format_str) != 4 &&
1761 sscanf(optarg, "%d:%dx%d",
1762 &plane_args[plane_count].con_id,
1763 &plane_args[plane_count].w,
1764 &plane_args[plane_count].h) != 3)
1765 usage(argv[0]);
1766 plane_count++;
1767 break;
1768 default:
1769 usage(argv[0]);
1770 break;
1771 }
1772 }
1774 if (argc == 1)
1775 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1777 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1778 printf("trying to load module %s...", modules[i]);
1779 fd = drmOpen(modules[i], NULL);
1780 if (fd < 0) {
1781 printf("failed.\n");
1782 } else {
1783 printf("success.\n");
1784 break;
1785 }
1786 }
1788 if (test_vsync && !page_flipping_supported()) {
1789 fprintf(stderr, "page flipping not supported by drm.\n");
1790 return -1;
1791 }
1793 if (i == ARRAY_SIZE(modules)) {
1794 fprintf(stderr, "failed to load any modules, aborting.\n");
1795 return -1;
1796 }
1798 resources = drmModeGetResources(fd);
1799 if (!resources) {
1800 fprintf(stderr, "drmModeGetResources failed: %s\n",
1801 strerror(errno));
1802 drmClose(fd);
1803 return 1;
1804 }
1806 dump_resource(encoders);
1807 dump_resource(connectors);
1808 dump_resource(crtcs);
1809 dump_resource(planes);
1810 dump_resource(framebuffers);
1812 if (count > 0) {
1813 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1814 getchar();
1815 }
1817 drmModeFreeResources(resources);
1819 return 0;
1820 }