From 5449aade6227a1e701ebc24ee683d94a810c9a70 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Mon, 10 Jul 2017 11:14:40 -0400 Subject: [PATCH] [Reland] More SkColorSpaceXformer caching * apply(SkColorFilter*) * apply(SkImage*) Also add purge logic to minimize caching scope. Change-Id: I295d20f760f8be0c3746858d6f9c73e351f10a36 Reviewed-on: https://skia-review.googlesource.com/22030 Commit-Queue: Florin Malita Reviewed-by: Mike Reed --- src/core/SkColorSpaceXformer.cpp | 96 +++++++++++++++++++++++++++----- src/core/SkColorSpaceXformer.h | 16 +++++- 2 files changed, 96 insertions(+), 16 deletions(-) diff --git a/src/core/SkColorSpaceXformer.cpp b/src/core/SkColorSpaceXformer.cpp index 71715add87..58b3f4958a 100644 --- a/src/core/SkColorSpaceXformer.cpp +++ b/src/core/SkColorSpaceXformer.cpp @@ -19,7 +19,8 @@ SkColorSpaceXformer::SkColorSpaceXformer(sk_sp dst, std::unique_ptr fromSRGB) : fDst(std::move(dst)) - , fFromSRGB(std::move(fromSRGB)) {} + , fFromSRGB(std::move(fromSRGB)) + , fReentryCount(0) {} SkColorSpaceXformer::~SkColorSpaceXformer() {} @@ -33,11 +34,76 @@ std::unique_ptr SkColorSpaceXformer::Make(sk_spfReentryCount++; + } + + ~AutoCachePurge() { + SkASSERT(fXformer->fReentryCount > 0); + if (--fXformer->fReentryCount == 0) { + fXformer->purgeCaches(); + } + } + +private: + SkColorSpaceXformer* fXformer; +}; + +template +sk_sp SkColorSpaceXformer::cachedApply(const T* src, Cache* cache, + sk_sp (*applyFunc)(const T*, SkColorSpaceXformer*)) { + if (!src) { + return nullptr; + } + + auto key = sk_ref_sp(const_cast(src)); + if (auto* xformed = cache->find(key)) { + return sk_ref_sp(xformed->get()); + } + + auto xformed = applyFunc(src, this); + cache->set(std::move(key), xformed); + + return xformed; +} + +void SkColorSpaceXformer::purgeCaches() { + fImageCache.reset(); + fColorFilterCache.reset(); + fImageFilterCache.reset(); +} + sk_sp SkColorSpaceXformer::apply(const SkImage* src) { - return src->makeColorSpace(fDst, SkTransferFunctionBehavior::kIgnore); + const AutoCachePurge autoPurge(this); + return this->cachedApply(src, &fImageCache, + [](const SkImage* img, SkColorSpaceXformer* xformer) { + return img->makeColorSpace(xformer->fDst, SkTransferFunctionBehavior::kIgnore); + }); } sk_sp SkColorSpaceXformer::apply(const SkBitmap& src) { + const AutoCachePurge autoPurge(this); sk_sp image = SkMakeImageFromRasterBitmap(src, kNever_SkCopyPixelsMode); if (!image) { return nullptr; @@ -50,25 +116,23 @@ sk_sp SkColorSpaceXformer::apply(const SkBitmap& src) { } sk_sp SkColorSpaceXformer::apply(const SkColorFilter* colorFilter) { - return colorFilter->makeColorSpace(this); + const AutoCachePurge autoPurge(this); + return this->cachedApply(colorFilter, &fColorFilterCache, + [](const SkColorFilter* f, SkColorSpaceXformer* xformer) { + return f->makeColorSpace(xformer); + }); } sk_sp SkColorSpaceXformer::apply(const SkImageFilter* imageFilter) { - if (!imageFilter) { - return nullptr; - } - - if (auto* xformedFilter = fFilterCache.find(imageFilter->fUniqueID)) { - return sk_ref_sp(xformedFilter->get()); - } - - auto xformedFilter = imageFilter->makeColorSpace(this); - fFilterCache.set(imageFilter->fUniqueID, xformedFilter); - - return xformedFilter; + const AutoCachePurge autoPurge(this); + return this->cachedApply(imageFilter, &fImageFilterCache, + [](const SkImageFilter* f, SkColorSpaceXformer* xformer) { + return f->makeColorSpace(xformer); + }); } sk_sp SkColorSpaceXformer::apply(const SkShader* shader) { + const AutoCachePurge autoPurge(this); return as_SB(shader)->makeColorSpace(this); } @@ -85,6 +149,8 @@ SkColor SkColorSpaceXformer::apply(SkColor srgb) { } SkPaint SkColorSpaceXformer::apply(const SkPaint& src) { + const AutoCachePurge autoPurge(this); + SkPaint dst = src; // All SkColorSpaces have the same black point. diff --git a/src/core/SkColorSpaceXformer.h b/src/core/SkColorSpaceXformer.h index fce8d03e43..5fa4d836b7 100644 --- a/src/core/SkColorSpaceXformer.h +++ b/src/core/SkColorSpaceXformer.h @@ -41,10 +41,24 @@ public: private: SkColorSpaceXformer(sk_sp dst, std::unique_ptr fromSRGB); + template + using Cache = SkTHashMap, sk_sp>; + + template + sk_sp cachedApply(const T*, Cache*, sk_sp (*)(const T*, SkColorSpaceXformer*)); + + void purgeCaches(); + + class AutoCachePurge; + sk_sp fDst; std::unique_ptr fFromSRGB; - SkTHashMap> fFilterCache; + size_t fReentryCount; // tracks the number of nested apply() calls for cache purging. + + Cache fImageCache; + Cache fColorFilterCache; + Cache fImageFilterCache; }; #endif