Avoid 9patch cache lookups when possible

This optimization saves up to 0.3ms per frame on the Play Store's
front page, on a Nexus 4 device.

Change-Id: Iaa4ef33c6e3b37e175efd5b9eea9ef59b43f14f3
diff --git a/libs/hwui/PatchCache.cpp b/libs/hwui/PatchCache.cpp
index c6ed275..c23e991 100644
--- a/libs/hwui/PatchCache.cpp
+++ b/libs/hwui/PatchCache.cpp
@@ -30,7 +30,9 @@
 // Constructors/destructor
 ///////////////////////////////////////////////////////////////////////////////
 
-PatchCache::PatchCache(): mCache(LruCache<PatchDescription, Patch*>::kUnlimitedCapacity) {
+PatchCache::PatchCache():
+        mSize(0), mCache(LruCache<PatchDescription, Patch*>::kUnlimitedCapacity),
+        mMeshBuffer(0), mGenerationId(0) {
     char property[PROPERTY_VALUE_MAX];
     if (property_get(PROPERTY_PATCH_CACHE_SIZE, property, NULL) > 0) {
         INIT_LOGD("  Setting patch cache size to %skB", property);
@@ -39,8 +41,6 @@
         INIT_LOGD("  Using default patch cache size of %.2fkB", DEFAULT_PATCH_CACHE_SIZE);
         mMaxSize = KB(DEFAULT_PATCH_CACHE_SIZE);
     }
-    mSize = 0;
-    mMeshBuffer = 0;
 }
 
 PatchCache::~PatchCache() {
@@ -58,7 +58,7 @@
     caches.resetVertexPointers();
 
     if (created) {
-        glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW);
+        createVertexBuffer();
     }
 }
 
@@ -99,6 +99,12 @@
     mCache.clear();
 }
 
+void PatchCache::createVertexBuffer() {
+    glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW);
+    mSize = 0;
+    mGenerationId++;
+}
+
 const Patch* PatchCache::get(const AssetAtlas::Entry* entry,
         const uint32_t bitmapWidth, const uint32_t bitmapHeight,
         const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch) {
@@ -127,8 +133,7 @@
             uint32_t size = newMesh->getSize();
             if (mSize + size > mMaxSize) {
                 clearCache();
-                glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW);
-                mSize = 0;
+                createVertexBuffer();
             }
 
             newMesh->offset = (GLintptr) mSize;