From 3b0270c3e44c4a9e0f95ac43069883df9eab6d01 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 20 Sep 2013 15:29:03 -0700 Subject: [PATCH] Initial import of libglfps demo for glaze This first implementation is a simple FPS-counter for OpenGL implemented via a library suitable for use with LD_PRELOAD. --- .gitignore | 1 + Makefile | 8 ++++++++ glfps.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 glfps.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..916f458 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +libglfps.so diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3e7e390 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CC ?= gcc +CFLAGS ?= -g -Wall -Wextra -Wmissing-declarations + +libglfps.so: glfps.c + $(CC) $(CFLAGS) -fPIC -shared -Wl,-Bsymbolic -o $@ $< + +clean: + rm -f libglfps.so diff --git a/glfps.c b/glfps.c new file mode 100644 index 0000000..68d389a --- /dev/null +++ b/glfps.c @@ -0,0 +1,48 @@ +#define _GNU_SOURCE /* For RTLD_NEXT */ +#include + +#include + +#include +#include +#include + +#include + +/* How many frames between reports. */ +#define REPORT_FREQ 60 + +static void +on_each_frame (void) +{ + static int count = 0; + static struct timeval tv_last; + struct timeval tv_now; + + if ((count % REPORT_FREQ) == 0) { + gettimeofday (&tv_now, NULL); + if (count == 0) { + printf ("glfps: Initializing FPS timer\n"); + } else { + double elapsed = ((tv_now.tv_sec - tv_last.tv_sec) + + (tv_now.tv_usec - tv_last.tv_usec) / 1e6); + printf ("FPS: %.3f\n", ((double) REPORT_FREQ) / elapsed); + } + tv_last = tv_now; + } + + count++; +} + +void +glXSwapBuffers (Display *dpy, GLXDrawable drawable) +{ + static typeof(&glXSwapBuffers) real_glXSwapBuffers = NULL; + + if (real_glXSwapBuffers == NULL) + real_glXSwapBuffers = dlsym (RTLD_NEXT, "glXSwapBuffers"); + + on_each_frame (); + + real_glXSwapBuffers (dpy, drawable); +} -- 2.43.0