blob: 906bc6a4beda3b0cd520bbc25755a9ce974a6430 [file] [log] [blame]
bsalomon744998e2014-08-28 09:54:34 -07001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrResourceKey_DEFINED
10#define GrResourceKey_DEFINED
11
12#include "GrTypes.h"
bsalomon7775c852014-12-30 12:50:52 -080013#include "SkTemplates.h"
bsalomon744998e2014-08-28 09:54:34 -070014
bsalomon24db3b12015-01-23 04:24:04 -080015uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
bsalomon7775c852014-12-30 12:50:52 -080016
bsalomon24db3b12015-01-23 04:24:04 -080017class GrResourceKey {
18public:
19 uint32_t hash() const {
20 this->validate();
21 return fKey[kHash_MetaDataIdx];
22 }
23
24 size_t size() const {
25 this->validate();
bsalomon3bd12ef2015-01-28 11:39:48 -080026 SkASSERT(this->isValid());
bsalomon24db3b12015-01-23 04:24:04 -080027 return this->internalSize();
28 }
29
bsalomon24db3b12015-01-23 04:24:04 -080030protected:
31 static const uint32_t kInvalidDomain = 0;
32
33 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080034
35 /** Reset to an invalid key. */
36 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080037 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080038 fKey.reset(kMetaDataCnt);
39 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080040 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080041 }
42
bsalomon24db3b12015-01-23 04:24:04 -080043 bool operator==(const GrResourceKey& that) const {
bsalomone167f962015-01-27 09:56:04 -080044 SkASSERT(this->isValid() && that.isValid());
bsalomon24db3b12015-01-23 04:24:04 -080045 return 0 == memcmp(fKey.get(), that.fKey.get(), this->size());
46 }
bsalomon7775c852014-12-30 12:50:52 -080047
bsalomon24db3b12015-01-23 04:24:04 -080048 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon23e619c2015-02-06 11:54:28 -080049 SkASSERT(that.isValid());
bsalomon4dffede2015-01-23 07:17:55 -080050 if (this != &that) {
51 size_t bytes = that.size();
52 SkASSERT(SkIsAlign4(bytes));
53 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
54 memcpy(fKey.get(), that.fKey.get(), bytes);
bsalomon23e619c2015-02-06 11:54:28 -080055 this->validate();
bsalomon4dffede2015-01-23 07:17:55 -080056 }
bsalomon7775c852014-12-30 12:50:52 -080057 return *this;
58 }
59
bsalomon24db3b12015-01-23 04:24:04 -080060 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080061
bsalomon24db3b12015-01-23 04:24:04 -080062 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
63
bsalomon3bd12ef2015-01-28 11:39:48 -080064 /** size of the key data, excluding meta-data (hash, domain, etc). */
65 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
66
67 /** ptr to the key data, excluding meta-data (hash, domain, etc). */
68 const uint32_t* data() const {
69 this->validate();
70 return &fKey[kMetaDataCnt];
71 }
72
bsalomon24db3b12015-01-23 04:24:04 -080073 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080074 class Builder {
75 public:
bsalomon24db3b12015-01-23 04:24:04 -080076 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080077 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080078 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080079 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080080 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080081 SkASSERT(SkToU16(size) == size);
82 SkASSERT(SkToU16(domain) == domain);
83 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080084 }
85
86 ~Builder() { this->finish(); }
87
bsalomon24db3b12015-01-23 04:24:04 -080088 void finish() {
89 if (NULL == fKey) {
90 return;
91 }
92 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
93 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
94 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
95 fKey->validate();
96 fKey = NULL;
97 }
bsalomon7775c852014-12-30 12:50:52 -080098
99 uint32_t& operator[](int dataIdx) {
100 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -0800101 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -0800102 SkASSERT(SkToU32(dataIdx) < dataCount);
103 return fKey->fKey[kMetaDataCnt + dataIdx];
104 }
105
106 private:
bsalomon24db3b12015-01-23 04:24:04 -0800107 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800108 };
109
110private:
111 enum MetaDataIdx {
112 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800113 // The key domain and size are packed into a single uint32_t.
114 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800115
bsalomon24db3b12015-01-23 04:24:04 -0800116 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800117 };
bsalomon7775c852014-12-30 12:50:52 -0800118 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
119
bsalomon3bd12ef2015-01-28 11:39:48 -0800120 size_t internalSize() const {
121 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
122 }
123
124 void validate() const {
125 SkASSERT(fKey[kHash_MetaDataIdx] ==
126 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
127 this->internalSize() - sizeof(uint32_t)));
128 SkASSERT(SkIsAlign4(this->internalSize()));
129 }
130
bsalomon1c60dfe2015-01-21 09:32:40 -0800131 friend class TestResource; // For unit test to access kMetaDataCnt.
132
bsalomon24db3b12015-01-23 04:24:04 -0800133 // bmp textures require 4 uint32_t values.
134 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800135};
136
bsalomon24db3b12015-01-23 04:24:04 -0800137/**
138 * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a
139 * hash, a data length, and type-specific data. A Builder object is used to initialize the
140 * key contents. The contents must be initialized before the key can be used.
141 */
142class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700143private:
bsalomon24db3b12015-01-23 04:24:04 -0800144 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700145
bsalomon24db3b12015-01-23 04:24:04 -0800146public:
147 /** Uniquely identifies the type of resource that is cached as scratch. */
148 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700149
bsalomon24db3b12015-01-23 04:24:04 -0800150 /** Generate a unique ResourceType. */
151 static ResourceType GenerateResourceType();
152
153 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
154 GrScratchKey() {}
155
156 GrScratchKey(const GrScratchKey& that) { *this = that; }
157
158 /** reset() returns the key to the invalid state. */
159 using INHERITED::reset;
160
161 using INHERITED::isValid;
162
163 ResourceType resourceType() const { return this->domain(); }
164
165 GrScratchKey& operator=(const GrScratchKey& that) {
166 this->INHERITED::operator=(that);
167 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700168 }
bsalomon24db3b12015-01-23 04:24:04 -0800169
170 bool operator==(const GrScratchKey& that) const {
171 return this->INHERITED::operator==(that);
172 }
173 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
174
175 class Builder : public INHERITED::Builder {
176 public:
177 Builder(GrScratchKey* key, ResourceType type, int data32Count)
178 : INHERITED::Builder(key, type, data32Count) {}
179 };
180};
181
182/**
183 * A key used to cache resources based on their content. The key consists of a domain type (use
184 * case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to
185 * initialize the key contents. The contents must be initialized before the key can be used.
186 */
187class GrContentKey : public GrResourceKey {
188private:
189 typedef GrResourceKey INHERITED;
190
191public:
192 typedef uint32_t Domain;
193 /** Generate a unique Domain of content keys. */
194 static Domain GenerateDomain();
195
196 /** Creates an invalid content key. It must be initialized using a Builder object before use. */
197 GrContentKey() {}
198
199 GrContentKey(const GrContentKey& that) { *this = that; }
200
201 /** reset() returns the key to the invalid state. */
202 using INHERITED::reset;
203
204 using INHERITED::isValid;
205
206 GrContentKey& operator=(const GrContentKey& that) {
207 this->INHERITED::operator=(that);
208 return *this;
209 }
210
211 bool operator==(const GrContentKey& that) const {
212 return this->INHERITED::operator==(that);
213 }
214 bool operator!=(const GrContentKey& that) const { return !(*this == that); }
215
216 class Builder : public INHERITED::Builder {
217 public:
218 Builder(GrContentKey* key, Domain domain, int data32Count)
219 : INHERITED::Builder(key, domain, data32Count) {}
220
221 /** Used to build a key that wraps another key and adds additional data. */
222 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain,
223 int extraData32Cnt)
bsalomon3bd12ef2015-01-28 11:39:48 -0800224 : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
bsalomon37f9a262015-02-02 13:00:10 -0800225 SkASSERT(&innerKey != key);
bsalomon24db3b12015-01-23 04:24:04 -0800226 // add the inner key to the end of the key so that op[] can be indexed normally.
bsalomon3bd12ef2015-01-28 11:39:48 -0800227 uint32_t* innerKeyData = &this->operator[](extraData32Cnt);
228 const uint32_t* srcData = innerKey.data();
229 (*innerKeyData++) = innerKey.domain();
230 memcpy(innerKeyData, srcData, innerKey.dataSize());
231 }
232
233 private:
234 static int Data32CntForInnerKey(const GrContentKey& innerKey) {
235 // key data + domain
236 return SkToInt((innerKey.dataSize() >> 2) + 1);
bsalomon24db3b12015-01-23 04:24:04 -0800237 }
238 };
bsalomon744998e2014-08-28 09:54:34 -0700239};
240
bsalomon23e619c2015-02-06 11:54:28 -0800241// The cache listens for these messages to purge junk resources proactively.
242class GrContentKeyInvalidatedMessage {
243public:
244 explicit GrContentKeyInvalidatedMessage(const GrContentKey& key) : fKey(key) {}
245 GrContentKeyInvalidatedMessage(const GrContentKeyInvalidatedMessage& that) : fKey(that.fKey) {}
246 GrContentKeyInvalidatedMessage& operator=(const GrContentKeyInvalidatedMessage& that) {
247 fKey = that.fKey;
248 return *this;
249 }
250 const GrContentKey& key() const { return fKey; }
251private:
252 GrContentKey fKey;
253};
bsalomon744998e2014-08-28 09:54:34 -0700254#endif