Merge "Fix native memory leak caused by small HWUI path cache creation"
diff --git a/libs/hwui/PathCache.cpp b/libs/hwui/PathCache.cpp
index 3236f6f..ec86101 100644
--- a/libs/hwui/PathCache.cpp
+++ b/libs/hwui/PathCache.cpp
@@ -138,7 +138,7 @@
 
 PathCache::PathCache():
         mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity),
-        mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
+        mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)), mTexNum(0) {
     char property[PROPERTY_VALUE_MAX];
     if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) {
         INIT_LOGD("  Setting %s cache size to %sMB", name, property);
@@ -202,6 +202,7 @@
                         "the cache in an inconsistent state", size);
             }
             mSize -= size;
+            mTexNum--;
         }
 
         PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
@@ -228,7 +229,7 @@
 }
 
 void PathCache::trim() {
-    while (mSize > mMaxSize) {
+    while (mSize > mMaxSize || mTexNum > DEFAULT_PATH_TEXTURE_CAP) {
         mCache.removeOldest();
     }
 }
@@ -299,6 +300,7 @@
 
     texture->setFilter(GL_LINEAR);
     texture->setWrap(GL_CLAMP_TO_EDGE);
+    mTexNum++;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/PathCache.h b/libs/hwui/PathCache.h
index 86c59b5..b361424 100644
--- a/libs/hwui/PathCache.h
+++ b/libs/hwui/PathCache.h
@@ -306,6 +306,12 @@
 
     bool mDebugEnabled;
 
+    /**
+     * Driver allocated 4k/8k/16k memory for small path cache,
+     * limit the number of PathTexture in case occupy too much memory in hardware.
+     */
+    uint32_t mTexNum;
+
     sp<PathProcessor> mProcessor;
 
     Vector<uint32_t> mGarbage;
diff --git a/libs/hwui/Properties.h b/libs/hwui/Properties.h
index 5dea493..64dcc7d 100644
--- a/libs/hwui/Properties.h
+++ b/libs/hwui/Properties.h
@@ -249,6 +249,9 @@
 #define DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD 64
 #define DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD 192
 
+// cap to 256 to limite paths in the path cache
+#define DEFAULT_PATH_TEXTURE_CAP 256
+
 ///////////////////////////////////////////////////////////////////////////////
 // Misc
 ///////////////////////////////////////////////////////////////////////////////