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 | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 17 | /** |
| 18 | * Base class for all GrGpuResource cache keys. There are two types of cache keys. Refer to the |
| 19 | * comments for each key type below. |
| 20 | */ |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 21 | class GrResourceKey { |
| 22 | public: |
| 23 | uint32_t hash() const { |
| 24 | this->validate(); |
| 25 | return fKey[kHash_MetaDataIdx]; |
| 26 | } |
| 27 | |
| 28 | size_t size() const { |
| 29 | this->validate(); |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 30 | SkASSERT(this->isValid()); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 31 | return this->internalSize(); |
| 32 | } |
| 33 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 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 | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 52 | SkASSERT(that.isValid()); |
bsalomon | 4dffede | 2015-01-23 07:17:55 -0800 | [diff] [blame] | 53 | if (this != &that) { |
| 54 | size_t bytes = that.size(); |
| 55 | SkASSERT(SkIsAlign4(bytes)); |
| 56 | fKey.reset(SkToInt(bytes / sizeof(uint32_t))); |
| 57 | memcpy(fKey.get(), that.fKey.get(), bytes); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 58 | this->validate(); |
bsalomon | 4dffede | 2015-01-23 07:17:55 -0800 | [diff] [blame] | 59 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 60 | return *this; |
| 61 | } |
| 62 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 63 | bool isValid() const { return kInvalidDomain != this->domain(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 64 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 65 | uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; } |
| 66 | |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 67 | /** size of the key data, excluding meta-data (hash, domain, etc). */ |
| 68 | size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; } |
| 69 | |
| 70 | /** ptr to the key data, excluding meta-data (hash, domain, etc). */ |
| 71 | const uint32_t* data() const { |
| 72 | this->validate(); |
| 73 | return &fKey[kMetaDataCnt]; |
| 74 | } |
| 75 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 76 | /** Used to initialize a key. */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 77 | class Builder { |
| 78 | public: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 79 | Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 80 | SkASSERT(data32Count >= 0); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 81 | SkASSERT(domain != kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 82 | key->fKey.reset(kMetaDataCnt + data32Count); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 83 | int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 84 | SkASSERT(SkToU16(size) == size); |
| 85 | SkASSERT(SkToU16(domain) == domain); |
| 86 | key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | ~Builder() { this->finish(); } |
| 90 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 91 | void finish() { |
| 92 | if (NULL == fKey) { |
| 93 | return; |
| 94 | } |
| 95 | GR_STATIC_ASSERT(0 == kHash_MetaDataIdx); |
| 96 | uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx]; |
| 97 | *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t)); |
| 98 | fKey->validate(); |
| 99 | fKey = NULL; |
| 100 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 101 | |
| 102 | uint32_t& operator[](int dataIdx) { |
| 103 | SkASSERT(fKey); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 104 | SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;) |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 105 | SkASSERT(SkToU32(dataIdx) < dataCount); |
| 106 | return fKey->fKey[kMetaDataCnt + dataIdx]; |
| 107 | } |
| 108 | |
| 109 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 110 | GrResourceKey* fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | private: |
| 114 | enum MetaDataIdx { |
| 115 | kHash_MetaDataIdx, |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 116 | // The key domain and size are packed into a single uint32_t. |
| 117 | kDomainAndSize_MetaDataIdx, |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 118 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 119 | kLastMetaDataIdx = kDomainAndSize_MetaDataIdx |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 120 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 121 | static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 122 | |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 123 | size_t internalSize() const { |
| 124 | return fKey[kDomainAndSize_MetaDataIdx] >> 16; |
| 125 | } |
| 126 | |
| 127 | void validate() const { |
| 128 | SkASSERT(fKey[kHash_MetaDataIdx] == |
| 129 | GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1, |
| 130 | this->internalSize() - sizeof(uint32_t))); |
| 131 | SkASSERT(SkIsAlign4(this->internalSize())); |
| 132 | } |
| 133 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 134 | friend class TestResource; // For unit test to access kMetaDataCnt. |
| 135 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 136 | // bmp textures require 4 uint32_t values. |
bsalomon | 0aa9479 | 2015-02-18 11:33:04 -0800 | [diff] [blame] | 137 | SkAutoSTMalloc<kMetaDataCnt + 4, uint32_t> fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 140 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 141 | * A key used for scratch resources. There are three important rules about scratch keys: |
| 142 | * * Multiple resources can share the same scratch key. Therefore resources assigned the same |
| 143 | * scratch key should be interchangeable with respect to the code that uses them. |
| 144 | * * A resource can have at most one scratch key and it is set at resource creation by the |
| 145 | * resource itself. |
| 146 | * * When a scratch resource is ref'ed it will not be returned from the |
| 147 | * cache for a subsequent cache request until all refs are released. This facilitates using |
| 148 | * a scratch key for multiple render-to-texture scenarios. An example is a separable blur: |
| 149 | * |
| 150 | * GrTexture* texture[2]; |
| 151 | * texture[0] = get_scratch_texture(scratchKey); |
| 152 | * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a |
| 153 | * // different one for texture[1] |
| 154 | * draw_mask(texture[0], path); // draws path mask to texture[0] |
| 155 | * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1] |
| 156 | * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0] |
| 157 | * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey |
| 158 | * consume_blur(texture[0]); |
| 159 | * texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 160 | */ |
| 161 | class GrScratchKey : public GrResourceKey { |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 162 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 163 | typedef GrResourceKey INHERITED; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 164 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 165 | public: |
| 166 | /** Uniquely identifies the type of resource that is cached as scratch. */ |
| 167 | typedef uint32_t ResourceType; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 168 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 169 | /** Generate a unique ResourceType. */ |
| 170 | static ResourceType GenerateResourceType(); |
| 171 | |
| 172 | /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */ |
| 173 | GrScratchKey() {} |
| 174 | |
| 175 | GrScratchKey(const GrScratchKey& that) { *this = that; } |
| 176 | |
| 177 | /** reset() returns the key to the invalid state. */ |
| 178 | using INHERITED::reset; |
| 179 | |
| 180 | using INHERITED::isValid; |
| 181 | |
| 182 | ResourceType resourceType() const { return this->domain(); } |
| 183 | |
| 184 | GrScratchKey& operator=(const GrScratchKey& that) { |
| 185 | this->INHERITED::operator=(that); |
| 186 | return *this; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 187 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 188 | |
| 189 | bool operator==(const GrScratchKey& that) const { |
| 190 | return this->INHERITED::operator==(that); |
| 191 | } |
| 192 | bool operator!=(const GrScratchKey& that) const { return !(*this == that); } |
| 193 | |
| 194 | class Builder : public INHERITED::Builder { |
| 195 | public: |
| 196 | Builder(GrScratchKey* key, ResourceType type, int data32Count) |
| 197 | : INHERITED::Builder(key, type, data32Count) {} |
| 198 | }; |
| 199 | }; |
| 200 | |
| 201 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 202 | * A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three |
| 203 | * rules governing the use of unique keys: |
| 204 | * * Only one resource can have a given unique key at a time. Hence, "unique". |
| 205 | * * A resource can have at most one unique key at a time. |
| 206 | * * Unlike scratch keys, multiple requests for a unique key will return the same |
| 207 | * resource even if the resource already has refs. |
| 208 | * This key type allows a code path to create cached resources for which it is the exclusive user. |
| 209 | * The code path creates a domain which it sets on its keys. This guarantees that there are no |
| 210 | * cross-domain collisions. |
| 211 | * |
| 212 | * Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its |
| 213 | * scratch key. It can become scratch again if the unique key is removed. |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 214 | */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 215 | class GrUniqueKey : public GrResourceKey { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 216 | private: |
| 217 | typedef GrResourceKey INHERITED; |
| 218 | |
| 219 | public: |
| 220 | typedef uint32_t Domain; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 221 | /** Generate a Domain for unique keys. */ |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 222 | static Domain GenerateDomain(); |
| 223 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 224 | /** Creates an invalid unique key. It must be initialized using a Builder object before use. */ |
| 225 | GrUniqueKey() {} |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 226 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 227 | GrUniqueKey(const GrUniqueKey& that) { *this = that; } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 228 | |
| 229 | /** reset() returns the key to the invalid state. */ |
| 230 | using INHERITED::reset; |
| 231 | |
| 232 | using INHERITED::isValid; |
| 233 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 234 | GrUniqueKey& operator=(const GrUniqueKey& that) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 235 | this->INHERITED::operator=(that); |
| 236 | return *this; |
| 237 | } |
| 238 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 239 | bool operator==(const GrUniqueKey& that) const { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 240 | return this->INHERITED::operator==(that); |
| 241 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 242 | bool operator!=(const GrUniqueKey& that) const { return !(*this == that); } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 243 | |
| 244 | class Builder : public INHERITED::Builder { |
| 245 | public: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 246 | Builder(GrUniqueKey* key, Domain domain, int data32Count) |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 247 | : INHERITED::Builder(key, domain, data32Count) {} |
| 248 | |
| 249 | /** Used to build a key that wraps another key and adds additional data. */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 250 | Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain, |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 251 | int extraData32Cnt) |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 252 | : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 253 | SkASSERT(&innerKey != key); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 254 | // 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] | 255 | uint32_t* innerKeyData = &this->operator[](extraData32Cnt); |
| 256 | const uint32_t* srcData = innerKey.data(); |
| 257 | (*innerKeyData++) = innerKey.domain(); |
| 258 | memcpy(innerKeyData, srcData, innerKey.dataSize()); |
| 259 | } |
| 260 | |
| 261 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 262 | static int Data32CntForInnerKey(const GrUniqueKey& innerKey) { |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 263 | // key data + domain |
| 264 | return SkToInt((innerKey.dataSize() >> 2) + 1); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 265 | } |
| 266 | }; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 267 | }; |
| 268 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 269 | // The cache listens for these messages to purge junk resources proactively. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 270 | class GrUniqueKeyInvalidatedMessage { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 271 | public: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 272 | explicit GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key) : fKey(key) {} |
| 273 | |
| 274 | GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage& that) : fKey(that.fKey) {} |
| 275 | |
| 276 | GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage& that) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 277 | fKey = that.fKey; |
| 278 | return *this; |
| 279 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 280 | |
| 281 | const GrUniqueKey& key() const { return fKey; } |
| 282 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 283 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 284 | GrUniqueKey fKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 285 | }; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 286 | #endif |