From: Carl Worth Date: Thu, 2 May 2013 21:33:12 +0000 (-0700) Subject: Reduce code duplication with with new 'glwrap_lookup' function. X-Git-Url: https://git.cworth.org/git?p=fips;a=commitdiff_plain;h=4ef9d74d4ff12afcc7e31bf13fe2a4993f06b987 Reduce code duplication with with new 'glwrap_lookup' function. We already had two copies of 'lookup', (and were anticipating additional modules which needed it as well). The DEFER macro is now also exported as GLWRAP_DEFER for use in additional modules. --- diff --git a/glwrap.c b/glwrap.c index c37bae3..9537271 100644 --- a/glwrap.c +++ b/glwrap.c @@ -103,8 +103,8 @@ add_counter (void) return counter->id; } -static void * -lookup (const char *name) +void * +glwrap_lookup (char *name) { const char *libgl_filename = "libGL.so.1"; static void *libgl_handle = NULL; @@ -121,20 +121,12 @@ lookup (const char *name) return dlwrap_real_dlsym (libgl_handle, name); } -/* Defer to the underlying, ''real'' function to do the real work. */ -#define DEFER(function,...) do { \ - static typeof(&function) real_ ## function; \ - if (! real_ ## function) \ - real_ ## function = lookup (#function); \ - real_ ## function(__VA_ARGS__); \ -} while (0); - /* Execute a glBegineQuery/glEndQuery pair around an OpenGL call. */ #define TIMED_DEFER(function,...) do { \ unsigned counter; \ counter = add_counter (); \ glBeginQuery (GL_TIME_ELAPSED, counter); \ - DEFER(function, __VA_ARGS__); \ + GLWRAP_DEFER(function, __VA_ARGS__); \ glEndQuery (GL_TIME_ELAPSED); \ } while (0); @@ -436,7 +428,7 @@ glUseProgram (GLuint program) { current_context.program = program; - DEFER(glUseProgram, program); + GLWRAP_DEFER(glUseProgram, program); } void @@ -444,7 +436,7 @@ glUseProgramObjectARB (GLhandleARB programObj) { current_context.program = programObj; - DEFER(glUseProgramObjectARB, programObj); + GLWRAP_DEFER(glUseProgramObjectARB, programObj); } static void diff --git a/glwrap.h b/glwrap.h index 81e1e34..2dc1838 100644 --- a/glwrap.h +++ b/glwrap.h @@ -22,6 +22,21 @@ #ifndef GLWRAP_H #define GLWRAP_H +/* Lookup a function named 'name' in the underlying, real, libGL.so */ +void * +glwrap_lookup (char *name); + +/* Defer to the real 'function' (from libGL.so) to do the real work. + * The symbol is looked up once and cached in a static variable for + * future uses. + */ +#define GLWRAP_DEFER(function,...) do { \ + static typeof(&function) real_ ## function; \ + if (! real_ ## function) \ + real_ ## function = glwrap_lookup (#function); \ + real_ ## function(__VA_ARGS__); \ +} while (0); + /* Should be called at the end of ever function wrapper for an OpenGL * function that ends a frame, (glXSwapBuffers and similar). */ diff --git a/glxwrap.c b/glxwrap.c index 78627d4..885f679 100644 --- a/glxwrap.c +++ b/glxwrap.c @@ -29,47 +29,10 @@ #include "glwrap.h" -typedef void (* fips_glXSwapBuffers_t)(Display *dpy, GLXDrawable drawable); - -static void * -lookup (const char *name) -{ - const char *libgl_filename = "libGL.so.1"; - static void *libgl_handle = NULL; - - if (! libgl_handle) { - libgl_handle = dlwrap_real_dlopen (libgl_filename, RTLD_NOW | RTLD_DEEPBIND); - if (! libgl_handle) { - fprintf (stderr, "Error: Failed to dlopen %s\n", - libgl_filename); - exit (1); - } - } - - return dlwrap_real_dlsym (libgl_handle, name); -} - -static void -call_glXSwapBuffers (Display *dpy, GLXDrawable drawable) -{ - static fips_glXSwapBuffers_t real_glXSwapBuffers = NULL; - const char *name = "glXSwapBuffers"; - - if (! real_glXSwapBuffers) { - real_glXSwapBuffers = (fips_glXSwapBuffers_t) lookup (name); - if (! real_glXSwapBuffers) { - fprintf (stderr, "Error: Failed to find function %s.\n", - name); - return; - } - } - real_glXSwapBuffers (dpy, drawable); -} - void glXSwapBuffers (Display *dpy, GLXDrawable drawable) { - call_glXSwapBuffers (dpy, drawable); + GLWRAP_DEFER (glXSwapBuffers, dpy, drawable); glwrap_end_frame (); } @@ -81,10 +44,10 @@ glXGetProcAddressARB (const GLubyte *func) { __GLXextFuncPtr ptr; static fips_glXGetProcAddressARB_t real_glXGetProcAddressARB = NULL; - const char *name = "glXGetProcAddressARB"; + char *name = "glXGetProcAddressARB"; if (! real_glXGetProcAddressARB) { - real_glXGetProcAddressARB = (fips_glXGetProcAddressARB_t) lookup (name); + real_glXGetProcAddressARB = glwrap_lookup (name); if (! real_glXGetProcAddressARB) { fprintf (stderr, "Error: Failed to find function %s.\n", name);