Correctly remove unused paths from the cache.

Change-Id: I41d9334dcd9871634037344ab49bf69383498161
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index 770e596..d08df44 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -127,7 +127,9 @@
     }
 
     mPathHeap = recorder.mPathHeap;
-    mPathHeap->safeRef();
+    if (mPathHeap) {
+        mPathHeap->safeRef();
+    }
 }
 
 DisplayList::~DisplayList() {
@@ -155,7 +157,12 @@
     }
     mMatrices.clear();
 
-    mPathHeap->safeUnref();
+    if (mPathHeap) {
+        for (int i = 0; i < mPathHeap->count(); i++) {
+            caches.pathCache.remove(&(*mPathHeap)[i]);
+        }
+        mPathHeap->safeUnref();
+    }
 }
 
 void DisplayList::init() {
diff --git a/libs/hwui/PathCache.cpp b/libs/hwui/PathCache.cpp
index b58785a..04d07db 100644
--- a/libs/hwui/PathCache.cpp
+++ b/libs/hwui/PathCache.cpp
@@ -92,10 +92,13 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) {
-    const uint32_t size = texture->width * texture->height;
-    mSize -= size;
-
     if (texture) {
+        const uint32_t size = texture->width * texture->height;
+        mSize -= size;
+
+        PATH_LOGD("PathCache::callback: delete path: name, size, mSize = %d, %d, %d",
+                texture->id, size, mSize);
+
         glDeleteTextures(1, &texture->id);
         delete texture;
     }
@@ -107,12 +110,18 @@
 
 void PathCache::remove(SkPath* path) {
     Mutex::Autolock _l(mLock);
+
     // TODO: Linear search...
+    Vector<uint32_t> pathsToRemove;
     for (uint32_t i = 0; i < mCache.size(); i++) {
         if (mCache.getKeyAt(i).path == path) {
-            mCache.removeAt(i);
+            pathsToRemove.push(i);
         }
     }
+
+    for (size_t i = 0; i < pathsToRemove.size(); i++) {
+        mCache.removeAt(pathsToRemove.itemAt(i));
+    }
 }
 
 PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
@@ -188,6 +197,8 @@
     if (size < mMaxSize) {
         mLock.lock();
         mSize += size;
+        PATH_LOGD("PathCache::get: create path: name, size, mSize = %d, %d, %d",
+                texture->id, size, mSize);
         mCache.put(entry, texture);
         mLock.unlock();
     } else {
diff --git a/libs/hwui/PathCache.h b/libs/hwui/PathCache.h
index db5ce08..aea71cb 100644
--- a/libs/hwui/PathCache.h
+++ b/libs/hwui/PathCache.h
@@ -28,6 +28,24 @@
 namespace android {
 namespace uirenderer {
 
+///////////////////////////////////////////////////////////////////////////////
+// Defines
+///////////////////////////////////////////////////////////////////////////////
+
+// Debug
+#define DEBUG_PATHS 0
+
+// Debug
+#if DEBUG_PATHS
+    #define PATH_LOGD(...) LOGD(__VA_ARGS__)
+#else
+    #define PATH_LOGD(...)
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
+// Classes
+///////////////////////////////////////////////////////////////////////////////
+
 /**
  * Describe a path in the path cache.
  */
diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp
index 0c7948f..2497925 100644
--- a/libs/hwui/TextureCache.cpp
+++ b/libs/hwui/TextureCache.cpp
@@ -94,8 +94,8 @@
     // This will be called already locked
     if (texture) {
         mSize -= texture->bitmapSize;
-        TEXTURE_LOGD("TextureCache::callback: removed size, mSize = %d, %d",
-                texture->bitmapSize, mSize);
+        TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
+                texture->id, texture->bitmapSize, mSize);
         glDeleteTextures(1, &texture->id);
         delete texture;
     }
@@ -133,8 +133,8 @@
         if (size < mMaxSize) {
             mLock.lock();
             mSize += size;
-            TEXTURE_LOGD("TextureCache::get: create texture(0x%p): size, mSize = %d, %d",
-                     bitmap, size, mSize);
+            TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
+                     bitmap, texture->id, size, mSize);
             mCache.put(bitmap, texture);
             mLock.unlock();
         } else {
diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h
index d9d2387..5c314fc 100644
--- a/libs/hwui/TextureCache.h
+++ b/libs/hwui/TextureCache.h
@@ -39,6 +39,10 @@
     #define TEXTURE_LOGD(...)
 #endif
 
+///////////////////////////////////////////////////////////////////////////////
+// Classes
+///////////////////////////////////////////////////////////////////////////////
+
 /**
  * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
  * Any texture added to the cache causing the cache to grow beyond the maximum