Add new runtime debug flags.

Change-Id: I07955de166a89b5053c6c13f250bb3e2936ca86e
diff --git a/libs/hwui/Caches.cpp b/libs/hwui/Caches.cpp
index 248e054..2b498c6 100644
--- a/libs/hwui/Caches.cpp
+++ b/libs/hwui/Caches.cpp
@@ -19,6 +19,7 @@
 #include <utils/Log.h>
 
 #include "Caches.h"
+#include "Properties.h"
 
 namespace android {
 
@@ -49,6 +50,9 @@
 
     mCurrentBuffer = meshBuffer;
     mRegionMesh = NULL;
+
+    mDebugLevel = readDebugLevel();
+    LOGD("Enabling debug mode %d", mDebugLevel);
 }
 
 Caches::~Caches() {
diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h
index 77bbcd1..2779dfd 100644
--- a/libs/hwui/Caches.h
+++ b/libs/hwui/Caches.h
@@ -83,7 +83,7 @@
 
     friend class Singleton<Caches>;
 
-    CacheLogger mlogger;
+    CacheLogger mLogger;
 
     GLuint mCurrentBuffer;
 
@@ -93,6 +93,14 @@
 
 public:
     /**
+     * Indicates whether the renderer is in debug mode.
+     * This debug mode provides limited information to app developers.
+     */
+    DebugLevel getDebugLevel() const {
+        return mDebugLevel;
+    }
+
+    /**
      * Binds the VBO used to render simple textured quads.
      */
     void bindMeshBuffer();
@@ -145,6 +153,9 @@
     ResourceCache resourceCache;
 
     Line line;
+
+private:
+    DebugLevel mDebugLevel;
 }; // class Caches
 
 }; // namespace uirenderer
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 19a5973..540f115 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -163,6 +163,10 @@
 #endif
 #if DEBUG_MEMORY_USAGE
     mCaches.dumpMemoryUsage();
+#else
+    if (mCaches.getDebugLevel() & kDebugMemory) {
+        mCaches.dumpMemoryUsage();
+    }
 #endif
 }
 
diff --git a/libs/hwui/PathCache.cpp b/libs/hwui/PathCache.cpp
index 04d07db..7ff26dc 100644
--- a/libs/hwui/PathCache.cpp
+++ b/libs/hwui/PathCache.cpp
@@ -63,6 +63,8 @@
     GLint maxTextureSize;
     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
     mMaxTextureSize = maxTextureSize;
+
+    mDebugEnabled = readDebugLevel() & kDebugCaches;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -98,6 +100,9 @@
 
         PATH_LOGD("PathCache::callback: delete path: name, size, mSize = %d, %d, %d",
                 texture->id, size, mSize);
+        if (mDebugEnabled) {
+            LOGD("Path deleted, size = %d", size);
+        }
 
         glDeleteTextures(1, &texture->id);
         delete texture;
@@ -199,6 +204,9 @@
         mSize += size;
         PATH_LOGD("PathCache::get: create path: name, size, mSize = %d, %d, %d",
                 texture->id, size, mSize);
+        if (mDebugEnabled) {
+            LOGD("Path created, size = %d", size);
+        }
         mCache.put(entry, texture);
         mLock.unlock();
     } else {
diff --git a/libs/hwui/PathCache.h b/libs/hwui/PathCache.h
index 23aa7ef..0193f43 100644
--- a/libs/hwui/PathCache.h
+++ b/libs/hwui/PathCache.h
@@ -176,6 +176,8 @@
     uint32_t mMaxSize;
     GLuint mMaxTextureSize;
 
+    bool mDebugEnabled;
+
     /**
      * Used to access mCache and mSize. All methods are accessed from a single
      * thread except for remove().
diff --git a/libs/hwui/Properties.h b/libs/hwui/Properties.h
index 813392b..96d8b69 100644
--- a/libs/hwui/Properties.h
+++ b/libs/hwui/Properties.h
@@ -18,12 +18,27 @@
 #define ANDROID_HWUI_PROPERTIES_H
 
 #include <cutils/properties.h>
+#include <stdlib.h>
 
 /**
  * This file contains the list of system properties used to configure
  * the OpenGLRenderer.
  */
 
+/**
+ * Debug level for app developers.
+ */
+#define PROPERTY_DEBUG "hwui.debug_level"
+
+/**
+ * Debug levels. Debug levels are used as flags.
+ */
+enum DebugLevel {
+    kDebugDisabled = 0,
+    kDebugMemory = 1,
+    kDebugCaches = 2
+};
+
 // These properties are defined in mega-bytes
 #define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
 #define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
@@ -56,4 +71,12 @@
 #define DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD 64
 #define DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD 192
 
+static DebugLevel readDebugLevel() {
+    char property[PROPERTY_VALUE_MAX];
+    if (property_get(PROPERTY_DEBUG, property, NULL) > 0) {
+        return (DebugLevel) atoi(property);
+    }
+    return kDebugDisabled;
+}
+
 #endif // ANDROID_HWUI_PROPERTIES_H
diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp
index 2497925..1be6868 100644
--- a/libs/hwui/TextureCache.cpp
+++ b/libs/hwui/TextureCache.cpp
@@ -62,6 +62,8 @@
 
     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
     LOGD("    Maximum texture dimension is %d pixels", mMaxTextureSize);
+
+    mDebugEnabled = readDebugLevel() & kDebugCaches;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -96,6 +98,9 @@
         mSize -= texture->bitmapSize;
         TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
                 texture->id, texture->bitmapSize, mSize);
+        if (mDebugEnabled) {
+            LOGD("Texture deleted, size = %d", texture->bitmapSize);
+        }
         glDeleteTextures(1, &texture->id);
         delete texture;
     }
@@ -135,6 +140,9 @@
             mSize += size;
             TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
                      bitmap, texture->id, size, mSize);
+            if (mDebugEnabled) {
+                LOGD("Texture created, size = %d", size);
+            }
             mCache.put(bitmap, texture);
             mLock.unlock();
         } else {
diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h
index 2ee88b1..27693df 100644
--- a/libs/hwui/TextureCache.h
+++ b/libs/hwui/TextureCache.h
@@ -107,6 +107,8 @@
     uint32_t mMaxSize;
     GLint mMaxTextureSize;
 
+    bool mDebugEnabled;
+
     /**
      * Used to access mCache and mSize. All methods are accessed from a single
      * thread except for remove().