Remove external SkImageFilter cache, and rename UniqueIDCache -> Cache.

There Can Only Be One.... Cache for SkImageFilter.

R=bsalomon@google.com

BUG=skia:

Author: senorblanco@chromium.org

Review URL: https://codereview.chromium.org/452923002
diff --git a/include/core/SkBitmapDevice.h b/include/core/SkBitmapDevice.h
index 5dde9e0..f8ce93a 100644
--- a/include/core/SkBitmapDevice.h
+++ b/include/core/SkBitmapDevice.h
@@ -156,7 +156,7 @@
     virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
     virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE;
 
-    virtual SkImageFilter::UniqueIDCache* getImageFilterCache() SK_OVERRIDE;
+    virtual SkImageFilter::Cache* getImageFilterCache() SK_OVERRIDE;
 
     SkBitmap    fBitmap;
 
diff --git a/include/core/SkDevice.h b/include/core/SkDevice.h
index 3e5e42b..a5682c2 100644
--- a/include/core/SkDevice.h
+++ b/include/core/SkDevice.h
@@ -380,7 +380,7 @@
      */
     virtual void flush() {}
 
-    virtual SkImageFilter::UniqueIDCache* getImageFilterCache() { return NULL; }
+    virtual SkImageFilter::Cache* getImageFilterCache() { return NULL; }
 
     SkIPoint    fOrigin;
     SkMetaData* fMetaData;
diff --git a/include/core/SkImageFilter.h b/include/core/SkImageFilter.h
index d2a4d3e..d4930c4 100644
--- a/include/core/SkImageFilter.h
+++ b/include/core/SkImageFilter.h
@@ -49,42 +49,30 @@
         uint32_t fFlags;
     };
 
-    class SK_API Cache : public SkRefCnt {
-    public:
-        // By default, we cache only image filters with 2 or more children.
-        // Values less than 2 mean always cache; values greater than 2 are not supported.
-        static Cache* Create(int minChildren = 2);
-        virtual ~Cache() {}
-        virtual bool get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset) = 0;
-        virtual void set(const SkImageFilter* key,
-                         const SkBitmap& result, const SkIPoint& offset) = 0;
-        virtual void remove(const SkImageFilter* key) = 0;
-    };
-
     // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to
     // (result, offset).
-    class UniqueIDCache : public SkRefCnt {
+    class Cache : public SkRefCnt {
     public:
         struct Key;
-        virtual ~UniqueIDCache() {}
-        static UniqueIDCache* Create(size_t maxBytes);
-        static UniqueIDCache* Get();
+        virtual ~Cache() {}
+        static Cache* Create(size_t maxBytes);
+        static Cache* Get();
         virtual bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const = 0;
         virtual void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) = 0;
     };
 
     class Context {
     public:
-        Context(const SkMatrix& ctm, const SkIRect& clipBounds, UniqueIDCache* cache) :
+        Context(const SkMatrix& ctm, const SkIRect& clipBounds, Cache* cache) :
             fCTM(ctm), fClipBounds(clipBounds), fCache(cache) {
         }
         const SkMatrix& ctm() const { return fCTM; }
         const SkIRect& clipBounds() const { return fClipBounds; }
-        UniqueIDCache* cache() const { return fCache; }
+        Cache* cache() const { return fCache; }
     private:
         SkMatrix fCTM;
         SkIRect  fClipBounds;
-        UniqueIDCache* fCache;
+        Cache* fCache;
     };
 
     class Proxy {
diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h
index b43213a..5ff26f6 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -169,7 +169,7 @@
 
     virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
 
-    virtual SkImageFilter::UniqueIDCache* getImageFilterCache() SK_OVERRIDE;
+    virtual SkImageFilter::Cache* getImageFilterCache() SK_OVERRIDE;
 
     // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
     // SkDraw's matrix and draw in device coords.
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp
index 8c92f71..5f205ce 100644
--- a/src/core/SkBitmapDevice.cpp
+++ b/src/core/SkBitmapDevice.cpp
@@ -367,8 +367,8 @@
     return NULL;
 }
 
-SkImageFilter::UniqueIDCache* SkBitmapDevice::getImageFilterCache() {
-    SkImageFilter::UniqueIDCache* cache = SkImageFilter::UniqueIDCache::Get();
+SkImageFilter::Cache* SkBitmapDevice::getImageFilterCache() {
+    SkImageFilter::Cache* cache = SkImageFilter::Cache::Get();
     cache->ref();
     return cache;
 }
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 757e711..cf7050f 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -1134,7 +1134,7 @@
             SkMatrix matrix = *iter.fMatrix;
             matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y()));
             SkIRect clipBounds = SkIRect::MakeWH(srcDev->width(), srcDev->height());
-            SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(dstDev->getImageFilterCache());
+            SkAutoTUnref<SkImageFilter::Cache> cache(dstDev->getImageFilterCache());
             SkImageFilter::Context ctx(matrix, clipBounds, cache.get());
             if (filter->filterImage(&proxy, src, ctx, &dst, &offset)) {
                 SkPaint tmpUnfiltered(*paint);
@@ -1174,7 +1174,7 @@
             SkMatrix matrix = *iter.fMatrix;
             matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y()));
             SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
-            SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(iter.fDevice->getImageFilterCache());
+            SkAutoTUnref<SkImageFilter::Cache> cache(iter.fDevice->getImageFilterCache());
             SkImageFilter::Context ctx(matrix, clipBounds, cache.get());
             if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) {
                 SkPaint tmpUnfiltered(*paint);
diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp
index 5fa6855..cf1f076 100644
--- a/src/core/SkImageFilter.cpp
+++ b/src/core/SkImageFilter.cpp
@@ -36,7 +36,7 @@
     return id;
 }
 
-struct SkImageFilter::UniqueIDCache::Key {
+struct SkImageFilter::Cache::Key {
     Key(const uint32_t uniqueID, const SkMatrix& matrix, const SkIRect& clipBounds, uint32_t srcGenID)
       : fUniqueID(uniqueID), fMatrix(matrix), fClipBounds(clipBounds), fSrcGenID(srcGenID) {
         // Assert that Key is tightly-packed, since it is hashed.
@@ -110,8 +110,6 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-SkImageFilter::Cache* gExternalCache;
-
 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect)
   : fInputCount(inputCount),
     fInputs(new SkImageFilter*[inputCount]),
@@ -174,13 +172,8 @@
     SkASSERT(result);
     SkASSERT(offset);
     uint32_t srcGenID = fUsesSrcInput ? src.getGenerationID() : 0;
-    Cache* externalCache = GetExternalCache();
-    UniqueIDCache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID);
-    if (NULL != externalCache) {
-        if (externalCache->get(this, result, offset)) {
-            return true;
-        }
-    } else if (context.cache()) {
+    Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID);
+    if (context.cache()) {
         if (context.cache()->get(key, result, offset)) {
             return true;
         }
@@ -191,9 +184,7 @@
      */
     if ((proxy && proxy->filterImage(this, src, context, result, offset)) ||
         this->onFilterImage(proxy, src, context, result, offset)) {
-        if (externalCache) {
-            externalCache->set(this, *result, *offset);
-        } else if (context.cache()) {
+        if (context.cache()) {
             context.cache()->set(key, *result, *offset);
         }
         return true;
@@ -205,16 +196,6 @@
                                  SkIRect* dst) const {
     SkASSERT(&src);
     SkASSERT(dst);
-    if (SkImageFilter::GetExternalCache()) {
-        /*
-         *  When the external cache is active, do not intersect the saveLayer
-         *  bounds with the clip bounds. This is so that the cached result
-         *  is always the full size of the primitive's bounds,
-         *  regardless of the clip active on first draw.
-         */
-        *dst = SkIRect::MakeLargest();
-        return true;
-    }
     return this->onFilterBounds(src, ctm, dst);
 }
 
@@ -389,14 +370,6 @@
     return false;
 }
 
-void SkImageFilter::SetExternalCache(Cache* cache) {
-    SkRefCnt_SafeAssign(gExternalCache, cache);
-}
-
-SkImageFilter::Cache* SkImageFilter::GetExternalCache() {
-    return gExternalCache;
-}
-
 #if SK_SUPPORT_GPU
 
 void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) {
@@ -434,83 +407,13 @@
 }
 #endif
 
-class CacheImpl : public SkImageFilter::Cache {
-public:
-    explicit CacheImpl(int minChildren) : fMinChildren(minChildren) {
-        SkASSERT(fMinChildren <= 2);
-    }
-
-    virtual ~CacheImpl();
-    bool get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset) SK_OVERRIDE;
-    void set(const SkImageFilter* key, const SkBitmap& result, const SkIPoint& offset) SK_OVERRIDE;
-    void remove(const SkImageFilter* key) SK_OVERRIDE;
-private:
-    typedef const SkImageFilter* Key;
-    struct Value {
-        Value(Key key, const SkBitmap& bitmap, const SkIPoint& offset)
-            : fKey(key), fBitmap(bitmap), fOffset(offset) {}
-        Key fKey;
-        SkBitmap fBitmap;
-        SkIPoint fOffset;
-        static const Key& GetKey(const Value& v) {
-            return v.fKey;
-        }
-        static uint32_t Hash(Key key) {
-            return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
-        }
-    };
-    SkTDynamicHash<Value, Key> fData;
-    int fMinChildren;
-};
-
-bool CacheImpl::get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset) {
-    Value* v = fData.find(key);
-    if (v) {
-        *result = v->fBitmap;
-        *offset = v->fOffset;
-        return true;
-    }
-    return false;
-}
-
-void CacheImpl::remove(const SkImageFilter* key) {
-    Value* v = fData.find(key);
-    if (v) {
-        fData.remove(key);
-        delete v;
-    }
-}
-
-void CacheImpl::set(const SkImageFilter* key, const SkBitmap& result, const SkIPoint& offset) {
-    if (fMinChildren < 2 || !key->unique()) {
-        // We take !key->unique() as a signal that there are probably at least 2 refs on the key,
-        // meaning this filter probably has at least two children and is worth caching when
-        // fMinChildren is 2.  If fMinChildren is less than two, we'll just always cache.
-        fData.add(new Value(key, result, offset));
-    }
-}
-
-SkImageFilter::Cache* SkImageFilter::Cache::Create(int minChildren) {
-    return new CacheImpl(minChildren);
-}
-
-CacheImpl::~CacheImpl() {
-    SkTDynamicHash<Value, Key>::Iter iter(&fData);
-
-    while (!iter.done()) {
-        Value* v = &*iter;
-        ++iter;
-        delete v;
-    }
-}
-
 namespace {
 
-class UniqueIDCacheImpl : public SkImageFilter::UniqueIDCache {
+class CacheImpl : public SkImageFilter::Cache {
 public:
-    UniqueIDCacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) {
+    CacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) {
     }
-    virtual ~UniqueIDCacheImpl() {
+    virtual ~CacheImpl() {
         SkTDynamicHash<Value, Key>::Iter iter(&fLookup);
 
         while (!iter.done()) {
@@ -579,17 +482,17 @@
     mutable SkMutex                    fMutex;
 };
 
-SkImageFilter::UniqueIDCache* CreateCache() {
-    return SkImageFilter::UniqueIDCache::Create(kDefaultCacheSize);
+SkImageFilter::Cache* CreateCache() {
+    return SkImageFilter::Cache::Create(kDefaultCacheSize);
 }
 
 } // namespace
 
-SkImageFilter::UniqueIDCache* SkImageFilter::UniqueIDCache::Create(size_t maxBytes) {
-    return SkNEW_ARGS(UniqueIDCacheImpl, (maxBytes));
+SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) {
+    return SkNEW_ARGS(CacheImpl, (maxBytes));
 }
 
-SkImageFilter::UniqueIDCache* SkImageFilter::UniqueIDCache::Get() {
-    SK_DECLARE_STATIC_LAZY_PTR(SkImageFilter::UniqueIDCache, cache, CreateCache);
+SkImageFilter::Cache* SkImageFilter::Cache::Get() {
+    SK_DECLARE_STATIC_LAZY_PTR(SkImageFilter::Cache, cache, CreateCache);
     return cache.get();
 }
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 48ae845..3ba2abc 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1427,7 +1427,7 @@
         SkMatrix matrix(*draw.fMatrix);
         matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
         SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
-        SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(getImageFilterCache());
+        SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
         // This cache is transient, and is freed (along with all its contained
         // textures) when it goes out of scope.
         SkImageFilter::Context ctx(matrix, clipBounds, cache);
@@ -1540,7 +1540,7 @@
         SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
         // This cache is transient, and is freed (along with all its contained
         // textures) when it goes out of scope.
-        SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(getImageFilterCache());
+        SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
         SkImageFilter::Context ctx(matrix, clipBounds, cache);
         if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
                            &offset)) {
@@ -2126,8 +2126,8 @@
     return true;
 }
 
-SkImageFilter::UniqueIDCache* SkGpuDevice::getImageFilterCache() {
+SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
     // We always return a transient cache, so it is freed after each
     // filter traversal.
-    return SkImageFilter::UniqueIDCache::Create(kDefaultImageFilterCacheSize);
+    return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
 }