modetest: support plane properties
[platform/upstream/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  */
26
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"
41
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>
52
53 #include "xf86drm.h"
54 #include "xf86drmMode.h"
55 #include "drm_fourcc.h"
56 #include "libkms.h"
57
58 #ifdef HAVE_CAIRO
59 #include <math.h>
60 #include <cairo.h>
61 #endif
62
63 drmModeRes *resources;
64 int fd, modes;
65
66 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
67
68 struct type_name {
69         int type;
70         char *name;
71 };
72
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 }
82
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 };
90
91 type_name_fn(encoder_type)
92
93 struct type_name connector_status_names[] = {
94         { DRM_MODE_CONNECTED, "connected" },
95         { DRM_MODE_DISCONNECTED, "disconnected" },
96         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
97 };
98
99 type_name_fn(connector_status)
100
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 };
118
119 type_name_fn(connector_type)
120
121 void dump_encoders(void)
122 {
123         drmModeEncoder *encoder;
124         int i;
125
126         printf("Encoders:\n");
127         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
128         for (i = 0; i < resources->count_encoders; i++) {
129                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
130
131                 if (!encoder) {
132                         fprintf(stderr, "could not get encoder %i: %s\n",
133                                 resources->encoders[i], strerror(errno));
134                         continue;
135                 }
136                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
137                        encoder->encoder_id,
138                        encoder->crtc_id,
139                        encoder_type_str(encoder->encoder_type),
140                        encoder->possible_crtcs,
141                        encoder->possible_clones);
142                 drmModeFreeEncoder(encoder);
143         }
144         printf("\n");
145 }
146
147 void dump_mode(drmModeModeInfo *mode)
148 {
149         printf("\t%s %d %d %d %d %d %d %d %d %d\n",
150                mode->name,
151                mode->vrefresh,
152                mode->hdisplay,
153                mode->hsync_start,
154                mode->hsync_end,
155                mode->htotal,
156                mode->vdisplay,
157                mode->vsync_start,
158                mode->vsync_end,
159                mode->vtotal);
160 }
161
162 static void
163 dump_blob(uint32_t blob_id)
164 {
165         uint32_t i;
166         unsigned char *blob_data;
167         drmModePropertyBlobPtr blob;
168
169         blob = drmModeGetPropertyBlob(fd, blob_id);
170         if (!blob)
171                 return;
172
173         blob_data = blob->data;
174
175         for (i = 0; i < blob->length; i++) {
176                 if (i % 16 == 0)
177                         printf("\n\t\t\t");
178                 printf("%.2hhx", blob_data[i]);
179         }
180         printf("\n");
181
182         drmModeFreePropertyBlob(blob);
183 }
184
185 static void
186 dump_prop(uint32_t prop_id, uint64_t value)
187 {
188         int i;
189         drmModePropertyPtr prop;
190
191         prop = drmModeGetProperty(fd, prop_id);
192
193         printf("\t%d", prop_id);
194         if (!prop) {
195                 printf("\n");
196                 return;
197         }
198
199         printf(" %s:\n", prop->name);
200
201         printf("\t\tflags:");
202         if (prop->flags & DRM_MODE_PROP_PENDING)
203                 printf(" pending");
204         if (prop->flags & DRM_MODE_PROP_RANGE)
205                 printf(" range");
206         if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
207                 printf(" immutable");
208         if (prop->flags & DRM_MODE_PROP_ENUM)
209                 printf(" enum");
210         if (prop->flags & DRM_MODE_PROP_BITMASK)
211                 printf(" bitmask");
212         if (prop->flags & DRM_MODE_PROP_BLOB)
213                 printf(" blob");
214         printf("\n");
215
216         if (prop->flags & DRM_MODE_PROP_RANGE) {
217                 printf("\t\tvalues:");
218                 for (i = 0; i < prop->count_values; i++)
219                         printf(" %"PRIu64, prop->values[i]);
220                 printf("\n");
221         }
222
223         if (prop->flags & DRM_MODE_PROP_ENUM) {
224                 printf("\t\tenums:");
225                 for (i = 0; i < prop->count_enums; i++)
226                         printf(" %s=%llu", prop->enums[i].name,
227                                prop->enums[i].value);
228                 printf("\n");
229         } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
230                 printf("\t\tvalues:");
231                 for (i = 0; i < prop->count_enums; i++)
232                         printf(" %s=0x%llx", prop->enums[i].name,
233                                (1LL << prop->enums[i].value));
234                 printf("\n");
235         } else {
236                 assert(prop->count_enums == 0);
237         }
238
239         if (prop->flags & DRM_MODE_PROP_BLOB) {
240                 printf("\t\tblobs:\n");
241                 for (i = 0; i < prop->count_blobs; i++)
242                         dump_blob(prop->blob_ids[i]);
243                 printf("\n");
244         } else {
245                 assert(prop->count_blobs == 0);
246         }
247
248         printf("\t\tvalue:");
249         if (prop->flags & DRM_MODE_PROP_BLOB)
250                 dump_blob(value);
251         else
252                 printf(" %"PRIu64"\n", value);
253
254         drmModeFreeProperty(prop);
255 }
256
257 void dump_connectors(void)
258 {
259         drmModeConnector *connector;
260         int i, j;
261
262         printf("Connectors:\n");
263         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
264         for (i = 0; i < resources->count_connectors; i++) {
265                 connector = drmModeGetConnector(fd, resources->connectors[i]);
266
267                 if (!connector) {
268                         fprintf(stderr, "could not get connector %i: %s\n",
269                                 resources->connectors[i], strerror(errno));
270                         continue;
271                 }
272
273                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
274                        connector->connector_id,
275                        connector->encoder_id,
276                        connector_status_str(connector->connection),
277                        connector_type_str(connector->connector_type),
278                        connector->mmWidth, connector->mmHeight,
279                        connector->count_modes);
280
281                 for (j = 0; j < connector->count_encoders; j++)
282                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
283                 printf("\n");
284
285                 if (connector->count_modes) {
286                         printf("  modes:\n");
287                         printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
288                                "vss vse vtot)\n");
289                         for (j = 0; j < connector->count_modes; j++)
290                                 dump_mode(&connector->modes[j]);
291
292                         printf("  props:\n");
293                         for (j = 0; j < connector->count_props; j++)
294                                 dump_prop(connector->props[j],
295                                           connector->prop_values[j]);
296                 }
297
298                 drmModeFreeConnector(connector);
299         }
300         printf("\n");
301 }
302
303 void dump_crtcs(void)
304 {
305         drmModeCrtc *crtc;
306         drmModeObjectPropertiesPtr props;
307         int i;
308         uint32_t j;
309
310         printf("CRTCs:\n");
311         printf("id\tfb\tpos\tsize\n");
312         for (i = 0; i < resources->count_crtcs; i++) {
313                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
314
315                 if (!crtc) {
316                         fprintf(stderr, "could not get crtc %i: %s\n",
317                                 resources->crtcs[i], strerror(errno));
318                         continue;
319                 }
320                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
321                        crtc->crtc_id,
322                        crtc->buffer_id,
323                        crtc->x, crtc->y,
324                        crtc->width, crtc->height);
325                 dump_mode(&crtc->mode);
326
327                 printf("  props:\n");
328                 props = drmModeObjectGetProperties(fd, crtc->crtc_id,
329                                                    DRM_MODE_OBJECT_CRTC);
330                 if (props) {
331                         for (j = 0; j < props->count_props; j++)
332                                 dump_prop(props->props[j],
333                                           props->prop_values[j]);
334                         drmModeFreeObjectProperties(props);
335                 } else {
336                         printf("\tcould not get crtc properties: %s\n",
337                                strerror(errno));
338                 }
339
340                 drmModeFreeCrtc(crtc);
341         }
342         printf("\n");
343 }
344
345 void dump_framebuffers(void)
346 {
347         drmModeFB *fb;
348         int i;
349
350         printf("Frame buffers:\n");
351         printf("id\tsize\tpitch\n");
352         for (i = 0; i < resources->count_fbs; i++) {
353                 fb = drmModeGetFB(fd, resources->fbs[i]);
354
355                 if (!fb) {
356                         fprintf(stderr, "could not get fb %i: %s\n",
357                                 resources->fbs[i], strerror(errno));
358                         continue;
359                 }
360                 printf("%u\t(%ux%u)\t%u\n",
361                        fb->fb_id,
362                        fb->width, fb->height,
363                        fb->pitch);
364
365                 drmModeFreeFB(fb);
366         }
367         printf("\n");
368 }
369
370 static void dump_planes(void)
371 {
372         drmModeObjectPropertiesPtr props;
373         drmModePlaneRes *plane_resources;
374         drmModePlane *ovr;
375         unsigned int i, j;
376
377         plane_resources = drmModeGetPlaneResources(fd);
378         if (!plane_resources) {
379                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
380                         strerror(errno));
381                 return;
382         }
383
384         printf("Planes:\n");
385         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
386         for (i = 0; i < plane_resources->count_planes; i++) {
387                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
388                 if (!ovr) {
389                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
390                                 strerror(errno));
391                         continue;
392                 }
393
394                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
395                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
396                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
397                        ovr->gamma_size);
398
399                 if (!ovr->count_formats)
400                         continue;
401
402                 printf("  formats:");
403                 for (j = 0; j < ovr->count_formats; j++)
404                         printf(" %4.4s", (char *)&ovr->formats[j]);
405                 printf("\n");
406
407                 printf("  props:\n");
408                 props = drmModeObjectGetProperties(fd, ovr->plane_id,
409                                                    DRM_MODE_OBJECT_PLANE);
410                 if (props) {
411                         for (j = 0; j < props->count_props; j++)
412                                 dump_prop(props->props[j],
413                                           props->prop_values[j]);
414                         drmModeFreeObjectProperties(props);
415                 } else {
416                         printf("\tcould not get plane properties: %s\n",
417                                strerror(errno));
418                 }
419
420                 drmModeFreePlane(ovr);
421         }
422         printf("\n");
423
424         drmModeFreePlaneResources(plane_resources);
425         return;
426 }
427
428 /*
429  * Mode setting with the kernel interfaces is a bit of a chore.
430  * First you have to find the connector in question and make sure the
431  * requested mode is available.
432  * Then you need to find the encoder attached to that connector so you
433  * can bind it with a free crtc.
434  */
435 struct connector {
436         uint32_t id;
437         char mode_str[64];
438         drmModeModeInfo *mode;
439         drmModeEncoder *encoder;
440         int crtc;
441         int pipe;
442         unsigned int fb_id[2], current_fb_id;
443         struct timeval start;
444
445         int swap_count;
446 };
447
448 struct plane {
449         uint32_t con_id;  /* the id of connector to bind to */
450         uint32_t w, h;
451         unsigned int fb_id;
452         char format_str[5]; /* need to leave room for terminating \0 */
453 };
454
455 static void
456 connector_find_mode(struct connector *c)
457 {
458         drmModeConnector *connector;
459         int i, j;
460
461         /* First, find the connector & mode */
462         c->mode = NULL;
463         for (i = 0; i < resources->count_connectors; i++) {
464                 connector = drmModeGetConnector(fd, resources->connectors[i]);
465
466                 if (!connector) {
467                         fprintf(stderr, "could not get connector %i: %s\n",
468                                 resources->connectors[i], strerror(errno));
469                         drmModeFreeConnector(connector);
470                         continue;
471                 }
472
473                 if (!connector->count_modes) {
474                         drmModeFreeConnector(connector);
475                         continue;
476                 }
477
478                 if (connector->connector_id != c->id) {
479                         drmModeFreeConnector(connector);
480                         continue;
481                 }
482
483                 for (j = 0; j < connector->count_modes; j++) {
484                         c->mode = &connector->modes[j];
485                         if (!strcmp(c->mode->name, c->mode_str))
486                                 break;
487                 }
488
489                 /* Found it, break out */
490                 if (c->mode)
491                         break;
492
493                 drmModeFreeConnector(connector);
494         }
495
496         if (!c->mode) {
497                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
498                 return;
499         }
500
501         /* Now get the encoder */
502         for (i = 0; i < resources->count_encoders; i++) {
503                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
504
505                 if (!c->encoder) {
506                         fprintf(stderr, "could not get encoder %i: %s\n",
507                                 resources->encoders[i], strerror(errno));
508                         drmModeFreeEncoder(c->encoder);
509                         continue;
510                 }
511
512                 if (c->encoder->encoder_id  == connector->encoder_id)
513                         break;
514
515                 drmModeFreeEncoder(c->encoder);
516         }
517
518         if (c->crtc == -1)
519                 c->crtc = c->encoder->crtc_id;
520
521         /* and figure out which crtc index it is: */
522         for (i = 0; i < resources->count_crtcs; i++) {
523                 if (c->crtc == resources->crtcs[i]) {
524                         c->pipe = i;
525                         break;
526                 }
527         }
528
529 }
530
531 static struct kms_bo *
532 allocate_buffer(struct kms_driver *kms,
533                 int width, int height, int *stride)
534 {
535         struct kms_bo *bo;
536         unsigned bo_attribs[] = {
537                 KMS_WIDTH,   0,
538                 KMS_HEIGHT,  0,
539                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
540                 KMS_TERMINATE_PROP_LIST
541         };
542         int ret;
543
544         bo_attribs[1] = width;
545         bo_attribs[3] = height;
546
547         ret = kms_bo_create(kms, bo_attribs, &bo);
548         if (ret) {
549                 fprintf(stderr, "failed to alloc buffer: %s\n",
550                         strerror(-ret));
551                 return NULL;
552         }
553
554         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
555         if (ret) {
556                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
557                         strerror(-ret));
558                 kms_bo_destroy(&bo);
559                 return NULL;
560         }
561
562         return bo;
563 }
564
565 static void
566 make_pwetty(void *data, int width, int height, int stride)
567 {
568 #ifdef HAVE_CAIRO
569         cairo_surface_t *surface;
570         cairo_t *cr;
571         int x, y;
572
573         surface = cairo_image_surface_create_for_data(data,
574                                                       CAIRO_FORMAT_ARGB32,
575                                                       width, height,
576                                                       stride);
577         cr = cairo_create(surface);
578         cairo_surface_destroy(surface);
579
580         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
581         for (x = 0; x < width; x += 250)
582                 for (y = 0; y < height; y += 250) {
583                         char buf[64];
584
585                         cairo_move_to(cr, x, y - 20);
586                         cairo_line_to(cr, x, y + 20);
587                         cairo_move_to(cr, x - 20, y);
588                         cairo_line_to(cr, x + 20, y);
589                         cairo_new_sub_path(cr);
590                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
591                         cairo_set_line_width(cr, 4);
592                         cairo_set_source_rgb(cr, 0, 0, 0);
593                         cairo_stroke_preserve(cr);
594                         cairo_set_source_rgb(cr, 1, 1, 1);
595                         cairo_set_line_width(cr, 2);
596                         cairo_stroke(cr);
597
598                         snprintf(buf, sizeof buf, "%d, %d", x, y);
599                         cairo_move_to(cr, x + 20, y + 20);
600                         cairo_text_path(cr, buf);
601                         cairo_set_source_rgb(cr, 0, 0, 0);
602                         cairo_stroke_preserve(cr);
603                         cairo_set_source_rgb(cr, 1, 1, 1);
604                         cairo_fill(cr);
605                 }
606
607         cairo_destroy(cr);
608 #endif
609 }
610
611 static int
612 create_test_buffer(struct kms_driver *kms,
613                    int width, int height, int *stride_out,
614                    struct kms_bo **bo_out)
615 {
616         struct kms_bo *bo;
617         int ret, i, j, stride;
618         void *virtual;
619
620         bo = allocate_buffer(kms, width, height, &stride);
621         if (!bo)
622                 return -1;
623
624         ret = kms_bo_map(bo, &virtual);
625         if (ret) {
626                 fprintf(stderr, "failed to map buffer: %s\n",
627                         strerror(-ret));
628                 kms_bo_destroy(&bo);
629                 return -1;
630         }
631
632         /* paint the buffer with colored tiles */
633         for (j = 0; j < height; j++) {
634                 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
635                 for (i = 0; i < width; i++) {
636                         div_t d = div(i, width);
637                         fb_ptr[i] =
638                                 0x00130502 * (d.quot >> 6) +
639                                 0x000a1120 * (d.rem >> 6);
640                 }
641         }
642
643         make_pwetty(virtual, width, height, stride);
644
645         kms_bo_unmap(bo);
646
647         *bo_out = bo;
648         *stride_out = stride;
649         return 0;
650 }
651
652 static int
653 create_grey_buffer(struct kms_driver *kms,
654                    int width, int height, int *stride_out,
655                    struct kms_bo **bo_out)
656 {
657         struct kms_bo *bo;
658         int size, ret, stride;
659         void *virtual;
660
661         bo = allocate_buffer(kms, width, height, &stride);
662         if (!bo)
663                 return -1;
664
665         ret = kms_bo_map(bo, &virtual);
666         if (ret) {
667                 fprintf(stderr, "failed to map buffer: %s\n",
668                         strerror(-ret));
669                 kms_bo_destroy(&bo);
670                 return -1;
671         }
672
673         size = stride * height;
674         memset(virtual, 0x77, size);
675         kms_bo_unmap(bo);
676
677         *bo_out = bo;
678         *stride_out = stride;
679
680         return 0;
681 }
682
683 void
684 page_flip_handler(int fd, unsigned int frame,
685                   unsigned int sec, unsigned int usec, void *data)
686 {
687         struct connector *c;
688         unsigned int new_fb_id;
689         struct timeval end;
690         double t;
691
692         c = data;
693         if (c->current_fb_id == c->fb_id[0])
694                 new_fb_id = c->fb_id[1];
695         else
696                 new_fb_id = c->fb_id[0];
697                         
698         drmModePageFlip(fd, c->crtc, new_fb_id,
699                         DRM_MODE_PAGE_FLIP_EVENT, c);
700         c->current_fb_id = new_fb_id;
701         c->swap_count++;
702         if (c->swap_count == 60) {
703                 gettimeofday(&end, NULL);
704                 t = end.tv_sec + end.tv_usec * 1e-6 -
705                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
706                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
707                 c->swap_count = 0;
708                 c->start = end;
709         }
710 }
711
712 /* swap these for big endian.. */
713 #define RED   2
714 #define GREEN 1
715 #define BLUE  0
716
717 static void
718 fill420(unsigned char *y, unsigned char *u, unsigned char *v,
719                 int cs /*chroma pixel stride */,
720                 int n, int width, int height, int stride)
721 {
722         int i, j;
723
724         /* paint the buffer with colored tiles, in blocks of 2x2 */
725         for (j = 0; j < height; j+=2) {
726                 unsigned char *y1p = y + j * stride;
727                 unsigned char *y2p = y1p + stride;
728                 unsigned char *up = u + (j/2) * stride * cs / 2;
729                 unsigned char *vp = v + (j/2) * stride * cs / 2;
730
731                 for (i = 0; i < width; i+=2) {
732                         div_t d = div(n+i+j, width);
733                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
734                         unsigned char *rgbp = (unsigned char *)&rgb;
735                         unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
736
737                         *(y2p++) = *(y1p++) = y;
738                         *(y2p++) = *(y1p++) = y;
739
740                         *up = (rgbp[BLUE] - y) * 0.565 + 128;
741                         *vp = (rgbp[RED] - y) * 0.713 + 128;
742                         up += cs;
743                         vp += cs;
744                 }
745         }
746 }
747
748 static void
749 fill422(unsigned char *virtual, int n, int width, int height, int stride)
750 {
751         int i, j;
752         /* paint the buffer with colored tiles */
753         for (j = 0; j < height; j++) {
754                 uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride);
755                 for (i = 0; i < width; i++) {
756                         div_t d = div(n+i+j, width);
757                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
758                         unsigned char *rgbp = (unsigned char *)&rgb;
759                         unsigned char y = (0.299 * rgbp[RED]) + (0.587 * rgbp[GREEN]) + (0.114 * rgbp[BLUE]);
760
761                         *(ptr++) = y;
762                         *(ptr++) = (rgbp[BLUE] - y) * 0.565 + 128;
763                         *(ptr++) = y;
764                         *(ptr++) = (rgbp[RED] - y) * 0.713 + 128;
765                 }
766         }
767 }
768
769 static void
770 fill1555(unsigned char *virtual, int n, int width, int height, int stride)
771 {
772         int i, j;
773         /* paint the buffer with colored tiles */
774         for (j = 0; j < height; j++) {
775                 uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride);
776                 for (i = 0; i < width; i++) {
777                         div_t d = div(n+i+j, width);
778                         uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
779                         unsigned char *rgbp = (unsigned char *)&rgb;
780
781                         *(ptr++) = 0x8000 |
782                                         (rgbp[RED] >> 3) << 10 |
783                                         (rgbp[GREEN] >> 3) << 5 |
784                                         (rgbp[BLUE] >> 3);
785                 }
786         }
787 }
788
789 static int
790 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
791 {
792         drmModePlaneRes *plane_resources;
793         drmModePlane *ovr;
794         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
795         uint32_t plane_id = 0;
796         struct kms_bo *plane_bo;
797         uint32_t plane_flags = 0, format;
798         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
799         unsigned int i;
800
801         /* find an unused plane which can be connected to our crtc */
802         plane_resources = drmModeGetPlaneResources(fd);
803         if (!plane_resources) {
804                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
805                         strerror(errno));
806                 return -1;
807         }
808
809         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
810                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
811                 if (!ovr) {
812                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
813                                 strerror(errno));
814                         return -1;
815                 }
816
817                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
818                         plane_id = ovr->plane_id;
819
820                 drmModeFreePlane(ovr);
821         }
822
823         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
824                         p->w, p->h, p->format_str);
825
826         if (!plane_id) {
827                 fprintf(stderr, "failed to find plane!\n");
828                 return -1;
829         }
830
831         if (!strcmp(p->format_str, "XR24")) {
832                 if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
833                         return -1;
834                 kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
835                 format = DRM_FORMAT_XRGB8888;
836         } else {
837                 void *virtual;
838
839                 /* TODO: this always allocates a buffer for 32bpp RGB.. but for
840                  * YUV formats, we don't use all of it..  since 4bytes/pixel is
841                  * worst case, so live with it for now and just don't use all
842                  * the buffer:
843                  */
844                 plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]);
845                 if (!plane_bo)
846                         return -1;
847
848                 ret = kms_bo_map(plane_bo, &virtual);
849                 if (ret) {
850                         fprintf(stderr, "failed to map buffer: %s\n",
851                                 strerror(-ret));
852                         kms_bo_destroy(&plane_bo);
853                         return -1;
854                 }
855
856                 /* just testing a limited # of formats to test single
857                  * and multi-planar path.. would be nice to add more..
858                  */
859                 if (!strcmp(p->format_str, "YUYV")) {
860                         pitches[0] = p->w * 2;
861                         offsets[0] = 0;
862                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
863
864                         fill422(virtual, 0, p->w, p->h, pitches[0]);
865
866                         format = DRM_FORMAT_YUYV;
867                 } else if (!strcmp(p->format_str, "NV12")) {
868                         pitches[0] = p->w;
869                         offsets[0] = 0;
870                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
871                         pitches[1] = p->w;
872                         offsets[1] = p->w * p->h;
873                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
874
875                         fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1,
876                                         2, 0, p->w, p->h, pitches[0]);
877
878                         format = DRM_FORMAT_NV12;
879                 } else if (!strcmp(p->format_str, "YV12")) {
880                         pitches[0] = p->w;
881                         offsets[0] = 0;
882                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
883                         pitches[1] = p->w / 2;
884                         offsets[1] = p->w * p->h;
885                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[1]);
886                         pitches[2] = p->w / 2;
887                         offsets[2] = offsets[1] + (p->w * p->h) / 4;
888                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[2]);
889
890                         fill420(virtual, virtual+offsets[1], virtual+offsets[2],
891                                         1, 0, p->w, p->h, pitches[0]);
892
893                         format = DRM_FORMAT_YVU420;
894                 } else if (!strcmp(p->format_str, "XR15")) {
895                         pitches[0] = p->w * 2;
896                         offsets[0] = 0;
897                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
898
899                         fill1555(virtual, 0, p->w, p->h, pitches[0]);
900
901                         format = DRM_FORMAT_XRGB1555;
902                 } else if (!strcmp(p->format_str, "AR15")) {
903                         pitches[0] = p->w * 2;
904                         offsets[0] = 0;
905                         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
906
907                         fill1555(virtual, 0, p->w, p->h, pitches[0]);
908
909                         format = DRM_FORMAT_ARGB1555;
910                 } else {
911                         fprintf(stderr, "Unknown format: %s\n", p->format_str);
912                         return -1;
913                 }
914
915                 kms_bo_unmap(plane_bo);
916         }
917
918         /* just use single plane format for now.. */
919         if (drmModeAddFB2(fd, p->w, p->h, format,
920                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
921                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
922                 return -1;
923         }
924
925         /* ok, boring.. but for now put in middle of screen: */
926         crtc_x = c->mode->hdisplay / 3;
927         crtc_y = c->mode->vdisplay / 3;
928         crtc_w = crtc_x;
929         crtc_h = crtc_y;
930
931         /* note src coords (last 4 args) are in Q16 format */
932         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
933                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
934                             0, 0, p->w << 16, p->h << 16)) {
935                 fprintf(stderr, "failed to enable plane: %s\n",
936                         strerror(errno));
937                 return -1;
938         }
939
940         return 0;
941 }
942
943 static void
944 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
945                 int page_flip)
946 {
947         struct kms_driver *kms;
948         struct kms_bo *bo, *other_bo;
949         unsigned int fb_id, other_fb_id;
950         int i, j, ret, width, height, x, stride;
951         unsigned handle;
952         drmEventContext evctx;
953
954         width = 0;
955         height = 0;
956         for (i = 0; i < count; i++) {
957                 connector_find_mode(&c[i]);
958                 if (c[i].mode == NULL)
959                         continue;
960                 width += c[i].mode->hdisplay;
961                 if (height < c[i].mode->vdisplay)
962                         height = c[i].mode->vdisplay;
963         }
964
965         ret = kms_create(fd, &kms);
966         if (ret) {
967                 fprintf(stderr, "failed to create kms driver: %s\n",
968                         strerror(-ret));
969                 return;
970         }
971
972         if (create_test_buffer(kms, width, height, &stride, &bo))
973                 return;
974
975         kms_bo_get_prop(bo, KMS_HANDLE, &handle);
976         ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
977         if (ret) {
978                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
979                         width, height, strerror(errno));
980                 return;
981         }
982
983         x = 0;
984         for (i = 0; i < count; i++) {
985                 if (c[i].mode == NULL)
986                         continue;
987
988                 printf("setting mode %s on connector %d, crtc %d\n",
989                        c[i].mode_str, c[i].id, c[i].crtc);
990
991                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
992                                      &c[i].id, 1, c[i].mode);
993
994                 /* XXX: Actually check if this is needed */
995                 drmModeDirtyFB(fd, fb_id, NULL, 0);
996
997                 x += c[i].mode->hdisplay;
998
999                 if (ret) {
1000                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1001                         return;
1002                 }
1003
1004                 /* if we have a plane/overlay to show, set that up now: */
1005                 for (j = 0; j < plane_count; j++)
1006                         if (p[j].con_id == c[i].id)
1007                                 if (set_plane(kms, &c[i], &p[j]))
1008                                         return;
1009         }
1010
1011         if (!page_flip)
1012                 return;
1013         
1014         if (create_grey_buffer(kms, width, height, &stride, &other_bo))
1015                 return;
1016
1017         kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
1018         ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
1019                            &other_fb_id);
1020         if (ret) {
1021                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1022                 return;
1023         }
1024
1025         for (i = 0; i < count; i++) {
1026                 if (c[i].mode == NULL)
1027                         continue;
1028
1029                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
1030                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
1031                 if (ret) {
1032                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1033                         return;
1034                 }
1035                 gettimeofday(&c[i].start, NULL);
1036                 c[i].swap_count = 0;
1037                 c[i].fb_id[0] = fb_id;
1038                 c[i].fb_id[1] = other_fb_id;
1039                 c[i].current_fb_id = other_fb_id;
1040         }
1041
1042         memset(&evctx, 0, sizeof evctx);
1043         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1044         evctx.vblank_handler = NULL;
1045         evctx.page_flip_handler = page_flip_handler;
1046         
1047         while (1) {
1048 #if 0
1049                 struct pollfd pfd[2];
1050
1051                 pfd[0].fd = 0;
1052                 pfd[0].events = POLLIN;
1053                 pfd[1].fd = fd;
1054                 pfd[1].events = POLLIN;
1055
1056                 if (poll(pfd, 2, -1) < 0) {
1057                         fprintf(stderr, "poll error\n");
1058                         break;
1059                 }
1060
1061                 if (pfd[0].revents)
1062                         break;
1063 #else
1064                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1065                 fd_set fds;
1066                 int ret;
1067
1068                 FD_ZERO(&fds);
1069                 FD_SET(0, &fds);
1070                 FD_SET(fd, &fds);
1071                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
1072
1073                 if (ret <= 0) {
1074                         fprintf(stderr, "select timed out or error (ret %d)\n",
1075                                 ret);
1076                         continue;
1077                 } else if (FD_ISSET(0, &fds)) {
1078                         break;
1079                 }
1080 #endif
1081
1082                 drmHandleEvent(fd, &evctx);
1083         }
1084
1085         kms_bo_destroy(&bo);
1086         kms_bo_destroy(&other_bo);
1087         kms_destroy(&kms);
1088 }
1089
1090 extern char *optarg;
1091 extern int optind, opterr, optopt;
1092 static char optstr[] = "ecpmfs:P:v";
1093
1094 void usage(char *name)
1095 {
1096         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
1097         fprintf(stderr, "\t-e\tlist encoders\n");
1098         fprintf(stderr, "\t-c\tlist connectors\n");
1099         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1100         fprintf(stderr, "\t-m\tlist modes\n");
1101         fprintf(stderr, "\t-f\tlist framebuffers\n");
1102         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1103         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
1104         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
1105         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
1106         fprintf(stderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n");
1107         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1108         exit(0);
1109 }
1110
1111 #define dump_resource(res) if (res) dump_##res()
1112
1113 static int page_flipping_supported(void)
1114 {
1115         /*FIXME: generic ioctl needed? */
1116         return 1;
1117 #if 0
1118         int ret, value;
1119         struct drm_i915_getparam gp;
1120
1121         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1122         gp.value = &value;
1123
1124         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1125         if (ret) {
1126                 fprintf(stderr, "drm_i915_getparam: %m\n");
1127                 return 0;
1128         }
1129
1130         return *gp.value;
1131 #endif
1132 }
1133
1134 int main(int argc, char **argv)
1135 {
1136         int c;
1137         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1138         int test_vsync = 0;
1139         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
1140         unsigned int i;
1141         int count = 0, plane_count = 0;
1142         struct connector con_args[2];
1143         struct plane plane_args[2] = {0};
1144         
1145         opterr = 0;
1146         while ((c = getopt(argc, argv, optstr)) != -1) {
1147                 switch (c) {
1148                 case 'e':
1149                         encoders = 1;
1150                         break;
1151                 case 'c':
1152                         connectors = 1;
1153                         break;
1154                 case 'p':
1155                         crtcs = 1;
1156                         planes = 1;
1157                         break;
1158                 case 'm':
1159                         modes = 1;
1160                         break;
1161                 case 'f':
1162                         framebuffers = 1;
1163                         break;
1164                 case 'v':
1165                         test_vsync = 1;
1166                         break;
1167                 case 's':
1168                         con_args[count].crtc = -1;
1169                         if (sscanf(optarg, "%d:%64s",
1170                                    &con_args[count].id,
1171                                    con_args[count].mode_str) != 2 &&
1172                             sscanf(optarg, "%d@%d:%64s",
1173                                    &con_args[count].id,
1174                                    &con_args[count].crtc,
1175                                    con_args[count].mode_str) != 3)
1176                                 usage(argv[0]);
1177                         count++;                                      
1178                         break;
1179                 case 'P':
1180                         strcpy(plane_args[plane_count].format_str, "XR24");
1181                         if (sscanf(optarg, "%d:%dx%d@%4s",
1182                                         &plane_args[plane_count].con_id,
1183                                         &plane_args[plane_count].w,
1184                                         &plane_args[plane_count].h,
1185                                         plane_args[plane_count].format_str) != 4 &&
1186                                 sscanf(optarg, "%d:%dx%d",
1187                                         &plane_args[plane_count].con_id,
1188                                         &plane_args[plane_count].w,
1189                                         &plane_args[plane_count].h) != 3)
1190                                 usage(argv[0]);
1191                         plane_count++;
1192                         break;
1193                 default:
1194                         usage(argv[0]);
1195                         break;
1196                 }
1197         }
1198
1199         if (argc == 1)
1200                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
1201
1202         for (i = 0; i < ARRAY_SIZE(modules); i++) {
1203                 printf("trying to load module %s...", modules[i]);
1204                 fd = drmOpen(modules[i], NULL);
1205                 if (fd < 0) {
1206                         printf("failed.\n");
1207                 } else {
1208                         printf("success.\n");
1209                         break;
1210                 }
1211         }
1212
1213         if (test_vsync && !page_flipping_supported()) {
1214                 fprintf(stderr, "page flipping not supported by drm.\n");
1215                 return -1;
1216         }
1217
1218         if (i == ARRAY_SIZE(modules)) {
1219                 fprintf(stderr, "failed to load any modules, aborting.\n");
1220                 return -1;
1221         }
1222
1223         resources = drmModeGetResources(fd);
1224         if (!resources) {
1225                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1226                         strerror(errno));
1227                 drmClose(fd);
1228                 return 1;
1229         }
1230
1231         dump_resource(encoders);
1232         dump_resource(connectors);
1233         dump_resource(crtcs);
1234         dump_resource(planes);
1235         dump_resource(framebuffers);
1236
1237         if (count > 0) {
1238                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1239                 getchar();
1240         }
1241
1242         drmModeFreeResources(resources);
1243
1244         return 0;
1245 }