]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - tests/modetest/modetest.c
modetest: Don't sleep when just dumping state, wait for key for modeset.
[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 <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <errno.h>
48 #include "xf86drm.h"
49 #include "xf86drmMode.h"
50 #include "intel_bufmgr.h"
52 drmModeRes *resources;
53 int fd, modes;
55 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
57 struct type_name {
58         int type;
59         char *name;
60 };
62 #define type_name_fn(res) \
63 char * res##_str(int type) {                    \
64         int i;                                          \
65         for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
66                 if (res##_names[i].type == type)        \
67                         return res##_names[i].name;     \
68         }                                               \
69         return "(invalid)";                             \
70 }
72 struct type_name encoder_type_names[] = {
73         { DRM_MODE_ENCODER_NONE, "none" },
74         { DRM_MODE_ENCODER_DAC, "DAC" },
75         { DRM_MODE_ENCODER_TMDS, "TMDS" },
76         { DRM_MODE_ENCODER_LVDS, "LVDS" },
77         { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
78 };
80 type_name_fn(encoder_type)
82 struct type_name connector_status_names[] = {
83         { DRM_MODE_CONNECTED, "connected" },
84         { DRM_MODE_DISCONNECTED, "disconnected" },
85         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
86 };
88 type_name_fn(connector_status)
90 struct type_name connector_type_names[] = {
91         { DRM_MODE_CONNECTOR_Unknown, "unknown" },
92         { DRM_MODE_CONNECTOR_VGA, "VGA" },
93         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
94         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
95         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
96         { DRM_MODE_CONNECTOR_Composite, "composite" },
97         { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
98         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
99         { DRM_MODE_CONNECTOR_Component, "component" },
100         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
101         { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
102         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
103         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
104 };
106 type_name_fn(connector_type)
108 void dump_encoders(void)
110         drmModeEncoder *encoder;
111         int i;
113         printf("Encoders:\n");
114         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
115         for (i = 0; i < resources->count_encoders; i++) {
116                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
118                 if (!encoder) {
119                         fprintf(stderr, "could not get encoder %i: %s\n",
120                                 resources->encoders[i], strerror(errno));
121                         continue;
122                 }
123                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
124                        encoder->encoder_id,
125                        encoder->crtc_id,
126                        encoder_type_str(encoder->encoder_type),
127                        encoder->possible_crtcs,
128                        encoder->possible_clones);
129                 drmModeFreeEncoder(encoder);
130         }
131         printf("\n");
134 void dump_mode(struct drm_mode_modeinfo *mode)
136         printf("  %s %.02f %d %d %d %d %d %d %d %d\n",
137                mode->name,
138                (float)mode->vrefresh / 1000,
139                mode->hdisplay,
140                mode->hsync_start,
141                mode->hsync_end,
142                mode->htotal,
143                mode->vdisplay,
144                mode->vsync_start,
145                mode->vsync_end,
146                mode->vtotal);
149 void dump_connectors(void)
151         drmModeConnector *connector;
152         int i, j;
154         printf("Connectors:\n");
155         printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
156         for (i = 0; i < resources->count_connectors; i++) {
157                 connector = drmModeGetConnector(fd, resources->connectors[i]);
159                 if (!connector) {
160                         fprintf(stderr, "could not get connector %i: %s\n",
161                                 resources->connectors[i], strerror(errno));
162                         continue;
163                 }
165                 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n",
166                        connector->connector_id,
167                        connector->encoder_id,
168                        connector_status_str(connector->connection),
169                        connector_type_str(connector->connector_type),
170                        connector->mmWidth, connector->mmHeight,
171                        connector->count_modes);
173                 if (!connector->count_modes)
174                         continue;
176                 printf("  modes:\n");
177                 printf("  name refresh (Hz) hdisp hss hse htot vdisp "
178                        "vss vse vtot)\n");
179                 for (j = 0; j < connector->count_modes; j++)
180                         dump_mode(&connector->modes[j]);
182                 drmModeFreeConnector(connector);
183         }
184         printf("\n");
187 void dump_crtcs(void)
189         drmModeCrtc *crtc;
190         int i;
192         printf("CRTCs:\n");
193         printf("id\tfb\tpos\tsize\n");
194         for (i = 0; i < resources->count_crtcs; i++) {
195                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
197                 if (!crtc) {
198                         fprintf(stderr, "could not get crtc %i: %s\n",
199                                 resources->crtcs[i], strerror(errno));
200                         continue;
201                 }
202                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
203                        crtc->crtc_id,
204                        crtc->buffer_id,
205                        crtc->x, crtc->y,
206                        crtc->width, crtc->height);
207                 dump_mode(&crtc->mode);
209                 drmModeFreeCrtc(crtc);
210         }
211         printf("\n");
214 void dump_framebuffers(void)
216         drmModeFB *fb;
217         int i;
219         printf("Frame buffers:\n");
220         printf("id\tsize\tpitch\n");
221         for (i = 0; i < resources->count_fbs; i++) {
222                 fb = drmModeGetFB(fd, resources->fbs[i]);
224                 if (!fb) {
225                         fprintf(stderr, "could not get fb %i: %s\n",
226                                 resources->fbs[i], strerror(errno));
227                         continue;
228                 }
229                 printf("%d\t(%dx%d)\t%d\n",
230                        fb->fb_id,
231                        fb->width, fb->height);
233                 drmModeFreeFB(fb);
234         }
235         printf("\n");
238 /*
239  * Mode setting with the kernel interfaces is a bit of a chore.
240  * First you have to find the connector in question and make sure the
241  * requested mode is available.
242  * Then you need to find the encoder attached to that connector so you
243  * can bind it with a free crtc.
244  */
245 struct connector {
246         int id;
247         char mode_str[64];
248         struct drm_mode_modeinfo *mode;
249         drmModeEncoder *encoder;
250 };      
252 static void
253 connector_find_mode(struct connector *c)
255         drmModeConnector *connector;
256         int i, j, size, ret, width, height;
258         /* First, find the connector & mode */
259         c->mode = NULL;
260         for (i = 0; i < resources->count_connectors; i++) {
261                 connector = drmModeGetConnector(fd, resources->connectors[i]);
263                 if (!connector) {
264                         fprintf(stderr, "could not get connector %i: %s\n",
265                                 resources->connectors[i], strerror(errno));
266                         drmModeFreeConnector(connector);
267                         continue;
268                 }
270                 if (!connector->count_modes) {
271                         drmModeFreeConnector(connector);
272                         continue;
273                 }
275                 if (connector->connector_id != c->id) {
276                         drmModeFreeConnector(connector);
277                         continue;
278                 }
280                 for (j = 0; j < connector->count_modes; j++) {
281                         c->mode = &connector->modes[j];
282                         if (!strcmp(c->mode->name, c->mode_str))
283                                 break;
284                 }
286                 /* Found it, break out */
287                 if (c->mode)
288                         break;
290                 drmModeFreeConnector(connector);
291         }
293         if (!c->mode) {
294                 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
295                 return;
296         }
298         /* Now get the encoder */
299         for (i = 0; i < resources->count_encoders; i++) {
300                 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
302                 if (!c->encoder) {
303                         fprintf(stderr, "could not get encoder %i: %s\n",
304                                 resources->encoders[i], strerror(errno));
305                         drmModeFreeEncoder(c->encoder);
306                         continue;
307                 }
309                 if (c->encoder->encoder_id  == connector->encoder_id)
310                         break;
312                 drmModeFreeEncoder(c->encoder);
313         }
316 static void
317 set_mode(struct connector *c, int count)
319         drmModeConnector *connector;
320         drmModeEncoder *encoder = NULL;
321         struct drm_mode_modeinfo *mode = NULL;
322         drm_intel_bufmgr *bufmgr;
323         drm_intel_bo *bo;
324         unsigned int fb_id, *fb_ptr;
325         int i, j, size, ret, width, height, x;
326         div_t d;
328         width = 0;
329         height = 0;
330         for (i = 0; i < count; i++) {
331                 connector_find_mode(&c[i]);
332                 if (c[i].mode == NULL)
333                         continue;
334                 width += c[i].mode->hdisplay;
335                 if (height < c[i].mode->vdisplay)
336                         height = c[i].mode->vdisplay;
337         }
339         bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
340         if (!bufmgr) {
341                 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
342                 return;
343         }
345         /* Mode size at 32 bpp */
346         size = width * height * 4;
348         bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
349         if (!bo) {
350                 fprintf(stderr, "failed to alloc buffer: %s\n",
351                         strerror(errno));
352                 return;
353         }
355         ret = drm_intel_gem_bo_map_gtt(bo);
356         if (ret) {
357                 fprintf(stderr, "failed to GTT map buffer: %s\n",
358                         strerror(errno));
359                 return;
360         }
362         fb_ptr = bo->virtual;
364         /* paint the buffer with colored tiles */
365         for (i = 0; i < width * height; i++) {
366                 d = div(i, width);
367                 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
368         }
370         ret = drmModeAddFB(fd, width, height, 32, 32, width * 4, bo->handle,
371                            &fb_id);
372         if (ret) {
373                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
374                 return;
375         }
377         x = 0;
378         for (i = 0; i < count; i++) {
379                 if (c[i].mode == NULL)
380                         continue;
381                 ret = drmModeSetCrtc(fd, c[i].encoder->crtc_id, fb_id, x, 0,
382                                      &c[i].id, 1, c[i].mode);
383                 x += c[i].mode->hdisplay;
385                 if (ret) {
386                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
387                         return;
388                 }
389         }
392 extern char *optarg;
393 extern int optind, opterr, optopt;
394 static char optstr[] = "ecpmfs:";
396 void usage(char *name)
398         fprintf(stderr, "usage: %s [-ecpmf]\n", name);
399         fprintf(stderr, "\t-e\tlist encoders\n");
400         fprintf(stderr, "\t-c\tlist connectors\n");
401         fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
402         fprintf(stderr, "\t-m\tlist modes\n");
403         fprintf(stderr, "\t-f\tlist framebuffers\n");
404         fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
405         fprintf(stderr, "\n\tDefault is to dump all info.\n");
406         exit(0);
409 #define dump_resource(res) if (res) dump_##res()
411 int main(int argc, char **argv)
413         int c;
414         int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
415         char *modules[] = { "i915", "radeon" };
416         char *modeset = NULL, *mode, *connector;
417         int i, connector_id, count = 0;
418         struct connector con_args[2];
419         
420         opterr = 0;
421         while ((c = getopt(argc, argv, optstr)) != -1) {
422                 switch (c) {
423                 case 'e':
424                         encoders = 1;
425                         break;
426                 case 'c':
427                         connectors = 1;
428                         break;
429                 case 'p':
430                         crtcs = 1;
431                         break;
432                 case 'm':
433                         modes = 1;
434                         break;
435                 case 'f':
436                         framebuffers = 1;
437                         break;
438                 case 's':
439                         modeset = strdup(optarg);
440                         if (sscanf(optarg, "%d:%64s",
441                                    &con_args[count].id,
442                                    &con_args[count].mode_str) != 2)
443                                 usage(argv[0]);
444                         printf("setting mode %s on connector %d\n",
445                                con_args[count].mode_str,
446                                con_args[count].id);
447                         count++;                                      
448                         break;
449                 default:
450                         usage(argv[0]);
451                         break;
452                 }
453         }
455         if (argc == 1)
456                 encoders = connectors = crtcs = modes = framebuffers = 1;
458         for (i = 0; i < ARRAY_SIZE(modules); i++) {
459                 printf("trying to load module %s...", modules[i]);
460                 fd = drmOpen(modules[i], NULL);
461                 if (fd < 0) {
462                         printf("failed.\n");
463                 } else {
464                         printf("success.\n");
465                         break;
466                 }
467         }
469         if (i == ARRAY_SIZE(modules)) {
470                 fprintf(stderr, "failed to load any modules, aborting.\n");
471                 return -1;
472         }
474         resources = drmModeGetResources(fd);
475         if (!resources) {
476                 fprintf(stderr, "drmModeGetResources failed: %s\n",
477                         strerror(errno));
478                 drmClose(fd);
479                 return 1;
480         }
482         dump_resource(encoders);
483         dump_resource(connectors);
484         dump_resource(crtcs);
485         dump_resource(framebuffers);
487         if (count > 0) {
488                 set_mode(con_args, count);
489                 getchar();
490         }
492         drmModeFreeResources(resources);
494         return 0;