ccpr: Unregister path listeners when their cache entries are evicted

Bug: skia:8452
Change-Id: I5cf63c07481db38fc37e920e04ca140bad8966e4
Reviewed-on: https://skia-review.googlesource.com/c/163560
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/core/SkPathRef.cpp b/src/core/SkPathRef.cpp
index f767323..85d1b9c 100644
--- a/src/core/SkPathRef.cpp
+++ b/src/core/SkPathRef.cpp
@@ -707,19 +707,31 @@
     if (nullptr == listener || this == gEmpty) {
         return;
     }
+
     SkAutoMutexAcquire lock(fGenIDChangeListenersMutex);
-    *fGenIDChangeListeners.append() = listener.release();
+
+    // Clean out any stale listeners before we append the new one.
+    for (int i = 0; i < fGenIDChangeListeners.count(); ++i) {
+        if (fGenIDChangeListeners[i]->shouldUnregisterFromPath()) {
+            fGenIDChangeListeners.removeShuffle(i--);  // No need to preserve the order after i.
+        }
+    }
+
+    SkASSERT(!listener->shouldUnregisterFromPath());
+    fGenIDChangeListeners.push_back(std::move(listener));
 }
 
 // we need to be called *before* the genID gets changed or zerod
 void SkPathRef::callGenIDChangeListeners() {
     SkAutoMutexAcquire lock(fGenIDChangeListenersMutex);
-    for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
-        fGenIDChangeListeners[i]->onChange();
+    for (const sk_sp<GenIDChangeListener>& listener : fGenIDChangeListeners) {
+        if (!listener->shouldUnregisterFromPath()) {
+            listener->onChange();
+        }
     }
 
     // Listeners get at most one shot, so whether these triggered or not, blow them away.
-    fGenIDChangeListeners.unrefAll();
+    fGenIDChangeListeners.reset();
 }
 
 SkRRect SkPathRef::getRRect() const {
diff --git a/src/gpu/ccpr/GrCCPathCache.cpp b/src/gpu/ccpr/GrCCPathCache.cpp
index db85641..32008c2 100644
--- a/src/gpu/ccpr/GrCCPathCache.cpp
+++ b/src/gpu/ccpr/GrCCPathCache.cpp
@@ -187,12 +187,15 @@
 }
 
 void GrCCPathCache::evict(GrCCPathCacheEntry* entry) {
-    bool isInCache = entry->fNext || (fLRU.tail() == entry);
-    SkASSERT(isInCache == fLRU.isInList(entry));
-    if (isInCache) {
-        fLRU.remove(entry);
-        fHashTable.remove(HashNode::GetKey(entry));  // Do this last, as it might delete the entry.
+    // Has the entry already been evicted? (SkPaths can post eviction messages from any thread.)
+    if (entry->shouldUnregisterFromPath()) {
+        SkASSERT(!fLRU.isInList(entry));
+        return;
     }
+
+    entry->markShouldUnregisterFromPath();
+    fLRU.remove(entry);
+    fHashTable.remove(HashNode::GetKey(entry));  // Do this last, as it might delete the entry.
 }
 
 void GrCCPathCache::purgeAsNeeded() {