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>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
37 #include <sys/select.h>
41 #include "xf86drmMode.h"
43 #include "util/common.h"
47 extern int optind, opterr, optopt;
48 static char optstr[] = "D:M:p:s";
52 #define DRM_VBLANK_HIGH_CRTC_SHIFT 1
53 static int high_pipe = 0;
56 unsigned int vbl_count;
60 static void vblank_handler(int fd, unsigned int frame, unsigned int sec,
61 unsigned int usec, void *data)
65 struct vbl_info *info = data;
68 vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
70 vbl.request.type |= DRM_VBLANK_SECONDARY;
72 vbl.request.type |= high_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
73 vbl.request.sequence = 1;
74 vbl.request.signal = (unsigned long)data;
76 drmWaitVBlank(fd, &vbl);
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);
90 static void usage(char *name)
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");
102 int main(int argc, char **argv)
104 const char *device = NULL, *module = NULL;
107 drmEventContext evctx;
108 struct vbl_info handler_info;
111 while ((c = getopt(argc, argv, optstr)) != -1) {
123 high_pipe = atoi(optarg);
131 if (secondary && high_pipe)
134 fd = util_open(device, module);
138 /* Get current count first */
139 vbl.request.type = DRM_VBLANK_RELATIVE;
141 vbl.request.type |= DRM_VBLANK_SECONDARY;
143 vbl.request.type |= high_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
144 vbl.request.sequence = 0;
145 ret = drmWaitVBlank(fd, &vbl);
147 printf("drmWaitVBlank (relative) failed ret: %i\n", ret);
151 printf("starting count: %d\n", vbl.request.sequence);
153 handler_info.vbl_count = 0;
154 gettimeofday(&handler_info.start, NULL);
156 /* Queue an event for frame + 1 */
157 vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
159 vbl.request.type |= DRM_VBLANK_SECONDARY;
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);
166 printf("drmWaitVBlank (relative, event) failed ret: %i\n", ret);
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;
176 /* Poll for events */
178 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
184 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
187 fprintf(stderr, "select timed out or error (ret %d)\n",
190 } else if (FD_ISSET(0, &fds)) {
194 ret = drmHandleEvent(fd, &evctx);
196 printf("drmHandleEvent failed: %i\n", ret);