aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/private/SkSpinlock.h10
-rw-r--r--src/gpu/GrColorSpaceXform.cpp73
2 files changed, 78 insertions, 5 deletions
diff --git a/include/private/SkSpinlock.h b/include/private/SkSpinlock.h
index a5d378289f..669a926c7e 100644
--- a/include/private/SkSpinlock.h
+++ b/include/private/SkSpinlock.h
@@ -23,6 +23,16 @@ public:
}
}
+ // Acquire the lock or fail (quickly). Lets the caller decide to do something other than wait.
+ bool tryAcquire() {
+ // To act as a mutex, we need an acquire barrier when we acquire the lock.
+ if (fLocked.exchange(true, std::memory_order_acquire)) {
+ // Lock was contended. Let the caller decide what to do.
+ return false;
+ }
+ return true;
+ }
+
void release() {
// To act as a mutex, we need a release barrier when we release the lock.
fLocked.store(false, std::memory_order_release);
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) {