-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathframetime.c
More file actions
192 lines (140 loc) · 4.45 KB
/
frametime.c
File metadata and controls
192 lines (140 loc) · 4.45 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
libframetime, a frame time dumper for all GL applications
Copyright (C) 2013 Lauri Kasanen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <GL/glx.h>
#ifndef NO_EGL
#include <EGL/egl.h>
#endif
typedef uint8_t u8;
typedef uint32_t u32;
static void *(*real_dlsym)(void *handle, const char *symbol) = NULL;
static __GLXextFuncPtr (*real_glXGetProcAddressARB)(const GLubyte *) = NULL;
static void (*real_glXSwapBuffers)(Display *dpy, GLXDrawable drawable) = NULL;
#ifndef NO_EGL
static EGLBoolean (*real_eglSwapBuffers)(EGLDisplay display, EGLSurface surface) = NULL;
#endif
#define PREFIX "libframetime: "
static FILE *f = NULL;
static struct timeval oldtime;
static u8 firstdone = 0;
static void timing() {
if (!firstdone) {
gettimeofday(&oldtime, NULL);
firstdone = 1;
} else {
struct timeval newtime;
gettimeofday(&newtime, NULL);
u32 usec = (newtime.tv_sec - oldtime.tv_sec) * 1000 * 1000;
usec += newtime.tv_usec - oldtime.tv_usec;
oldtime = newtime;
fprintf(f, "Frametime %u us\n", usec);
fflush(f);
}
}
static void die(const char fmt[], ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
static void init_dlsym() {
void *libdl = dlopen("libdl.so", RTLD_LAZY);
if (!libdl) die(PREFIX "Failed to open libdl.so\n");
real_dlsym = dlvsym(libdl, "dlsym", "GLIBC_2.2.5");
if (!real_dlsym)
real_dlsym = dlvsym(libdl, "dlsym", "GLIBC_2.0");
if (!real_dlsym) die(PREFIX "Failed loading dlsym\n");
}
void *dlsym(void *handle, const char *symbol) {
// High evil wrapping this function.
if (symbol && !strcmp(symbol, "glXGetProcAddressARB"))
return glXGetProcAddressARB;
if (symbol && !strcmp(symbol, "glXSwapBuffers"))
return glXSwapBuffers;
#ifndef NO_EGL
if (symbol && !strcmp(symbol, "eglSwapBuffers"))
return eglSwapBuffers;
#endif
if (!real_dlsym)
init_dlsym();
return real_dlsym(handle, symbol);
}
__GLXextFuncPtr glXGetProcAddressARB(const GLubyte *name) {
if (name && !strcmp((char *) name, "glXSwapBuffers"))
return (__GLXextFuncPtr) glXSwapBuffers;
return real_glXGetProcAddressARB(name);
}
void glXSwapBuffers(Display * const dpy, GLXDrawable drawable) {
timing();
real_glXSwapBuffers(dpy, drawable);
}
#ifndef NO_EGL
EGLBoolean eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
timing();
return real_eglSwapBuffers(display, surface);
}
#endif
static void init() __attribute__((constructor));
static void deinit() __attribute__((destructor));
static void init() {
const char * const env = getenv("LIBFRAMETIME_FILE");
const char * const name = env ? env : "/tmp/libframetime.out";
f = fopen(name, "w");
if (!f)
die(PREFIX "Failed to open %s for writing\n", name);
if (!real_dlsym)
init_dlsym();
dlerror();
real_glXSwapBuffers = real_dlsym(RTLD_NEXT, "glXSwapBuffers");
const char *err = dlerror();
if (err)
die(PREFIX "dlsym failed: %s\n", err);
real_glXGetProcAddressARB = real_dlsym(RTLD_NEXT, "glXGetProcAddressARB");
// If the app loads libs dynamically, the symbol may be NULL.
if (!real_glXSwapBuffers) {
void *libgl = dlopen("libGL.so", RTLD_LAZY);
if (!libgl)
die(PREFIX "dynamic libGL failed\n");
real_glXSwapBuffers = real_dlsym(libgl, "glXSwapBuffers");
real_glXGetProcAddressARB = real_dlsym(libgl, "glXGetProcAddressARB");
}
#ifndef NO_EGL
dlerror();
real_eglSwapBuffers = real_dlsym(RTLD_NEXT, "eglSwapBuffers");
err = dlerror();
if (err)
die(PREFIX "dlsym failed: %s\n", err);
// If the app loads libs dynamically, the symbol may be NULL.
if (!real_eglSwapBuffers) {
void *libegl = dlopen("libEGL.so", RTLD_LAZY);
if (!libegl)
die(PREFIX "dynamic libEGL failed\n");
real_eglSwapBuffers = real_dlsym(libegl, "eglSwapBuffers");
}
#endif
}
static void deinit() {
fflush(f);
fsync(fileno(f));
fclose(f);
}