blob: 9cf955be9817f39b4abceda4aaf5b2c8dd9cb824 [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#include "GrBinHashKey.h"
15
bsalomon24db3b12015-01-23 04:24:04 -080016uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
bsalomon7775c852014-12-30 12:50:52 -080017
bsalomon24db3b12015-01-23 04:24:04 -080018class GrResourceKey {
19public:
20 uint32_t hash() const {
21 this->validate();
22 return fKey[kHash_MetaDataIdx];
23 }
24
25 size_t size() const {
26 this->validate();
27 return this->internalSize();
28 }
29
30 const uint32_t* data() const {
31 this->validate();
32 return &fKey[kMetaDataCnt];
33 }
34
35protected:
36 static const uint32_t kInvalidDomain = 0;
37
38 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080039
40 /** Reset to an invalid key. */
41 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080042 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080043 fKey.reset(kMetaDataCnt);
44 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080045 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080046 }
47
bsalomon24db3b12015-01-23 04:24:04 -080048 bool operator==(const GrResourceKey& that) const {
49 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) {
bsalomon1c60dfe2015-01-21 09:32:40 -080053 size_t bytes = that.size();
bsalomon24db3b12015-01-23 04:24:04 -080054 SkASSERT(SkIsAlign4(bytes));
bsalomon1c60dfe2015-01-21 09:32:40 -080055 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
56 memcpy(fKey.get(), that.fKey.get(), bytes);
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
64 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080065 class Builder {
66 public:
bsalomon24db3b12015-01-23 04:24:04 -080067 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080068 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080069 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080070 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080071 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080072 SkASSERT(SkToU16(size) == size);
73 SkASSERT(SkToU16(domain) == domain);
74 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080075 }
76
77 ~Builder() { this->finish(); }
78
bsalomon24db3b12015-01-23 04:24:04 -080079 void finish() {
80 if (NULL == fKey) {
81 return;
82 }
83 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
84 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
85 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
86 fKey->validate();
87 fKey = NULL;
88 }
bsalomon7775c852014-12-30 12:50:52 -080089
90 uint32_t& operator[](int dataIdx) {
91 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -080092 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -080093 SkASSERT(SkToU32(dataIdx) < dataCount);
94 return fKey->fKey[kMetaDataCnt + dataIdx];
95 }
96
97 private:
bsalomon24db3b12015-01-23 04:24:04 -080098 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -080099 };
100
101private:
bsalomon24db3b12015-01-23 04:24:04 -0800102 size_t internalSize() const {
103 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
104 }
105
106 void validate() const {
107 SkASSERT(fKey[kHash_MetaDataIdx] ==
108 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
109 this->internalSize() - sizeof(uint32_t)));
110 }
111
bsalomon7775c852014-12-30 12:50:52 -0800112 enum MetaDataIdx {
113 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800114 // The key domain and size are packed into a single uint32_t.
115 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800116
bsalomon24db3b12015-01-23 04:24:04 -0800117 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800118 };
bsalomon7775c852014-12-30 12:50:52 -0800119 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
120
bsalomon1c60dfe2015-01-21 09:32:40 -0800121 friend class TestResource; // For unit test to access kMetaDataCnt.
122
bsalomon24db3b12015-01-23 04:24:04 -0800123 // bmp textures require 4 uint32_t values.
124 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800125};
126
bsalomon24db3b12015-01-23 04:24:04 -0800127/**
128 * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a
129 * hash, a data length, and type-specific data. A Builder object is used to initialize the
130 * key contents. The contents must be initialized before the key can be used.
131 */
132class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700133private:
bsalomon24db3b12015-01-23 04:24:04 -0800134 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700135
bsalomon24db3b12015-01-23 04:24:04 -0800136public:
137 /** Uniquely identifies the type of resource that is cached as scratch. */
138 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700139
bsalomon24db3b12015-01-23 04:24:04 -0800140 /** Generate a unique ResourceType. */
141 static ResourceType GenerateResourceType();
142
143 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
144 GrScratchKey() {}
145
146 GrScratchKey(const GrScratchKey& that) { *this = that; }
147
148 /** reset() returns the key to the invalid state. */
149 using INHERITED::reset;
150
151 using INHERITED::isValid;
152
153 ResourceType resourceType() const { return this->domain(); }
154
155 GrScratchKey& operator=(const GrScratchKey& that) {
156 this->INHERITED::operator=(that);
157 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700158 }
bsalomon24db3b12015-01-23 04:24:04 -0800159
160 bool operator==(const GrScratchKey& that) const {
161 return this->INHERITED::operator==(that);
162 }
163 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
164
165 class Builder : public INHERITED::Builder {
166 public:
167 Builder(GrScratchKey* key, ResourceType type, int data32Count)
168 : INHERITED::Builder(key, type, data32Count) {}
169 };
170};
171
172/**
173 * A key used to cache resources based on their content. The key consists of a domain type (use
174 * case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to
175 * initialize the key contents. The contents must be initialized before the key can be used.
176 */
177class GrContentKey : public GrResourceKey {
178private:
179 typedef GrResourceKey INHERITED;
180
181public:
182 typedef uint32_t Domain;
183 /** Generate a unique Domain of content keys. */
184 static Domain GenerateDomain();
185
186 /** Creates an invalid content key. It must be initialized using a Builder object before use. */
187 GrContentKey() {}
188
189 GrContentKey(const GrContentKey& that) { *this = that; }
190
191 /** reset() returns the key to the invalid state. */
192 using INHERITED::reset;
193
194 using INHERITED::isValid;
195
196 GrContentKey& operator=(const GrContentKey& that) {
197 this->INHERITED::operator=(that);
198 return *this;
199 }
200
201 bool operator==(const GrContentKey& that) const {
202 return this->INHERITED::operator==(that);
203 }
204 bool operator!=(const GrContentKey& that) const { return !(*this == that); }
205
206 class Builder : public INHERITED::Builder {
207 public:
208 Builder(GrContentKey* key, Domain domain, int data32Count)
209 : INHERITED::Builder(key, domain, data32Count) {}
210
211 /** Used to build a key that wraps another key and adds additional data. */
212 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain,
213 int extraData32Cnt)
214 : INHERITED::Builder(key, domain, (SkToInt(innerKey.size()) >> 2) + extraData32Cnt) {
215 int innerKeyCnt = SkToInt(innerKey.size()) >> 2;
216 // add the inner key to the end of the key so that op[] can be indexed normally.
217 for (int i = 0; i < innerKeyCnt; ++i) {
218 this->operator[](extraData32Cnt + i) = innerKey.data()[i];
219 }
220 }
221 };
bsalomon744998e2014-08-28 09:54:34 -0700222};
223
224#endif