Increase encapsulation of GrThreadSafeCache::Entry

Prepare this class to hold either a proxyView or a vertex blob

Bug: 1108408
Change-Id: Ib6abb4da64ccc70b9e2af2546e1b071396dd42cb
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/328836
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrThreadSafeCache.cpp b/src/gpu/GrThreadSafeCache.cpp
index e63a5cd..a275075 100644
--- a/src/gpu/GrThreadSafeCache.cpp
+++ b/src/gpu/GrThreadSafeCache.cpp
@@ -60,8 +60,8 @@
             return;
         }
 
-        if (cur->fView.proxy()->unique()) {
-            fUniquelyKeyedEntryMap.remove(cur->fKey);
+        if (cur->uniquelyHeld()) {
+            fUniquelyKeyedEntryMap.remove(cur->key());
             fUniquelyKeyedEntryList.remove(cur);
             this->recycleEntry(cur);
         }
@@ -84,8 +84,8 @@
             return;
         }
 
-        if (cur->fView.proxy()->unique()) {
-            fUniquelyKeyedEntryMap.remove(cur->fKey);
+        if (cur->uniquelyHeld()) {
+            fUniquelyKeyedEntryMap.remove(cur->key());
             fUniquelyKeyedEntryList.remove(cur);
             this->recycleEntry(cur);
         }
@@ -104,7 +104,7 @@
         tmp->fLastAccess = GrStdSteadyClock::now();
         fUniquelyKeyedEntryList.remove(tmp);
         fUniquelyKeyedEntryList.addToHead(tmp);
-        return { tmp->fView, tmp->fKey.refCustomData() };
+        return { tmp->view(), tmp->refCustomData() };
     }
 
     return {};
@@ -134,8 +134,7 @@
         fFreeEntryList = entry->fNext;
         entry->fNext = nullptr;
 
-        entry->fKey = key;
-        entry->fView = view;
+        entry->set(key, view);
     } else {
         entry = fEntryAllocator.make<Entry>(key, view);
     }
@@ -150,8 +149,7 @@
 void GrThreadSafeCache::recycleEntry(Entry* dead) {
     SkASSERT(!dead->fPrev && !dead->fNext && !dead->fList);
 
-    dead->fKey.reset();
-    dead->fView.reset();
+    dead->makeEmpty();
 
     dead->fNext = fFreeEntryList;
     fFreeEntryList = dead;
@@ -167,7 +165,7 @@
         SkASSERT(fUniquelyKeyedEntryMap.find(key));
     }
 
-    return { tmp->fView, tmp->fKey.refCustomData() };
+    return { tmp->view(), tmp->refCustomData() };
 }
 
 GrSurfaceProxyView GrThreadSafeCache::add(const GrUniqueKey& key, const GrSurfaceProxyView& view) {
diff --git a/src/gpu/GrThreadSafeCache.h b/src/gpu/GrThreadSafeCache.h
index 15e775e..e2d506e 100644
--- a/src/gpu/GrThreadSafeCache.h
+++ b/src/gpu/GrThreadSafeCache.h
@@ -110,18 +110,73 @@
                                                                             SkBackingFit);
 private:
     struct Entry {
-        Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
+        Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view)
+                : fKey(key)
+                , fView(view)
+                , fTag(Entry::kView) {
+        }
 
-        // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
-        GrUniqueKey                  fKey;
-        GrSurfaceProxyView           fView;
+        bool uniquelyHeld() const {
+            SkASSERT(fTag != kEmpty);
+
+            if (fTag == kView && fView.proxy()->unique()) {
+                return true;
+            }
+
+            return false;
+        }
+
+        const GrUniqueKey& key() const {
+            SkASSERT(fTag != kEmpty);
+            return fKey;
+        }
+
+        sk_sp<SkData> refCustomData() const {
+            SkASSERT(fTag != kEmpty);
+            return fKey.refCustomData();
+        }
+
+        GrSurfaceProxyView view() {
+            SkASSERT(fTag == kView);
+            return fView;
+        }
+
+        void set(const GrUniqueKey& key, const GrSurfaceProxyView& view) {
+            SkASSERT(fTag == kEmpty);
+            fKey = key;
+            fView = view;
+            fTag = kView;
+        }
+
+        void makeEmpty() {
+            SkASSERT(fTag != kEmpty);
+
+            fKey.reset();
+            fView.reset();
+            fTag = kEmpty;
+        }
+
+        // The thread-safe cache gets to manipulate the llist and last-access members
         GrStdSteadyClock::time_point fLastAccess;
 
         SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry);
 
         // for SkTDynamicHash
-        static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; }
+        static const GrUniqueKey& GetKey(const Entry& e) {
+            SkASSERT(e.fTag != kEmpty);
+            return e.fKey;
+        }
         static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
+
+    private:
+        // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
+        GrUniqueKey                  fKey;
+        GrSurfaceProxyView           fView;
+
+        enum {
+            kEmpty,
+            kView,
+        } fTag { kEmpty };
     };
 
     Entry* getEntry(const GrUniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);