Revert "Revert of Use the GrCacheable ID to eliminate the need for notifications to GrGpuGL when textures and RTs are… (https://codereview.chromium.org/376703009/)"

This reverts commit 249171e7d29b5559f3eefe9dbd437030bfad3fda.

Uses 32 bit id instead of 64. Renamed instanceID to uniqueID to match existing code.

R=robertphillips@google.com

Author: bsalomon@google.com

Review URL: https://codereview.chromium.org/414493002
diff --git a/src/gpu/GrBitmapTextContext.cpp b/src/gpu/GrBitmapTextContext.cpp
index 25c13ec..3ca98e4 100755
--- a/src/gpu/GrBitmapTextContext.cpp
+++ b/src/gpu/GrBitmapTextContext.cpp
@@ -56,7 +56,7 @@
 
     fCurrTexture = NULL;
     fCurrVertex = 0;
-    fEffectTextureGenID = 0;
+    fEffectTextureUniqueID = SK_InvalidUniqueID;
 
     fVertices = NULL;
     fMaxVertices = 0;
@@ -94,11 +94,11 @@
         SkASSERT(fCurrTexture);
         GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNone_FilterMode);
 
-        uint32_t textureGenID = fCurrTexture->getGenerationID();
+        uint32_t textureUniqueID = fCurrTexture->getUniqueID();
         
-        if (textureGenID != fEffectTextureGenID) {
+        if (textureUniqueID != fEffectTextureUniqueID) {
             fCachedEffect.reset(GrCustomCoordsTextureEffect::Create(fCurrTexture, params));
-            fEffectTextureGenID = textureGenID;
+            fEffectTextureUniqueID = textureUniqueID;
         }
 
         // This effect could be stored with one of the cache objects (atlas?)
diff --git a/src/gpu/GrBitmapTextContext.h b/src/gpu/GrBitmapTextContext.h
index b9d5ce7..6d4ea6d 100644
--- a/src/gpu/GrBitmapTextContext.h
+++ b/src/gpu/GrBitmapTextContext.h
@@ -48,7 +48,8 @@
     int32_t                     fMaxVertices;
     GrTexture*                  fCurrTexture;
     SkAutoTUnref<GrEffect>      fCachedEffect;
-    uint32_t                    fEffectTextureGenID;
+    // Used to check whether fCachedEffect is still valid.
+    uint32_t                    fEffectTextureUniqueID;
     int                         fCurrVertex;
     SkRect                      fVertexBounds;
 };
diff --git a/src/gpu/GrCacheable.cpp b/src/gpu/GrCacheable.cpp
index 120be78..5dc5258 100644
--- a/src/gpu/GrCacheable.cpp
+++ b/src/gpu/GrCacheable.cpp
@@ -8,10 +8,11 @@
 
 #include "GrCacheable.h"
 
-uint32_t GrCacheable::getGenerationID() const {
-    static int32_t gPathRefGenerationID;
-    while (!fGenID) {
-        fGenID = static_cast<uint32_t>(sk_atomic_inc(&gPathRefGenerationID) + 1);
-    }
-    return fGenID;
+uint32_t GrCacheable::CreateUniqueID() {
+    static int32_t gCacheableID = SK_InvalidUniqueID;
+    uint32_t id;
+    do {
+        id = static_cast<uint32_t>(sk_atomic_inc(&gCacheableID) + 1);
+    } while (id == SK_InvalidUniqueID);
+    return id;
 }
diff --git a/src/gpu/gl/GrGLRenderTarget.cpp b/src/gpu/gl/GrGLRenderTarget.cpp
index f2a0cdc..2486396 100644
--- a/src/gpu/gl/GrGLRenderTarget.cpp
+++ b/src/gpu/gl/GrGLRenderTarget.cpp
@@ -79,7 +79,6 @@
 }
 
 void GrGLRenderTarget::onRelease() {
-    GPUGL->notifyRenderTargetDelete(this);
     if (!this->isWrapped()) {
         if (fTexFBOID) {
             GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
diff --git a/src/gpu/gl/GrGLTexture.cpp b/src/gpu/gl/GrGLTexture.cpp
index 856cfb1..255cdd9 100644
--- a/src/gpu/gl/GrGLTexture.cpp
+++ b/src/gpu/gl/GrGLTexture.cpp
@@ -49,7 +49,6 @@
 }
 
 void GrGLTexture::onRelease() {
-    GPUGL->notifyTextureDelete(this);
     fTexIDObj.reset(NULL);
     INHERITED::onRelease();
 }
diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp
index d29c141..a0e08e5 100644
--- a/src/gpu/gl/GrGpuGL.cpp
+++ b/src/gpu/gl/GrGpuGL.cpp
@@ -136,7 +136,7 @@
     SkASSERT(ctx.isInitialized());
     fCaps.reset(SkRef(ctx.caps()));
 
-    fHWBoundTextures.reset(this->glCaps().maxFragmentTextureUnits());
+    fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
     fHWPathTexGenSettings.reset(this->glCaps().maxFixedFunctionTextureCoords());
 
     GrGLClearErr(fGLContext.interface());
@@ -287,8 +287,8 @@
     fHWActiveTextureUnitIdx = -1; // invalid
 
     if (resetBits & kTextureBinding_GrGLBackendState) {
-        for (int s = 0; s < fHWBoundTextures.count(); ++s) {
-            fHWBoundTextures[s] = NULL;
+        for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
+            fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
         }
     }
 
@@ -312,7 +312,7 @@
     }
 
     if (resetBits & kRenderTarget_GrGLBackendState) {
-        fHWBoundRenderTarget = NULL;
+        fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
     }
 
     if (resetBits & kPathRendering_GrGLBackendState) {
@@ -889,7 +889,7 @@
     }
 
     // below here we may bind the FBO
-    fHWBoundRenderTarget = NULL;
+    fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
     if (desc->fRTFBOID != desc->fTexFBOID) {
         SkASSERT(desc->fSampleCnt > 0);
         GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
@@ -1274,7 +1274,7 @@
         GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
         GrGLuint rb = glsb->renderbufferID();
 
-        fHWBoundRenderTarget = NULL;
+        fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
         GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
                                         GR_GL_STENCIL_ATTACHMENT,
@@ -1485,8 +1485,8 @@
     }
 
     GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
-    if (renderTarget != fHWBoundRenderTarget) {
-        fHWBoundRenderTarget = NULL;
+    if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
+        fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
     }
     switch (this->glCaps().invalidateFBType()) {
@@ -1749,7 +1749,8 @@
         static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
     SkASSERT(NULL != rt);
 
-    if (fHWBoundRenderTarget != rt) {
+    uint32_t rtID = rt->getUniqueID();
+    if (fHWBoundRenderTargetUniqueID != rtID) {
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
 #ifdef SK_DEBUG
         // don't do this check in Chromium -- this is causing
@@ -1764,7 +1765,7 @@
             }
         }
 #endif
-        fHWBoundRenderTarget = rt;
+        fHWBoundRenderTargetUniqueID = rtID;
         const GrGLIRect& vp = rt->getViewport();
         if (fHWViewport != vp) {
             vp.pushToGLViewport(this->glInterface());
@@ -2007,7 +2008,7 @@
             GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
             // make sure we go through flushRenderTarget() since we've modified
             // the bound DRAW FBO ID.
-            fHWBoundRenderTarget = NULL;
+            fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
             const GrGLIRect& vp = rt->getViewport();
             const SkIRect dirtyRect = rt->getResolveRect();
             GrGLIRect r;
@@ -2301,10 +2302,11 @@
         this->onResolveRenderTarget(texRT);
     }
 
-    if (fHWBoundTextures[unitIdx] != texture) {
+    uint32_t textureID = texture->getUniqueID();
+    if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
         this->setTextureUnit(unitIdx);
         GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
-        fHWBoundTextures[unitIdx] = texture;
+        fHWBoundTextureUniqueIDs[unitIdx] = textureID;
     }
 
     ResetTimestamp timestamp;
@@ -2532,23 +2534,6 @@
     }
 }
 
-void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
-    SkASSERT(NULL != renderTarget);
-    if (fHWBoundRenderTarget == renderTarget) {
-        fHWBoundRenderTarget = NULL;
-    }
-}
-
-void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
-    for (int s = 0; s < fHWBoundTextures.count(); ++s) {
-        if (fHWBoundTextures[s] == texture) {
-            // deleting bound texture does implied bind to 0
-            fHWBoundTextures[s] = NULL;
-       }
-    }
-}
-
-
 GrGLuint GrGpuGL::createGLPathObject() {
     if (NULL == fPathNameAllocator.get()) {
         static const int range = 65536;
@@ -2719,7 +2704,7 @@
 }
 
 void GrGpuGL::setTextureUnit(int unit) {
-    SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
+    SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
     if (unit != fHWActiveTextureUnitIdx) {
         GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
         fHWActiveTextureUnitIdx = unit;
@@ -2728,14 +2713,14 @@
 
 void GrGpuGL::setScratchTextureUnit() {
     // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
-    int lastUnitIdx = fHWBoundTextures.count() - 1;
+    int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
     if (lastUnitIdx != fHWActiveTextureUnitIdx) {
         GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
         fHWActiveTextureUnitIdx = lastUnitIdx;
     }
     // clear out the this field so that if a program does use this unit it will rebind the correct
     // texture.
-    fHWBoundTextures[lastUnitIdx] = NULL;
+    fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
 }
 
 namespace {
@@ -2872,7 +2857,7 @@
         GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
         SkASSERT(NULL != dstTex);
         // We modified the bound FBO
-        fHWBoundRenderTarget = NULL;
+        fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
         GrGLIRect srcGLRect;
         srcGLRect.setRelativeTo(srcVP,
                                 srcRect.fLeft,
@@ -2914,7 +2899,7 @@
             dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
             srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
             // We modified the bound FBO
-            fHWBoundRenderTarget = NULL;
+            fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
             GrGLIRect srcGLRect;
             GrGLIRect dstGLRect;
             srcGLRect.setRelativeTo(srcVP,
diff --git a/src/gpu/gl/GrGpuGL.h b/src/gpu/gl/GrGpuGL.h
index 526e1ca..f41903a 100644
--- a/src/gpu/gl/GrGpuGL.h
+++ b/src/gpu/gl/GrGpuGL.h
@@ -104,8 +104,6 @@
     void notifyIndexBufferDelete(GrGLuint id) {
         fHWGeometryState.notifyIndexBufferDelete(id);
     }
-    void notifyTextureDelete(GrGLTexture* texture);
-    void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
 
     // These functions should be used to generate and delete GL path names. They have their own
     // allocator that runs on the client side, so they are much faster than going through GenPaths.
@@ -465,8 +463,8 @@
     GrDrawState::DrawFace       fHWDrawFace;
     TriState                    fHWWriteToColor;
     TriState                    fHWDitherEnabled;
-    GrRenderTarget*             fHWBoundRenderTarget;
-    SkTArray<GrTexture*, true>  fHWBoundTextures;
+    uint32_t                    fHWBoundRenderTargetUniqueID;
+    SkTArray<uint32_t, true>    fHWBoundTextureUniqueIDs;
 
     struct PathTexGenData {
         GrGLenum  fMode;