diff options
author | 2014-04-14 15:51:48 +0000 | |
---|---|---|
committer | 2014-04-14 15:51:48 +0000 | |
commit | 1a479e7547d4efe2d1d06fab5c9442b77ec6c954 (patch) | |
tree | a2e15b7cd71e7440933cb8164f282102897f6c82 | |
parent | 8f9e681093aac6c46c71df604ac685cef46309a2 (diff) |
Allow clients to specify an external SkImageFilter cache.
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
-rw-r--r-- | include/core/SkImageFilter.h | 13 | ||||
-rw-r--r-- | src/core/SkCanvas.cpp | 16 | ||||
-rw-r--r-- | src/core/SkImageFilter.cpp | 19 |
3 files changed, 43 insertions, 5 deletions
diff --git a/include/core/SkImageFilter.h b/include/core/SkImageFilter.h index 7a65f53788..b3d7480955 100644 --- a/include/core/SkImageFilter.h +++ b/include/core/SkImageFilter.h @@ -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; diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index a3e2969514..569e9e59ba 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -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); diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp index 8d0c22de28..05f559b02d 100644 --- a/src/core/SkImageFilter.cpp +++ b/src/core/SkImageFilter.cpp @@ -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)); |