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
This commit is contained in:
senorblanco@chromium.org 2014-04-14 15:51:48 +00:00
parent 8f9e681093
commit 1a479e7547
3 changed files with 43 additions and 5 deletions

View 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;

View 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);

View 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));