Replace GrResourceCache with GrResourceCache2.

BUG=skia:2889

Committed: https://skia.googlesource.com/skia/+/66a450f21a3da174b7eed89a1d5fc8591e8b6ee6

Committed: https://skia.googlesource.com/skia/+/407aa584d183c1bf314f5defd1cf0202e8a96c89

Review URL: https://codereview.chromium.org/716143004
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 886a68a..0a742ed 100755
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -25,7 +25,6 @@
 #include "GrOvalRenderer.h"
 #include "GrPathRenderer.h"
 #include "GrPathUtils.h"
-#include "GrResourceCache.h"
 #include "GrResourceCache2.h"
 #include "GrSoftwarePathRenderer.h"
 #include "GrStencilBuffer.h"
@@ -52,9 +51,6 @@
     #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
 #endif
 
-static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT;
-static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024;
-
 static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
 static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
 
@@ -103,7 +99,6 @@
     fClip = NULL;
     fPathRendererChain = NULL;
     fSoftwarePathRenderer = NULL;
-    fResourceCache = NULL;
     fResourceCache2 = NULL;
     fFontCache = NULL;
     fDrawBuffer = NULL;
@@ -130,11 +125,8 @@
 void GrContext::initCommon() {
     fDrawState = SkNEW(GrDrawState);
 
-    fResourceCache = SkNEW_ARGS(GrResourceCache, (fGpu->caps(),
-                                                  MAX_RESOURCE_CACHE_COUNT,
-                                                  MAX_RESOURCE_CACHE_BYTES));
-    fResourceCache->setOverbudgetCallback(OverbudgetCB, this);
     fResourceCache2 = SkNEW(GrResourceCache2);
+    fResourceCache2->setOverBudgetCallback(OverBudgetCB, this);
 
     fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
 
@@ -160,9 +152,6 @@
     }
 
     SkDELETE(fResourceCache2);
-    fResourceCache2 = NULL;
-    SkDELETE(fResourceCache);
-    fResourceCache = NULL;
     SkDELETE(fFontCache);
     SkDELETE(fDrawBuffer);
     SkDELETE(fDrawBufferVBAllocPool);
@@ -201,8 +190,6 @@
     fAARectRenderer->reset();
     fOvalRenderer->reset();
 
-    fResourceCache->purgeAllUnlocked();
-
     fFontCache->freeAll();
     fLayerCache->freeAll();
 }
@@ -221,7 +208,6 @@
     fAARectRenderer->reset();
     fOvalRenderer->reset();
 
-    fResourceCache->purgeAllUnlocked();
     fFontCache->freeAll();
     fLayerCache->freeAll();
     // a path renderer may be holding onto resources
@@ -230,12 +216,12 @@
 }
 
 void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
-  if (resourceCount) {
-    *resourceCount = fResourceCache->getCachedResourceCount();
-  }
-  if (resourceBytes) {
-    *resourceBytes = fResourceCache->getCachedResourceBytes();
-  }
+    if (resourceCount) {
+        *resourceCount = fResourceCache2->getResourceCount();
+    }
+    if (resourceBytes) {
+        *resourceBytes = fResourceCache2->getResourceBytes();
+    }
 }
 
 GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
@@ -273,12 +259,13 @@
 }
 
 void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
+    // TODO: Make GrStencilBuffers use the scratch mechanism rather than content keys.
     ASSERT_OWNED_RESOURCE(sb);
 
     GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
                                                             sb->height(),
                                                             sb->numSamples());
-    fResourceCache->addResource(resourceKey, sb);
+    SkAssertResult(sb->cacheAccess().setContentKey(resourceKey));
 }
 
 GrStencilBuffer* GrContext::findAndRefStencilBuffer(int width, int height, int sampleCnt) {
@@ -420,25 +407,19 @@
     }
 
     if (texture) {
-        fResourceCache->addResource(resourceKey, texture);
-
-        if (cacheKey) {
-            *cacheKey = resourceKey;
+        if (texture->cacheAccess().setContentKey(resourceKey)) {
+            if (cacheKey) {
+                *cacheKey = resourceKey;
+            }
+        } else {
+            texture->unref();
+            texture = NULL;
         }
     }
 
     return texture;
 }
 
-GrTexture* GrContext::createNewScratchTexture(const GrSurfaceDesc& desc) {
-    GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
-    if (!texture) {
-        return NULL;
-    }
-    fResourceCache->addResource(texture->cacheAccess().getScratchKey(), texture);
-    return texture;
-}
-
 GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexMatch match,
                                         bool calledDuringFlush) {
     // kNoStencil has no meaning if kRT isn't set.
@@ -473,7 +454,6 @@
             }
             GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
             if (resource) {
-                fResourceCache->makeResourceMRU(resource);
                 return static_cast<GrSurface*>(resource)->asTexture();
             }
 
@@ -496,21 +476,19 @@
         desc.writable()->fFlags = origFlags;
     }
 
-    GrTexture* texture = this->createNewScratchTexture(*desc);
+    GrTexture* texture = fGpu->createTexture(*desc, NULL, 0);
     SkASSERT(NULL == texture || 
              texture->cacheAccess().getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
     return texture;
 }
 
-bool GrContext::OverbudgetCB(void* data) {
+void GrContext::OverBudgetCB(void* data) {
     SkASSERT(data);
 
     GrContext* context = reinterpret_cast<GrContext*>(data);
 
     // Flush the InOrderDrawBuffer to possibly free up some textures
     context->fFlushToReduceCacheSize = true;
-
-    return true;
 }
 
 
@@ -522,11 +500,16 @@
 }
 
 void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
-    fResourceCache->getLimits(maxTextures, maxTextureBytes);
+    if (maxTextures) {
+        *maxTextures = fResourceCache2->getMaxResourceCount();
+    }
+    if (maxTextureBytes) {
+        *maxTextureBytes = fResourceCache2->getMaxResourceBytes();
+    }
 }
 
 void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
-    fResourceCache->setLimits(maxTextures, maxTextureBytes);
+    fResourceCache2->setLimits(maxTextures, maxTextureBytes);
 }
 
 int GrContext::getMaxTextureSize() const {
@@ -1242,7 +1225,6 @@
     } else {
         fDrawBuffer->flush();
     }
-    fResourceCache->purgeAsNeeded();
     fFlushToReduceCacheSize = false;
 }
 
@@ -1748,15 +1730,11 @@
 }
 
 void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
-    fResourceCache->addResource(resourceKey, resource);
+    resource->cacheAccess().setContentKey(resourceKey);
 }
 
 GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
-    GrGpuResource* resource = fResourceCache2->findAndRefContentResource(resourceKey);
-    if (resource) {
-        fResourceCache->makeResourceMRU(resource);
-    }
-    return resource;
+    return fResourceCache2->findAndRefContentResource(resourceKey);
 }
 
 void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
@@ -1776,7 +1754,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 #if GR_CACHE_STATS
 void GrContext::printCacheStats() const {
-    fResourceCache->printStats();
+    fResourceCache2->printStats();
 }
 #endif