]> git.cworth.org Git - fips/commitdiff
Reduce code duplication with with new 'glwrap_lookup' function.
authorCarl Worth <cworth@cworth.org>
Thu, 2 May 2013 21:33:12 +0000 (14:33 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 2 May 2013 21:33:12 +0000 (14:33 -0700)
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.

glwrap.c
glwrap.h
glxwrap.c

index c37bae3a16ab1b63ad1883ed5f6c46fdf4def4c8..9537271243c4cfef41969ca01161c43a05c9910e 100644 (file)
--- 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
index 81e1e34c9f0ccc11980de3e70caf42366dd501c2..2dc18383c0c864e40e5b033ce67f2f209fe895dd 100644 (file)
--- a/glwrap.h
+++ b/glwrap.h
 #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).
  */
index 78627d42005a223eca6128dd12177d06e8278257..885f679c860bed17b0004c89e27986bb21515742 100644 (file)
--- a/glxwrap.c
+++ b/glxwrap.c
 
 #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);