Skip to content

Instantly share code, notes, and snippets.

@makotokato
Created March 6, 2016 12:17
Show Gist options
  • Save makotokato/9fdf0602981733731d59 to your computer and use it in GitHub Desktop.
Save makotokato/9fdf0602981733731d59 to your computer and use it in GitHub Desktop.
get vsync using libdrm
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include "xf86drm.h"
// gcc -I/usr/include/libdrm/ -ldrm vsync.c
int64_t get_time(void)
{
struct timeval now_tv;
gettimeofday(&now_tv, NULL);
return now_tv.tv_sec * 1000000LL + now_tv.tv_usec;
}
int main()
{
drm_wait_vblank_t blank;
int fd;
int i;
fd = open("/dev/dri/card0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open\n");
return -1;
}
i = 0;
while (i++ < 100) {
int64_t before = get_time();
blank.request.type = DRM_VBLANK_RELATIVE;
blank.request.sequence = 1;
if (drmWaitVBlank(fd, (drmVBlankPtr) &blank)) {
fprintf(stderr, "drmWaitVBlank\n");
return -1;
}
printf("%ld\n", get_time() - before);
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment