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 * Mode setting with the kernel interfaces is a bit of a chore.
479 * First you have to find the connector in question and make sure the
480 * requested mode is available.
481 * Then you need to find the encoder attached to that connector so you
482 * can bind it with a free crtc.
483 */
484 struct connector {
485 uint32_t id;
486 char mode_str[64];
487 drmModeModeInfo *mode;
488 drmModeEncoder *encoder;
489 int crtc;
490 int pipe;
491 unsigned int fb_id[2], current_fb_id;
492 struct timeval start;
494 int swap_count;
495 };
497 struct plane {
498 uint32_t con_id; /* the id of connector to bind to */
499 uint32_t w, h;
500 unsigned int fb_id;
501 char format_str[5]; /* need to leave room for terminating \0 */
502 };
504 static void
505 connector_find_mode(struct connector *c)
506 {
507 drmModeConnector *connector;
508 int i, j;
510 /* First, find the connector & mode */
511 c->mode = NULL;
512 for (i = 0; i < resources->count_connectors; i++) {
513 connector = drmModeGetConnector(fd, resources->connectors[i]);
515 if (!connector) {
516 fprintf(stderr, "could not get connector %i: %s\n",
517 resources->connectors[i], strerror(errno));
518 drmModeFreeConnector(connector);
519 continue;
520 }
522 if (!connector->count_modes) {
523 drmModeFreeConnector(connector);
524 continue;
525 }
527 if (connector->connector_id != c->id) {
528 drmModeFreeConnector(connector);
529 continue;
530 }
532 for (j = 0; j < connector->count_modes; j++) {
533 c->mode = &connector->modes[j];
534 if (!strcmp(c->mode->name, c->mode_str))
535 break;
536 }
538 /* Found it, break out */
539 if (c->mode)
540 break;
542 drmModeFreeConnector(connector);
543 }
545 if (!c->mode) {
546 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
547 return;
548 }
550 /* Now get the encoder */
551 for (i = 0; i < resources->count_encoders; i++) {
552 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
554 if (!c->encoder) {
555 fprintf(stderr, "could not get encoder %i: %s\n",
556 resources->encoders[i], strerror(errno));
557 drmModeFreeEncoder(c->encoder);
558 continue;
559 }
561 if (c->encoder->encoder_id == connector->encoder_id)
562 break;
564 drmModeFreeEncoder(c->encoder);
565 }
567 if (c->crtc == -1)
568 c->crtc = c->encoder->crtc_id;
570 /* and figure out which crtc index it is: */
571 for (i = 0; i < resources->count_crtcs; i++) {
572 if (c->crtc == resources->crtcs[i]) {
573 c->pipe = i;
574 break;
575 }
576 }
578 }
580 static struct kms_bo *
581 allocate_buffer(struct kms_driver *kms,
582 int width, int height, int *stride)
583 {
584 struct kms_bo *bo;
585 unsigned bo_attribs[] = {
586 KMS_WIDTH, 0,
587 KMS_HEIGHT, 0,
588 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
589 KMS_TERMINATE_PROP_LIST
590 };
591 int ret;
593 bo_attribs[1] = width;
594 bo_attribs[3] = height;
596 ret = kms_bo_create(kms, bo_attribs, &bo);
597 if (ret) {
598 fprintf(stderr, "failed to alloc buffer: %s\n",
599 strerror(-ret));
600 return NULL;
601 }
603 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
604 if (ret) {
605 fprintf(stderr, "failed to retreive buffer stride: %s\n",
606 strerror(-ret));
607 kms_bo_destroy(&bo);
608 return NULL;
609 }
611 return bo;
612 }
614 static void
615 make_pwetty(void *data, int width, int height, int stride)
616 {
617 #ifdef HAVE_CAIRO
618 cairo_surface_t *surface;
619 cairo_t *cr;
620 int x, y;
622 surface = cairo_image_surface_create_for_data(data,
623 CAIRO_FORMAT_ARGB32,
624 width, height,
625 stride);
626 cr = cairo_create(surface);
627 cairo_surface_destroy(surface);
629 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
630 for (x = 0; x < width; x += 250)
631 for (y = 0; y < height; y += 250) {
632 char buf[64];
634 cairo_move_to(cr, x, y - 20);
635 cairo_line_to(cr, x, y + 20);
636 cairo_move_to(cr, x - 20, y);
637 cairo_line_to(cr, x + 20, y);
638 cairo_new_sub_path(cr);
639 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
640 cairo_set_line_width(cr, 4);
641 cairo_set_source_rgb(cr, 0, 0, 0);
642 cairo_stroke_preserve(cr);
643 cairo_set_source_rgb(cr, 1, 1, 1);
644 cairo_set_line_width(cr, 2);
645 cairo_stroke(cr);
647 snprintf(buf, sizeof buf, "%d, %d", x, y);
648 cairo_move_to(cr, x + 20, y + 20);
649 cairo_text_path(cr, buf);
650 cairo_set_source_rgb(cr, 0, 0, 0);
651 cairo_stroke_preserve(cr);
652 cairo_set_source_rgb(cr, 1, 1, 1);
653 cairo_fill(cr);
654 }
656 cairo_destroy(cr);
657 #endif
658 }
660 static int
661 create_test_buffer(struct kms_driver *kms,
662 int width, int height, int *stride_out,
663 struct kms_bo **bo_out)
664 {
665 struct kms_bo *bo;
666 int ret, i, j, stride;
667 void *virtual;
669 bo = allocate_buffer(kms, width, height, &stride);
670 if (!bo)
671 return -1;
673 ret = kms_bo_map(bo, &virtual);
674 if (ret) {
675 fprintf(stderr, "failed to map buffer: %s\n",
676 strerror(-ret));
677 kms_bo_destroy(&bo);
678 return -1;
679 }
681 /* paint the buffer with colored tiles */
682 for (j = 0; j < height; j++) {
683 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
684 for (i = 0; i < width; i++) {
685 div_t d = div(i, width);
686 fb_ptr[i] =
687 0x00130502 * (d.quot >> 6) +
688 0x000a1120 * (d.rem >> 6);
689 }
690 }
692 make_pwetty(virtual, width, height, stride);
694 kms_bo_unmap(bo);
696 *bo_out = bo;
697 *stride_out = stride;
698 return 0;
699 }
701 static int
702 create_grey_buffer(struct kms_driver *kms,
703 int width, int height, int *stride_out,
704 struct kms_bo **bo_out)
705 {
706 struct kms_bo *bo;
707 int size, ret, stride;
708 void *virtual;
710 bo = allocate_buffer(kms, width, height, &stride);
711 if (!bo)
712 return -1;
714 ret = kms_bo_map(bo, &virtual);
715 if (ret) {
716 fprintf(stderr, "failed to map buffer: %s\n",
717 strerror(-ret));
718 kms_bo_destroy(&bo);
719 return -1;
720 }
722 size = stride * height;
723 memset(virtual, 0x77, size);
724 kms_bo_unmap(bo);
726 *bo_out = bo;
727 *stride_out = stride;
729 return 0;
730 }
732 void
733 page_flip_handler(int fd, unsigned int frame,
734 unsigned int sec, unsigned int usec, void *data)
735 {
736 struct connector *c;
737 unsigned int new_fb_id;
738 struct timeval end;
739 double t;
741 c = data;
742 if (c->current_fb_id == c->fb_id[0])
743 new_fb_id = c->fb_id[1];
744 else
745 new_fb_id = c->fb_id[0];
747 drmModePageFlip(fd, c->crtc, new_fb_id,
748 DRM_MODE_PAGE_FLIP_EVENT, c);
749 c->current_fb_id = new_fb_id;
750 c->swap_count++;
751 if (c->swap_count == 60) {
752 gettimeofday(&end, NULL);
753 t = end.tv_sec + end.tv_usec * 1e-6 -
754 (c->start.tv_sec + c->start.tv_usec * 1e-6);
755 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
756 c->swap_count = 0;
757 c->start = end;
758 }
759 }
761 /* swap these for big endian.. */
762 #define RED 2
763 #define GREEN 1
764 #define BLUE 0
766 static void
767 fill420(unsigned char *y, unsigned char *u, unsigned char *v,
768 int cs /*chroma pixel stride */,
769 int n, int width, int height, int stride)
770 {
771 int i, j;
773 /* paint the buffer with colored tiles, in blocks of 2x2 */
774 for (j = 0; j < height; j+=2) {
775 unsigned char *y1p = y + j * stride;
776 unsigned char *y2p = y1p + stride;
777 unsigned char *up = u + (j/2) * stride * cs / 2;
778 unsigned char *vp = v + (j/2) * stride * cs / 2;
780 for (i = 0; i < width; i+=2) {
781 div_t d = div(n+i+j, width);
782 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
783 unsigned char *rgbp = (unsigned char *)&rgb;
784 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
786 *(y2p++) = *(y1p++) = y;
787 *(y2p++) = *(y1p++) = y;
789 *up = (rgbp[BLUE] - y) * 0.565 + 128;
790 *vp = (rgbp[RED] - y) * 0.713 + 128;
791 up += cs;
792 vp += cs;
793 }
794 }
795 }
797 static void
798 fill422(unsigned char *virtual, int n, int width, int height, int stride)
799 {
800 int i, j;
801 /* paint the buffer with colored tiles */
802 for (j = 0; j < height; j++) {
803 uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride);
804 for (i = 0; i < width; i++) {
805 div_t d = div(n+i+j, width);
806 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
807 unsigned char *rgbp = (unsigned char *)&rgb;
808 unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
810 *(ptr++) = y;
811 *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
812 *(ptr++) = y;
813 *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
814 }
815 }
816 }
818 static void
819 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
820 {
821 int i, j;
822 /* paint the buffer with colored tiles */
823 for (j = 0; j < height; j++) {
824 uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride);
825 for (i = 0; i < width; i++) {
826 div_t d = div(n+i+j, width);
827 uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
828 unsigned char *rgbp = (unsigned char *)&rgb;
830 *(ptr++) = 0x8000 |
831 (rgbp[RED] >> 3) << 10 |
832 (rgbp[GREEN] >> 3) << 5 |
833 (rgbp[BLUE] >> 3);
834 }
835 }
836 }
838 static int
839 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
840 {
841 drmModePlaneRes *plane_resources;
842 drmModePlane *ovr;
843 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
844 uint32_t plane_id = 0;
845 struct kms_bo *plane_bo;
846 uint32_t plane_flags = 0, format;
847 int ret, crtc_x, crtc_y, crtc_w, crtc_h;
848 unsigned int i;
850 /* find an unused plane which can be connected to our crtc */
851 plane_resources = drmModeGetPlaneResources(fd);
852 if (!plane_resources) {
853 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
854 strerror(errno));
855 return -1;
856 }
858 for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
859 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
860 if (!ovr) {
861 fprintf(stderr, "drmModeGetPlane failed: %s\n",
862 strerror(errno));
863 return -1;
864 }
866 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
867 plane_id = ovr->plane_id;
869 drmModeFreePlane(ovr);
870 }
872 fprintf(stderr, "testing %dx%d@%s overlay plane\n",
873 p->w, p->h, p->format_str);
875 if (!plane_id) {
876 fprintf(stderr, "failed to find plane!\n");
877 return -1;
878 }
880 if (!strcmp(p->format_str, "XR24")) {
881 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
882 return -1;
883 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
884 format = DRM_FORMAT_XRGB8888;
885 } else {
886 void *virtual;
888 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
889 * YUV formats, we don't use all of it.. since 4bytes/pixel is
890 * worst case, so live with it for now and just don't use all
891 * the buffer:
892 */
893 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
894 if (!plane_bo)
895 return -1;
897 ret = kms_bo_map(plane_bo, &virtual);
898 if (ret) {
899 fprintf(stderr, "failed to map buffer: %s\n",
900 strerror(-ret));
901 kms_bo_destroy(&plane_bo);
902 return -1;
903 }
905 /* just testing a limited # of formats to test single
906 * and multi-planar path.. would be nice to add more..
907 */
908 if (!strcmp(p->format_str, "YUYV")) {
909 pitches[0] = p->w * 2;
910 offsets[0] = 0;
911 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
913 fill422(virtual, 0, p->w, p->h, pitches[0]);
915 format = DRM_FORMAT_YUYV;
916 } else if (!strcmp(p->format_str, "NV12")) {
917 pitches[0] = p->w;
918 offsets[0] = 0;
919 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
920 pitches[1] = p->w;
921 offsets[1] = p->w * p->h;
922 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
924 fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
925 2, 0, p->w, p->h, pitches[0]);
927 format = DRM_FORMAT_NV12;
928 } else if (!strcmp(p->format_str, "YV12")) {
929 pitches[0] = p->w;
930 offsets[0] = 0;
931 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
932 pitches[1] = p->w / 2;
933 offsets[1] = p->w * p->h;
934 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
935 pitches[2] = p->w / 2;
936 offsets[2] = offsets[1] + (p->w * p->h) / 4;
937 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[2]);
939 fill420(virtual, virtual+offsets[1], virtual+offsets[2],
940 1, 0, p->w, p->h, pitches[0]);
942 format = DRM_FORMAT_YVU420;
943 } else if (!strcmp(p->format_str, "XR15")) {
944 pitches[0] = p->w * 2;
945 offsets[0] = 0;
946 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
948 fill1555(virtual, 0, p->w, p->h, pitches[0]);
950 format = DRM_FORMAT_XRGB1555;
951 } else if (!strcmp(p->format_str, "AR15")) {
952 pitches[0] = p->w * 2;
953 offsets[0] = 0;
954 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
956 fill1555(virtual, 0, p->w, p->h, pitches[0]);
958 format = DRM_FORMAT_ARGB1555;
959 } else {
960 fprintf(stderr, "Unknown format: %s\n", p->format_str);
961 return -1;
962 }
964 kms_bo_unmap(plane_bo);
965 }
967 /* just use single plane format for now.. */
968 if (drmModeAddFB2(fd, p->w, p->h, format,
969 handles, pitches, offsets, &p->fb_id, plane_flags)) {
970 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
971 return -1;
972 }
974 /* ok, boring.. but for now put in middle of screen: */
975 crtc_x = c->mode->hdisplay / 3;
976 crtc_y = c->mode->vdisplay / 3;
977 crtc_w = crtc_x;
978 crtc_h = crtc_y;
980 /* note src coords (last 4 args) are in Q16 format */
981 if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
982 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
983 0, 0, p->w << 16, p->h << 16)) {
984 fprintf(stderr, "failed to enable plane: %s\n",
985 strerror(errno));
986 return -1;
987 }
989 return 0;
990 }
992 static void
993 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
994 int page_flip)
995 {
996 struct kms_driver *kms;
997 struct kms_bo *bo, *other_bo;
998 unsigned int fb_id, other_fb_id;
999 int i, j, ret, width, height, x, stride;
1000 unsigned handle;
1001 drmEventContext evctx;
1003 width = 0;
1004 height = 0;
1005 for (i = 0; i < count; i++) {
1006 connector_find_mode(&c[i]);
1007 if (c[i].mode == NULL)
1008 continue;
1009 width += c[i].mode->hdisplay;
1010 if (height < c[i].mode->vdisplay)
1011 height = c[i].mode->vdisplay;
1012 }
1014 ret = kms_create(fd, &kms);
1015 if (ret) {
1016 fprintf(stderr, "failed to create kms driver: %s\n",
1017 strerror(-ret));
1018 return;
1019 }
1021 if (create_test_buffer(kms, width, height, &stride, &bo))
1022 return;
1024 kms_bo_get_prop(bo, KMS_HANDLE, &handle);
1025 ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
1026 if (ret) {
1027 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1028 width, height, strerror(errno));
1029 return;
1030 }
1032 x = 0;
1033 for (i = 0; i < count; i++) {
1034 if (c[i].mode == NULL)
1035 continue;
1037 printf("setting mode %s on connector %d, crtc %d\n",
1038 c[i].mode_str, c[i].id, c[i].crtc);
1040 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
1041 &c[i].id, 1, c[i].mode);
1043 /* XXX: Actually check if this is needed */
1044 drmModeDirtyFB(fd, fb_id, NULL, 0);
1046 x += c[i].mode->hdisplay;
1048 if (ret) {
1049 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1050 return;
1051 }
1053 /* if we have a plane/overlay to show, set that up now: */
1054 for (j = 0; j < plane_count; j++)
1055 if (p[j].con_id == c[i].id)
1056 if (set_plane(kms, &c[i], &p[j]))
1057 return;
1058 }
1060 if (!page_flip)
1061 return;
1063 if (create_grey_buffer(kms, width, height, &stride, &other_bo))
1064 return;
1066 kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
1067 ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
1068 &other_fb_id);
1069 if (ret) {
1070 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1071 return;
1072 }
1074 for (i = 0; i < count; i++) {
1075 if (c[i].mode == NULL)
1076 continue;
1078 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1079 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1080 if (ret) {
1081 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1082 return;
1083 }
1084 gettimeofday(&c[i].start, NULL);
1085 c[i].swap_count = 0;
1086 c[i].fb_id[0] = fb_id;
1087 c[i].fb_id[1] = other_fb_id;
1088 c[i].current_fb_id = other_fb_id;
1089 }
1091 memset(&evctx, 0, sizeof evctx);
1092 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1093 evctx.vblank_handler = NULL;
1094 evctx.page_flip_handler = page_flip_handler;
1096 while (1) {
1097 #if 0
1098 struct pollfd pfd[2];
1100 pfd[0].fd = 0;
1101 pfd[0].events = POLLIN;
1102 pfd[1].fd = fd;
1103 pfd[1].events = POLLIN;
1105 if (poll(pfd, 2, -1) < 0) {
1106 fprintf(stderr, "poll error\n");
1107 break;
1108 }
1110 if (pfd[0].revents)
1111 break;
1112 #else
1113 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1114 fd_set fds;
1115 int ret;
1117 FD_ZERO(&fds);
1118 FD_SET(0, &fds);
1119 FD_SET(fd, &fds);
1120 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1122 if (ret <= 0) {
1123 fprintf(stderr, "select timed out or error (ret %d)\n",
1124 ret);
1125 continue;
1126 } else if (FD_ISSET(0, &fds)) {
1127 break;
1128 }
1129 #endif
1131 drmHandleEvent(fd, &evctx);
1132 }
1134 kms_bo_destroy(&bo);
1135 kms_bo_destroy(&other_bo);
1136 kms_destroy(&kms);
1137 }
1139 extern char *optarg;
1140 extern int optind, opterr, optopt;
1141 static char optstr[] = "ecpmfs:P:v";
1143 void usage(char *name)
1144 {
1145 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1146 fprintf(stderr, "\t-e\tlist encoders\n");
1147 fprintf(stderr, "\t-c\tlist connectors\n");
1148 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1149 fprintf(stderr, "\t-m\tlist modes\n");
1150 fprintf(stderr, "\t-f\tlist framebuffers\n");
1151 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1152 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1153 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1154 fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1155 fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1156 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1157 exit(0);
1158 }
1160 #define dump_resource(res) if (res) dump_##res()
1162 static int page_flipping_supported(void)
1163 {
1164 /*FIXME: generic ioctl needed? */
1165 return 1;
1166 #if 0
1167 int ret, value;
1168 struct drm_i915_getparam gp;
1170 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1171 gp.value = &value;
1173 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1174 if (ret) {
1175 fprintf(stderr, "drm_i915_getparam: %m\n");
1176 return 0;
1177 }
1179 return *gp.value;
1180 #endif
1181 }
1183 int main(int argc, char **argv)
1184 {
1185 int c;
1186 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1187 int test_vsync = 0;
1188 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1189 unsigned int i;
1190 int count = 0, plane_count = 0;
1191 struct connector con_args[2];
1192 struct plane plane_args[2] = {0};
1194 opterr = 0;
1195 while ((c = getopt(argc, argv, optstr)) != -1) {
1196 switch (c) {
1197 case 'e':
1198 encoders = 1;
1199 break;
1200 case 'c':
1201 connectors = 1;
1202 break;
1203 case 'p':
1204 crtcs = 1;
1205 planes = 1;
1206 break;
1207 case 'm':
1208 modes = 1;
1209 break;
1210 case 'f':
1211 framebuffers = 1;
1212 break;
1213 case 'v':
1214 test_vsync = 1;
1215 break;
1216 case 's':
1217 con_args[count].crtc = -1;
1218 if (sscanf(optarg, "%d:%64s",
1219 &con_args[count].id,
1220 con_args[count].mode_str) != 2 &&
1221 sscanf(optarg, "%d@%d:%64s",
1222 &con_args[count].id,
1223 &con_args[count].crtc,
1224 con_args[count].mode_str) != 3)
1225 usage(argv[0]);
1226 count++;
1227 break;
1228 case 'P':
1229 strcpy(plane_args[plane_count].format_str, "XR24");
1230 if (sscanf(optarg, "%d:%dx%d@%4s",
1231 &plane_args[plane_count].con_id,
1232 &plane_args[plane_count].w,
1233 &plane_args[plane_count].h,
1234 plane_args[plane_count].format_str) != 4 &&
1235 sscanf(optarg, "%d:%dx%d",
1236 &plane_args[plane_count].con_id,
1237 &plane_args[plane_count].w,
1238 &plane_args[plane_count].h) != 3)
1239 usage(argv[0]);
1240 plane_count++;
1241 break;
1242 default:
1243 usage(argv[0]);
1244 break;
1245 }
1246 }
1248 if (argc == 1)
1249 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1251 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1252 printf("trying to load module %s...", modules[i]);
1253 fd = drmOpen(modules[i], NULL);
1254 if (fd < 0) {
1255 printf("failed.\n");
1256 } else {
1257 printf("success.\n");
1258 break;
1259 }
1260 }
1262 if (test_vsync && !page_flipping_supported()) {
1263 fprintf(stderr, "page flipping not supported by drm.\n");
1264 return -1;
1265 }
1267 if (i == ARRAY_SIZE(modules)) {
1268 fprintf(stderr, "failed to load any modules, aborting.\n");
1269 return -1;
1270 }
1272 resources = drmModeGetResources(fd);
1273 if (!resources) {
1274 fprintf(stderr, "drmModeGetResources failed: %s\n",
1275 strerror(errno));
1276 drmClose(fd);
1277 return 1;
1278 }
1280 dump_resource(encoders);
1281 dump_resource(connectors);
1282 dump_resource(crtcs);
1283 dump_resource(planes);
1284 dump_resource(framebuffers);
1286 if (count > 0) {
1287 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1288 getchar();
1289 }
1291 drmModeFreeResources(resources);
1293 return 0;
1294 }