Add runtime debugging capabilities to OpenGL

The shell property debug.egl.trace can now be set to:

0
  disables tracing
1
  logs all GL calls
error
  checks glGetError after every GL call, logs a stack trace on error
systrace
  logs each GL call to systrace

Change-Id: I34a2a2d4e19c373fd9eaa1b0cd93e67c87378996
diff --git a/opengl/libs/EGL/trace.cpp b/opengl/libs/EGL/trace.cpp
index 52907c1..a51b086 100644
--- a/opengl/libs/EGL/trace.cpp
+++ b/opengl/libs/EGL/trace.cpp
@@ -26,6 +26,11 @@
 
 #include <cutils/log.h>
 
+#define ATRACE_TAG ATRACE_TAG_GRAPHICS
+#include <utils/Trace.h>
+
+#include <utils/CallStack.h>
+
 #include "egl_tls.h"
 #include "hooks.h"
 
@@ -314,6 +319,10 @@
     va_end(argp);
 }
 
+///////////////////////////////////////////////////////////////////////////
+// Log trace
+///////////////////////////////////////////////////////////////////////////
+
 #undef TRACE_GL_VOID
 #undef TRACE_GL
 
@@ -349,7 +358,6 @@
 };
 #undef GL_ENTRY
 
-
 #undef TRACE_GL_VOID
 #undef TRACE_GL
 
@@ -372,6 +380,99 @@
 #include "../debug.in"
 }
 
+///////////////////////////////////////////////////////////////////////////
+// Systrace
+///////////////////////////////////////////////////////////////////////////
+
+#undef TRACE_GL_VOID
+#undef TRACE_GL
+
+#define TRACE_GL_VOID(_api, _args, _argList, ...)                         \
+static void Systrace_ ## _api _args {                                     \
+    ATRACE_NAME(#_api);                                                   \
+    gl_hooks_t::gl_t const * const _c = &getGLTraceThreadSpecific()->gl;  \
+    _c->_api _argList;                                                    \
+}
+
+#define TRACE_GL(_type, _api, _args, _argList, ...)                       \
+static _type Systrace_ ## _api _args {                                    \
+    ATRACE_NAME(#_api);                                                   \
+    gl_hooks_t::gl_t const * const _c = &getGLTraceThreadSpecific()->gl;  \
+    return _c->_api _argList;                                             \
+}
+
+extern "C" {
+#include "../trace.in"
+}
+
+#undef TRACE_GL_VOID
+#undef TRACE_GL
+
+#define GL_ENTRY(_r, _api, ...) Systrace_ ## _api,
+EGLAPI gl_hooks_t gHooksSystrace = {
+    {
+        #include "entries.in"
+    },
+    {
+        {0}
+    }
+};
+#undef GL_ENTRY
+
+///////////////////////////////////////////////////////////////////////////
+//
+///////////////////////////////////////////////////////////////////////////
+
+#undef TRACE_GL_VOID
+#undef TRACE_GL
+
+#define CHECK_ERROR(_c, _api)                                             \
+    GLenum status = GL_NO_ERROR;                                          \
+    bool error = false;                                                   \
+    while ((status = _c->glGetError()) != GL_NO_ERROR) {                  \
+        ALOGD("[" #_api "] 0x%x", status);                                \
+        error = true;                                                     \
+    }                                                                     \
+    if (error) {                                                          \
+        CallStack s;                                                      \
+        s.update();                                                       \
+        s.dump("glGetError:" #_api);                                      \
+    }                                                                     \
+
+#define TRACE_GL_VOID(_api, _args, _argList, ...)                         \
+static void ErrorTrace_ ## _api _args {                                   \
+    gl_hooks_t::gl_t const * const _c = &getGLTraceThreadSpecific()->gl;  \
+    _c->_api _argList;                                                    \
+    CHECK_ERROR(_c, _api);                                                \
+}
+
+#define TRACE_GL(_type, _api, _args, _argList, ...)                       \
+static _type ErrorTrace_ ## _api _args {                                  \
+    gl_hooks_t::gl_t const * const _c = &getGLTraceThreadSpecific()->gl;  \
+    _type _r = _c->_api _argList;                                         \
+    CHECK_ERROR(_c, _api);                                                \
+    return _r;                                                            \
+}
+
+extern "C" {
+#include "../trace.in"
+}
+
+#undef TRACE_GL_VOID
+#undef TRACE_GL
+
+#define GL_ENTRY(_r, _api, ...) ErrorTrace_ ## _api,
+EGLAPI gl_hooks_t gHooksErrorTrace = {
+    {
+        #include "entries.in"
+    },
+    {
+        {0}
+    }
+};
+#undef GL_ENTRY
+#undef CHECK_ERROR
+
 #undef TRACE_GL_VOID
 #undef TRACE_GL