modeset: Split buffer allocation to a separate file
[glsdk/libdrm.git] / tests / modetest / modetest.c
1 /*
2  * DRM based mode setting test program
3  * Copyright 2008 Tungsten Graphics
4  *   Jakob Bornecrantz <jakob@tungstengraphics.com>
5  * Copyright 2008 Intel Corporation
6  *   Jesse Barnes <jesse.barnes@intel.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
27 /*
28  * This fairly simple test program dumps output in a similar format to the
29  * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
30  * since the kernel separates outputs into encoder and connector structures,
31  * each with their own unique ID.  The program also allows test testing of the
32  * memory management and mode setting APIs by allowing the user to specify a
33  * connector and mode to use for mode setting.  If all works as expected, a
34  * blue background should be painted on the monitor attached to the specified
35  * connector after the selected mode is set.
36  *
37  * TODO: use cairo to write the mode info on the selected output once
38  *       the mode has been programmed, along with possible test patterns.
39  */
40 #include "config.h"
42 #include <assert.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdint.h>
46 #include <inttypes.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <sys/poll.h>
51 #include <sys/time.h>
53 #include "xf86drm.h"
54 #include "xf86drmMode.h"
55 #include "drm_fourcc.h"
56 #include "libkms.h"
58 #include "buffers.h"
60 drmModeRes *resources;
61 int fd, modes;
63 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
65 struct type_name {
66         int type;
67         char *name;
68 };
70 #define type_name_fn(res) \
71 char * res##_str(int type) {                    \
72         unsigned int i;                                 \
73         for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
74                 if (res##_names[i].type == type)        \
75                         return res##_names[i].name;     \
76         }                                               \
77         return "(invalid)";                             \
78 }
80 struct type_name encoder_type_names[] = {
81         { DRM_MODE_ENCODER_NONE, "none" },
82         { DRM_MODE_ENCODER_DAC, "DAC" },
83         { DRM_MODE_ENCODER_TMDS, "TMDS" },
84         { DRM_MODE_ENCODER_LVDS, "LVDS" },
85         { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
86 };
88 type_name_fn(encoder_type)
90 struct type_name connector_status_names[] = {
91         { DRM_MODE_CONNECTED, "connected" },
92         { DRM_MODE_DISCONNECTED, "disconnected" },
93         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
94 };
96 type_name_fn(connector_status)
98 struct type_name connector_type_names[] = {
99         { DRM_MODE_CONNECTOR_Unknown, "unknown" },
100         { DRM_MODE_CONNECTOR_VGA, "VGA" },
101         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
102         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
103         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
104         { DRM_MODE_CONNECTOR_Composite, "composite" },
105         { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
106         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
107         { DRM_MODE_CONNECTOR_Component, "component" },
108         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
109         { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
110         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
111         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
112         { DRM_MODE_CONNECTOR_TV, "TV" },
113         { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
114 };
116 type_name_fn(connector_type)
118 #define bit_name_fn(res)                                        \
119 char * res##_str(int type) {                                    \
120         int i;                                                  \
121         const char *sep = "";                                   \
122         for (i = 0; i < ARRAY_SIZE(res##_names); i++) {         \
123                 if (type & (1 << i)) {                          \
124                         printf("%s%s", sep, res##_names[i]);    \
125                         sep = ", ";                             \
126                 }                                               \
127         }                                                       \
130 static const char *mode_type_names[] = {
131         "builtin",
132         "clock_c",
133         "crtc_c",
134         "preferred",
135         "default",
136         "userdef",
137         "driver",
138 };
140 bit_name_fn(mode_type)
142 static const char *mode_flag_names[] = {
143         "phsync",
144         "nhsync",
145         "pvsync",
146         "nvsync",
147         "interlace",
148         "dblscan",
149         "csync",
150         "pcsync",
151         "ncsync",
152         "hskew",
153         "bcast",
154         "pixmux",
155         "dblclk",
156         "clkdiv2"
157 };
159 bit_name_fn(mode_flag)
161 void dump_encoders(void)
163         drmModeEncoder *encoder;
164         int i;
166         printf("Encoders:\n");
167         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
168         for (i = 0; i < resources->count_encoders; i++) {
169                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
171                 if (!encoder) {
172                         fprintf(stderr, "could not get encoder %i: %s\n",
173                                 resources->encoders[i], strerror(errno));
174                         continue;
175                 }
176                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
177                        encoder->encoder_id,
178                        encoder->crtc_id,
179                        encoder_type_str(encoder->encoder_type),
180                        encoder->possible_crtcs,
181                        encoder->possible_clones);
182                 drmModeFreeEncoder(encoder);
183         }
184         printf("\n");
187 void dump_mode(drmModeModeInfo *mode)
189         printf("  %s %d %d %d %d %d %d %d %d %d",
190                mode->name,
191                mode->vrefresh,
192                mode->hdisplay,
193                mode->hsync_start,
194                mode->hsync_end,
195                mode->htotal,
196                mode->vdisplay,
197                mode->vsync_start,
198                mode->vsync_end,
199                mode->vtotal);
201         printf(" flags: ");
202         mode_flag_str(mode->flags);
203         printf("; type: ");
204         mode_type_str(mode->type);
205         printf("\n");
208 static void
209 dump_blob(uint32_t blob_id)
211         uint32_t i;
212         unsigned char *blob_data;
213         drmModePropertyBlobPtr blob;
215         blob = drmModeGetPropertyBlob(fd, blob_id);
216         if (!blob)
217                 return;
219         blob_data = blob->data;
221         for (i = 0; i < blob->length; i++) {
222                 if (i % 16 == 0)
223                         printf("\n\t\t\t");
224                 printf("%.2hhx", blob_data[i]);
225         }
226         printf("\n");
228         drmModeFreePropertyBlob(blob);
231 static void
232 dump_prop(uint32_t prop_id, uint64_t value)
234         int i;
235         drmModePropertyPtr prop;
237         prop = drmModeGetProperty(fd, prop_id);
239         printf("\t%d", prop_id);
240         if (!prop) {
241                 printf("\n");
242                 return;
243         }
245         printf(" %s:\n", prop->name);
247         printf("\t\tflags:");
248         if (prop->flags & DRM_MODE_PROP_PENDING)
249                 printf(" pending");
250         if (prop->flags & DRM_MODE_PROP_RANGE)
251                 printf(" range");
252         if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
253                 printf(" immutable");
254         if (prop->flags & DRM_MODE_PROP_ENUM)
255                 printf(" enum");
256         if (prop->flags & DRM_MODE_PROP_BITMASK)
257                 printf(" bitmask");
258         if (prop->flags & DRM_MODE_PROP_BLOB)
259                 printf(" blob");
260         printf("\n");
262         if (prop->flags & DRM_MODE_PROP_RANGE) {
263                 printf("\t\tvalues:");
264                 for (i = 0; i < prop->count_values; i++)
265                         printf(" %"PRIu64, prop->values[i]);
266                 printf("\n");
267         }
269         if (prop->flags & DRM_MODE_PROP_ENUM) {
270                 printf("\t\tenums:");
271                 for (i = 0; i < prop->count_enums; i++)
272                         printf(" %s=%llu", prop->enums[i].name,
273                                prop->enums[i].value);
274                 printf("\n");
275         } else if (prop->flags & DRM_MODE_PROP_BITMASK) {
276                 printf("\t\tvalues:");
277                 for (i = 0; i < prop->count_enums; i++)
278                         printf(" %s=0x%llx", prop->enums[i].name,
279                                (1LL << prop->enums[i].value));
280                 printf("\n");
281         } else {
282                 assert(prop->count_enums == 0);
283         }
285         if (prop->flags & DRM_MODE_PROP_BLOB) {
286                 printf("\t\tblobs:\n");
287                 for (i = 0; i < prop->count_blobs; i++)
288                         dump_blob(prop->blob_ids[i]);
289                 printf("\n");
290         } else {
291                 assert(prop->count_blobs == 0);
292         }
294         printf("\t\tvalue:");
295         if (prop->flags & DRM_MODE_PROP_BLOB)
296                 dump_blob(value);
297         else
298                 printf(" %"PRIu64"\n", value);
300         drmModeFreeProperty(prop);
303 void dump_connectors(void)
305         drmModeConnector *connector;
306         int i, j;
308         printf("Connectors:\n");
309         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
310         for (i = 0; i < resources->count_connectors; i++) {
311                 connector = drmModeGetConnector(fd, resources->connectors[i]);
313                 if (!connector) {
314                         fprintf(stderr, "could not get connector %i: %s\n",
315                                 resources->connectors[i], strerror(errno));
316                         continue;
317                 }
319                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
320                        connector->connector_id,
321                        connector->encoder_id,
322                        connector_status_str(connector->connection),
323                        connector_type_str(connector->connector_type),
324                        connector->mmWidth, connector->mmHeight,
325                        connector->count_modes);
327                 for (j = 0; j < connector->count_encoders; j++)
328                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
329                 printf("\n");
331                 if (connector->count_modes) {
332                         printf("  modes:\n");
333                         printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
334                                "vss vse vtot)\n");
335                         for (j = 0; j < connector->count_modes; j++)
336                                 dump_mode(&connector->modes[j]);
338                         printf("  props:\n");
339                         for (j = 0; j < connector->count_props; j++)
340                                 dump_prop(connector->props[j],
341                                           connector->prop_values[j]);
342                 }
344                 drmModeFreeConnector(connector);
345         }
346         printf("\n");
349 void dump_crtcs(void)
351         drmModeCrtc *crtc;
352         drmModeObjectPropertiesPtr props;
353         int i;
354         uint32_t j;
356         printf("CRTCs:\n");
357         printf("id\tfb\tpos\tsize\n");
358         for (i = 0; i < resources->count_crtcs; i++) {
359                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
361                 if (!crtc) {
362                         fprintf(stderr, "could not get crtc %i: %s\n",
363                                 resources->crtcs[i], strerror(errno));
364                         continue;
365                 }
366                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
367                        crtc->crtc_id,
368                        crtc->buffer_id,
369                        crtc->x, crtc->y,
370                        crtc->width, crtc->height);
371                 dump_mode(&crtc->mode);
373                 printf("  props:\n");
374                 props = drmModeObjectGetProperties(fd, crtc->crtc_id,
375                                                    DRM_MODE_OBJECT_CRTC);
376                 if (props) {
377                         for (j = 0; j < props->count_props; j++)
378                                 dump_prop(props->props[j],
379                                           props->prop_values[j]);
380                         drmModeFreeObjectProperties(props);
381                 } else {
382                         printf("\tcould not get crtc properties: %s\n",
383                                strerror(errno));
384                 }
386                 drmModeFreeCrtc(crtc);
387         }
388         printf("\n");
391 void dump_framebuffers(void)
393         drmModeFB *fb;
394         int i;
396         printf("Frame buffers:\n");
397         printf("id\tsize\tpitch\n");
398         for (i = 0; i < resources->count_fbs; i++) {
399                 fb = drmModeGetFB(fd, resources->fbs[i]);
401                 if (!fb) {
402                         fprintf(stderr, "could not get fb %i: %s\n",
403                                 resources->fbs[i], strerror(errno));
404                         continue;
405                 }
406                 printf("%u\t(%ux%u)\t%u\n",
407                        fb->fb_id,
408                        fb->width, fb->height,
409                        fb->pitch);
411                 drmModeFreeFB(fb);
412         }
413         printf("\n");
416 static void dump_planes(void)
418         drmModeObjectPropertiesPtr props;
419         drmModePlaneRes *plane_resources;
420         drmModePlane *ovr;
421         unsigned int i, j;
423         plane_resources = drmModeGetPlaneResources(fd);
424         if (!plane_resources) {
425                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
426                         strerror(errno));
427                 return;
428         }
430         printf("Planes:\n");
431         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
432         for (i = 0; i < plane_resources->count_planes; i++) {
433                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
434                 if (!ovr) {
435                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
436                                 strerror(errno));
437                         continue;
438                 }
440                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
441                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
442                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
443                        ovr->gamma_size);
445                 if (!ovr->count_formats)
446                         continue;
448                 printf("  formats:");
449                 for (j = 0; j < ovr->count_formats; j++)
450                         printf(" %4.4s", (char *)&ovr->formats[j]);
451                 printf("\n");
453                 printf("  props:\n");
454                 props = drmModeObjectGetProperties(fd, ovr->plane_id,
455                                                    DRM_MODE_OBJECT_PLANE);
456                 if (props) {
457                         for (j = 0; j < props->count_props; j++)
458                                 dump_prop(props->props[j],
459                                           props->prop_values[j]);
460                         drmModeFreeObjectProperties(props);
461                 } else {
462                         printf("\tcould not get plane properties: %s\n",
463                                strerror(errno));
464                 }
466                 drmModeFreePlane(ovr);
467         }
468         printf("\n");
470         drmModeFreePlaneResources(plane_resources);
471         return;
474 /* -----------------------------------------------------------------------------
475  * Connectors and planes
476  */
478 /*
479  * Mode setting with the kernel interfaces is a bit of a chore.
480  * First you have to find the connector in question and make sure the
481  * requested mode is available.
482  * Then you need to find the encoder attached to that connector so you
483  * can bind it with a free crtc.
484  */
485 struct connector {
486         uint32_t id;
487         char mode_str[64];
488         char format_str[5];
489         unsigned int fourcc;
490         drmModeModeInfo *mode;
491         drmModeEncoder *encoder;
492         int crtc;
493         int pipe;
494         unsigned int fb_id[2], current_fb_id;
495         struct timeval start;
497         int swap_count;
498 };
500 struct plane {
501         uint32_t con_id;  /* the id of connector to bind to */
502         uint32_t w, h;
503         unsigned int fb_id;
504         char format_str[5]; /* need to leave room for terminating \0 */
505         unsigned int fourcc;
506 };
508 static void
509 connector_find_mode(struct connector *c)
511         drmModeConnector *connector;
512         int i, j;
514         /* First, find the connector & mode */
515         c->mode = NULL;
516         for (i = 0; i < resources->count_connectors; i++) {
517                 connector = drmModeGetConnector(fd, resources->connectors[i]);
519                 if (!connector) {
520                         fprintf(stderr, "could not get connector %i: %s\n",
521                                 resources->connectors[i], strerror(errno));
522                         drmModeFreeConnector(connector);
523                         continue;
524                 }
526                 if (!connector->count_modes) {
527                         drmModeFreeConnector(connector);
528                         continue;
529                 }
531                 if (connector->connector_id != c->id) {
532                         drmModeFreeConnector(connector);
533                         continue;
534                 }
536                 for (j = 0; j < connector->count_modes; j++) {
537                         c->mode = &connector->modes[j];
538                         if (!strcmp(c->mode->name, c->mode_str))
539                                 break;
540                 }
542                 /* Found it, break out */
543                 if (c->mode)
544                         break;
546                 drmModeFreeConnector(connector);
547         }
549         if (!c->mode) {
550                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
551                 return;
552         }
554         /* Now get the encoder */
555         for (i = 0; i < resources->count_encoders; i++) {
556                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
558                 if (!c->encoder) {
559                         fprintf(stderr, "could not get encoder %i: %s\n",
560                                 resources->encoders[i], strerror(errno));
561                         drmModeFreeEncoder(c->encoder);
562                         continue;
563                 }
565                 if (c->encoder->encoder_id  == connector->encoder_id)
566                         break;
568                 drmModeFreeEncoder(c->encoder);
569         }
571         if (c->crtc == -1)
572                 c->crtc = c->encoder->crtc_id;
574         /* and figure out which crtc index it is: */
575         for (i = 0; i < resources->count_crtcs; i++) {
576                 if (c->crtc == resources->crtcs[i]) {
577                         c->pipe = i;
578                         break;
579                 }
580         }
584 /* -------------------------------------------------------------------------- */
586 void
587 page_flip_handler(int fd, unsigned int frame,
588                   unsigned int sec, unsigned int usec, void *data)
590         struct connector *c;
591         unsigned int new_fb_id;
592         struct timeval end;
593         double t;
595         c = data;
596         if (c->current_fb_id == c->fb_id[0])
597                 new_fb_id = c->fb_id[1];
598         else
599                 new_fb_id = c->fb_id[0];
601         drmModePageFlip(fd, c->crtc, new_fb_id,
602                         DRM_MODE_PAGE_FLIP_EVENT, c);
603         c->current_fb_id = new_fb_id;
604         c->swap_count++;
605         if (c->swap_count == 60) {
606                 gettimeofday(&end, NULL);
607                 t = end.tv_sec + end.tv_usec * 1e-6 -
608                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
609                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
610                 c->swap_count = 0;
611                 c->start = end;
612         }
615 static int
616 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
618         drmModePlaneRes *plane_resources;
619         drmModePlane *ovr;
620         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
621         uint32_t plane_id = 0;
622         struct kms_bo *plane_bo;
623         uint32_t plane_flags = 0;
624         int ret, crtc_x, crtc_y, crtc_w, crtc_h;
625         unsigned int i;
627         /* find an unused plane which can be connected to our crtc */
628         plane_resources = drmModeGetPlaneResources(fd);
629         if (!plane_resources) {
630                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
631                         strerror(errno));
632                 return -1;
633         }
635         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
636                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
637                 if (!ovr) {
638                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
639                                 strerror(errno));
640                         return -1;
641                 }
643                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
644                         plane_id = ovr->plane_id;
646                 drmModeFreePlane(ovr);
647         }
649         fprintf(stderr, "testing %dx%d@%s overlay plane\n",
650                         p->w, p->h, p->format_str);
652         if (!plane_id) {
653                 fprintf(stderr, "failed to find plane!\n");
654                 return -1;
655         }
657         plane_bo = create_test_buffer(kms, p->fourcc, p->w, p->h, handles,
658                                       pitches, offsets, PATTERN_TILES);
659         if (plane_bo == NULL)
660                 return -1;
662         /* just use single plane format for now.. */
663         if (drmModeAddFB2(fd, p->w, p->h, p->fourcc,
664                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
665                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
666                 return -1;
667         }
669         /* ok, boring.. but for now put in middle of screen: */
670         crtc_x = c->mode->hdisplay / 3;
671         crtc_y = c->mode->vdisplay / 3;
672         crtc_w = crtc_x;
673         crtc_h = crtc_y;
675         /* note src coords (last 4 args) are in Q16 format */
676         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
677                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
678                             0, 0, p->w << 16, p->h << 16)) {
679                 fprintf(stderr, "failed to enable plane: %s\n",
680                         strerror(errno));
681                 return -1;
682         }
684         return 0;
687 static void
688 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
689                 int page_flip)
691         struct kms_driver *kms;
692         struct kms_bo *bo, *other_bo;
693         unsigned int fb_id, other_fb_id;
694         int i, j, ret, width, height, x;
695         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
696         drmEventContext evctx;
698         width = 0;
699         height = 0;
700         for (i = 0; i < count; i++) {
701                 connector_find_mode(&c[i]);
702                 if (c[i].mode == NULL)
703                         continue;
704                 width += c[i].mode->hdisplay;
705                 if (height < c[i].mode->vdisplay)
706                         height = c[i].mode->vdisplay;
707         }
709         ret = kms_create(fd, &kms);
710         if (ret) {
711                 fprintf(stderr, "failed to create kms driver: %s\n",
712                         strerror(-ret));
713                 return;
714         }
716         bo = create_test_buffer(kms, c->fourcc, width, height, handles,
717                                 pitches, offsets, PATTERN_SMPTE);
718         if (bo == NULL)
719                 return;
721         ret = drmModeAddFB2(fd, width, height, c->fourcc,
722                             handles, pitches, offsets, &fb_id, 0);
723         if (ret) {
724                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
725                         width, height, strerror(errno));
726                 return;
727         }
729         x = 0;
730         for (i = 0; i < count; i++) {
731                 if (c[i].mode == NULL)
732                         continue;
734                 printf("setting mode %s@%s on connector %d, crtc %d\n",
735                        c[i].mode_str, c[i].format_str, c[i].id, c[i].crtc);
737                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
738                                      &c[i].id, 1, c[i].mode);
740                 /* XXX: Actually check if this is needed */
741                 drmModeDirtyFB(fd, fb_id, NULL, 0);
743                 x += c[i].mode->hdisplay;
745                 if (ret) {
746                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
747                         return;
748                 }
750                 /* if we have a plane/overlay to show, set that up now: */
751                 for (j = 0; j < plane_count; j++)
752                         if (p[j].con_id == c[i].id)
753                                 if (set_plane(kms, &c[i], &p[j]))
754                                         return;
755         }
757         if (!page_flip)
758                 return;
759         
760         other_bo = create_test_buffer(kms, c->fourcc, width, height, handles,
761                                       pitches, offsets, PATTERN_PLAIN);
762         if (other_bo == NULL)
763                 return;
765         ret = drmModeAddFB2(fd, width, height, c->fourcc, handles, pitches, offsets,
766                             &other_fb_id, 0);
767         if (ret) {
768                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
769                 return;
770         }
772         for (i = 0; i < count; i++) {
773                 if (c[i].mode == NULL)
774                         continue;
776                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
777                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
778                 if (ret) {
779                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
780                         return;
781                 }
782                 gettimeofday(&c[i].start, NULL);
783                 c[i].swap_count = 0;
784                 c[i].fb_id[0] = fb_id;
785                 c[i].fb_id[1] = other_fb_id;
786                 c[i].current_fb_id = other_fb_id;
787         }
789         memset(&evctx, 0, sizeof evctx);
790         evctx.version = DRM_EVENT_CONTEXT_VERSION;
791         evctx.vblank_handler = NULL;
792         evctx.page_flip_handler = page_flip_handler;
793         
794         while (1) {
795 #if 0
796                 struct pollfd pfd[2];
798                 pfd[0].fd = 0;
799                 pfd[0].events = POLLIN;
800                 pfd[1].fd = fd;
801                 pfd[1].events = POLLIN;
803                 if (poll(pfd, 2, -1) < 0) {
804                         fprintf(stderr, "poll error\n");
805                         break;
806                 }
808                 if (pfd[0].revents)
809                         break;
810 #else
811                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
812                 fd_set fds;
813                 int ret;
815                 FD_ZERO(&fds);
816                 FD_SET(0, &fds);
817                 FD_SET(fd, &fds);
818                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
820                 if (ret <= 0) {
821                         fprintf(stderr, "select timed out or error (ret %d)\n",
822                                 ret);
823                         continue;
824                 } else if (FD_ISSET(0, &fds)) {
825                         break;
826                 }
827 #endif
829                 drmHandleEvent(fd, &evctx);
830         }
832         kms_bo_destroy(&bo);
833         kms_bo_destroy(&other_bo);
834         kms_destroy(&kms);
837 extern char *optarg;
838 extern int optind, opterr, optopt;
839 static char optstr[] = "ecpmfs:P:v";
841 #define min(a, b)       ((a) < (b) ? (a) : (b))
843 static int parse_connector(struct connector *c, const char *arg)
845         unsigned int len;
846         const char *p;
847         char *endp;
849         c->crtc = -1;
850         strcpy(c->format_str, "XR24");
852         c->id = strtoul(arg, &endp, 10);
853         if (*endp == '@') {
854                 arg = endp + 1;
855                 c->crtc = strtoul(arg, &endp, 10);
856         }
857         if (*endp != ':')
858                 return -1;
860         arg = endp + 1;
862         p = strchrnul(arg, '@');
863         len = min(sizeof c->mode_str - 1, p - arg);
864         strncpy(c->mode_str, arg, len);
865         c->mode_str[len] = '\0';
867         if (*p == '@') {
868                 strncpy(c->format_str, p + 1, 4);
869                 c->format_str[4] = '\0';
871                 c->fourcc = format_fourcc(p + 1);
872                 if (c->fourcc == 0)  {
873                         fprintf(stderr, "unknown format %s\n", c->format_str);
874                         return -1;
875                 }
876         }
878         return 0;
881 static int parse_plane(struct plane *p, const char *arg)
883         strcpy(p->format_str, "XR24");
885         if (sscanf(arg, "%d:%dx%d@%4s", &p->con_id, &p->w, &p->h, &p->format_str) != 4 &&
886             sscanf(arg, "%d:%dx%d", &p->con_id, &p->w, &p->h) != 3)
887                 return -1;
889         p->fourcc = format_fourcc(p->format_str);
890         if (p->fourcc == 0) {
891                 fprintf(stderr, "unknown format %s\n", p->format_str);
892                 return -1;
893         }
895         return 0;
898 void usage(char *name)
900         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
901         fprintf(stderr, "\t-e\tlist encoders\n");
902         fprintf(stderr, "\t-c\tlist connectors\n");
903         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
904         fprintf(stderr, "\t-m\tlist modes\n");
905         fprintf(stderr, "\t-f\tlist framebuffers\n");
906         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
907         fprintf(stderr, "\t-s <connector_id>[@<crtc_id>]:<mode>[@<format>]\tset a mode\n");
908         fprintf(stderr, "\t-P <connector_id>:<w>x<h>[@<format>]\tset a plane\n");
909         fprintf(stderr, "\n\tDefault is to dump all info.\n");
910         exit(0);
913 #define dump_resource(res) if (res) dump_##res()
915 static int page_flipping_supported(void)
917         /*FIXME: generic ioctl needed? */
918         return 1;
919 #if 0
920         int ret, value;
921         struct drm_i915_getparam gp;
923         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
924         gp.value = &value;
926         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
927         if (ret) {
928                 fprintf(stderr, "drm_i915_getparam: %m\n");
929                 return 0;
930         }
932         return *gp.value;
933 #endif
936 int main(int argc, char **argv)
938         int c;
939         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
940         int test_vsync = 0;
941         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" };
942         unsigned int i;
943         int count = 0, plane_count = 0;
944         struct connector con_args[2];
945         struct plane plane_args[2] = {0};
946         
947         opterr = 0;
948         while ((c = getopt(argc, argv, optstr)) != -1) {
949                 switch (c) {
950                 case 'e':
951                         encoders = 1;
952                         break;
953                 case 'c':
954                         connectors = 1;
955                         break;
956                 case 'p':
957                         crtcs = 1;
958                         planes = 1;
959                         break;
960                 case 'm':
961                         modes = 1;
962                         break;
963                 case 'f':
964                         framebuffers = 1;
965                         break;
966                 case 'v':
967                         test_vsync = 1;
968                         break;
969                 case 's':
970                         if (parse_connector(&con_args[count], optarg) < 0)
971                                 usage(argv[0]);
972                         count++;                                      
973                         break;
974                 case 'P':
975                         if (parse_plane(&plane_args[plane_count], optarg) < 0)
976                                 usage(argv[0]);
977                         plane_count++;
978                         break;
979                 default:
980                         usage(argv[0]);
981                         break;
982                 }
983         }
985         if (argc == 1)
986                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
988         for (i = 0; i < ARRAY_SIZE(modules); i++) {
989                 printf("trying to load module %s...", modules[i]);
990                 fd = drmOpen(modules[i], NULL);
991                 if (fd < 0) {
992                         printf("failed.\n");
993                 } else {
994                         printf("success.\n");
995                         break;
996                 }
997         }
999         if (test_vsync && !page_flipping_supported()) {
1000                 fprintf(stderr, "page flipping not supported by drm.\n");
1001                 return -1;
1002         }
1004         if (i == ARRAY_SIZE(modules)) {
1005                 fprintf(stderr, "failed to load any modules, aborting.\n");
1006                 return -1;
1007         }
1009         resources = drmModeGetResources(fd);
1010         if (!resources) {
1011                 fprintf(stderr, "drmModeGetResources failed: %s\n",
1012                         strerror(errno));
1013                 drmClose(fd);
1014                 return 1;
1015         }
1017         dump_resource(encoders);
1018         dump_resource(connectors);
1019         dump_resource(crtcs);
1020         dump_resource(planes);
1021         dump_resource(framebuffers);
1023         if (count > 0) {
1024                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
1025                 getchar();
1026         }
1028         drmModeFreeResources(resources);
1030         return 0;