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 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 15 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 16 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 17 | class GrResourceKey { |
| 18 | public: |
| 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 | |
| 34 | protected: |
| 35 | static const uint32_t kInvalidDomain = 0; |
| 36 | |
| 37 | GrResourceKey() { this->reset(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 38 | |
| 39 | /** Reset to an invalid key. */ |
| 40 | void reset() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 41 | GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 42 | fKey.reset(kMetaDataCnt); |
| 43 | fKey[kHash_MetaDataIdx] = 0; |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 44 | fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 47 | bool operator==(const GrResourceKey& that) const { |
| 48 | return 0 == memcmp(fKey.get(), that.fKey.get(), this->size()); |
| 49 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 50 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 51 | GrResourceKey& operator=(const GrResourceKey& that) { |
bsalomon | 4dffede | 2015-01-23 07:17:55 -0800 | [diff] [blame] | 52 | if (this != &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 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 58 | return *this; |
| 59 | } |
| 60 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 61 | bool isValid() const { return kInvalidDomain != this->domain(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 62 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 63 | uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; } |
| 64 | |
| 65 | /** Used to initialize a key. */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 66 | class Builder { |
| 67 | public: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 68 | Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 69 | SkASSERT(data32Count >= 0); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 70 | SkASSERT(domain != kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 71 | key->fKey.reset(kMetaDataCnt + data32Count); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 72 | int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 73 | SkASSERT(SkToU16(size) == size); |
| 74 | SkASSERT(SkToU16(domain) == domain); |
| 75 | key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | ~Builder() { this->finish(); } |
| 79 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 80 | void finish() { |
| 81 | if (NULL == fKey) { |
| 82 | return; |
| 83 | } |
| 84 | GR_STATIC_ASSERT(0 == kHash_MetaDataIdx); |
| 85 | uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx]; |
| 86 | *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t)); |
| 87 | fKey->validate(); |
| 88 | fKey = NULL; |
| 89 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 90 | |
| 91 | uint32_t& operator[](int dataIdx) { |
| 92 | SkASSERT(fKey); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 93 | SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;) |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 94 | SkASSERT(SkToU32(dataIdx) < dataCount); |
| 95 | return fKey->fKey[kMetaDataCnt + dataIdx]; |
| 96 | } |
| 97 | |
| 98 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 99 | GrResourceKey* fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 103 | size_t internalSize() const { |
| 104 | return fKey[kDomainAndSize_MetaDataIdx] >> 16; |
| 105 | } |
| 106 | |
| 107 | void validate() const { |
| 108 | SkASSERT(fKey[kHash_MetaDataIdx] == |
| 109 | GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1, |
| 110 | this->internalSize() - sizeof(uint32_t))); |
| 111 | } |
| 112 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 113 | enum MetaDataIdx { |
| 114 | kHash_MetaDataIdx, |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 115 | // The key domain and size are packed into a single uint32_t. |
| 116 | kDomainAndSize_MetaDataIdx, |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 117 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 118 | kLastMetaDataIdx = kDomainAndSize_MetaDataIdx |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 119 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 120 | static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 121 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 122 | friend class TestResource; // For unit test to access kMetaDataCnt. |
| 123 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 124 | // bmp textures require 4 uint32_t values. |
| 125 | SkAutoSTArray<kMetaDataCnt + 4, uint32_t> fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 128 | /** |
| 129 | * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a |
| 130 | * hash, a data length, and type-specific data. A Builder object is used to initialize the |
| 131 | * key contents. The contents must be initialized before the key can be used. |
| 132 | */ |
| 133 | class GrScratchKey : public GrResourceKey { |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 134 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 135 | typedef GrResourceKey INHERITED; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 136 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 137 | public: |
| 138 | /** Uniquely identifies the type of resource that is cached as scratch. */ |
| 139 | typedef uint32_t ResourceType; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 140 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 141 | /** Generate a unique ResourceType. */ |
| 142 | static ResourceType GenerateResourceType(); |
| 143 | |
| 144 | /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */ |
| 145 | GrScratchKey() {} |
| 146 | |
| 147 | GrScratchKey(const GrScratchKey& that) { *this = that; } |
| 148 | |
| 149 | /** reset() returns the key to the invalid state. */ |
| 150 | using INHERITED::reset; |
| 151 | |
| 152 | using INHERITED::isValid; |
| 153 | |
| 154 | ResourceType resourceType() const { return this->domain(); } |
| 155 | |
| 156 | GrScratchKey& operator=(const GrScratchKey& that) { |
| 157 | this->INHERITED::operator=(that); |
| 158 | return *this; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 159 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 160 | |
| 161 | bool operator==(const GrScratchKey& that) const { |
| 162 | return this->INHERITED::operator==(that); |
| 163 | } |
| 164 | bool operator!=(const GrScratchKey& that) const { return !(*this == that); } |
| 165 | |
| 166 | class Builder : public INHERITED::Builder { |
| 167 | public: |
| 168 | Builder(GrScratchKey* key, ResourceType type, int data32Count) |
| 169 | : INHERITED::Builder(key, type, data32Count) {} |
| 170 | }; |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * A key used to cache resources based on their content. The key consists of a domain type (use |
| 175 | * case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to |
| 176 | * initialize the key contents. The contents must be initialized before the key can be used. |
| 177 | */ |
| 178 | class GrContentKey : public GrResourceKey { |
| 179 | private: |
| 180 | typedef GrResourceKey INHERITED; |
| 181 | |
| 182 | public: |
| 183 | typedef uint32_t Domain; |
| 184 | /** Generate a unique Domain of content keys. */ |
| 185 | static Domain GenerateDomain(); |
| 186 | |
| 187 | /** Creates an invalid content key. It must be initialized using a Builder object before use. */ |
| 188 | GrContentKey() {} |
| 189 | |
| 190 | GrContentKey(const GrContentKey& that) { *this = that; } |
| 191 | |
| 192 | /** reset() returns the key to the invalid state. */ |
| 193 | using INHERITED::reset; |
| 194 | |
| 195 | using INHERITED::isValid; |
| 196 | |
| 197 | GrContentKey& operator=(const GrContentKey& that) { |
| 198 | this->INHERITED::operator=(that); |
| 199 | return *this; |
| 200 | } |
| 201 | |
| 202 | bool operator==(const GrContentKey& that) const { |
| 203 | return this->INHERITED::operator==(that); |
| 204 | } |
| 205 | bool operator!=(const GrContentKey& that) const { return !(*this == that); } |
| 206 | |
| 207 | class Builder : public INHERITED::Builder { |
| 208 | public: |
| 209 | Builder(GrContentKey* key, Domain domain, int data32Count) |
| 210 | : INHERITED::Builder(key, domain, data32Count) {} |
| 211 | |
| 212 | /** Used to build a key that wraps another key and adds additional data. */ |
| 213 | Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain, |
| 214 | int extraData32Cnt) |
| 215 | : INHERITED::Builder(key, domain, (SkToInt(innerKey.size()) >> 2) + extraData32Cnt) { |
| 216 | int innerKeyCnt = SkToInt(innerKey.size()) >> 2; |
| 217 | // add the inner key to the end of the key so that op[] can be indexed normally. |
| 218 | for (int i = 0; i < innerKeyCnt; ++i) { |
| 219 | this->operator[](extraData32Cnt + i) = innerKey.data()[i]; |
| 220 | } |
| 221 | } |
| 222 | }; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | #endif |