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