vbltest.c 4.6 KB

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