Setup for another attempt to split up opLists

Split out of: https://skia-review.googlesource.com/c/14186 (Split up opLists (take 3))

Change-Id: Ifa600c88fb9185991d3197c7776c820f54c9bf0f
Reviewed-on: https://skia-review.googlesource.com/16540
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/include/private/GrSurfaceProxy.h b/include/private/GrSurfaceProxy.h
index b2ccfe0..29bb862 100644
--- a/include/private/GrSurfaceProxy.h
+++ b/include/private/GrSurfaceProxy.h
@@ -45,27 +45,24 @@
             fTarget->unref();
         }
 
-        if (!(--fRefCnt)) {
-            delete this;
-            return;
-        }
-
-        this->validate();
+        --fRefCnt;
+        this->didRemoveRefOrPendingIO();
     }
 
     void validate() const {
-#ifdef SK_DEBUG    
-        SkASSERT(fRefCnt >= 1);
+#ifdef SK_DEBUG
+        SkASSERT(fRefCnt >= 0);
         SkASSERT(fPendingReads >= 0);
         SkASSERT(fPendingWrites >= 0);
         SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
 
         if (fTarget) {
-            SkASSERT(!fPendingReads && !fPendingWrites);
             // The backing GrSurface can have more refs than the proxy if the proxy
             // started off wrapping an external resource (that came in with refs).
             // The GrSurface should never have fewer refs than the proxy however.
             SkASSERT(fTarget->fRefCnt >= fRefCnt);
+            SkASSERT(fTarget->fPendingReads >= fPendingReads);
+            SkASSERT(fTarget->fPendingWrites >= fPendingWrites);
         }
 #endif
     }
@@ -96,9 +93,6 @@
         fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
         fTarget->fPendingReads += fPendingReads;
         fTarget->fPendingWrites += fPendingWrites;
-
-        fPendingReads = 0;
-        fPendingWrites = 0;
     }
 
     bool internalHasPendingIO() const {
@@ -123,18 +117,16 @@
 
 private:
     // This class is used to manage conversion of refs to pending reads/writes.
-    friend class GrTextureProxyRef;
+    friend class GrSurfaceProxyRef;
     template <typename, GrIOType> friend class GrPendingIOResource;
 
     void addPendingRead() const {
         this->validate();
 
+        ++fPendingReads;
         if (fTarget) {
             fTarget->addPendingRead();
-            return;
         }
-
-        ++fPendingReads;
     }
 
     void completedRead() const {
@@ -142,21 +134,19 @@
 
         if (fTarget) {
             fTarget->completedRead();
-            return;
         }
-    
-        SkFAIL("How was the read completed if the Proxy hasn't been instantiated?");
+
+        --fPendingReads;
+        this->didRemoveRefOrPendingIO();
     }
 
     void addPendingWrite() const {
         this->validate();
 
+        ++fPendingWrites;
         if (fTarget) {
             fTarget->addPendingWrite();
-            return;
         }
-
-        ++fPendingWrites;
     }
 
     void completedWrite() const {
@@ -164,10 +154,16 @@
 
         if (fTarget) {
             fTarget->completedWrite();
-            return;
         }
-    
-        SkFAIL("How was the write completed if the Proxy hasn't been instantiated?");
+
+        --fPendingWrites;
+        this->didRemoveRefOrPendingIO();
+    }
+
+    void didRemoveRefOrPendingIO() const {
+        if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
+            delete this;
+        }
     }
 
     mutable int32_t fRefCnt;
@@ -374,14 +370,13 @@
     mutable size_t      fGpuMemorySize;
 
     // The last opList that wrote to or is currently going to write to this surface
-    // The opList can be closed (e.g., no render target context is currently bound
-    // to this renderTarget).
+    // The opList can be closed (e.g., no surface context is currently bound
+    // to this proxy).
     // This back-pointer is required so that we can add a dependancy between
     // the opList used to create the current contents of this surface
     // and the opList of a destination surface to which this one is being drawn or copied.
     GrOpList* fLastOpList;
 
-
     typedef GrIORefProxy INHERITED;
 };