Revert "Use scratch keys for stencil buffers."

This reverts commit 91175f19664a62851da4ca4e0984a7c7c45b258f.

Revert "Cleanup res cache bench and split out into a unit test."

This reverts commit 4e4303f002c5958c6c958e7ba8e49b24c25f0b22.

Revert "rebaselines"

This reverts commit 65ba7b57759bfca60b24bc34dc46fc8caaf146f0.

TBR=tomhudson@google.com

Review URL: https://codereview.chromium.org/752233002
diff --git a/bench/GrResourceCacheBench.cpp b/bench/GrResourceCacheBench.cpp
index e30dd30..e1ec90d 100644
--- a/bench/GrResourceCacheBench.cpp
+++ b/bench/GrResourceCacheBench.cpp
@@ -14,63 +14,181 @@
 #include "GrContext.h"
 #include "GrGpu.h"
 #include "GrResourceCache2.h"
+#include "GrStencilBuffer.h"
+#include "GrTexture.h"
+#include "GrTexturePriv.h"
 #include "SkCanvas.h"
 
 enum {
-    CACHE_SIZE_COUNT = 4096,
+    CACHE_SIZE_COUNT = 2048,
+    CACHE_SIZE_BYTES = 2 * 1024 * 1024,
 };
 
-class BenchResource : public GrGpuResource {
+class StencilResource : public GrGpuResource {
 public:
-    SK_DECLARE_INST_COUNT(BenchResource);
-    BenchResource (GrGpu* gpu)
-        : INHERITED(gpu, false) {
+    SK_DECLARE_INST_COUNT(StencilResource);
+    StencilResource(GrGpu* gpu, int id)
+        : INHERITED(gpu, false)
+        , fID(id) {
         this->registerWithCache();
     }
 
-    static GrResourceKey ComputeKey(int i) {
+    static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
+        return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
+    }
+
+    int fID;
+
+private:
+    virtual size_t onGpuMemorySize() const SK_OVERRIDE {
+        return 100 + ((fID % 1 == 0) ? -5 : 6);
+    }
+
+    typedef GrGpuResource INHERITED;
+};
+
+class TextureResource : public GrGpuResource {
+public:
+    SK_DECLARE_INST_COUNT(TextureResource);
+    TextureResource(GrGpu* gpu, int id)
+        : INHERITED(gpu, false)
+        , fID(id) {
+        this->registerWithCache();
+    }
+
+    static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) {
         GrCacheID::Key key;
         memset(&key, 0, sizeof(key));
-        key.fData32[0] = i;
+        key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
+        key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
+        key.fData32[2] = desc.fFlags;
         static int gType = GrResourceKey::GenerateResourceType();
         static int gDomain = GrCacheID::GenerateDomain();
         return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
     }
 
+    int fID;
 
 private:
-    size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
+    virtual size_t onGpuMemorySize() const SK_OVERRIDE {
+        return 100 + ((fID % 1 == 0) ? -40 : 33);
+    }
 
     typedef GrGpuResource INHERITED;
 };
 
+static void get_stencil(int i, int* w, int* h, int* s) {
+    *w = i % 1024;
+    *h = i * 2 % 1024;
+    *s = i % 1 == 0 ? 0 : 4;
+}
+
+static void get_texture_desc(int i, GrSurfaceDesc* desc) {
+    desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
+    desc->fWidth  = i % 1024;
+    desc->fHeight = i * 2 % 1024;
+    desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
+    desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4;
+}
+
 static void populate_cache(GrGpu* gpu, int resourceCount) {
     for (int i = 0; i < resourceCount; ++i) {
-        GrResourceKey key = BenchResource::ComputeKey(i);
-        GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
+        int w, h, s;
+        get_stencil(i, &w, &h, &s);
+        GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
+        GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
+        resource->cacheAccess().setContentKey(key);
+        resource->unref();
+    }
+
+    for (int i = 0; i < resourceCount; ++i) {
+        GrSurfaceDesc desc;
+        get_texture_desc(i, &desc);
+        GrResourceKey key =  TextureResource::ComputeKey(desc);
+        GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
         resource->cacheAccess().setContentKey(key);
         resource->unref();
     }
 }
 
+static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
+    // Benchmark find calls that succeed.
+    {
+        GrSurfaceDesc desc;
+        get_texture_desc(k, &desc);
+        GrResourceKey key = TextureResource::ComputeKey(desc);
+        SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
+        if (!item) {
+            SkFAIL("cache add does not work as expected");
+            return;
+        }
+        if (static_cast<TextureResource*>(item.get())->fID != k) {
+            SkFAIL("cache add does not work as expected");
+            return;
+        }
+    }
+    {
+        int w, h, s;
+        get_stencil(k, &w, &h, &s);
+        GrResourceKey key = StencilResource::ComputeKey(w, h, s);
+        SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
+        if (!item) {
+            SkFAIL("cache add does not work as expected");
+            return;
+        }
+        if (static_cast<TextureResource*>(item.get())->fID != k) {
+            SkFAIL("cache add does not work as expected");
+            return;
+        }
+    }
+
+    // Benchmark also find calls that always fail.
+    {
+        GrSurfaceDesc desc;
+        get_texture_desc(k, &desc);
+        desc.fHeight |= 1;
+        GrResourceKey key = TextureResource::ComputeKey(desc);
+        SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
+        if (item) {
+            SkFAIL("cache add does not work as expected");
+            return;
+        }
+    }
+    {
+        int w, h, s;
+        get_stencil(k, &w, &h, &s);
+        h |= 1;
+        GrResourceKey key = StencilResource::ComputeKey(w, h, s);
+        SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
+        if (item) {
+            SkFAIL("cache add does not work as expected");
+            return;
+        }
+    }
+}
+
 class GrResourceCacheBenchAdd : public Benchmark {
+    enum {
+        RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
+    };
+
 public:
-    bool isSuitableFor(Backend backend) SK_OVERRIDE {
+    virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
         return backend == kNonRendering_Backend;
     }
 
 protected:
-    const char* onGetName() SK_OVERRIDE {
+    virtual const char* onGetName() SK_OVERRIDE {
         return "grresourcecache_add";
     }
 
-    void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
+    virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
         SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
         if (NULL == context) {
             return;
         }
         // Set the cache budget to be very large so no purging occurs.
-        context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
+        context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
 
         GrResourceCache2* cache2 = context->getResourceCache2();
 
@@ -81,8 +199,15 @@
         GrGpu* gpu = context->getGpu();
 
         for (int i = 0; i < loops; ++i) {
-            populate_cache(gpu, CACHE_SIZE_COUNT);
-            SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
+            SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
+
+            populate_cache(gpu, RESOURCE_COUNT);
+
+            // Check that cache works.
+            for (int k = 0; k < RESOURCE_COUNT; k += 33) {
+                check_cache_contents_or_die(cache2, k);
+            }
+            cache2->purgeAllUnlocked();
         }
     }
 
@@ -91,52 +216,46 @@
 };
 
 class GrResourceCacheBenchFind : public Benchmark {
+    enum {
+        RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
+    };
+
 public:
-    bool isSuitableFor(Backend backend) SK_OVERRIDE {
+    virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
         return backend == kNonRendering_Backend;
     }
 
 protected:
-    const char* onGetName() SK_OVERRIDE {
+    virtual const char* onGetName() SK_OVERRIDE {
         return "grresourcecache_find";
     }
 
-    void onPreDraw() SK_OVERRIDE {
-        fContext.reset(GrContext::CreateMockContext());
-        if (!fContext) {
+    virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
+        SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
+        if (NULL == context) {
             return;
         }
         // Set the cache budget to be very large so no purging occurs.
-        fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
+        context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
 
-        GrResourceCache2* cache2 = fContext->getResourceCache2();
+        GrResourceCache2* cache2 = context->getResourceCache2();
 
         // Make sure the cache is empty.
         cache2->purgeAllUnlocked();
         SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
 
-        GrGpu* gpu = fContext->getGpu();
+        GrGpu* gpu = context->getGpu();
 
-        populate_cache(gpu, CACHE_SIZE_COUNT);
-    }
+        populate_cache(gpu, RESOURCE_COUNT);
 
-    void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
-        if (!fContext) {
-            return;
-        }
-        GrResourceCache2* cache2 = fContext->getResourceCache2();
-        SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
         for (int i = 0; i < loops; ++i) {
-            for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
-                GrResourceKey key = BenchResource::ComputeKey(k);
-                SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key));
-                SkASSERT(resource);
+            for (int k = 0; k < RESOURCE_COUNT; ++k) {
+                check_cache_contents_or_die(cache2, k);
             }
         }
     }
 
 private:
-    SkAutoTUnref<GrContext> fContext;
     typedef Benchmark INHERITED;
 };
 
diff --git a/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Debug/expected-results.json b/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Debug/expected-results.json
index e124222..f6bd4ab 100644
--- a/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Debug/expected-results.json
+++ b/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Debug/expected-results.json
@@ -3857,7 +3857,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          18165407505633347407
+          4938590834664658305
         ]
       ], 
       "reviewed-by-human": true
@@ -5852,7 +5852,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          9412995609628467341
+          17492314805851855065
         ]
       ], 
       "reviewed-by-human": true
@@ -5970,7 +5970,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          9298514044054867967
+          571194008531839949
         ]
       ], 
       "ignore-failure": false, 
diff --git a/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Release/expected-results.json b/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Release/expected-results.json
index 0828577..7fc9263 100644
--- a/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Release/expected-results.json
+++ b/expectations/gm/Test-Android-GalaxyS4-SGX544-Arm7-Release/expected-results.json
@@ -3857,7 +3857,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          18165407505633347407
+          4938590834664658305
         ]
       ], 
       "reviewed-by-human": true
@@ -5844,7 +5844,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          2311109700305266403
+          167979044054175148
         ]
       ], 
       "reviewed-by-human": true
@@ -5962,7 +5962,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          5148862615526465916
+          9298514044054867967
         ]
       ], 
       "ignore-failure": false, 
diff --git a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Debug/expected-results.json b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Debug/expected-results.json
index b070adc..d88cb25 100644
--- a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Debug/expected-results.json
+++ b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Debug/expected-results.json
@@ -7059,7 +7059,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          11009857797348161863
+          10566742216990551435
         ]
       ], 
       "reviewed-by-human": true
diff --git a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Release/expected-results.json b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Release/expected-results.json
index db659f8..4500604 100644
--- a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Release/expected-results.json
+++ b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86-Release/expected-results.json
@@ -7061,7 +7061,7 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          11009857797348161863
+          10566742216990551435
         ]
       ], 
       "reviewed-by-human": true
diff --git a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Debug/expected-results.json b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Debug/expected-results.json
index 348249a..5ebd31f 100644
--- a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Debug/expected-results.json
+++ b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Debug/expected-results.json
@@ -7131,10 +7131,13 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          11009857797348161863
+          10566742216990551435
         ]
       ], 
-      "reviewed-by-human": true
+      "bugs": [
+        2325
+      ], 
+      "reviewed-by-human": false
     }, 
     "dashing2_pdf-poppler.png": {
       "allowed-digests": [
diff --git a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Release/expected-results.json b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Release/expected-results.json
index 72c5250..3ed57a6 100644
--- a/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Release/expected-results.json
+++ b/expectations/gm/Test-Ubuntu12-ShuttleA-GTX660-x86_64-Release/expected-results.json
@@ -7131,10 +7131,13 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          11009857797348161863
+          10566742216990551435
         ]
       ], 
-      "reviewed-by-human": true
+      "bugs": [
+        2325
+      ], 
+      "reviewed-by-human": false
     }, 
     "dashing2_pdf-poppler.png": {
       "allowed-digests": [
diff --git a/expectations/gm/Test-Win8-ShuttleA-GTX660-x86_64-Debug/expected-results.json b/expectations/gm/Test-Win8-ShuttleA-GTX660-x86_64-Debug/expected-results.json
index 928a743..bd7c443 100644
--- a/expectations/gm/Test-Win8-ShuttleA-GTX660-x86_64-Debug/expected-results.json
+++ b/expectations/gm/Test-Win8-ShuttleA-GTX660-x86_64-Debug/expected-results.json
@@ -7116,10 +7116,10 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          11009857797348161863
+          10566742216990551435
         ]
       ], 
-      "reviewed-by-human": true
+      "reviewed-by-human": false
     }, 
     "dashing2_pdf-poppler.png": {
       "allowed-digests": [
@@ -11911,10 +11911,10 @@
       "allowed-digests": [
         [
           "bitmap-64bitMD5", 
-          12821620961617303185
+          17326560431224736462
         ]
       ], 
-      "reviewed-by-human": true
+      "reviewed-by-human": false
     }, 
     "glyph_pos_n_b_nvprmsaa4.png": {
       "allowed-digests": [
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index c6dd2d7..b83df45 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -34,6 +34,7 @@
 class GrPathRenderer;
 class GrResourceEntry;
 class GrResourceCache2;
+class GrStencilBuffer;
 class GrTestTarget;
 class GrTextContext;
 class GrTextureParams;
@@ -889,6 +890,13 @@
     void addGpuTraceMarker(const GrGpuTraceMarker* marker);
     void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
 
+    /**
+     * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is
+     * called to check the cache for a SB that matches an RT's criteria.
+     */
+    void addStencilBuffer(GrStencilBuffer* sb);
+    GrStencilBuffer* findAndRefStencilBuffer(int width, int height, int sampleCnt);
+
     GrPathRenderer* getPathRenderer(
                     const GrDrawTarget* target,
                     const GrDrawState*,
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 607d3c3..657e57d 100755
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -24,6 +24,7 @@
 #include "GrPathUtils.h"
 #include "GrResourceCache2.h"
 #include "GrSoftwarePathRenderer.h"
+#include "GrStencilBuffer.h"
 #include "GrStencilAndCoverTextContext.h"
 #include "GrStrokeInfo.h"
 #include "GrSurfacePriv.h"
@@ -251,6 +252,22 @@
     return fResourceCache2->hasContentKey(resourceKey);
 }
 
+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());
+    SkAssertResult(sb->cacheAccess().setContentKey(resourceKey));
+}
+
+GrStencilBuffer* GrContext::findAndRefStencilBuffer(int width, int height, int sampleCnt) {
+    GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width, height, sampleCnt);
+    GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
+    return static_cast<GrStencilBuffer*>(resource);
+}
+
 static void stretch_image(void* dst,
                           int dstW,
                           int dstH,
diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp
index 6b742c4..2bda594 100644
--- a/src/gpu/GrGpu.cpp
+++ b/src/gpu/GrGpu.cpp
@@ -13,12 +13,14 @@
 #include "GrContext.h"
 #include "GrDrawTargetCaps.h"
 #include "GrIndexBuffer.h"
-#include "GrResourceCache2.h"
 #include "GrStencilBuffer.h"
 #include "GrVertexBuffer.h"
 
 ////////////////////////////////////////////////////////////////////////////////
 
+#define DEBUG_INVAL_BUFFER    0xdeadcafe
+#define DEBUG_INVAL_START_IDX -1
+
 GrGpu::GrGpu(GrContext* context)
     : fResetTimestamp(kExpiredTimestamp+1)
     , fResetBits(kAll_GrBackendState)
@@ -76,9 +78,8 @@
 
 bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
     SkASSERT(NULL == rt->getStencilBuffer());
-    GrResourceKey sbKey = GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples());
-    SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
-        this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey)));
+    SkAutoTUnref<GrStencilBuffer> sb(
+        this->getContext()->findAndRefStencilBuffer(rt->width(), rt->height(), rt->numSamples()));
     if (sb) {
         rt->setStencilBuffer(sb);
         bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
diff --git a/src/gpu/GrStencilBuffer.cpp b/src/gpu/GrStencilBuffer.cpp
index 5aa56e0..16b0150 100644
--- a/src/gpu/GrStencilBuffer.cpp
+++ b/src/gpu/GrStencilBuffer.cpp
@@ -12,10 +12,14 @@
 #include "GrGpu.h"
 #include "GrResourceCache2.h"
 
+void GrStencilBuffer::transferToCache() {
+    this->getGpu()->getContext()->addStencilBuffer(this);
+}
+
 namespace {
 // we should never have more than one stencil buffer with same combo of (width,height,samplecount)
 void gen_cache_id(int width, int height, int sampleCnt, GrCacheID* cacheID) {
-    static const GrCacheID::Domain gStencilBufferDomain = GrResourceKey::ScratchDomain();
+    static const GrCacheID::Domain gStencilBufferDomain = GrCacheID::GenerateDomain();
     GrCacheID::Key key;
     uint32_t* keyData = key.fData32;
     keyData[0] = width;
diff --git a/src/gpu/GrStencilBuffer.h b/src/gpu/GrStencilBuffer.h
index 187556b..86fef50 100644
--- a/src/gpu/GrStencilBuffer.h
+++ b/src/gpu/GrStencilBuffer.h
@@ -47,6 +47,9 @@
                !fLastClipStackRect.contains(clipSpaceRect);
     }
 
+    // Places the sb in the cache. The cache takes a ref of the stencil buffer.
+    void transferToCache();
+
     static GrResourceKey ComputeKey(int width, int height, int sampleCnt);
 
 protected:
@@ -57,7 +60,6 @@
         , fBits(bits)
         , fSampleCnt(sampleCnt)
         , fLastClipStackGenID(SkClipStack::kInvalidGenID) {
-        this->setScratchKey(ComputeKey(width, height, sampleCnt));
         fLastClipStackRect.setEmpty();
     }
 
diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp
index c873c1d..9e1f754 100644
--- a/src/gpu/gl/GrGpuGL.cpp
+++ b/src/gpu/gl/GrGpuGL.cpp
@@ -1121,7 +1121,8 @@
 }
 }
 
-bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
+bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
+                                                 int width, int height) {
 
     // All internally created RTs are also textures. We don't create
     // SBs for a client's standalone RT (that is a RT that isn't also a texture).
@@ -1175,6 +1176,7 @@
             sbID = 0; 
             if (this->attachStencilBufferToRenderTarget(sb, rt)) {
                 fLastSuccessfulStencilFmtIdx = sIdx;
+                sb->transferToCache();
                 rt->setStencilBuffer(sb);
                 return true;
             }
diff --git a/tests/ResourceCacheTest.cpp b/tests/ResourceCacheTest.cpp
index 45e2842..930bd12 100644
--- a/tests/ResourceCacheTest.cpp
+++ b/tests/ResourceCacheTest.cpp
@@ -569,76 +569,6 @@
     }
 }
 
-static void test_large_resource_count(skiatest::Reporter* reporter) {
-    SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
-    REPORTER_ASSERT(reporter, SkToBool(context));
-    if (NULL == context) {
-        return;
-    }
-
-    static const int kResourceCnt = 2000;
-    // Set the cache size to double the resource count because we're going to create 2x that number
-    // resources, using two different key domains. Add a little slop to the bytes because we resize
-    // down to 1 byte after creating the resource.
-    context->setResourceCacheLimits(2 * kResourceCnt, 2 * kResourceCnt + 1000);
-    GrResourceCache2* cache2 = context->getResourceCache2();
-    cache2->purgeAllUnlocked();
-    SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
-
-    GrCacheID::Domain domain0 = GrCacheID::GenerateDomain();
-    GrCacheID::Domain domain1 = GrCacheID::GenerateDomain();
-    GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
-
-    GrCacheID::Key keyData;
-    memset(&keyData, 0, sizeof(keyData));
-
-    for (int i = 0; i < kResourceCnt; ++i) {
-        TestResource* resource;
-        keyData.fData32[0] = i;
-
-        GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
-        resource = SkNEW_ARGS(TestResource, (context->getGpu()));
-        resource->cacheAccess().setContentKey(key0);
-        resource->setSize(1);
-        resource->unref();
-
-        GrResourceKey key1(GrCacheID(domain1, keyData), t, 0);
-        resource = SkNEW_ARGS(TestResource, (context->getGpu()));
-        resource->cacheAccess().setContentKey(key1);
-        resource->setSize(1);
-        resource->unref();
-    }
-
-    REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
-    REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourceCnt);
-    REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourceCnt);
-    REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt);
-    REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt);
-    for (int i = 0; i < kResourceCnt; ++i) {
-        keyData.fData32[0] = i;
-        GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
-        REPORTER_ASSERT(reporter, cache2->hasContentKey(key0));
-        GrResourceKey key1(GrCacheID(domain0, keyData), t, 0);
-        REPORTER_ASSERT(reporter, cache2->hasContentKey(key1));
-    }
-
-    cache2->purgeAllUnlocked();
-    REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
-    REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0);
-    REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0);
-    REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0);
-    REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0);
-
-    for (int i = 0; i < kResourceCnt; ++i) {
-        keyData.fData32[0] = i;
-        GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
-        REPORTER_ASSERT(reporter, !cache2->hasContentKey(key0));
-        GrResourceKey key1(GrCacheID(domain0, keyData), t, 0);
-        REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
-    }
-}
-
-
 ////////////////////////////////////////////////////////////////////////////////
 DEF_GPUTEST(ResourceCache, reporter, factory) {
     for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
@@ -668,7 +598,6 @@
     test_purge_invalidated(reporter);
     test_cache_chained_purge(reporter);
     test_resource_size_changed(reporter);
-    test_large_resource_count(reporter);
 }
 
 #endif