2a09d28ed00a682e5111997d0b6c1b1c261ac12c
[platform/upstream/libdrm.git] / tests / vbltest / vbltest.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 <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/poll.h>
50 #include <sys/time.h>
51
52 #include "xf86drm.h"
53 #include "xf86drmMode.h"
54
55 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
56
57 extern char *optarg;
58 extern int optind, opterr, optopt;
59 static char optstr[] = "s";
60
61 int secondary = 0;
62
63 struct vbl_info {
64         unsigned int vbl_count;
65         struct timeval start;
66 };
67
68 static void vblank_handler(int fd, unsigned int frame, unsigned int sec,
69                            unsigned int usec, void *data)
70 {
71         drmVBlank vbl;
72         struct timeval end;
73         struct vbl_info *info = data;
74         double t;
75
76         vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
77         if (secondary)
78                 vbl.request.type |= DRM_VBLANK_SECONDARY;
79         vbl.request.sequence = 1;
80         vbl.request.signal = (unsigned long)data;
81
82         drmWaitVBlank(fd, &vbl);
83
84         info->vbl_count++;
85
86         if (info->vbl_count == 60) {
87                 gettimeofday(&end, NULL);
88                 t = end.tv_sec + end.tv_usec * 1e-6 -
89                         (info->start.tv_sec + info->start.tv_usec * 1e-6);
90                 fprintf(stderr, "freq: %.02fHz\n", info->vbl_count / t);
91                 info->vbl_count = 0;
92                 info->start = end;
93         }
94 }
95
96 static void usage(char *name)
97 {
98         fprintf(stderr, "usage: %s [-s]\n", name);
99         fprintf(stderr, "\t-s\tuse secondary pipe\n");
100         exit(0);
101 }
102
103 int main(int argc, char **argv)
104 {
105         int i, c, fd, ret;
106         char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "exynos", "omapdrm", "tilcdc", "msm" };
107         drmVBlank vbl;
108         drmEventContext evctx;
109         struct vbl_info handler_info;
110
111         opterr = 0;
112         while ((c = getopt(argc, argv, optstr)) != -1) {
113                 switch (c) {
114                 case 's':
115                         secondary = 1;
116                         break;
117                 default:
118                         usage(argv[0]);
119                         break;
120                 }
121         }
122
123         for (i = 0; i < ARRAY_SIZE(modules); i++) {
124                 printf("trying to load module %s...", modules[i]);
125                 fd = drmOpen(modules[i], NULL);
126                 if (fd < 0) {
127                         printf("failed.\n");
128                 } else {
129                         printf("success.\n");
130                         break;
131                 }
132         }
133
134         if (i == ARRAY_SIZE(modules)) {
135                 fprintf(stderr, "failed to load any modules, aborting.\n");
136                 return -1;
137         }
138
139         /* Get current count first */
140         vbl.request.type = DRM_VBLANK_RELATIVE;
141         if (secondary)
142                 vbl.request.type |= DRM_VBLANK_SECONDARY;
143         vbl.request.sequence = 0;
144         ret = drmWaitVBlank(fd, &vbl);
145         if (ret != 0) {
146                 printf("drmWaitVBlank (relative) failed ret: %i\n", ret);
147                 return -1;
148         }
149
150         printf("starting count: %d\n", vbl.request.sequence);
151
152         handler_info.vbl_count = 0;
153         gettimeofday(&handler_info.start, NULL);
154
155         /* Queue an event for frame + 1 */
156         vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
157         if (secondary)
158                 vbl.request.type |= DRM_VBLANK_SECONDARY;
159         vbl.request.sequence = 1;
160         vbl.request.signal = (unsigned long)&handler_info;
161         ret = drmWaitVBlank(fd, &vbl);
162         if (ret != 0) {
163                 printf("drmWaitVBlank (relative, event) failed ret: %i\n", ret);
164                 return -1;
165         }
166
167         /* Set up our event handler */
168         memset(&evctx, 0, sizeof evctx);
169         evctx.version = DRM_EVENT_CONTEXT_VERSION;
170         evctx.vblank_handler = vblank_handler;
171         evctx.page_flip_handler = NULL;
172
173         /* Poll for events */
174         while (1) {
175                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
176                 fd_set fds;
177                 int ret;
178
179                 FD_ZERO(&fds);
180                 FD_SET(0, &fds);
181                 FD_SET(fd, &fds);
182                 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
183
184                 if (ret <= 0) {
185                         fprintf(stderr, "select timed out or error (ret %d)\n",
186                                 ret);
187                         continue;
188                 } else if (FD_ISSET(0, &fds)) {
189                         break;
190                 }
191
192                 ret = drmHandleEvent(fd, &evctx);
193                 if (ret != 0) {
194                         printf("drmHandleEvent failed: %i\n", ret);
195                         return -1;
196                 }
197         }
198
199         return 0;
200 }