Chromium Code Reviews| 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 #include "GrBinHashKey.h" | 14 #include "GrBinHashKey.h" |
| 15 | 15 |
| 16 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size); | |
| 17 | |
| 18 class GrResourceKey { | |
|
bsalomon
2015/01/21 19:54:58
The diff sucks... this is basically the old GrScra
| |
| 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 fKey.reset(SkToInt(bytes / sizeof(uint32_t))); | |
|
robertphillips
2015/01/22 16:49:12
// bytes is guaranteed to be a multiple of 4 ?
bsalomon
2015/01/22 19:23:17
Added an assert to that effect.
| |
| 55 memcpy(fKey.get(), that.fKey.get(), bytes); | |
| 56 return *this; | |
| 57 } | |
| 58 | |
| 59 bool isValid() const { return kInvalidDomain != this->domain(); } | |
| 60 | |
| 61 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; } | |
| 62 | |
| 63 /** Used to initialize a key. */ | |
| 64 class Builder { | |
| 65 public: | |
| 66 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key ) { | |
| 67 SkASSERT(data32Count >= 0); | |
| 68 SkASSERT(domain != kInvalidDomain); | |
| 69 key->fKey.reset(kMetaDataCnt + data32Count); | |
| 70 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); | |
| 71 SkASSERT(SkToU16(size) == size); | |
| 72 SkASSERT(SkToU16(domain) == domain); | |
| 73 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16); | |
| 74 } | |
| 75 | |
| 76 ~Builder() { this->finish(); } | |
| 77 | |
| 78 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(ui nt32_t)); | |
| 85 fKey->validate(); | |
| 86 fKey = NULL; | |
| 87 } | |
| 88 | |
| 89 uint32_t& operator[](int dataIdx) { | |
| 90 SkASSERT(fKey); | |
| 91 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_ t) - kMetaDataCnt;) | |
| 92 SkASSERT(SkToU32(dataIdx) < dataCount); | |
| 93 return fKey->fKey[kMetaDataCnt + dataIdx]; | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 GrResourceKey* fKey; | |
| 98 }; | |
| 99 | |
| 100 private: | |
| 101 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 | |
| 111 enum MetaDataIdx { | |
| 112 kHash_MetaDataIdx, | |
| 113 // The key domain and size are packed into a single uint32_t. | |
| 114 kDomainAndSize_MetaDataIdx, | |
| 115 | |
| 116 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx | |
| 117 }; | |
| 118 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; | |
| 119 | |
| 120 friend class TestResource; // For unit test to access kMetaDataCnt. | |
| 121 | |
| 122 // bmp textures require 4 uint32_t values. | |
| 123 SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey; | |
| 124 }; | |
| 125 | |
| 16 /** | 126 /** |
| 17 * A key used for scratch resources. The key consists of a resource type (subcla ss) identifier, a | 127 * 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 | 128 * 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. | 129 * key contents. The contents must be initialized before the key can be used. |
| 20 */ | 130 */ |
| 21 class GrScratchKey { | 131 class GrScratchKey : public GrResourceKey { |
|
robertphillips
2015/01/22 16:49:12
Shouldn't this be last ?
bsalomon
2015/01/22 19:23:17
it breaks using it in the builder to put it last
| |
| 132 private: | |
| 133 typedef GrResourceKey INHERITED; | |
| 134 | |
| 22 public: | 135 public: |
|
robertphillips
2015/01/22 16:49:12
Does this need to be public ?
bsalomon
2015/01/22 19:23:17
Removed it
| |
| 136 static const uint32_t kInvalidResourceType = INHERITED::kInvalidDomain; | |
| 137 | |
| 23 /** Uniquely identifies the type of resource that is cached as scratch. */ | 138 /** Uniquely identifies the type of resource that is cached as scratch. */ |
| 24 typedef uint32_t ResourceType; | 139 typedef uint32_t ResourceType; |
| 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); | |
| 34 fKey[kHash_MetaDataIdx] = 0; | |
| 35 fKey[kTypeAndSize_MetaDataIdx] = kInvalidResourceType; | |
| 36 } | |
| 37 | 150 |
| 38 bool isValid() const { return kInvalidResourceType != this->resourceType(); } | 151 bool isValid() const { return kInvalidResourceType != this->resourceType(); } |
| 39 | 152 |
| 40 ResourceType resourceType() const { return fKey[kTypeAndSize_MetaDataIdx] & 0xffff; } | 153 ResourceType resourceType() const { return this->domain(); } |
| 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(key, type, data32Count) {} |
| 65 SkASSERT(type != kInvalidResourceType); | |
| 66 key->fKey.reset(kMetaDataCnt + data32Count); | |
| 67 SkASSERT(type <= SK_MaxU16); | |
| 68 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); | |
| 69 SkASSERT(size <= SK_MaxU16); | |
| 70 key->fKey[kTypeAndSize_MetaDataIdx] = type | (size << 16); | |
| 71 } | |
| 72 | |
| 73 ~Builder() { this->finish(); } | |
| 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: | 169 private: |
| 85 GrScratchKey* fKey; | 170 typedef INHERITED::Builder INHERITED; |
| 86 }; | 171 }; |
| 87 | 172 }; |
| 173 | |
| 174 /** | |
| 175 * A key used to cache resources based on their content. The key consists of a d omain type (use | |
| 176 * case for the cache), a hash, a data length, and domain-specific data. A Build er object is used to | |
| 177 * initialize the key contents. The contents must be initialized before the key can be used. | |
| 178 */ | |
| 179 class GrContentKey : public GrResourceKey { | |
|
robertphillips
2015/01/22 16:49:12
Shouldn't this be at the end ?
| |
| 88 private: | 180 private: |
| 89 enum MetaDataIdx { | 181 typedef GrResourceKey INHERITED; |
| 90 kHash_MetaDataIdx, | 182 |
| 91 // The resource type and size are packed into a single uint32_t. | 183 public: |
|
robertphillips
2015/01/22 16:49:11
Can this be hidden ?
bsalomon
2015/01/22 19:23:17
Removed it.
| |
| 92 kTypeAndSize_MetaDataIdx, | 184 static const uint32_t kInvalidDomain = INHERITED::kInvalidDomain; |
| 93 | 185 |
| 94 kLastMetaDataIdx = kTypeAndSize_MetaDataIdx | 186 typedef uint32_t Domain; |
| 95 }; | 187 /** Generate a unique Domain of content keys. */ |
| 96 static const uint32_t kInvalidResourceType = 0; | 188 static Domain GenerateDomain(); |
| 97 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; | 189 |
| 98 | 190 /** Creates an invalid content key. It must be initialized using a Builder o bject before use. */ |
| 99 friend class TestResource; // For unit test to access kMetaDataCnt. | 191 GrContentKey() {} |
| 100 | 192 |
| 101 // Stencil and textures each require 2 uint32_t values. | 193 GrContentKey(const GrContentKey& that) { *this = that; } |
| 102 SkAutoSTArray<kMetaDataCnt + 2, uint32_t> fKey; | 194 |
| 195 /** reset() returns the key to the invalid state. */ | |
| 196 using INHERITED::reset; | |
| 197 | |
|
robertphillips
2015/01/22 16:49:12
return this->INHERITED::isValid(); ?
bsalomon
2015/01/22 19:23:17
Done.
| |
| 198 bool isValid() const { return kInvalidDomain != this->domain(); } | |
| 199 | |
| 200 GrContentKey& operator=(const GrContentKey& that) { | |
| 201 this->INHERITED::operator=(that); | |
| 202 return *this; | |
| 203 } | |
| 204 | |
| 205 bool operator==(const GrContentKey& that) const { | |
| 206 return this->INHERITED::operator==(that); | |
| 207 } | |
| 208 bool operator!=(const GrContentKey& that) const { return !(*this == that); } | |
| 209 | |
| 210 class Builder : public INHERITED::Builder { | |
| 211 public: | |
| 212 Builder(GrContentKey* key, Domain domain, int data32Count) | |
| 213 : INHERITED(key, domain, data32Count) {} | |
| 214 | |
| 215 /** Used to build a key that wraps another key and adds additional data. */ | |
| 216 Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain, | |
| 217 int extraData32Cnt) | |
| 218 : INHERITED(key, domain, (SkToInt(innerKey.size()) >> 2) + extraData 32Cnt) { | |
| 219 int innerKeyCnt = SkToInt(innerKey.size()) >> 2; | |
| 220 for (int i = 0; i < innerKeyCnt; ++i) { | |
|
robertphillips
2015/01/22 16:49:12
Maybe move this comment up ?
bsalomon
2015/01/22 19:23:17
Done.
| |
| 221 // add the inner key to the end of the key so that op[] can be i ndexed normally. | |
| 222 this->operator[](extraData32Cnt + i) = innerKey.data()[i]; | |
| 223 } | |
| 224 } | |
| 225 private: | |
| 226 typedef INHERITED::Builder INHERITED; | |
| 227 }; | |
| 103 }; | 228 }; |
| 104 | 229 |
| 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 | 230 #endif |
| OLD | NEW |