Remove external SkImageFilter cache, and rename UniqueIDCache -> Cache.
authorsenorblanco <senorblanco@chromium.org>
Fri, 8 Aug 2014 14:14:35 +0000 (07:14 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 8 Aug 2014 14:14:35 +0000 (07:14 -0700)
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

include/core/SkBitmapDevice.h
include/core/SkDevice.h
include/core/SkImageFilter.h
include/gpu/SkGpuDevice.h
src/core/SkBitmapDevice.cpp
src/core/SkCanvas.cpp
src/core/SkImageFilter.cpp
src/gpu/SkGpuDevice.cpp

index 5dde9e0..f8ce93a 100644 (file)
@@ -156,7 +156,7 @@ private:
     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;
 
index 3e5e42b..a5682c2 100644 (file)
@@ -380,7 +380,7 @@ private:
      */
     virtual void flush() {}
 
-    virtual SkImageFilter::UniqueIDCache* getImageFilterCache() { return NULL; }
+    virtual SkImageFilter::Cache* getImageFilterCache() { return NULL; }
 
     SkIPoint    fOrigin;
     SkMetaData* fMetaData;
index d2a4d3e..d4930c4 100644 (file)
@@ -49,42 +49,30 @@ public:
         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 {
index b43213a..5ff26f6 100644 (file)
@@ -169,7 +169,7 @@ private:
 
     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.
index 8c92f71..5f205ce 100644 (file)
@@ -367,8 +367,8 @@ const void* SkBitmapDevice::peekPixels(SkImageInfo* info, size_t* rowBytes) {
     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;
 }
index 757e711..cf7050f 100644 (file)
@@ -1134,7 +1134,7 @@ void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y,
             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 @@ void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
             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);
index 5fa6855..cf1f076 100644 (file)
@@ -36,7 +36,7 @@ static int32_t next_image_filter_unique_id() {
     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 @@ bool SkImageFilter::Common::unflatten(SkReadBuffer& buffer, int expectedCount) {
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-SkImageFilter::Cache* gExternalCache;
-
 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect)
   : fInputCount(inputCount),
     fInputs(new SkImageFilter*[inputCount]),
@@ -174,13 +172,8 @@ bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src,
     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 @@ bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src,
      */
     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 @@ bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm,
                                  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 @@ bool SkImageFilter::asColorFilter(SkColorFilter**) const {
     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 @@ bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy,
 }
 #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 @@ private:
     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();
 }
index 48ae845..3ba2abc 100644 (file)
@@ -1427,7 +1427,7 @@ void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
         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 @@ void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
         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 @@ bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture
     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);
 }