blob: 58f160cd18b83b3c786b5e45f1e75b09df0896ff [file] [log] [blame]
brianosman54f30c12016-07-18 10:53:52 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrColorSpaceXform.h"
9#include "SkColorSpace.h"
brianosman971cd492016-09-08 10:10:11 -070010#include "SkColorSpace_Base.h"
brianosman9f978822016-07-27 05:25:26 -070011#include "SkMatrix44.h"
Brian Osman7c2114f2016-10-20 15:34:06 -040012#include "SkSpinlock.h"
13
14class GrColorSpaceXformCache {
15public:
16 using NewValueFn = std::function<sk_sp<GrColorSpaceXform>(void)>;
17
18 GrColorSpaceXformCache() : fSequence(0) {}
19
20 sk_sp<GrColorSpaceXform> findOrAdd(uint64_t key, NewValueFn newValue) {
21 int oldest = 0;
22 for (int i = 0; i < kEntryCount; ++i) {
23 if (fEntries[i].fKey == key) {
24 fEntries[i].fLastUse = fSequence++;
25 return fEntries[i].fXform;
26 }
27 if (fEntries[i].fLastUse < fEntries[oldest].fLastUse) {
28 oldest = i;
29 }
30 }
31 fEntries[oldest].fKey = key;
32 fEntries[oldest].fXform = newValue();
33 fEntries[oldest].fLastUse = fSequence++;
34 return fEntries[oldest].fXform;
35 }
36
37private:
38 enum { kEntryCount = 32 };
39
40 struct Entry {
41 // The default Entry is "valid". Any 64-bit key that is the same 32-bit value repeated
42 // implies no xform is necessary, so nullptr should be returned. This particular case should
43 // never happen, but by initializing all entries with this data, we can avoid special cases
44 // for the array not yet being full.
45 Entry() : fKey(0), fXform(nullptr), fLastUse(0) {}
46
47 uint64_t fKey;
48 sk_sp<GrColorSpaceXform> fXform;
49 uint64_t fLastUse;
50 };
51
52 Entry fEntries[kEntryCount];
53 uint64_t fSequence;
54};
brianosman54f30c12016-07-18 10:53:52 -070055
Brian Osmane411a072017-03-14 10:26:58 -040056GrColorSpaceXform::GrColorSpaceXform(const SkMatrix44& srcToDst)
brianosman51924752016-09-12 08:50:19 -070057 : fSrcToDst(srcToDst) {}
brianosman9f978822016-07-27 05:25:26 -070058
Brian Osman7c2114f2016-10-20 15:34:06 -040059static SkSpinlock gColorSpaceXformCacheSpinlock;
60
Brian Osmane411a072017-03-14 10:26:58 -040061sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(const SkColorSpace* src, const SkColorSpace* dst) {
brianosman54f30c12016-07-18 10:53:52 -070062 if (!src || !dst) {
63 // Invalid
64 return nullptr;
65 }
66
67 if (src == dst) {
68 // Quick equality check - no conversion needed in this case
69 return nullptr;
70 }
Brian Osman7c2114f2016-10-20 15:34:06 -040071
raftias94888332016-10-18 10:02:51 -070072 const SkMatrix44* toXYZD50 = as_CSB(src)->toXYZD50();
73 const SkMatrix44* fromXYZD50 = as_CSB(dst)->fromXYZD50();
74 if (!toXYZD50 || !fromXYZD50) {
75 // unsupported colour spaces -- cannot specify gamut as a matrix
76 return nullptr;
77 }
brianosman54f30c12016-07-18 10:53:52 -070078
Brian Osman7c2114f2016-10-20 15:34:06 -040079 uint32_t srcHash = as_CSB(src)->toXYZD50Hash();
80 uint32_t dstHash = as_CSB(dst)->toXYZD50Hash();
81 if (srcHash == dstHash) {
Brian Osmanbbf251b2016-10-19 14:56:07 -040082 // Identical gamut - no conversion needed in this case
83 SkASSERT(*toXYZD50 == *as_CSB(dst)->toXYZD50() && "Hash collision");
brianosman54f30c12016-07-18 10:53:52 -070084 return nullptr;
85 }
86
Brian Osman7c2114f2016-10-20 15:34:06 -040087 auto deferredResult = [fromXYZD50, toXYZD50]() {
88 SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor);
89 srcToDst.setConcat(*fromXYZD50, *toXYZD50);
90 return sk_make_sp<GrColorSpaceXform>(srcToDst);
91 };
Brian Osmanbbf251b2016-10-19 14:56:07 -040092
Brian Osman7c2114f2016-10-20 15:34:06 -040093 if (gColorSpaceXformCacheSpinlock.tryAcquire()) {
94 static GrColorSpaceXformCache* gCache;
95 if (nullptr == gCache) {
96 gCache = new GrColorSpaceXformCache();
97 }
98
99 uint64_t key = static_cast<uint64_t>(srcHash) << 32 | static_cast<uint64_t>(dstHash);
100 sk_sp<GrColorSpaceXform> xform = gCache->findOrAdd(key, deferredResult);
101 gColorSpaceXformCacheSpinlock.release();
102 return xform;
103 } else {
104 // Rather than wait for the spin lock, just bypass the cache
105 return deferredResult();
106 }
brianosman54f30c12016-07-18 10:53:52 -0700107}
brianosman5a7ae7e2016-09-12 12:07:25 -0700108
brianosmanb9c51372016-09-15 11:09:45 -0700109bool GrColorSpaceXform::Equals(const GrColorSpaceXform* a, const GrColorSpaceXform* b) {
110 if (a == b) {
111 return true;
112 }
113
114 if (!a || !b) {
115 return false;
116 }
117
118 return a->fSrcToDst == b->fSrcToDst;
119}
120
brianosman5a7ae7e2016-09-12 12:07:25 -0700121GrColor4f GrColorSpaceXform::apply(const GrColor4f& srcColor) {
122 GrColor4f result;
123 fSrcToDst.mapScalars(srcColor.fRGBA, result.fRGBA);
Brian Osman0bd783f2017-01-04 12:54:07 -0500124 // We always operate on unpremul colors, so clamp to [0,1].
Brian Osman01648962016-12-01 17:04:43 -0500125 for (int i = 0; i < 4; ++i) {
126 result.fRGBA[i] = SkTPin(result.fRGBA[i], 0.0f, 1.0f);
127 }
brianosman5a7ae7e2016-09-12 12:07:25 -0700128 return result;
129}