Allow clients to specify an external SkImageFilter cache.
authorsenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 14 Apr 2014 15:51:48 +0000 (15:51 +0000)
committersenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 14 Apr 2014 15:51:48 +0000 (15:51 +0000)
This change allows external callers to substitute their own
SkImageFilter cache for the default intra-frame cache in Skia. This
allows the caller to perform inter-frame caching for example, by the
maintaining a persistent cache between frames and doing custom
invalidation.

R=reed@google.com

Review URL: https://codereview.chromium.org/225903010

git-svn-id: http://skia.googlecode.com/svn/trunk@14181 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkImageFilter.h
src/core/SkCanvas.cpp
src/core/SkImageFilter.cpp

index 7a65f53..b3d7480 100644 (file)
@@ -56,6 +56,7 @@ public:
         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;
     };
 
     class Context {
@@ -183,6 +184,17 @@ public:
                            SkBitmap* result, SkIPoint* offset) const;
 #endif
 
+    /**
+     *  Set an external cache to be used for all image filter processing. This
+     *  will replace the default intra-frame cache.
+     */
+    static void SetExternalCache(Cache* cache);
+
+    /**
+     *  Returns the currently-set external cache, or NULL if none is set.
+     */
+    static Cache* GetExternalCache();
+
     SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter)
 
 protected:
@@ -274,7 +286,6 @@ protected:
                              const SkMatrix& matrix,
                              const SkIRect& bounds) const;
 
-
 private:
     typedef SkFlattenable INHERITED;
     int fInputCount;
index a3e2969..569e9e5 100644 (file)
@@ -1258,8 +1258,12 @@ 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());
-            SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
-            SkAutoUnref aur(cache);
+            SkImageFilter::Cache* cache = SkImageFilter::GetExternalCache();
+            SkAutoUnref aur(NULL);
+            if (!cache) {
+                cache = SkImageFilter::Cache::Create();
+                aur.reset(cache);
+            }
             SkImageFilter::Context ctx(matrix, clipBounds, cache);
             if (filter->filterImage(&proxy, src, ctx, &dst, &offset)) {
                 SkPaint tmpUnfiltered(*paint);
@@ -1300,8 +1304,12 @@ 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());
-            SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
-            SkAutoUnref aur(cache);
+            SkImageFilter::Cache* cache = SkImageFilter::GetExternalCache();
+            SkAutoUnref aur(NULL);
+            if (!cache) {
+                cache = SkImageFilter::Cache::Create();
+                aur.reset(cache);
+            }
             SkImageFilter::Context ctx(matrix, clipBounds, cache);
             if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) {
                 SkPaint tmpUnfiltered(*paint);
index 8d0c22d..05f559b 100644 (file)
@@ -20,6 +20,8 @@
 #include "SkGr.h"
 #endif
 
+SkImageFilter::Cache* gExternalCache;
+
 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect)
   : fInputCount(inputCount),
     fInputs(new SkImageFilter*[inputCount]),
@@ -295,6 +297,14 @@ 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) {
@@ -363,6 +373,7 @@ public:
     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 {
@@ -392,6 +403,14 @@ bool CacheImpl::get(const SkImageFilter* key, SkBitmap* result, SkIPoint* offset
     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 (key->getRefCnt() >= fMinChildren) {
         fData.add(new Value(key, result, offset));