/
test_vsync.cpp
executable file
·95 lines (68 loc) · 1.39 KB
/
test_vsync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "X11Window.h"
#include "Stopwatch.h"
#include "GL.h"
//extern "C"
//{
//#include <GLES2/gl2.h>
//#include <GLES3/gl3.h>
//}
//#define GL_GLEXT_PROTOTYPES
//#include <GLES2/gl2.h>
#include <cstdlib>
#include <xf86drm.h>
int main()
{
int fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
if (fd < 0)
{
throw Exception("DRM device open failed.");
}
Stopwatch sw;
std::shared_ptr<X11Window> window = std::make_shared<X11Window>();
window->SetFullscreen(true);
sw.Start();
bool isRunning = true;
int frames = 0;
float totalTime = 0;
while (isRunning)
{
isRunning = window->ProcessMessages();
// Render
float red = rand() % 256 / 255.0f;
float green = rand() % 256 / 255.0f;
float blue = rand() % 256 / 255.0f;
glClearColor(red, green, blue, 1.0f);
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
// Wait for VSYNC
glFinish();
drmVBlank vbl =
{
.request =
{
.type = DRM_VBLANK_RELATIVE,
.sequence = 1,
}
};
int io = drmWaitVBlank(fd, &vbl);
if (io)
{
throw Exception("drmWaitVBlank failed.");
}
// Swap
window->SwapBuffers();
// Measure FPS
++frames;
totalTime += (float)sw.Elapsed(); //GetTime();
if (totalTime >= 1.0f)
{
int fps = (int)(frames / totalTime);
fprintf(stderr, "FPS: %i\n", fps);
frames = 0;
totalTime = 0;
}
sw.Reset();
}
return 0;
}