Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1043)

Side by Side Diff: include/gpu/GrResourceKey.h

Issue 858123002: Add specialized content key class for resources. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: remove default template arg Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/gpu/GrGpuResource.h ('k') | include/gpu/GrTexture.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "GrBinHashKey.h" 14 #include "GrBinHashKey.h"
15 15
16 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
17
18 class GrResourceKey {
19 public:
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
35 protected:
36 static const uint32_t kInvalidDomain = 0;
37
38 GrResourceKey() { this->reset(); }
39
40 /** Reset to an invalid key. */
41 void reset() {
42 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
43 fKey.reset(kMetaDataCnt);
44 fKey[kHash_MetaDataIdx] = 0;
45 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
46 }
47
48 bool operator==(const GrResourceKey& that) const {
49 return 0 == memcmp(fKey.get(), that.fKey.get(), this->size());
50 }
51
52 GrResourceKey& operator=(const GrResourceKey& 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 return *this;
58 }
59
60 bool isValid() const { return kInvalidDomain != this->domain(); }
61
62 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
63
64 /** Used to initialize a key. */
65 class Builder {
66 public:
67 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key ) {
68 SkASSERT(data32Count >= 0);
69 SkASSERT(domain != kInvalidDomain);
70 key->fKey.reset(kMetaDataCnt + data32Count);
71 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
72 SkASSERT(SkToU16(size) == size);
73 SkASSERT(SkToU16(domain) == domain);
74 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
75 }
76
77 ~Builder() { this->finish(); }
78
79 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(ui nt32_t));
86 fKey->validate();
87 fKey = NULL;
88 }
89
90 uint32_t& operator[](int dataIdx) {
91 SkASSERT(fKey);
92 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_ t) - kMetaDataCnt;)
93 SkASSERT(SkToU32(dataIdx) < dataCount);
94 return fKey->fKey[kMetaDataCnt + dataIdx];
95 }
96
97 private:
98 GrResourceKey* fKey;
99 };
100
101 private:
102 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
112 enum MetaDataIdx {
113 kHash_MetaDataIdx,
114 // The key domain and size are packed into a single uint32_t.
115 kDomainAndSize_MetaDataIdx,
116
117 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
118 };
119 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
120
121 friend class TestResource; // For unit test to access kMetaDataCnt.
122
123 // bmp textures require 4 uint32_t values.
124 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey;
125 };
126
16 /** 127 /**
17 * A key used for scratch resources. The key consists of a resource type (subcla ss) identifier, a 128 * A key used for scratch resources. The key consists of a resource type (subcla ss) identifier, a
18 * hash, a data length, and type-specific data. A Builder object is used to init ialize the 129 * hash, a data length, and type-specific data. A Builder object is used to init ialize the
19 * key contents. The contents must be initialized before the key can be used. 130 * key contents. The contents must be initialized before the key can be used.
20 */ 131 */
21 class GrScratchKey { 132 class GrScratchKey : public GrResourceKey {
133 private:
134 typedef GrResourceKey INHERITED;
135
22 public: 136 public:
23 /** Uniquely identifies the type of resource that is cached as scratch. */ 137 /** Uniquely identifies the type of resource that is cached as scratch. */
24 typedef uint32_t ResourceType; 138 typedef uint32_t ResourceType;
139
25 /** Generate a unique ResourceType. */ 140 /** Generate a unique ResourceType. */
26 static ResourceType GenerateResourceType(); 141 static ResourceType GenerateResourceType();
27 142
28 GrScratchKey() { this->reset(); } 143 /** Creates an invalid scratch key. It must be initialized using a Builder o bject before use. */
144 GrScratchKey() {}
145
29 GrScratchKey(const GrScratchKey& that) { *this = that; } 146 GrScratchKey(const GrScratchKey& that) { *this = that; }
30 147
31 /** Reset to an invalid key. */ 148 /** reset() returns the key to the invalid state. */
32 void reset() { 149 using INHERITED::reset;
33 fKey.reset(kMetaDataCnt); 150
34 fKey[kHash_MetaDataIdx] = 0; 151 using INHERITED::isValid;
35 fKey[kTypeAndSize_MetaDataIdx] = kInvalidResourceType; 152
36 } 153 ResourceType resourceType() const { return this->domain(); }
37
38 bool isValid() const { return kInvalidResourceType != this->resourceType(); }
39
40 ResourceType resourceType() const { return fKey[kTypeAndSize_MetaDataIdx] & 0xffff; }
41
42 uint32_t hash() const { return fKey[kHash_MetaDataIdx]; }
43
44 size_t size() const { return SkToInt(fKey[kTypeAndSize_MetaDataIdx] >> 16); }
45
46 const uint32_t* data() const { return &fKey[kMetaDataCnt]; }
47 154
48 GrScratchKey& operator=(const GrScratchKey& that) { 155 GrScratchKey& operator=(const GrScratchKey& that) {
49 size_t bytes = that.size(); 156 this->INHERITED::operator=(that);
50 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
51 memcpy(fKey.get(), that.fKey.get(), bytes);
52 return *this; 157 return *this;
53 } 158 }
54 159
55 bool operator==(const GrScratchKey& that) const { 160 bool operator==(const GrScratchKey& that) const {
56 return 0 == memcmp(fKey.get(), that.fKey.get(), this->size()); 161 return this->INHERITED::operator==(that);
57 } 162 }
58 bool operator!=(const GrScratchKey& that) const { return !(*this == that); } 163 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
59 164
60 /** Used to initialize scratch key. */ 165 class Builder : public INHERITED::Builder {
61 class Builder {
62 public: 166 public:
63 Builder(GrScratchKey* key, ResourceType type, int data32Count) : fKey(ke y) { 167 Builder(GrScratchKey* key, ResourceType type, int data32Count)
64 SkASSERT(data32Count >= 0); 168 : INHERITED::Builder(key, type, data32Count) {}
65 SkASSERT(type != kInvalidResourceType); 169 };
66 key->fKey.reset(kMetaDataCnt + data32Count); 170 };
67 SkASSERT(type <= SK_MaxU16); 171
68 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); 172 /**
69 SkASSERT(size <= SK_MaxU16); 173 * A key used to cache resources based on their content. The key consists of a d omain type (use
70 key->fKey[kTypeAndSize_MetaDataIdx] = type | (size << 16); 174 * case for the cache), a hash, a data length, and domain-specific data. A Build er object is used to
71 } 175 * initialize the key contents. The contents must be initialized before the key can be used.
72 176 */
73 ~Builder() { this->finish(); } 177 class GrContentKey : public GrResourceKey {
74
75 void finish();
76
77 uint32_t& operator[](int dataIdx) {
78 SkASSERT(fKey);
79 SkDEBUGCODE(size_t dataCount = fKey->size() / sizeof(uint32_t) - kMe taDataCnt;)
80 SkASSERT(SkToU32(dataIdx) < dataCount);
81 return fKey->fKey[kMetaDataCnt + dataIdx];
82 }
83
84 private:
85 GrScratchKey* fKey;
86 };
87
88 private: 178 private:
89 enum MetaDataIdx { 179 typedef GrResourceKey INHERITED;
90 kHash_MetaDataIdx, 180
91 // The resource type and size are packed into a single uint32_t. 181 public:
92 kTypeAndSize_MetaDataIdx, 182 typedef uint32_t Domain;
93 183 /** Generate a unique Domain of content keys. */
94 kLastMetaDataIdx = kTypeAndSize_MetaDataIdx 184 static Domain GenerateDomain();
95 }; 185
96 static const uint32_t kInvalidResourceType = 0; 186 /** Creates an invalid content key. It must be initialized using a Builder o bject before use. */
97 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; 187 GrContentKey() {}
98 188
99 friend class TestResource; // For unit test to access kMetaDataCnt. 189 GrContentKey(const GrContentKey& that) { *this = that; }
100 190
101 // Stencil and textures each require 2 uint32_t values. 191 /** reset() returns the key to the invalid state. */
102 SkAutoSTArray<kMetaDataCnt + 2, uint32_t> fKey; 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 index ed normally.
217 for (int i = 0; i < innerKeyCnt; ++i) {
218 this->operator[](extraData32Cnt + i) = innerKey.data()[i];
219 }
220 }
221 };
103 }; 222 };
104 223
105 class GrResourceKey {
106 public:
107 /** Flags set by the GrGpuResource subclass. */
108 typedef uint8_t ResourceFlags;
109
110 /** Creates a key for resource */
111 GrResourceKey(const GrCacheID& id, ResourceFlags flags) {
112 this->init(id.getDomain(), id.getKey(), flags);
113 };
114
115 GrResourceKey(const GrResourceKey& src) { fKey = src.fKey; }
116
117 GrResourceKey() { fKey.reset(); }
118
119 void reset(const GrCacheID& id, ResourceFlags flags) {
120 this->init(id.getDomain(), id.getKey(), flags);
121 }
122
123 uint32_t getHash() const { return fKey.getHash(); }
124
125 ResourceFlags getResourceFlags() const {
126 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() +
127 kResourceFlagsOffset);
128 }
129
130 bool operator==(const GrResourceKey& other) const { return fKey == other.fKe y; }
131
132 // A key indicating that the resource is not usable as a scratch resource.
133 static GrResourceKey& NullScratchKey();
134
135 private:
136 enum {
137 kCacheIDKeyOffset = 0,
138 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key),
139 kResourceFlagsOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain),
140 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags),
141 kKeySize = SkAlign4(kPadOffset),
142 kPadSize = kKeySize - kPadOffset
143 };
144
145 void init(const GrCacheID::Domain domain, const GrCacheID::Key& key, Resourc eFlags flags) {
146 union {
147 uint8_t fKey8[kKeySize];
148 uint32_t fKey32[kKeySize / 4];
149 } keyData;
150
151 uint8_t* k = keyData.fKey8;
152 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key));
153 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain));
154 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags));
155 memset(k + kPadOffset, 0, kPadSize);
156 fKey.setKeyData(keyData.fKey32);
157 }
158 GrBinHashKey<kKeySize> fKey;
159 };
160
161 #endif 224 #endif
OLDNEW
« no previous file with comments | « include/gpu/GrGpuResource.h ('k') | include/gpu/GrTexture.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698