Work around un-safe downcast in PromiseLazyInstantiateCallback destructor after a context abandon

90% of this CL is just renaming. The only two substantive changes are:
   holding a GrTexture* (rather than a GrGpuResource*) in the GrTextureFreedMessage (this avoids the downcast)
   adding the abandoned() checks to the two GrContext::purgeUnlockedResources methods (this prevents accesses of the message queue)

Bug: 1011368, 1014993
Change-Id: Ifff3842f7802add6b7cd34595578369543698ba1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/249001
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrResourceCache.cpp b/src/gpu/GrResourceCache.cpp
index e6655d2..c7b91b9 100644
--- a/src/gpu/GrResourceCache.cpp
+++ b/src/gpu/GrResourceCache.cpp
@@ -27,7 +27,7 @@
 
 DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
 
-DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
+DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage);
 
 #define ASSERT_SINGLE_OWNER \
     SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
@@ -72,47 +72,47 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
-inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref() = default;
+inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
 
-inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(GrGpuResource* resource)
-        : fResource(resource), fNumUnrefs(1) {}
+inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
+        : fTexture(texture), fNumUnrefs(1) {}
 
-inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(ResourceAwaitingUnref&& that) {
-    fResource = skstd::exchange(that.fResource, nullptr);
+inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
+    fTexture = skstd::exchange(that.fTexture, nullptr);
     fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
 }
 
-inline GrResourceCache::ResourceAwaitingUnref& GrResourceCache::ResourceAwaitingUnref::operator=(
-        ResourceAwaitingUnref&& that) {
-    fResource = skstd::exchange(that.fResource, nullptr);
+inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
+        TextureAwaitingUnref&& that) {
+    fTexture = skstd::exchange(that.fTexture, nullptr);
     fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
     return *this;
 }
 
-inline GrResourceCache::ResourceAwaitingUnref::~ResourceAwaitingUnref() {
-    if (fResource) {
+inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
+    if (fTexture) {
         for (int i = 0; i < fNumUnrefs; ++i) {
-            fResource->unref();
+            fTexture->unref();
         }
     }
 }
 
-inline void GrResourceCache::ResourceAwaitingUnref::addRef() { ++fNumUnrefs; }
+inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
 
-inline void GrResourceCache::ResourceAwaitingUnref::unref() {
+inline void GrResourceCache::TextureAwaitingUnref::unref() {
     SkASSERT(fNumUnrefs > 0);
-    fResource->unref();
+    fTexture->unref();
     --fNumUnrefs;
 }
 
-inline bool GrResourceCache::ResourceAwaitingUnref::finished() { return !fNumUnrefs; }
+inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
 
 //////////////////////////////////////////////////////////////////////////////
 
 GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
                                  uint32_t contextUniqueID)
         : fInvalidUniqueKeyInbox(contextUniqueID)
-        , fFreedGpuResourceInbox(contextUniqueID)
+        , fFreedTextureInbox(contextUniqueID)
         , fContextUniqueID(contextUniqueID)
         , fSingleOwner(singleOwner)
         , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
@@ -204,7 +204,7 @@
 
     // We need to make sure to free any resources that were waiting on a free message but never
     // received one.
-    fResourcesAwaitingUnref.reset();
+    fTexturesAwaitingUnref.reset();
 
     while (fNonpurgeableResources.count()) {
         GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
@@ -226,7 +226,7 @@
     SkASSERT(!fBudgetedCount);
     SkASSERT(!fBudgetedBytes);
     SkASSERT(!fPurgeableBytes);
-    SkASSERT(!fResourcesAwaitingUnref.count());
+    SkASSERT(!fTexturesAwaitingUnref.count());
 }
 
 void GrResourceCache::releaseAll() {
@@ -236,7 +236,7 @@
 
     // We need to make sure to free any resources that were waiting on a free message but never
     // received one.
-    fResourcesAwaitingUnref.reset();
+    fTexturesAwaitingUnref.reset();
 
     SkASSERT(fProxyProvider); // better have called setProxyProvider
     // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
@@ -263,7 +263,7 @@
     SkASSERT(!fBudgetedCount);
     SkASSERT(!fBudgetedBytes);
     SkASSERT(!fPurgeableBytes);
-    SkASSERT(!fResourcesAwaitingUnref.count());
+    SkASSERT(!fTexturesAwaitingUnref.count());
 }
 
 void GrResourceCache::refResource(GrGpuResource* resource) {
@@ -624,23 +624,23 @@
 }
 
 
-void GrResourceCache::insertDelayedResourceUnref(GrGpuResource* resource) {
-    resource->ref();
-    uint32_t id = resource->uniqueID().asUInt();
-    if (auto* data = fResourcesAwaitingUnref.find(id)) {
+void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
+    texture->ref();
+    uint32_t id = texture->uniqueID().asUInt();
+    if (auto* data = fTexturesAwaitingUnref.find(id)) {
         data->addRef();
     } else {
-        fResourcesAwaitingUnref.set(id, {resource});
+        fTexturesAwaitingUnref.set(id, {texture});
     }
 }
 
 void GrResourceCache::processFreedGpuResources() {
-    SkTArray<GrGpuResourceFreedMessage> msgs;
-    fFreedGpuResourceInbox.poll(&msgs);
+    SkTArray<GrTextureFreedMessage> msgs;
+    fFreedTextureInbox.poll(&msgs);
     for (int i = 0; i < msgs.count(); ++i) {
         SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
-        uint32_t id = msgs[i].fResource->uniqueID().asUInt();
-        ResourceAwaitingUnref* info = fResourcesAwaitingUnref.find(id);
+        uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
+        TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
         // If we called release or abandon on the GrContext we will have already released our ref on
         // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
         // we will try to process the message when we destroy the GrContext. This protects us from
@@ -648,7 +648,7 @@
         if (info) {
             info->unref();
             if (info->finished()) {
-                fResourcesAwaitingUnref.remove(id);
+                fTexturesAwaitingUnref.remove(id);
             }
         }
     }