#include "fips.h"
+#include "fips-dispatch.h"
+
#include <EGL/egl.h>
+#include "context.h"
+#include "eglwrap.h"
#include "dlwrap.h"
#include "metrics.h"
-static void *
+/* Defer to the real 'function' (from libEGL.so.1) to do the real work.
+ * The symbol is looked up once and cached in a static variable for
+ * future uses.
+ */
+#define EGLWRAP_DEFER(function,...) do { \
+ static typeof(&function) real_ ## function; \
+ if (! real_ ## function) \
+ real_ ## function = eglwrap_lookup (#function); \
+ real_ ## function(__VA_ARGS__); \
+} while (0);
+
+/* As EGLWRAP_DEFER, but also set 'ret' to the return value */
+#define EGLWRAP_DEFER_WITH_RETURN(ret, function,...) do { \
+ static typeof(&function) real_ ## function; \
+ if (! real_ ## function) \
+ real_ ## function = eglwrap_lookup (#function); \
+ (ret) = real_ ## function(__VA_ARGS__); \
+} while (0);
+
+
+/* Note: We only need to perform a lookup in libEGL.so.1, (not
+ * libGLESv2.so.2). This is because the functions we wrap, (currently
+ * eglSwapBufers, eglGetProcAddress, and eglMakeCurrent), exist only
+ * in libEGL.so.1.
+ */
+void *
eglwrap_lookup (char *name)
{
const char *libegl_filename = "libEGL.so.1";
eglSwapBuffers (EGLDisplay dpy, EGLSurface surface)
{
EGLBoolean ret;
- static typeof(&eglSwapBuffers) real_eglSwapBuffers;
- if (! real_eglSwapBuffers)
- real_eglSwapBuffers = eglwrap_lookup ("eglSwapBuffers");
+ EGLWRAP_DEFER_WITH_RETURN (ret, eglSwapBuffers, dpy, surface);
- ret = real_eglSwapBuffers (dpy, surface);
+ metrics_counter_stop ();
metrics_end_frame ();
+ metrics_counter_start ();
+
+ return ret;
+}
+
+void (*eglGetProcAddress (char const *func))(void)
+{
+ void *ret;
+
+ /* If our library has this symbol, that's what we want to give. */
+ ret = dlwrap_real_dlsym (NULL, (const char *) func);
+ if (ret)
+ return ret;
+
+ /* Otherwise, just defer to the real eglGetProcAddress */
+ EGLWRAP_DEFER_WITH_RETURN (ret, eglGetProcAddress, func);
+
+ return ret;
+}
+
+EGLBoolean
+eglMakeCurrent (EGLDisplay display, EGLSurface draw, EGLSurface read,
+ EGLContext context)
+{
+ EGLBoolean ret;
+
+ context_leave ();
+
+ EGLWRAP_DEFER_WITH_RETURN (ret, eglMakeCurrent, display, draw, read, context);
+
+ context_enter (FIPS_API_EGL, context);
+
return ret;
}