diff options
author | Brian Osman <brianosman@google.com> | 2016-10-20 15:34:06 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2016-10-20 20:47:33 +0000 |
commit | 7c2114fc2f138f210aaed47059888658bb2b297a (patch) | |
tree | 5425bd66860363d9700da500056fe1c307024d06 /src | |
parent | bbf251bf225489a0939fff6df938035a290f4d16 (diff) |
Cache GrColorSpaceXforms
Even with a modest cache, we're going to get nearly 100% hit rate
for typical usage scenarios. I'm hoping to avoid the special case
caching of sRGB -> destination, and just rely on the more general
mechanism.
Yes, this is yet-another cache class. I wanted to use one of many
that are laying around, but couldn't find a good fit. On the plus
side, it's not much code.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=3726
Change-Id: I943be5c99f0d691a87ffe8c5bc3067a8eb491fc2
Reviewed-on: https://skia-review.googlesource.com/3726
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/gpu/GrColorSpaceXform.cpp | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/src/gpu/GrColorSpaceXform.cpp b/src/gpu/GrColorSpaceXform.cpp index 8cfd374e8a..525bff9947 100644 --- a/src/gpu/GrColorSpaceXform.cpp +++ b/src/gpu/GrColorSpaceXform.cpp @@ -9,10 +9,55 @@ #include "SkColorSpace.h" #include "SkColorSpace_Base.h" #include "SkMatrix44.h" +#include "SkSpinlock.h" + +class GrColorSpaceXformCache { +public: + using NewValueFn = std::function<sk_sp<GrColorSpaceXform>(void)>; + + GrColorSpaceXformCache() : fSequence(0) {} + + sk_sp<GrColorSpaceXform> findOrAdd(uint64_t key, NewValueFn newValue) { + int oldest = 0; + for (int i = 0; i < kEntryCount; ++i) { + if (fEntries[i].fKey == key) { + fEntries[i].fLastUse = fSequence++; + return fEntries[i].fXform; + } + if (fEntries[i].fLastUse < fEntries[oldest].fLastUse) { + oldest = i; + } + } + fEntries[oldest].fKey = key; + fEntries[oldest].fXform = newValue(); + fEntries[oldest].fLastUse = fSequence++; + return fEntries[oldest].fXform; + } + +private: + enum { kEntryCount = 32 }; + + struct Entry { + // The default Entry is "valid". Any 64-bit key that is the same 32-bit value repeated + // implies no xform is necessary, so nullptr should be returned. This particular case should + // never happen, but by initializing all entries with this data, we can avoid special cases + // for the array not yet being full. + Entry() : fKey(0), fXform(nullptr), fLastUse(0) {} + + uint64_t fKey; + sk_sp<GrColorSpaceXform> fXform; + uint64_t fLastUse; + }; + + Entry fEntries[kEntryCount]; + uint64_t fSequence; +}; GrColorSpaceXform::GrColorSpaceXform(const SkMatrix44& srcToDst) : fSrcToDst(srcToDst) {} +static SkSpinlock gColorSpaceXformCacheSpinlock; + sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkColorSpace* dst) { if (!src || !dst) { // Invalid @@ -23,7 +68,7 @@ sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkColorSpace // Quick equality check - no conversion needed in this case return nullptr; } - + const SkMatrix44* toXYZD50 = as_CSB(src)->toXYZD50(); const SkMatrix44* fromXYZD50 = as_CSB(dst)->fromXYZD50(); if (!toXYZD50 || !fromXYZD50) { @@ -31,16 +76,34 @@ sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkColorSpace return nullptr; } - if (as_CSB(src)->toXYZD50Hash() == as_CSB(dst)->toXYZD50Hash()) { + uint32_t srcHash = as_CSB(src)->toXYZD50Hash(); + uint32_t dstHash = as_CSB(dst)->toXYZD50Hash(); + if (srcHash == dstHash) { // Identical gamut - no conversion needed in this case SkASSERT(*toXYZD50 == *as_CSB(dst)->toXYZD50() && "Hash collision"); return nullptr; } - SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor); - srcToDst.setConcat(*fromXYZD50, *toXYZD50); + auto deferredResult = [fromXYZD50, toXYZD50]() { + SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor); + srcToDst.setConcat(*fromXYZD50, *toXYZD50); + return sk_make_sp<GrColorSpaceXform>(srcToDst); + }; + + if (gColorSpaceXformCacheSpinlock.tryAcquire()) { + static GrColorSpaceXformCache* gCache; + if (nullptr == gCache) { + gCache = new GrColorSpaceXformCache(); + } - return sk_make_sp<GrColorSpaceXform>(srcToDst); + uint64_t key = static_cast<uint64_t>(srcHash) << 32 | static_cast<uint64_t>(dstHash); + sk_sp<GrColorSpaceXform> xform = gCache->findOrAdd(key, deferredResult); + gColorSpaceXformCacheSpinlock.release(); + return xform; + } else { + // Rather than wait for the spin lock, just bypass the cache + return deferredResult(); + } } bool GrColorSpaceXform::Equals(const GrColorSpaceXform* a, const GrColorSpaceXform* b) { |