1 #define _GNU_SOURCE /* For RTLD_NEXT */
14 /* How many frames between reports. */
15 #define REPORT_FREQ 60
21 static struct timeval tv_last;
22 struct timeval tv_now;
24 if ((count % REPORT_FREQ) == 0) {
25 gettimeofday (&tv_now, NULL);
27 printf ("glfps: Initializing FPS timer\n");
29 double elapsed = ((tv_now.tv_sec - tv_last.tv_sec) +
30 (tv_now.tv_usec - tv_last.tv_usec) / 1e6);
31 printf ("FPS: %.3f\n", ((double) REPORT_FREQ) / elapsed);
39 static void *do_real_dlsym (void *handle, const char *symbol);
41 static typeof(&glXSwapBuffers) real_glXSwapBuffers;
44 glXSwapBuffers (Display *dpy, GLXDrawable drawable)
46 if (real_glXSwapBuffers == NULL)
47 real_glXSwapBuffers = do_real_dlsym (RTLD_NEXT, "glXSwapBuffers");
51 real_glXSwapBuffers (dpy, drawable);
54 static typeof(&glXGetProcAddressARB) real_glXGetProcAddressARB;
57 (*glXGetProcAddressARB (const GLubyte *func))(void)
59 if (strcmp((char *) func, "glXSwapBuffers") == 0)
60 return (void*) glXSwapBuffers;
62 if (real_glXGetProcAddressARB == NULL)
63 real_glXGetProcAddressARB = do_real_dlsym (RTLD_NEXT, "glXGetProcAddressARB");
65 return real_glXGetProcAddressARB (func);
68 static typeof(&glXGetProcAddress) real_glXGetProcAddress;
71 (*glXGetProcAddress (const GLubyte *func))(void)
73 if (strcmp((char *) func, "glXSwapBuffers") == 0)
74 return (void*) glXSwapBuffers;
76 if (real_glXGetProcAddress == NULL)
77 real_glXGetProcAddress = do_real_dlsym (RTLD_NEXT, "glXGetProcAddress");
79 return real_glXGetProcAddress (func);
82 /* We rely on an internal symbol within glibc in order to be able to
83 * get a handle on the real dlsym function, (we can't call dlsym to
84 * find the address of dlsym itself of course). */
85 void * __libc_dlsym(void *, const char *);
88 do_real_dlsym (void *handle, const char *symbol)
90 static typeof(&dlsym) real_dlsym = NULL;
92 if (real_dlsym == NULL) {
93 void *libdl_handle = dlopen ("libdl.so.2", RTLD_LAZY);
94 real_dlsym = __libc_dlsym(libdl_handle, "dlsym");
97 return real_dlsym (handle, symbol);
101 dlsym (void *handle, const char *symbol)
105 ret = do_real_dlsym (handle, symbol);
107 if (strcmp (symbol, "glXSwapBuffers") == 0) {
108 real_glXSwapBuffers = ret;
109 return &glXSwapBuffers;
112 if (strcmp (symbol, "glXGetProcAddressARB") == 0) {
113 real_glXGetProcAddressARB = ret;
114 return &glXGetProcAddressARB;
117 if (strcmp (symbol, "glXGetProcAddress") == 0) {
118 real_glXGetProcAddress = ret;
119 return &glXGetProcAddress;