bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 1 | |
| 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" |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 13 | #include "SkTemplates.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 14 | #include "GrBinHashKey.h" |
| 15 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 16 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 17 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 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(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 39 | |
| 40 | /** Reset to an invalid key. */ |
| 41 | void reset() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 42 | GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 43 | fKey.reset(kMetaDataCnt); |
| 44 | fKey[kHash_MetaDataIdx] = 0; |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 45 | fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 46 | } |
| 47 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 48 | bool operator==(const GrResourceKey& that) const { |
| 49 | return 0 == memcmp(fKey.get(), that.fKey.get(), this->size()); |
| 50 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 51 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 52 | GrResourceKey& operator=(const GrResourceKey& that) { |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 53 | size_t bytes = that.size(); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 54 | SkASSERT(SkIsAlign4(bytes)); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 55 | fKey.reset(SkToInt(bytes / sizeof(uint32_t))); |
| 56 | memcpy(fKey.get(), that.fKey.get(), bytes); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 57 | return *this; |
| 58 | } |
| 59 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 60 | bool isValid() const { return kInvalidDomain != this->domain(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 61 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 62 | uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; } |
| 63 | |
| 64 | /** Used to initialize a key. */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 65 | class Builder { |
| 66 | public: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 67 | Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 68 | SkASSERT(data32Count >= 0); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 69 | SkASSERT(domain != kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 70 | key->fKey.reset(kMetaDataCnt + data32Count); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 71 | int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 72 | SkASSERT(SkToU16(size) == size); |
| 73 | SkASSERT(SkToU16(domain) == domain); |
| 74 | key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ~Builder() { this->finish(); } |
| 78 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 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(uint32_t)); |
| 86 | fKey->validate(); |
| 87 | fKey = NULL; |
| 88 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 89 | |
| 90 | uint32_t& operator[](int dataIdx) { |
| 91 | SkASSERT(fKey); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 92 | SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;) |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 93 | SkASSERT(SkToU32(dataIdx) < dataCount); |
| 94 | return fKey->fKey[kMetaDataCnt + dataIdx]; |
| 95 | } |
| 96 | |
| 97 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 98 | GrResourceKey* fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 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 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 112 | enum MetaDataIdx { |
| 113 | kHash_MetaDataIdx, |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 114 | // The key domain and size are packed into a single uint32_t. |
| 115 | kDomainAndSize_MetaDataIdx, |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 116 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 117 | kLastMetaDataIdx = kDomainAndSize_MetaDataIdx |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 118 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 119 | static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 120 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 121 | friend class TestResource; // For unit test to access kMetaDataCnt. |
| 122 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 123 | // bmp textures require 4 uint32_t values. |
| 124 | SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 127 | /** |
| 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 | */ |
| 132 | class GrScratchKey : public GrResourceKey { |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 133 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 134 | typedef GrResourceKey INHERITED; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 135 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 136 | public: |
| 137 | /** Uniquely identifies the type of resource that is cached as scratch. */ |
| 138 | typedef uint32_t ResourceType; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 139 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 140 | /** 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; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 158 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame^] | 159 | |
| 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 | */ |
| 177 | class GrContentKey : public GrResourceKey { |
| 178 | private: |
| 179 | typedef GrResourceKey INHERITED; |
| 180 | |
| 181 | public: |
| 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 | }; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | #endif |