Move all debug flags in a single place.

This change also adds a new memory usage flag. When turned on, the
following is printed after every frame:

D/OpenGLRenderer( 3723): Current memory usage / total memory usage (bytes):
D/OpenGLRenderer( 3723):   TextureCache          3766680 / 20971520
D/OpenGLRenderer( 3723):   LayerCache            3538944 /  8388608
D/OpenGLRenderer( 3723):   GradientCache          135168 /   524288
D/OpenGLRenderer( 3723):   PathCache               41180 /  4194304
D/OpenGLRenderer( 3723):   TextDropShadowCache         0 /  2097152
D/OpenGLRenderer( 3723):   FontRenderer 0         262144 /   262144
D/OpenGLRenderer( 3723):   FontRenderer 1         262144 /   262144
D/OpenGLRenderer( 3723):   FontRenderer 2         262144 /   262144
D/OpenGLRenderer( 3723): Other:
D/OpenGLRenderer( 3723):   FboCache                    2 /       12
D/OpenGLRenderer( 3723):   PatchCache                 31 /      512
D/OpenGLRenderer( 3723): Total memory usage:
D/OpenGLRenderer( 3723):   8268404 bytes, 7.89 MB

This should help tracking possibe memory issues.

Change-Id: I83f483ca1d2dbef904829bce368e33fe5503e8d6
diff --git a/libs/hwui/Caches.cpp b/libs/hwui/Caches.cpp
index 93c5b34..248e054 100644
--- a/libs/hwui/Caches.cpp
+++ b/libs/hwui/Caches.cpp
@@ -16,6 +16,8 @@
 
 #define LOG_TAG "OpenGLRenderer"
 
+#include <utils/Log.h>
+
 #include "Caches.h"
 
 namespace android {
@@ -54,6 +56,41 @@
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// Debug
+///////////////////////////////////////////////////////////////////////////////
+
+void Caches::dumpMemoryUsage() {
+    LOGD("Current memory usage / total memory usage (bytes):");
+    LOGD("  TextureCache         %8d / %8d", textureCache.getSize(), textureCache.getMaxSize());
+    LOGD("  LayerCache           %8d / %8d", layerCache.getSize(), layerCache.getMaxSize());
+    LOGD("  GradientCache        %8d / %8d", gradientCache.getSize(), gradientCache.getMaxSize());
+    LOGD("  PathCache            %8d / %8d", pathCache.getSize(), pathCache.getMaxSize());
+    LOGD("  TextDropShadowCache  %8d / %8d", dropShadowCache.getSize(),
+            dropShadowCache.getMaxSize());
+    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
+        const uint32_t size = fontRenderer.getFontRendererSize(i);
+        LOGD("  FontRenderer %d       %8d / %8d", i, size, size);
+    }
+    LOGD("Other:");
+    LOGD("  FboCache             %8d / %8d", fboCache.getSize(), fboCache.getMaxSize());
+    LOGD("  PatchCache           %8d / %8d", patchCache.getSize(), patchCache.getMaxSize());
+
+    uint32_t total = 0;
+    total += textureCache.getSize();
+    total += layerCache.getSize();
+    total += gradientCache.getSize();
+    total += pathCache.getSize();
+    total += dropShadowCache.getSize();
+    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
+        total += fontRenderer.getFontRendererSize(i);
+    }
+
+    LOGD("Total memory usage:");
+    LOGD("  %d bytes, %.2f MB", total, total / 1024.0f / 1024.0f);
+    LOGD("\n");
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // VBO
 ///////////////////////////////////////////////////////////////////////////////
 
diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h
index c019fd1..77bbcd1 100644
--- a/libs/hwui/Caches.h
+++ b/libs/hwui/Caches.h
@@ -114,6 +114,11 @@
      */
     TextureVertex* getRegionMesh();
 
+    /**
+     * Displays the memory usage of each cache and the total sum.
+     */
+    void dumpMemoryUsage();
+
     bool blend;
     GLenum lastSrcMode;
     GLenum lastDstMode;
diff --git a/libs/hwui/Debug.h b/libs/hwui/Debug.h
new file mode 100644
index 0000000..feaba25
--- /dev/null
+++ b/libs/hwui/Debug.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HWUI_DEBUG_H
+#define ANDROID_HWUI_DEBUG_H
+
+// Turn on to check for OpenGL errors on each frame
+#define DEBUG_OPENGL 1
+
+// Turn on to enable memory usage summary on each frame
+#define DEBUG_MEMORY_USAGE 0
+
+// Turn on to enable layers debugging when renderered as regions
+#define DEBUG_LAYERS_AS_REGIONS 0
+
+// Turn on to display debug info about vertex/fragment shaders
+#define DEBUG_PROGRAMS 0
+
+// Turn on to display info about layers
+#define DEBUG_LAYERS 0
+
+// Turn on to display debug infor about 9patch objects
+#define DEBUG_PATCHES 0
+
+// Turn on to display debug info about paths
+#define DEBUG_PATHS 0
+
+// Turn on to display debug info about textures
+#define DEBUG_TEXTURES 0
+
+#endif // ANDROID_HWUI_DEBUG_H
diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h
index a76cb86..7e749ca 100644
--- a/libs/hwui/FontRenderer.h
+++ b/libs/hwui/FontRenderer.h
@@ -169,6 +169,14 @@
         return mTextureId;
     }
 
+    uint32_t getCacheWidth() const {
+        return mCacheWidth;
+    }
+
+    uint32_t getCacheHeight() const {
+        return mCacheHeight;
+    }
+
 protected:
     friend class Font;
 
@@ -207,14 +215,6 @@
         }
     };
 
-    uint32_t getCacheWidth() const {
-        return mCacheWidth;
-    }
-
-    uint32_t getCacheHeight() const {
-        return mCacheHeight;
-    }
-
     void initTextTexture();
     bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
 
diff --git a/libs/hwui/GammaFontRenderer.h b/libs/hwui/GammaFontRenderer.h
index b59ae81..96d960c 100644
--- a/libs/hwui/GammaFontRenderer.h
+++ b/libs/hwui/GammaFontRenderer.h
@@ -29,6 +29,22 @@
 
     FontRenderer& getFontRenderer(const SkPaint* paint);
 
+    uint32_t getFontRendererCount() const {
+        return 3;
+    }
+
+    uint32_t getFontRendererSize(uint32_t fontRenderer) const {
+        switch (fontRenderer) {
+            case 0:
+                return mDefaultRenderer.getCacheHeight() * mDefaultRenderer.getCacheWidth();
+            case 1:
+                return mBlackGammaRenderer.getCacheHeight() * mBlackGammaRenderer.getCacheWidth();
+            case 2:
+                return mWhiteGammaRenderer.getCacheHeight() * mWhiteGammaRenderer.getCacheWidth();
+        }
+        return 0;
+    }
+
 private:
     FontRenderer mDefaultRenderer;
     FontRenderer mBlackGammaRenderer;
diff --git a/libs/hwui/LayerCache.h b/libs/hwui/LayerCache.h
index 4df8ab3..1333a73 100644
--- a/libs/hwui/LayerCache.h
+++ b/libs/hwui/LayerCache.h
@@ -17,6 +17,7 @@
 #ifndef ANDROID_HWUI_LAYER_CACHE_H
 #define ANDROID_HWUI_LAYER_CACHE_H
 
+#include "Debug.h"
 #include "Layer.h"
 #include "utils/SortedList.h"
 
@@ -27,9 +28,6 @@
 // Defines
 ///////////////////////////////////////////////////////////////////////////////
 
-// Debug
-#define DEBUG_LAYERS 0
-
 // Indicates whether to remove the biggest layers first, or the smaller ones
 #define LAYER_REMOVE_BIGGEST 0
 // Textures used by layers must have dimensions multiples of this number
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 782d9b2..19a5973 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -161,6 +161,9 @@
         LOGD("GL error from OpenGLRenderer: 0x%x", status);
     }
 #endif
+#if DEBUG_MEMORY_USAGE
+    mCaches.dumpMemoryUsage();
+#endif
 }
 
 void OpenGLRenderer::acquireContext() {
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 3340b4a..94d96a5 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -30,6 +30,7 @@
 #include <utils/RefBase.h>
 #include <utils/Vector.h>
 
+#include "Debug.h"
 #include "Extensions.h"
 #include "Matrix.h"
 #include "Program.h"
@@ -47,12 +48,8 @@
 // Defines
 ///////////////////////////////////////////////////////////////////////////////
 
-// Debug
-#define DEBUG_OPENGL 1
-
 // If turned on, layers drawn inside FBOs are optimized with regions
 #define RENDER_LAYERS_AS_REGIONS 0
-#define DEBUG_LAYERS_AS_REGIONS 0
 
 ///////////////////////////////////////////////////////////////////////////////
 // Renderer
diff --git a/libs/hwui/PatchCache.h b/libs/hwui/PatchCache.h
index deba40d..c38cd99 100644
--- a/libs/hwui/PatchCache.h
+++ b/libs/hwui/PatchCache.h
@@ -19,6 +19,7 @@
 
 #include <utils/KeyedVector.h>
 
+#include "Debug.h"
 #include "Patch.h"
 
 namespace android {
@@ -29,9 +30,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 // Debug
-#define DEBUG_PATCHES 0
-
-// Debug
 #if DEBUG_PATCHES
     #define PATCH_LOGD(...) LOGD(__VA_ARGS__)
 #else
@@ -54,6 +52,14 @@
             const uint32_t width, const uint32_t height, const int8_t numColors);
     void clear();
 
+    uint32_t getSize() const {
+        return mCache.size();
+    }
+
+    uint32_t getMaxSize() const {
+        return mMaxEntries;
+    }
+
 private:
     uint32_t mMaxEntries;
     KeyedVector<PatchDescription, Patch*> mCache;
diff --git a/libs/hwui/PathCache.h b/libs/hwui/PathCache.h
index aea71cb..23aa7ef 100644
--- a/libs/hwui/PathCache.h
+++ b/libs/hwui/PathCache.h
@@ -21,6 +21,7 @@
 #include <SkPaint.h>
 #include <SkPath.h>
 
+#include "Debug.h"
 #include "Texture.h"
 #include "utils/Compare.h"
 #include "utils/GenerationCache.h"
@@ -33,9 +34,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 // Debug
-#define DEBUG_PATHS 0
-
-// Debug
 #if DEBUG_PATHS
     #define PATH_LOGD(...) LOGD(__VA_ARGS__)
 #else
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index 3cd85c8..f2f1adb 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -456,11 +456,11 @@
         }
 
         if (fast) {
-            if (DEBUG_PROGRAM_CACHE) {
+#if DEBUG_PROGRAMS
                 PROGRAM_LOGD("*** Fast case:\n");
                 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
                 printLongString(shader);
-            }
+#endif
 
             return shader;
         }
@@ -542,10 +542,10 @@
     // End the shader
     shader.append(gFS_Footer);
 
-    if (DEBUG_PROGRAM_CACHE) {
+#if DEBUG_PROGRAMS
         PROGRAM_LOGD("*** Generated fragment shader:\n\n");
         printLongString(shader);
-    }
+#endif
 
     return shader;
 }
diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h
index 186e869..fc3e248 100644
--- a/libs/hwui/ProgramCache.h
+++ b/libs/hwui/ProgramCache.h
@@ -25,6 +25,7 @@
 
 #include <SkXfermode.h>
 
+#include "Debug.h"
 #include "Program.h"
 
 namespace android {
@@ -35,10 +36,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 // Debug
-#define DEBUG_PROGRAM_CACHE 0
-
-// Debug
-#if DEBUG_PROGRAM_CACHE
+#if DEBUG_PROGRAMS
     #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__)
 #else
     #define PROGRAM_LOGD(...)
diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h
index 5c314fc..2ee88b1 100644
--- a/libs/hwui/TextureCache.h
+++ b/libs/hwui/TextureCache.h
@@ -19,6 +19,7 @@
 
 #include <SkBitmap.h>
 
+#include "Debug.h"
 #include "Texture.h"
 #include "utils/GenerationCache.h"
 
@@ -30,9 +31,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 // Debug
-#define DEBUG_TEXTURES 0
-
-// Debug
 #if DEBUG_TEXTURES
     #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__)
 #else