blob: 26373a785963b2c0f8ff212a19f15bb58405a5fb [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();
26 return this->internalSize();
27 }
28
29 const uint32_t* data() const {
30 this->validate();
31 return &fKey[kMetaDataCnt];
32 }
33
34protected:
35 static const uint32_t kInvalidDomain = 0;
36
37 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080038
39 /** Reset to an invalid key. */
40 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080041 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080042 fKey.reset(kMetaDataCnt);
43 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080044 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080045 }
46
bsalomon24db3b12015-01-23 04:24:04 -080047 bool operator==(const GrResourceKey& that) const {
bsalomone167f962015-01-27 09:56:04 -080048 SkASSERT(this->isValid() && that.isValid());
bsalomon24db3b12015-01-23 04:24:04 -080049 return 0 == memcmp(fKey.get(), that.fKey.get(), this->size());
50 }
bsalomon7775c852014-12-30 12:50:52 -080051
bsalomon24db3b12015-01-23 04:24:04 -080052 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon4dffede2015-01-23 07:17:55 -080053 if (this != &that) {
54 size_t bytes = that.size();
55 SkASSERT(SkIsAlign4(bytes));
56 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
57 memcpy(fKey.get(), that.fKey.get(), bytes);
58 }
bsalomon7775c852014-12-30 12:50:52 -080059 return *this;
60 }
61
bsalomon24db3b12015-01-23 04:24:04 -080062 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080063
bsalomon24db3b12015-01-23 04:24:04 -080064 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
65
66 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080067 class Builder {
68 public:
bsalomon24db3b12015-01-23 04:24:04 -080069 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080070 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080071 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080072 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080073 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080074 SkASSERT(SkToU16(size) == size);
75 SkASSERT(SkToU16(domain) == domain);
76 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080077 }
78
79 ~Builder() { this->finish(); }
80
bsalomon24db3b12015-01-23 04:24:04 -080081 void finish() {
82 if (NULL == fKey) {
83 return;
84 }
85 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
86 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
87 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
88 fKey->validate();
89 fKey = NULL;
90 }
bsalomon7775c852014-12-30 12:50:52 -080091
92 uint32_t& operator[](int dataIdx) {
93 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -080094 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -080095 SkASSERT(SkToU32(dataIdx) < dataCount);
96 return fKey->fKey[kMetaDataCnt + dataIdx];
97 }
98
99 private:
bsalomon24db3b12015-01-23 04:24:04 -0800100 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800101 };
102
103private:
bsalomon24db3b12015-01-23 04:24:04 -0800104 size_t internalSize() const {
105 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
106 }
107
108 void validate() const {
109 SkASSERT(fKey[kHash_MetaDataIdx] ==
110 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
111 this->internalSize() - sizeof(uint32_t)));
112 }
113
bsalomon7775c852014-12-30 12:50:52 -0800114 enum MetaDataIdx {
115 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800116 // The key domain and size are packed into a single uint32_t.
117 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800118
bsalomon24db3b12015-01-23 04:24:04 -0800119 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800120 };
bsalomon7775c852014-12-30 12:50:52 -0800121 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
122
bsalomon1c60dfe2015-01-21 09:32:40 -0800123 friend class TestResource; // For unit test to access kMetaDataCnt.
124
bsalomon24db3b12015-01-23 04:24:04 -0800125 // bmp textures require 4 uint32_t values.
126 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800127};
128
bsalomon24db3b12015-01-23 04:24:04 -0800129/**
130 * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a
131 * hash, a data length, and type-specific data. A Builder object is used to initialize the
132 * key contents. The contents must be initialized before the key can be used.
133 */
134class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700135private:
bsalomon24db3b12015-01-23 04:24:04 -0800136 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700137
bsalomon24db3b12015-01-23 04:24:04 -0800138public:
139 /** Uniquely identifies the type of resource that is cached as scratch. */
140 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700141
bsalomon24db3b12015-01-23 04:24:04 -0800142 /** Generate a unique ResourceType. */
143 static ResourceType GenerateResourceType();
144
145 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
146 GrScratchKey() {}
147
148 GrScratchKey(const GrScratchKey& that) { *this = that; }
149
150 /** reset() returns the key to the invalid state. */
151 using INHERITED::reset;
152
153 using INHERITED::isValid;
154
155 ResourceType resourceType() const { return this->domain(); }
156
157 GrScratchKey& operator=(const GrScratchKey& that) {
158 this->INHERITED::operator=(that);
159 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700160 }
bsalomon24db3b12015-01-23 04:24:04 -0800161
162 bool operator==(const GrScratchKey& that) const {
163 return this->INHERITED::operator==(that);
164 }
165 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
166
167 class Builder : public INHERITED::Builder {
168 public:
169 Builder(GrScratchKey* key, ResourceType type, int data32Count)
170 : INHERITED::Builder(key, type, data32Count) {}
171 };
172};
173
174/**
175 * A key used to cache resources based on their content. The key consists of a domain type (use
176 * case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to
177 * initialize the key contents. The contents must be initialized before the key can be used.
178 */
179class GrContentKey : public GrResourceKey {
180private:
181 typedef GrResourceKey INHERITED;
182
183public:
184 typedef uint32_t Domain;
185 /** Generate a unique Domain of content keys. */
186 static Domain GenerateDomain();
187
188 /** Creates an invalid content key. It must be initialized using a Builder object before use. */
189 GrContentKey() {}
190
191 GrContentKey(const GrContentKey& that) { *this = that; }
192
193 /** reset() returns the key to the invalid state. */
194 using INHERITED::reset;
195
196 using INHERITED::isValid;
197
198 GrContentKey& operator=(const GrContentKey& that) {
199 this->INHERITED::operator=(that);
200 return *this;
201 }
202
203 bool operator==(const GrContentKey& that) const {
204 return this->INHERITED::operator==(that);
205 }
206 bool operator!=(const GrContentKey& that) const { return !(*this == that); }
207
208 class Builder : public INHERITED::Builder {
209 public:
210 Builder(GrContentKey* key, Domain domain, int data32Count)
211 : INHERITED::Builder(key, domain, data32Count) {}
212
213 /** Used to build a key that wraps another key and adds additional data. */
214 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain,
215 int extraData32Cnt)
216 : INHERITED::Builder(key, domain, (SkToInt(innerKey.size()) >> 2) + extraData32Cnt) {
217 int innerKeyCnt = SkToInt(innerKey.size()) >> 2;
218 // add the inner key to the end of the key so that op[] can be indexed normally.
219 for (int i = 0; i < innerKeyCnt; ++i) {
220 this->operator[](extraData32Cnt + i) = innerKey.data()[i];
221 }
222 }
223 };
bsalomon744998e2014-08-28 09:54:34 -0700224};
225
226#endif