gltrace: Expose a function to set OpenGL trace level.

This patch adds a function setGlDebugLevel() to libEGL to enable
GL tracing. This will be used by the Java layer to add an option
to "am start" that can enable tracing for a particular application.

Change-Id: Ie1dbdd550f502df8633553595cb33ee9d9ae44e1
diff --git a/opengl/libs/EGL/egl.cpp b/opengl/libs/EGL/egl.cpp
index eec5ce1..e8a14d8 100644
--- a/opengl/libs/EGL/egl.cpp
+++ b/opengl/libs/EGL/egl.cpp
@@ -61,11 +61,23 @@
 
 // ----------------------------------------------------------------------------
 
-int gEGLDebugLevel;
-
+/**
+ * There are two different tracing methods:
+ * 1. libs/EGL/trace.cpp: Traces all functions to logcat.
+ *    To enable:
+ *      - set system property "debug.egl.trace" to 1 to trace all apps.
+ *      - or call setGLTraceLevel(1) from an app to enable tracing for that app.
+ * 2. libs/GLES_trace: Traces all functions via protobuf to host.
+ *    To enable:
+ *        - set system property "debug.egl.debug_proc" to the application name.
+ *      - or call setGLDebugLevel(1) from the app.
+ */
 static int sEGLTraceLevel;
 static int sEGLApplicationTraceLevel;
 
+int gEGLDebugLevel;
+static int sEGLApplicationDebugLevel;
+
 extern gl_hooks_t gHooksTrace;
 
 static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
@@ -82,27 +94,31 @@
     int propertyLevel = atoi(value);
     int applicationLevel = sEGLApplicationTraceLevel;
     sEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
+}
 
+void initEglDebugLevel() {
+    int propertyLevel = 0;
+    char value[PROPERTY_VALUE_MAX];
     property_get("debug.egl.debug_proc", value, "");
-    if (strlen(value) == 0)
-        return;
-
-    long pid = getpid();
-    char procPath[128] = {};
-    sprintf(procPath, "/proc/%ld/cmdline", pid);
-    FILE * file = fopen(procPath, "r");
-    if (file) {
-        char cmdline[256] = {};
-        if (fgets(cmdline, sizeof(cmdline) - 1, file)) {
-            if (!strncmp(value, cmdline, strlen(value))) {
-                // set EGL debug if the "debug.egl.debug_proc" property
-                // matches the prefix of this application's command line
-                gEGLDebugLevel = 1;
+    if (strlen(value) > 0) {
+        long pid = getpid();
+        char procPath[128] = {};
+        sprintf(procPath, "/proc/%ld/cmdline", pid);
+        FILE * file = fopen(procPath, "r");
+        if (file) {
+            char cmdline[256] = {};
+            if (fgets(cmdline, sizeof(cmdline) - 1, file)) {
+                if (!strncmp(value, cmdline, strlen(value))) {
+                    // set EGL debug if the "debug.egl.debug_proc" property
+                    // matches the prefix of this application's command line
+                    propertyLevel = 1;
+                }
             }
+            fclose(file);
         }
-        fclose(file);
     }
 
+    gEGLDebugLevel = propertyLevel || sEGLApplicationDebugLevel;
     if (gEGLDebugLevel > 0) {
         GLTrace_start();
     }
@@ -129,6 +145,15 @@
     sEGLApplicationTraceLevel = level;
 }
 
+/*
+ * Global entry point to allow applications to modify their own debug level.
+ * Debugging is enabled if either the application requested it, or if the system property
+ * matches the application's name.
+ */
+void EGLAPI setGLDebugLevel(int level) {
+    sEGLApplicationDebugLevel = level;
+}
+
 #else
 
 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
@@ -162,6 +187,7 @@
 #if EGL_TRACE
     pthread_key_create(&gGLTraceKey, NULL);
     initEglTraceLevel();
+    initEglDebugLevel();
 #endif
     uint32_t addr = (uint32_t)((void*)gl_no_context);
     android_memset32(
diff --git a/opengl/libs/EGL/egl_display.cpp b/opengl/libs/EGL/egl_display.cpp
index c85b4ce..f1e493d 100644
--- a/opengl/libs/EGL/egl_display.cpp
+++ b/opengl/libs/EGL/egl_display.cpp
@@ -58,6 +58,7 @@
 //      "EGL_ANDROID_blob_cache "               // strongly recommended
 
 extern void initEglTraceLevel();
+extern void initEglDebugLevel();
 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
 
 // ----------------------------------------------------------------------------
@@ -144,6 +145,7 @@
     // Called both at early_init time and at this time. (Early_init is pre-zygote, so
     // the information from that call may be stale.)
     initEglTraceLevel();
+    initEglDebugLevel();
 
 #endif