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