]> git.cworth.org Git - fips/blobdiff - glxwrap.c
Add explicit link to libpthread, to work around debugging issues
[fips] / glxwrap.c
index 9ebe799c70aea1f5678d0c3283ecd4162aa67d98..4b87642e08846612d8d3f2ce1739e39db7373cb8 100644 (file)
--- a/glxwrap.c
+++ b/glxwrap.c
  * THE SOFTWARE.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "fips.h"
 
-#include <string.h>
-
-#include <dlfcn.h>
+#include "fips-dispatch.h"
 
 #include <X11/Xlib.h>
 #include <GL/gl.h>
 #include <GL/glx.h>
-#include <GL/glext.h>
-
-#include <sys/time.h>
 
+#include "context.h"
 #include "dlwrap.h"
+#include "glwrap.h"
+#include "metrics.h"
 
-typedef void (* fips_glXSwapBuffers_t)(Display *dpy, GLXDrawable drawable);
-
-static void *
-lookup (const char *name)
+void
+glXSwapBuffers (Display *dpy, GLXDrawable drawable)
 {
-       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);
+       GLWRAP_DEFER (glXSwapBuffers, dpy, drawable);
+
+       context_counter_stop ();
+
+       context_end_frame ();
+
+       context_counter_start ();
 }
 
-static void
-call_glXSwapBuffers (Display *dpy, GLXDrawable drawable)
+/* glXGetProcAddressARB is a function which accepts a string and
+ * returns a generic function pointer (which nominally accepts void and
+ * has void return type). Of course, the user is expected to cast the
+ * returned function pointer to a function pointer of the expected
+ * type.
+ */
+void (*glXGetProcAddressARB (const GLubyte *func))(void)
 {
-       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);
-}      
+       static void *libfips_handle = NULL;
+       void *ret;
 
-void
-glXSwapBuffers (Display *dpy, GLXDrawable drawable)
+       if (libfips_handle == NULL)
+               libfips_handle = dlwrap_dlopen_libfips ();
+
+       /* If our library has this symbol, that's what we want to give. */
+       ret = dlwrap_real_dlsym (libfips_handle, (const char *) func);
+       if (ret)
+               return ret;
+
+       /* Otherwise, just defer to the real glXGetProcAddressARB. */
+       GLWRAP_DEFER_WITH_RETURN (ret, glXGetProcAddressARB, func);
+
+       return ret;
+}
+
+void (*glXGetProcAddress (const GLubyte *func))(void)
 {
-       static int initialized = 0;
-       static int frames;
-       static struct timeval tv_start, tv_now;
+       /* This comment must not be removed. It ensures that the
+        * glXGetProcAddress function ends up in our exported symbol
+        * list even though there's not otherwise any code saying:
+        *
+        * GLWRAP_DEFER_WITH_RETURN (ret, glXGetProcAddress, func);
+        */
+       return glXGetProcAddressARB(func);
+}
 
-       if (! initialized) {
-               frames = 0;
-               gettimeofday (&tv_start, NULL);
-               initialized = 1;
-       }
+Bool
+glXMakeCurrent (Display *dpy, GLXDrawable drawable, GLXContext ctx)
+{
+       Bool ret;
 
-       call_glXSwapBuffers (dpy, drawable);
+       context_leave ();
 
-       frames++;
-       if (frames % 60 == 0) {
-               double fps;
-               gettimeofday (&tv_now, NULL);
+       GLWRAP_DEFER_WITH_RETURN (ret, glXMakeCurrent, dpy, drawable, ctx);
 
-               fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
-                                        (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
+       context_enter (FIPS_API_GLX, ctx);
 
-               printf("FPS: %.3f\n", fps);
-       }
+       return ret;
 }
 
-
-typedef __GLXextFuncPtr (* fips_glXGetProcAddressARB_t)(const GLubyte *func);
-__GLXextFuncPtr
-glXGetProcAddressARB (const GLubyte *func)
+Bool
+glXMakeContextCurrent (Display *dpy, GLXDrawable drawable, GLXDrawable read, GLXContext ctx)
 {
-       static fips_glXGetProcAddressARB_t real_glXGetProcAddressARB = NULL;
-       const char *name = "glXGetProcAddressARB";
-
-       if (! real_glXGetProcAddressARB) {
-               real_glXGetProcAddressARB = (fips_glXGetProcAddressARB_t) lookup (name);
-               if (! real_glXGetProcAddressARB) {
-                       fprintf (stderr, "Error: Failed to find function %s.\n",
-                                name);
-                       return NULL;
-               }
-       }
-
-       if (strcmp ((const char *)func, "glXSwapBuffers") == 0)
-               return (__GLXextFuncPtr) glXSwapBuffers;
-       else
-               return real_glXGetProcAddressARB (func);
+       Bool ret;
+
+       context_leave ();
+
+       GLWRAP_DEFER_WITH_RETURN (ret, glXMakeContextCurrent, dpy, drawable, read, ctx);
+
+       context_enter (FIPS_API_GLX, ctx);
+
+       return ret;
 }