modetest: add drm_plane support
[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 <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/poll.h>
50 #include <sys/time.h>
52 #include "xf86drm.h"
53 #include "xf86drmMode.h"
54 #include "drm_fourcc.h"
55 #include "libkms.h"
57 #ifdef HAVE_CAIRO
58 #include <math.h>
59 #include <cairo.h>
60 #endif
62 drmModeRes *resources;
63 int fd, modes;
65 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
67 struct type_name {
68         int type;
69         char *name;
70 };
72 #define type_name_fn(res) \
73 char * res##_str(int type) {                    \
74         int i;                                          \
75         for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
76                 if (res##_names[i].type == type)        \
77                         return res##_names[i].name;     \
78         }                                               \
79         return "(invalid)";                             \
80 }
82 struct type_name encoder_type_names[] = {
83         { DRM_MODE_ENCODER_NONE, "none" },
84         { DRM_MODE_ENCODER_DAC, "DAC" },
85         { DRM_MODE_ENCODER_TMDS, "TMDS" },
86         { DRM_MODE_ENCODER_LVDS, "LVDS" },
87         { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
88 };
90 type_name_fn(encoder_type)
92 struct type_name connector_status_names[] = {
93         { DRM_MODE_CONNECTED, "connected" },
94         { DRM_MODE_DISCONNECTED, "disconnected" },
95         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
96 };
98 type_name_fn(connector_status)
100 struct type_name connector_type_names[] = {
101         { DRM_MODE_CONNECTOR_Unknown, "unknown" },
102         { DRM_MODE_CONNECTOR_VGA, "VGA" },
103         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
104         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
105         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
106         { DRM_MODE_CONNECTOR_Composite, "composite" },
107         { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
108         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
109         { DRM_MODE_CONNECTOR_Component, "component" },
110         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
111         { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
112         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
113         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
114         { DRM_MODE_CONNECTOR_TV, "TV" },
115         { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
116 };
118 type_name_fn(connector_type)
120 void dump_encoders(void)
122         drmModeEncoder *encoder;
123         int i;
125         printf("Encoders:\n");
126         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
127         for (i = 0; i < resources->count_encoders; i++) {
128                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
130                 if (!encoder) {
131                         fprintf(stderr, "could not get encoder %i: %s\n",
132                                 resources->encoders[i], strerror(errno));
133                         continue;
134                 }
135                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
136                        encoder->encoder_id,
137                        encoder->crtc_id,
138                        encoder_type_str(encoder->encoder_type),
139                        encoder->possible_crtcs,
140                        encoder->possible_clones);
141                 drmModeFreeEncoder(encoder);
142         }
143         printf("\n");
146 void dump_mode(drmModeModeInfo *mode)
148         printf("  %s %d %d %d %d %d %d %d %d %d\n",
149                mode->name,
150                mode->vrefresh,
151                mode->hdisplay,
152                mode->hsync_start,
153                mode->hsync_end,
154                mode->htotal,
155                mode->vdisplay,
156                mode->vsync_start,
157                mode->vsync_end,
158                mode->vtotal);
161 static void
162 dump_props(drmModeConnector *connector)
164         drmModePropertyPtr props;
165         int i;
167         for (i = 0; i < connector->count_props; i++) {
168                 props = drmModeGetProperty(fd, connector->props[i]);
169                 printf("\t%s, flags %d\n", props->name, props->flags);
170                 drmModeFreeProperty(props);
171         }
174 void dump_connectors(void)
176         drmModeConnector *connector;
177         int i, j;
179         printf("Connectors:\n");
180         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
181         for (i = 0; i < resources->count_connectors; i++) {
182                 connector = drmModeGetConnector(fd, resources->connectors[i]);
184                 if (!connector) {
185                         fprintf(stderr, "could not get connector %i: %s\n",
186                                 resources->connectors[i], strerror(errno));
187                         continue;
188                 }
190                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
191                        connector->connector_id,
192                        connector->encoder_id,
193                        connector_status_str(connector->connection),
194                        connector_type_str(connector->connector_type),
195                        connector->mmWidth, connector->mmHeight,
196                        connector->count_modes);
198                 for (j = 0; j < connector->count_encoders; j++)
199                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
200                 printf("\n");
202                 if (!connector->count_modes)
203                         continue;
205                 printf("  modes:\n");
206                 printf("  name refresh (Hz) hdisp hss hse htot vdisp "
207                        "vss vse vtot)\n");
208                 for (j = 0; j < connector->count_modes; j++)
209                         dump_mode(&connector->modes[j]);
211                 printf("  props:\n");
212                 dump_props(connector);
214                 drmModeFreeConnector(connector);
215         }
216         printf("\n");
219 void dump_crtcs(void)
221         drmModeCrtc *crtc;
222         int i;
224         printf("CRTCs:\n");
225         printf("id\tfb\tpos\tsize\n");
226         for (i = 0; i < resources->count_crtcs; i++) {
227                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
229                 if (!crtc) {
230                         fprintf(stderr, "could not get crtc %i: %s\n",
231                                 resources->crtcs[i], strerror(errno));
232                         continue;
233                 }
234                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
235                        crtc->crtc_id,
236                        crtc->buffer_id,
237                        crtc->x, crtc->y,
238                        crtc->width, crtc->height);
239                 dump_mode(&crtc->mode);
241                 drmModeFreeCrtc(crtc);
242         }
243         printf("\n");
246 void dump_framebuffers(void)
248         drmModeFB *fb;
249         int i;
251         printf("Frame buffers:\n");
252         printf("id\tsize\tpitch\n");
253         for (i = 0; i < resources->count_fbs; i++) {
254                 fb = drmModeGetFB(fd, resources->fbs[i]);
256                 if (!fb) {
257                         fprintf(stderr, "could not get fb %i: %s\n",
258                                 resources->fbs[i], strerror(errno));
259                         continue;
260                 }
261                 printf("%u\t(%ux%u)\t%u\n",
262                        fb->fb_id,
263                        fb->width, fb->height,
264                        fb->pitch);
266                 drmModeFreeFB(fb);
267         }
268         printf("\n");
271 static void dump_planes(void)
273         drmModePlaneRes *plane_resources;
274         drmModePlane *ovr;
275         int i, j;
277         plane_resources = drmModeGetPlaneResources(fd);
278         if (!plane_resources) {
279                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
280                         strerror(errno));
281                 return;
282         }
284         printf("Planes:\n");
285         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n");
286         for (i = 0; i < plane_resources->count_planes; i++) {
287                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
288                 if (!ovr) {
289                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
290                                 strerror(errno));
291                         continue;
292                 }
294                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n",
295                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
296                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
297                        ovr->gamma_size);
299                 if (!ovr->count_formats)
300                         continue;
302                 printf("  formats:");
303                 for (j = 0; j < ovr->count_formats; j++)
304                         printf(" %4.4s", (char *)&ovr->formats[j]);
305                 printf("\n");
307                 drmModeFreePlane(ovr);
308         }
309         printf("\n");
311         return;
314 /*
315  * Mode setting with the kernel interfaces is a bit of a chore.
316  * First you have to find the connector in question and make sure the
317  * requested mode is available.
318  * Then you need to find the encoder attached to that connector so you
319  * can bind it with a free crtc.
320  */
321 struct connector {
322         uint32_t id;
323         char mode_str[64];
324         drmModeModeInfo *mode;
325         drmModeEncoder *encoder;
326         int crtc;
327         int pipe;
328         unsigned int fb_id[2], current_fb_id;
329         struct timeval start;
331         int swap_count;
332 };
334 struct plane {
335         uint32_t con_id;  /* the id of connector to bind to */
336         uint32_t w, h;
337         unsigned int fb_id;
338 };
340 static void
341 connector_find_mode(struct connector *c)
343         drmModeConnector *connector;
344         int i, j;
346         /* First, find the connector & mode */
347         c->mode = NULL;
348         for (i = 0; i < resources->count_connectors; i++) {
349                 connector = drmModeGetConnector(fd, resources->connectors[i]);
351                 if (!connector) {
352                         fprintf(stderr, "could not get connector %i: %s\n",
353                                 resources->connectors[i], strerror(errno));
354                         drmModeFreeConnector(connector);
355                         continue;
356                 }
358                 if (!connector->count_modes) {
359                         drmModeFreeConnector(connector);
360                         continue;
361                 }
363                 if (connector->connector_id != c->id) {
364                         drmModeFreeConnector(connector);
365                         continue;
366                 }
368                 for (j = 0; j < connector->count_modes; j++) {
369                         c->mode = &connector->modes[j];
370                         if (!strcmp(c->mode->name, c->mode_str))
371                                 break;
372                 }
374                 /* Found it, break out */
375                 if (c->mode)
376                         break;
378                 drmModeFreeConnector(connector);
379         }
381         if (!c->mode) {
382                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
383                 return;
384         }
386         /* Now get the encoder */
387         for (i = 0; i < resources->count_encoders; i++) {
388                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
390                 if (!c->encoder) {
391                         fprintf(stderr, "could not get encoder %i: %s\n",
392                                 resources->encoders[i], strerror(errno));
393                         drmModeFreeEncoder(c->encoder);
394                         continue;
395                 }
397                 if (c->encoder->encoder_id  == connector->encoder_id)
398                         break;
400                 drmModeFreeEncoder(c->encoder);
401         }
403         if (c->crtc == -1)
404                 c->crtc = c->encoder->crtc_id;
406         /* and figure out which crtc index it is: */
407         for (i = 0; i < resources->count_crtcs; i++) {
408                 if (c->crtc == resources->crtcs[i]) {
409                         c->pipe = i;
410                         break;
411                 }
412         }
416 static struct kms_bo *
417 allocate_buffer(struct kms_driver *kms,
418                 int width, int height, int *stride)
420         struct kms_bo *bo;
421         unsigned bo_attribs[] = {
422                 KMS_WIDTH,   0,
423                 KMS_HEIGHT,  0,
424                 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
425                 KMS_TERMINATE_PROP_LIST
426         };
427         int ret;
429         bo_attribs[1] = width;
430         bo_attribs[3] = height;
432         ret = kms_bo_create(kms, bo_attribs, &bo);
433         if (ret) {
434                 fprintf(stderr, "failed to alloc buffer: %s\n",
435                         strerror(-ret));
436                 return NULL;
437         }
439         ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
440         if (ret) {
441                 fprintf(stderr, "failed to retreive buffer stride: %s\n",
442                         strerror(-ret));
443                 kms_bo_destroy(&bo);
444                 return NULL;
445         }
447         return bo;
450 static void
451 make_pwetty(void *data, int width, int height, int stride)
453 #ifdef HAVE_CAIRO
454         cairo_surface_t *surface;
455         cairo_t *cr;
456         int x, y;
458         surface = cairo_image_surface_create_for_data(data,
459                                                       CAIRO_FORMAT_ARGB32,
460                                                       width, height,
461                                                       stride);
462         cr = cairo_create(surface);
463         cairo_surface_destroy(surface);
465         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
466         for (x = 0; x < width; x += 250)
467                 for (y = 0; y < height; y += 250) {
468                         char buf[64];
470                         cairo_move_to(cr, x, y - 20);
471                         cairo_line_to(cr, x, y + 20);
472                         cairo_move_to(cr, x - 20, y);
473                         cairo_line_to(cr, x + 20, y);
474                         cairo_new_sub_path(cr);
475                         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
476                         cairo_set_line_width(cr, 4);
477                         cairo_set_source_rgb(cr, 0, 0, 0);
478                         cairo_stroke_preserve(cr);
479                         cairo_set_source_rgb(cr, 1, 1, 1);
480                         cairo_set_line_width(cr, 2);
481                         cairo_stroke(cr);
483                         snprintf(buf, sizeof buf, "%d, %d", x, y);
484                         cairo_move_to(cr, x + 20, y + 20);
485                         cairo_text_path(cr, buf);
486                         cairo_set_source_rgb(cr, 0, 0, 0);
487                         cairo_stroke_preserve(cr);
488                         cairo_set_source_rgb(cr, 1, 1, 1);
489                         cairo_fill(cr);
490                 }
492         cairo_destroy(cr);
493 #endif
496 static int
497 create_test_buffer(struct kms_driver *kms,
498                    int width, int height, int *stride_out,
499                    struct kms_bo **bo_out)
501         struct kms_bo *bo;
502         int ret, i, j, stride;
503         void *virtual;
505         bo = allocate_buffer(kms, width, height, &stride);
506         if (!bo)
507                 return -1;
509         ret = kms_bo_map(bo, &virtual);
510         if (ret) {
511                 fprintf(stderr, "failed to map buffer: %s\n",
512                         strerror(-ret));
513                 kms_bo_destroy(&bo);
514                 return -1;
515         }
517         /* paint the buffer with colored tiles */
518         for (j = 0; j < height; j++) {
519                 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
520                 for (i = 0; i < width; i++) {
521                         div_t d = div(i, width);
522                         fb_ptr[i] =
523                                 0x00130502 * (d.quot >> 6) +
524                                 0x000a1120 * (d.rem >> 6);
525                 }
526         }
528         make_pwetty(virtual, width, height, stride);
530         kms_bo_unmap(bo);
532         *bo_out = bo;
533         *stride_out = stride;
534         return 0;
537 static int
538 create_grey_buffer(struct kms_driver *kms,
539                    int width, int height, int *stride_out,
540                    struct kms_bo **bo_out)
542         struct kms_bo *bo;
543         int size, ret, stride;
544         void *virtual;
546         bo = allocate_buffer(kms, width, height, &stride);
547         if (!bo)
548                 return -1;
550         ret = kms_bo_map(bo, &virtual);
551         if (ret) {
552                 fprintf(stderr, "failed to map buffer: %s\n",
553                         strerror(-ret));
554                 kms_bo_destroy(&bo);
555                 return -1;
556         }
558         size = stride * height;
559         memset(virtual, 0x77, size);
560         kms_bo_unmap(bo);
562         *bo_out = bo;
563         *stride_out = stride;
565         return 0;
568 void
569 page_flip_handler(int fd, unsigned int frame,
570                   unsigned int sec, unsigned int usec, void *data)
572         struct connector *c;
573         unsigned int new_fb_id;
574         struct timeval end;
575         double t;
577         c = data;
578         if (c->current_fb_id == c->fb_id[0])
579                 new_fb_id = c->fb_id[1];
580         else
581                 new_fb_id = c->fb_id[0];
582                         
583         drmModePageFlip(fd, c->crtc, new_fb_id,
584                         DRM_MODE_PAGE_FLIP_EVENT, c);
585         c->current_fb_id = new_fb_id;
586         c->swap_count++;
587         if (c->swap_count == 60) {
588                 gettimeofday(&end, NULL);
589                 t = end.tv_sec + end.tv_usec * 1e-6 -
590                         (c->start.tv_sec + c->start.tv_usec * 1e-6);
591                 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
592                 c->swap_count = 0;
593                 c->start = end;
594         }
597 static int
598 set_plane(struct kms_driver *kms, struct connector *c, struct plane *p)
600         drmModePlaneRes *plane_resources;
601         drmModePlane *ovr;
602         uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
603         uint32_t plane_id = 0;
604         struct kms_bo *plane_bo;
605         uint32_t plane_flags = 0;
606         int i, crtc_x, crtc_y, crtc_w, crtc_h;
608         /* find an unused plane which can be connected to our crtc */
609         plane_resources = drmModeGetPlaneResources(fd);
610         if (!plane_resources) {
611                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
612                         strerror(errno));
613                 return -1;
614         }
616         for (i = 0; i < plane_resources->count_planes && !plane_id; i++) {
617                 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
618                 if (!ovr) {
619                         fprintf(stderr, "drmModeGetPlane failed: %s\n",
620                                 strerror(errno));
621                         return -1;
622                 }
624                 if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id)
625                         plane_id = ovr->plane_id;
627                 drmModeFreePlane(ovr);
628         }
630         if (!plane_id) {
631                 fprintf(stderr, "failed to find plane!\n");
632                 return -1;
633         }
635         /* TODO.. would be nice to test YUV overlays.. */
636         if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo))
637                 return -1;
639         kms_bo_get_prop(plane_bo, KMS_HANDLE, &handles[0]);
641         /* just use single plane format for now.. */
642         if (drmModeAddFB2(fd, p->w, p->h, DRM_FORMAT_XRGB8888,
643                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
644                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
645                 return -1;
646         }
648         /* ok, boring.. but for now put in middle of screen: */
649         crtc_x = c->mode->hdisplay / 3;
650         crtc_y = c->mode->vdisplay / 3;
651         crtc_w = crtc_x;
652         crtc_h = crtc_y;
654         /* note src coords (last 4 args) are in Q16 format */
655         if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id,
656                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
657                             0, 0, p->w << 16, p->h << 16)) {
658                 fprintf(stderr, "failed to enable plane: %s\n",
659                         strerror(errno));
660                 return -1;
661         }
663         return 0;
666 static void
667 set_mode(struct connector *c, int count, struct plane *p, int plane_count,
668                 int page_flip)
670         struct kms_driver *kms;
671         struct kms_bo *bo, *other_bo;
672         unsigned int fb_id, other_fb_id;
673         int i, j, ret, width, height, x, stride;
674         unsigned handle;
675         drmEventContext evctx;
677         width = 0;
678         height = 0;
679         for (i = 0; i < count; i++) {
680                 connector_find_mode(&c[i]);
681                 if (c[i].mode == NULL)
682                         continue;
683                 width += c[i].mode->hdisplay;
684                 if (height < c[i].mode->vdisplay)
685                         height = c[i].mode->vdisplay;
686         }
688         ret = kms_create(fd, &kms);
689         if (ret) {
690                 fprintf(stderr, "failed to create kms driver: %s\n",
691                         strerror(-ret));
692                 return;
693         }
695         if (create_test_buffer(kms, width, height, &stride, &bo))
696                 return;
698         kms_bo_get_prop(bo, KMS_HANDLE, &handle);
699         ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
700         if (ret) {
701                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
702                         width, height, strerror(errno));
703                 return;
704         }
706         x = 0;
707         for (i = 0; i < count; i++) {
708                 if (c[i].mode == NULL)
709                         continue;
711                 printf("setting mode %s on connector %d, crtc %d\n",
712                        c[i].mode_str, c[i].id, c[i].crtc);
714                 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
715                                      &c[i].id, 1, c[i].mode);
717                 /* XXX: Actually check if this is needed */
718                 drmModeDirtyFB(fd, fb_id, NULL, 0);
720                 x += c[i].mode->hdisplay;
722                 if (ret) {
723                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
724                         return;
725                 }
727                 /* if we have a plane/overlay to show, set that up now: */
728                 for (j = 0; j < plane_count; j++)
729                         if (p[j].con_id == c[i].id)
730                                 if (set_plane(kms, &c[i], &p[j]))
731                                         return;
732         }
734         if (!page_flip)
735                 return;
736         
737         if (create_grey_buffer(kms, width, height, &stride, &other_bo))
738                 return;
740         kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
741         ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
742                            &other_fb_id);
743         if (ret) {
744                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
745                 return;
746         }
748         for (i = 0; i < count; i++) {
749                 if (c[i].mode == NULL)
750                         continue;
752                 ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
753                                       DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
754                 if (ret) {
755                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
756                         return;
757                 }
758                 gettimeofday(&c[i].start, NULL);
759                 c[i].swap_count = 0;
760                 c[i].fb_id[0] = fb_id;
761                 c[i].fb_id[1] = other_fb_id;
762                 c[i].current_fb_id = other_fb_id;
763         }
765         memset(&evctx, 0, sizeof evctx);
766         evctx.version = DRM_EVENT_CONTEXT_VERSION;
767         evctx.vblank_handler = NULL;
768         evctx.page_flip_handler = page_flip_handler;
769         
770         while (1) {
771 #if 0
772                 struct pollfd pfd[2];
774                 pfd[0].fd = 0;
775                 pfd[0].events = POLLIN;
776                 pfd[1].fd = fd;
777                 pfd[1].events = POLLIN;
779                 if (poll(pfd, 2, -1) < 0) {
780                         fprintf(stderr, "poll error\n");
781                         break;
782                 }
784                 if (pfd[0].revents)
785                         break;
786 #else
787                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
788                 fd_set fds;
789                 int ret;
791                 FD_ZERO(&fds);
792                 FD_SET(0, &fds);
793                 FD_SET(fd, &fds);
794                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
796                 if (ret <= 0) {
797                         fprintf(stderr, "select timed out or error (ret %d)\n",
798                                 ret);
799                         continue;
800                 } else if (FD_ISSET(0, &fds)) {
801                         break;
802                 }
803 #endif
805                 drmHandleEvent(fd, &evctx);
806         }
808         kms_bo_destroy(&bo);
809         kms_bo_destroy(&other_bo);
810         kms_destroy(&kms);
813 extern char *optarg;
814 extern int optind, opterr, optopt;
815 static char optstr[] = "ecpmfs:P:v";
817 void usage(char *name)
819         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
820         fprintf(stderr, "\t-e\tlist encoders\n");
821         fprintf(stderr, "\t-c\tlist connectors\n");
822         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
823         fprintf(stderr, "\t-m\tlist modes\n");
824         fprintf(stderr, "\t-f\tlist framebuffers\n");
825         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
826         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
827         fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
828         fprintf(stderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n");
829         fprintf(stderr, "\n\tDefault is to dump all info.\n");
830         exit(0);
833 #define dump_resource(res) if (res) dump_##res()
835 static int page_flipping_supported(int fd)
837         /*FIXME: generic ioctl needed? */
838         return 1;
839 #if 0
840         int ret, value;
841         struct drm_i915_getparam gp;
843         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
844         gp.value = &value;
846         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
847         if (ret) {
848                 fprintf(stderr, "drm_i915_getparam: %m\n");
849                 return 0;
850         }
852         return *gp.value;
853 #endif
856 int main(int argc, char **argv)
858         int c;
859         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
860         int test_vsync = 0;
861         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm" };
862         char *modeset = NULL;
863         int i, count = 0, plane_count = 0;
864         struct connector con_args[2];
865         struct plane plane_args[2];
866         
867         opterr = 0;
868         while ((c = getopt(argc, argv, optstr)) != -1) {
869                 switch (c) {
870                 case 'e':
871                         encoders = 1;
872                         break;
873                 case 'c':
874                         connectors = 1;
875                         break;
876                 case 'p':
877                         crtcs = 1;
878                         planes = 1;
879                         break;
880                 case 'm':
881                         modes = 1;
882                         break;
883                 case 'f':
884                         framebuffers = 1;
885                         break;
886                 case 'v':
887                         test_vsync = 1;
888                         break;
889                 case 's':
890                         modeset = strdup(optarg);
891                         con_args[count].crtc = -1;
892                         if (sscanf(optarg, "%d:%64s",
893                                    &con_args[count].id,
894                                    con_args[count].mode_str) != 2 &&
895                             sscanf(optarg, "%d@%d:%64s",
896                                    &con_args[count].id,
897                                    &con_args[count].crtc,
898                                    con_args[count].mode_str) != 3)
899                                 usage(argv[0]);
900                         count++;                                      
901                         break;
902                 case 'P':
903                         if (sscanf(optarg, "%d:%dx%d",
904                                         &plane_args[plane_count].con_id,
905                                         &plane_args[plane_count].w,
906                                         &plane_args[plane_count].h) != 3)
907                                 usage(argv[0]);
908                         plane_count++;
909                         break;
910                 default:
911                         usage(argv[0]);
912                         break;
913                 }
914         }
916         if (argc == 1)
917                 encoders = connectors = crtcs = planes = modes = framebuffers = 1;
919         for (i = 0; i < ARRAY_SIZE(modules); i++) {
920                 printf("trying to load module %s...", modules[i]);
921                 fd = drmOpen(modules[i], NULL);
922                 if (fd < 0) {
923                         printf("failed.\n");
924                 } else {
925                         printf("success.\n");
926                         break;
927                 }
928         }
930         if (test_vsync && !page_flipping_supported(fd)) {
931                 fprintf(stderr, "page flipping not supported by drm.\n");
932                 return -1;
933         }
935         if (i == ARRAY_SIZE(modules)) {
936                 fprintf(stderr, "failed to load any modules, aborting.\n");
937                 return -1;
938         }
940         resources = drmModeGetResources(fd);
941         if (!resources) {
942                 fprintf(stderr, "drmModeGetResources failed: %s\n",
943                         strerror(errno));
944                 drmClose(fd);
945                 return 1;
946         }
948         dump_resource(encoders);
949         dump_resource(connectors);
950         dump_resource(crtcs);
951         dump_resource(planes);
952         dump_resource(framebuffers);
954         if (count > 0) {
955                 set_mode(con_args, count, plane_args, plane_count, test_vsync);
956                 getchar();
957         }
959         drmModeFreeResources(resources);
961         return 0;