blob: 15d7b439f10827d7a1fd479987b94d7947cba705 [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 {
48 return 0 == memcmp(fKey.get(), that.fKey.get(), this->size());
49 }
bsalomon7775c852014-12-30 12:50:52 -080050
bsalomon24db3b12015-01-23 04:24:04 -080051 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon4dffede2015-01-23 07:17:55 -080052 if (this != &that) {
53 size_t bytes = that.size();
54 SkASSERT(SkIsAlign4(bytes));
55 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
56 memcpy(fKey.get(), that.fKey.get(), bytes);
57 }
bsalomon7775c852014-12-30 12:50:52 -080058 return *this;
59 }
60
bsalomon24db3b12015-01-23 04:24:04 -080061 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080062
bsalomon24db3b12015-01-23 04:24:04 -080063 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
64
65 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080066 class Builder {
67 public:
bsalomon24db3b12015-01-23 04:24:04 -080068 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080069 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080070 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080071 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080072 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080073 SkASSERT(SkToU16(size) == size);
74 SkASSERT(SkToU16(domain) == domain);
75 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080076 }
77
78 ~Builder() { this->finish(); }
79
bsalomon24db3b12015-01-23 04:24:04 -080080 void finish() {
81 if (NULL == fKey) {
82 return;
83 }
84 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
85 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
86 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
87 fKey->validate();
88 fKey = NULL;
89 }
bsalomon7775c852014-12-30 12:50:52 -080090
91 uint32_t& operator[](int dataIdx) {
92 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -080093 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -080094 SkASSERT(SkToU32(dataIdx) < dataCount);
95 return fKey->fKey[kMetaDataCnt + dataIdx];
96 }
97
98 private:
bsalomon24db3b12015-01-23 04:24:04 -080099 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800100 };
101
102private:
bsalomon24db3b12015-01-23 04:24:04 -0800103 size_t internalSize() const {
104 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
105 }
106
107 void validate() const {
108 SkASSERT(fKey[kHash_MetaDataIdx] ==
109 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
110 this->internalSize() - sizeof(uint32_t)));
111 }
112
bsalomon7775c852014-12-30 12:50:52 -0800113 enum MetaDataIdx {
114 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800115 // The key domain and size are packed into a single uint32_t.
116 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800117
bsalomon24db3b12015-01-23 04:24:04 -0800118 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800119 };
bsalomon7775c852014-12-30 12:50:52 -0800120 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
121
bsalomon1c60dfe2015-01-21 09:32:40 -0800122 friend class TestResource; // For unit test to access kMetaDataCnt.
123
bsalomon24db3b12015-01-23 04:24:04 -0800124 // bmp textures require 4 uint32_t values.
125 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800126};
127
bsalomon24db3b12015-01-23 04:24:04 -0800128/**
129 * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a
130 * hash, a data length, and type-specific data. A Builder object is used to initialize the
131 * key contents. The contents must be initialized before the key can be used.
132 */
133class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700134private:
bsalomon24db3b12015-01-23 04:24:04 -0800135 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700136
bsalomon24db3b12015-01-23 04:24:04 -0800137public:
138 /** Uniquely identifies the type of resource that is cached as scratch. */
139 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700140
bsalomon24db3b12015-01-23 04:24:04 -0800141 /** Generate a unique ResourceType. */
142 static ResourceType GenerateResourceType();
143
144 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
145 GrScratchKey() {}
146
147 GrScratchKey(const GrScratchKey& that) { *this = that; }
148
149 /** reset() returns the key to the invalid state. */
150 using INHERITED::reset;
151
152 using INHERITED::isValid;
153
154 ResourceType resourceType() const { return this->domain(); }
155
156 GrScratchKey& operator=(const GrScratchKey& that) {
157 this->INHERITED::operator=(that);
158 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700159 }
bsalomon24db3b12015-01-23 04:24:04 -0800160
161 bool operator==(const GrScratchKey& that) const {
162 return this->INHERITED::operator==(that);
163 }
164 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
165
166 class Builder : public INHERITED::Builder {
167 public:
168 Builder(GrScratchKey* key, ResourceType type, int data32Count)
169 : INHERITED::Builder(key, type, data32Count) {}
170 };
171};
172
173/**
174 * A key used to cache resources based on their content. The key consists of a domain type (use
175 * case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to
176 * initialize the key contents. The contents must be initialized before the key can be used.
177 */
178class GrContentKey : public GrResourceKey {
179private:
180 typedef GrResourceKey INHERITED;
181
182public:
183 typedef uint32_t Domain;
184 /** Generate a unique Domain of content keys. */
185 static Domain GenerateDomain();
186
187 /** Creates an invalid content key. It must be initialized using a Builder object before use. */
188 GrContentKey() {}
189
190 GrContentKey(const GrContentKey& that) { *this = that; }
191
192 /** reset() returns the key to the invalid state. */
193 using INHERITED::reset;
194
195 using INHERITED::isValid;
196
197 GrContentKey& operator=(const GrContentKey& that) {
198 this->INHERITED::operator=(that);
199 return *this;
200 }
201
202 bool operator==(const GrContentKey& that) const {
203 return this->INHERITED::operator==(that);
204 }
205 bool operator!=(const GrContentKey& that) const { return !(*this == that); }
206
207 class Builder : public INHERITED::Builder {
208 public:
209 Builder(GrContentKey* key, Domain domain, int data32Count)
210 : INHERITED::Builder(key, domain, data32Count) {}
211
212 /** Used to build a key that wraps another key and adds additional data. */
213 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain,
214 int extraData32Cnt)
215 : INHERITED::Builder(key, domain, (SkToInt(innerKey.size()) >> 2) + extraData32Cnt) {
216 int innerKeyCnt = SkToInt(innerKey.size()) >> 2;
217 // add the inner key to the end of the key so that op[] can be indexed normally.
218 for (int i = 0; i < innerKeyCnt; ++i) {
219 this->operator[](extraData32Cnt + i) = innerKey.data()[i];
220 }
221 }
222 };
bsalomon744998e2014-08-28 09:54:34 -0700223};
224
225#endif