OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #ifndef GrResourceKey_DEFINED | 9 #ifndef GrResourceKey_DEFINED |
10 #define GrResourceKey_DEFINED | 10 #define GrResourceKey_DEFINED |
11 | 11 |
12 #include "GrTypes.h" | 12 #include "GrTypes.h" |
13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
14 | 14 |
15 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size); | 15 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size); |
16 | 16 |
17 class GrResourceKey { | 17 class GrResourceKey { |
18 public: | 18 public: |
19 uint32_t hash() const { | 19 uint32_t hash() const { |
20 this->validate(); | 20 this->validate(); |
21 return fKey[kHash_MetaDataIdx]; | 21 return fKey[kHash_MetaDataIdx]; |
22 } | 22 } |
23 | 23 |
24 size_t size() const { | 24 size_t size() const { |
25 this->validate(); | 25 this->validate(); |
| 26 SkASSERT(this->isValid()); |
26 return this->internalSize(); | 27 return this->internalSize(); |
27 } | 28 } |
28 | 29 |
29 const uint32_t* data() const { | |
30 this->validate(); | |
31 return &fKey[kMetaDataCnt]; | |
32 } | |
33 | |
34 protected: | 30 protected: |
35 static const uint32_t kInvalidDomain = 0; | 31 static const uint32_t kInvalidDomain = 0; |
36 | 32 |
37 GrResourceKey() { this->reset(); } | 33 GrResourceKey() { this->reset(); } |
38 | 34 |
39 /** Reset to an invalid key. */ | 35 /** Reset to an invalid key. */ |
40 void reset() { | 36 void reset() { |
41 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain); | 37 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain); |
42 fKey.reset(kMetaDataCnt); | 38 fKey.reset(kMetaDataCnt); |
43 fKey[kHash_MetaDataIdx] = 0; | 39 fKey[kHash_MetaDataIdx] = 0; |
(...skipping 12 matching lines...) Expand all Loading... |
56 fKey.reset(SkToInt(bytes / sizeof(uint32_t))); | 52 fKey.reset(SkToInt(bytes / sizeof(uint32_t))); |
57 memcpy(fKey.get(), that.fKey.get(), bytes); | 53 memcpy(fKey.get(), that.fKey.get(), bytes); |
58 } | 54 } |
59 return *this; | 55 return *this; |
60 } | 56 } |
61 | 57 |
62 bool isValid() const { return kInvalidDomain != this->domain(); } | 58 bool isValid() const { return kInvalidDomain != this->domain(); } |
63 | 59 |
64 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff;
} | 60 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff;
} |
65 | 61 |
| 62 /** size of the key data, excluding meta-data (hash, domain, etc). */ |
| 63 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; } |
| 64 |
| 65 /** ptr to the key data, excluding meta-data (hash, domain, etc). */ |
| 66 const uint32_t* data() const { |
| 67 this->validate(); |
| 68 return &fKey[kMetaDataCnt]; |
| 69 } |
| 70 |
66 /** Used to initialize a key. */ | 71 /** Used to initialize a key. */ |
67 class Builder { | 72 class Builder { |
68 public: | 73 public: |
69 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key
) { | 74 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key
) { |
70 SkASSERT(data32Count >= 0); | 75 SkASSERT(data32Count >= 0); |
71 SkASSERT(domain != kInvalidDomain); | 76 SkASSERT(domain != kInvalidDomain); |
72 key->fKey.reset(kMetaDataCnt + data32Count); | 77 key->fKey.reset(kMetaDataCnt + data32Count); |
73 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); | 78 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); |
74 SkASSERT(SkToU16(size) == size); | 79 SkASSERT(SkToU16(size) == size); |
75 SkASSERT(SkToU16(domain) == domain); | 80 SkASSERT(SkToU16(domain) == domain); |
(...skipping 18 matching lines...) Expand all Loading... |
94 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_
t) - kMetaDataCnt;) | 99 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_
t) - kMetaDataCnt;) |
95 SkASSERT(SkToU32(dataIdx) < dataCount); | 100 SkASSERT(SkToU32(dataIdx) < dataCount); |
96 return fKey->fKey[kMetaDataCnt + dataIdx]; | 101 return fKey->fKey[kMetaDataCnt + dataIdx]; |
97 } | 102 } |
98 | 103 |
99 private: | 104 private: |
100 GrResourceKey* fKey; | 105 GrResourceKey* fKey; |
101 }; | 106 }; |
102 | 107 |
103 private: | 108 private: |
| 109 enum MetaDataIdx { |
| 110 kHash_MetaDataIdx, |
| 111 // The key domain and size are packed into a single uint32_t. |
| 112 kDomainAndSize_MetaDataIdx, |
| 113 |
| 114 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx |
| 115 }; |
| 116 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 117 |
104 size_t internalSize() const { | 118 size_t internalSize() const { |
105 return fKey[kDomainAndSize_MetaDataIdx] >> 16; | 119 return fKey[kDomainAndSize_MetaDataIdx] >> 16; |
106 } | 120 } |
107 | 121 |
108 void validate() const { | 122 void validate() const { |
109 SkASSERT(fKey[kHash_MetaDataIdx] == | 123 SkASSERT(fKey[kHash_MetaDataIdx] == |
110 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1, | 124 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1, |
111 this->internalSize() - sizeof(uint32_t))); | 125 this->internalSize() - sizeof(uint32_t))); |
| 126 SkASSERT(SkIsAlign4(this->internalSize())); |
112 } | 127 } |
113 | 128 |
114 enum MetaDataIdx { | |
115 kHash_MetaDataIdx, | |
116 // The key domain and size are packed into a single uint32_t. | |
117 kDomainAndSize_MetaDataIdx, | |
118 | |
119 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx | |
120 }; | |
121 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; | |
122 | |
123 friend class TestResource; // For unit test to access kMetaDataCnt. | 129 friend class TestResource; // For unit test to access kMetaDataCnt. |
124 | 130 |
125 // bmp textures require 4 uint32_t values. | 131 // bmp textures require 4 uint32_t values. |
126 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey; | 132 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey; |
127 }; | 133 }; |
128 | 134 |
129 /** | 135 /** |
130 * A key used for scratch resources. The key consists of a resource type (subcla
ss) identifier, a | 136 * A key used for scratch resources. The key consists of a resource type (subcla
ss) identifier, a |
131 * hash, a data length, and type-specific data. A Builder object is used to init
ialize the | 137 * hash, a data length, and type-specific data. A Builder object is used to init
ialize the |
132 * key contents. The contents must be initialized before the key can be used. | 138 * key contents. The contents must be initialized before the key can be used. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 bool operator!=(const GrContentKey& that) const { return !(*this == that); } | 212 bool operator!=(const GrContentKey& that) const { return !(*this == that); } |
207 | 213 |
208 class Builder : public INHERITED::Builder { | 214 class Builder : public INHERITED::Builder { |
209 public: | 215 public: |
210 Builder(GrContentKey* key, Domain domain, int data32Count) | 216 Builder(GrContentKey* key, Domain domain, int data32Count) |
211 : INHERITED::Builder(key, domain, data32Count) {} | 217 : INHERITED::Builder(key, domain, data32Count) {} |
212 | 218 |
213 /** Used to build a key that wraps another key and adds additional data.
*/ | 219 /** Used to build a key that wraps another key and adds additional data.
*/ |
214 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain, | 220 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain, |
215 int extraData32Cnt) | 221 int extraData32Cnt) |
216 : INHERITED::Builder(key, domain, (SkToInt(innerKey.size()) >> 2) +
extraData32Cnt) { | 222 : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + e
xtraData32Cnt) { |
217 int innerKeyCnt = SkToInt(innerKey.size()) >> 2; | |
218 // add the inner key to the end of the key so that op[] can be index
ed normally. | 223 // add the inner key to the end of the key so that op[] can be index
ed normally. |
219 for (int i = 0; i < innerKeyCnt; ++i) { | 224 uint32_t* innerKeyData = &this->operator[](extraData32Cnt); |
220 this->operator[](extraData32Cnt + i) = innerKey.data()[i]; | 225 const uint32_t* srcData = innerKey.data(); |
221 } | 226 (*innerKeyData++) = innerKey.domain(); |
| 227 memcpy(innerKeyData, srcData, innerKey.dataSize()); |
| 228 } |
| 229 |
| 230 private: |
| 231 static int Data32CntForInnerKey(const GrContentKey& innerKey) { |
| 232 // key data + domain |
| 233 return SkToInt((innerKey.dataSize() >> 2) + 1); |
222 } | 234 } |
223 }; | 235 }; |
224 }; | 236 }; |
225 | 237 |
226 #endif | 238 #endif |
OLD | NEW |