blob: d500a65657923f4ca2fa304075d5432655c1db8a [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) {
bsalomon1c60dfe2015-01-21 09:32:40 -080052 size_t bytes = that.size();
bsalomon24db3b12015-01-23 04:24:04 -080053 SkASSERT(SkIsAlign4(bytes));
bsalomon1c60dfe2015-01-21 09:32:40 -080054 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
55 memcpy(fKey.get(), that.fKey.get(), bytes);
bsalomon7775c852014-12-30 12:50:52 -080056 return *this;
57 }
58
bsalomon24db3b12015-01-23 04:24:04 -080059 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080060
bsalomon24db3b12015-01-23 04:24:04 -080061 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
62
63 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080064 class Builder {
65 public:
bsalomon24db3b12015-01-23 04:24:04 -080066 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080067 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080068 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080069 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080070 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080071 SkASSERT(SkToU16(size) == size);
72 SkASSERT(SkToU16(domain) == domain);
73 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080074 }
75
76 ~Builder() { this->finish(); }
77
bsalomon24db3b12015-01-23 04:24:04 -080078 void finish() {
79 if (NULL == fKey) {
80 return;
81 }
82 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
83 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
84 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
85 fKey->validate();
86 fKey = NULL;
87 }
bsalomon7775c852014-12-30 12:50:52 -080088
89 uint32_t& operator[](int dataIdx) {
90 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -080091 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -080092 SkASSERT(SkToU32(dataIdx) < dataCount);
93 return fKey->fKey[kMetaDataCnt + dataIdx];
94 }
95
96 private:
bsalomon24db3b12015-01-23 04:24:04 -080097 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -080098 };
99
100private:
bsalomon24db3b12015-01-23 04:24:04 -0800101 size_t internalSize() const {
102 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
103 }
104
105 void validate() const {
106 SkASSERT(fKey[kHash_MetaDataIdx] ==
107 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
108 this->internalSize() - sizeof(uint32_t)));
109 }
110
bsalomon7775c852014-12-30 12:50:52 -0800111 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
bsalomon1c60dfe2015-01-21 09:32:40 -0800120 friend class TestResource; // For unit test to access kMetaDataCnt.
121
bsalomon24db3b12015-01-23 04:24:04 -0800122 // bmp textures require 4 uint32_t values.
123 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800124};
125
bsalomon24db3b12015-01-23 04:24:04 -0800126/**
127 * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a
128 * hash, a data length, and type-specific data. A Builder object is used to initialize the
129 * key contents. The contents must be initialized before the key can be used.
130 */
131class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700132private:
bsalomon24db3b12015-01-23 04:24:04 -0800133 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700134
bsalomon24db3b12015-01-23 04:24:04 -0800135public:
136 /** Uniquely identifies the type of resource that is cached as scratch. */
137 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700138
bsalomon24db3b12015-01-23 04:24:04 -0800139 /** Generate a unique ResourceType. */
140 static ResourceType GenerateResourceType();
141
142 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
143 GrScratchKey() {}
144
145 GrScratchKey(const GrScratchKey& that) { *this = that; }
146
147 /** reset() returns the key to the invalid state. */
148 using INHERITED::reset;
149
150 using INHERITED::isValid;
151
152 ResourceType resourceType() const { return this->domain(); }
153
154 GrScratchKey& operator=(const GrScratchKey& that) {
155 this->INHERITED::operator=(that);
156 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700157 }
bsalomon24db3b12015-01-23 04:24:04 -0800158
159 bool operator==(const GrScratchKey& that) const {
160 return this->INHERITED::operator==(that);
161 }
162 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
163
164 class Builder : public INHERITED::Builder {
165 public:
166 Builder(GrScratchKey* key, ResourceType type, int data32Count)
167 : INHERITED::Builder(key, type, data32Count) {}
168 };
169};
170
171/**
172 * A key used to cache resources based on their content. The key consists of a domain type (use
173 * case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to
174 * initialize the key contents. The contents must be initialized before the key can be used.
175 */
176class GrContentKey : public GrResourceKey {
177private:
178 typedef GrResourceKey INHERITED;
179
180public:
181 typedef uint32_t Domain;
182 /** Generate a unique Domain of content keys. */
183 static Domain GenerateDomain();
184
185 /** Creates an invalid content key. It must be initialized using a Builder object before use. */
186 GrContentKey() {}
187
188 GrContentKey(const GrContentKey& that) { *this = that; }
189
190 /** reset() returns the key to the invalid state. */
191 using INHERITED::reset;
192
193 using INHERITED::isValid;
194
195 GrContentKey& operator=(const GrContentKey& that) {
196 this->INHERITED::operator=(that);
197 return *this;
198 }
199
200 bool operator==(const GrContentKey& that) const {
201 return this->INHERITED::operator==(that);
202 }
203 bool operator!=(const GrContentKey& that) const { return !(*this == that); }
204
205 class Builder : public INHERITED::Builder {
206 public:
207 Builder(GrContentKey* key, Domain domain, int data32Count)
208 : INHERITED::Builder(key, domain, data32Count) {}
209
210 /** Used to build a key that wraps another key and adds additional data. */
211 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain,
212 int extraData32Cnt)
213 : INHERITED::Builder(key, domain, (SkToInt(innerKey.size()) >> 2) + extraData32Cnt) {
214 int innerKeyCnt = SkToInt(innerKey.size()) >> 2;
215 // add the inner key to the end of the key so that op[] can be indexed normally.
216 for (int i = 0; i < innerKeyCnt; ++i) {
217 this->operator[](extraData32Cnt + i) = innerKey.data()[i];
218 }
219 }
220 };
bsalomon744998e2014-08-28 09:54:34 -0700221};
222
223#endif