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 | |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 12 | #include "../private/SkOnce.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 13 | #include "../private/SkTemplates.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 14 | #include "GrTypes.h" |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 15 | #include "SkData.h" |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 16 | #include "SkString.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 17 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 18 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 19 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 20 | /** |
| 21 | * Base class for all GrGpuResource cache keys. There are two types of cache keys. Refer to the |
| 22 | * comments for each key type below. |
| 23 | */ |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 24 | class GrResourceKey { |
| 25 | public: |
| 26 | uint32_t hash() const { |
| 27 | this->validate(); |
| 28 | return fKey[kHash_MetaDataIdx]; |
| 29 | } |
| 30 | |
| 31 | size_t size() const { |
| 32 | this->validate(); |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 33 | SkASSERT(this->isValid()); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 34 | return this->internalSize(); |
| 35 | } |
| 36 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 37 | protected: |
| 38 | static const uint32_t kInvalidDomain = 0; |
| 39 | |
| 40 | GrResourceKey() { this->reset(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 41 | |
| 42 | /** Reset to an invalid key. */ |
| 43 | void reset() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 44 | GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 45 | fKey.reset(kMetaDataCnt); |
| 46 | fKey[kHash_MetaDataIdx] = 0; |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 47 | fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 48 | } |
| 49 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 50 | bool operator==(const GrResourceKey& that) const { |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 51 | return this->hash() == that.hash() && |
| 52 | 0 == memcmp(&fKey[kHash_MetaDataIdx + 1], |
| 53 | &that.fKey[kHash_MetaDataIdx + 1], |
| 54 | this->internalSize() - sizeof(uint32_t)); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 55 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 56 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 57 | GrResourceKey& operator=(const GrResourceKey& that) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 58 | SkASSERT(that.isValid()); |
bsalomon | 4dffede | 2015-01-23 07:17:55 -0800 | [diff] [blame] | 59 | if (this != &that) { |
| 60 | size_t bytes = that.size(); |
| 61 | SkASSERT(SkIsAlign4(bytes)); |
| 62 | fKey.reset(SkToInt(bytes / sizeof(uint32_t))); |
| 63 | memcpy(fKey.get(), that.fKey.get(), bytes); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 64 | this->validate(); |
bsalomon | 4dffede | 2015-01-23 07:17:55 -0800 | [diff] [blame] | 65 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 66 | return *this; |
| 67 | } |
| 68 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 69 | bool isValid() const { return kInvalidDomain != this->domain(); } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 70 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 71 | uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; } |
| 72 | |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 73 | /** size of the key data, excluding meta-data (hash, domain, etc). */ |
| 74 | size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; } |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 75 | |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 76 | /** ptr to the key data, excluding meta-data (hash, domain, etc). */ |
| 77 | const uint32_t* data() const { |
| 78 | this->validate(); |
| 79 | return &fKey[kMetaDataCnt]; |
| 80 | } |
| 81 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 82 | /** Used to initialize a key. */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 83 | class Builder { |
| 84 | public: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 85 | Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 86 | SkASSERT(data32Count >= 0); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 87 | SkASSERT(domain != kInvalidDomain); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 88 | key->fKey.reset(kMetaDataCnt + data32Count); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 89 | int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 90 | SkASSERT(SkToU16(size) == size); |
| 91 | SkASSERT(SkToU16(domain) == domain); |
| 92 | key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ~Builder() { this->finish(); } |
| 96 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 97 | void finish() { |
| 98 | if (NULL == fKey) { |
| 99 | return; |
| 100 | } |
| 101 | GR_STATIC_ASSERT(0 == kHash_MetaDataIdx); |
| 102 | uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx]; |
| 103 | *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t)); |
| 104 | fKey->validate(); |
| 105 | fKey = NULL; |
| 106 | } |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 107 | |
| 108 | uint32_t& operator[](int dataIdx) { |
| 109 | SkASSERT(fKey); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 110 | SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;) |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 111 | SkASSERT(SkToU32(dataIdx) < dataCount); |
| 112 | return fKey->fKey[kMetaDataCnt + dataIdx]; |
| 113 | } |
| 114 | |
| 115 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 116 | GrResourceKey* fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | private: |
| 120 | enum MetaDataIdx { |
| 121 | kHash_MetaDataIdx, |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 122 | // The key domain and size are packed into a single uint32_t. |
| 123 | kDomainAndSize_MetaDataIdx, |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 124 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 125 | kLastMetaDataIdx = kDomainAndSize_MetaDataIdx |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 126 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 127 | static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 128 | |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 129 | size_t internalSize() const { |
| 130 | return fKey[kDomainAndSize_MetaDataIdx] >> 16; |
| 131 | } |
| 132 | |
| 133 | void validate() const { |
| 134 | SkASSERT(fKey[kHash_MetaDataIdx] == |
| 135 | GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1, |
| 136 | this->internalSize() - sizeof(uint32_t))); |
| 137 | SkASSERT(SkIsAlign4(this->internalSize())); |
| 138 | } |
| 139 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 140 | friend class TestResource; // For unit test to access kMetaDataCnt. |
| 141 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 142 | // bmp textures require 5 uint32_t values. |
| 143 | SkAutoSTMalloc<kMetaDataCnt + 5, uint32_t> fKey; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 146 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 147 | * A key used for scratch resources. There are three important rules about scratch keys: |
| 148 | * * Multiple resources can share the same scratch key. Therefore resources assigned the same |
| 149 | * scratch key should be interchangeable with respect to the code that uses them. |
| 150 | * * A resource can have at most one scratch key and it is set at resource creation by the |
| 151 | * resource itself. |
| 152 | * * When a scratch resource is ref'ed it will not be returned from the |
| 153 | * cache for a subsequent cache request until all refs are released. This facilitates using |
| 154 | * a scratch key for multiple render-to-texture scenarios. An example is a separable blur: |
| 155 | * |
| 156 | * GrTexture* texture[2]; |
| 157 | * texture[0] = get_scratch_texture(scratchKey); |
| 158 | * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a |
| 159 | * // different one for texture[1] |
| 160 | * draw_mask(texture[0], path); // draws path mask to texture[0] |
| 161 | * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1] |
| 162 | * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0] |
| 163 | * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey |
| 164 | * consume_blur(texture[0]); |
| 165 | * 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] | 166 | */ |
| 167 | class GrScratchKey : public GrResourceKey { |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 168 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 169 | typedef GrResourceKey INHERITED; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 170 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 171 | public: |
| 172 | /** Uniquely identifies the type of resource that is cached as scratch. */ |
| 173 | typedef uint32_t ResourceType; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 174 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 175 | /** Generate a unique ResourceType. */ |
| 176 | static ResourceType GenerateResourceType(); |
| 177 | |
| 178 | /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */ |
| 179 | GrScratchKey() {} |
| 180 | |
| 181 | GrScratchKey(const GrScratchKey& that) { *this = that; } |
| 182 | |
| 183 | /** reset() returns the key to the invalid state. */ |
| 184 | using INHERITED::reset; |
| 185 | |
| 186 | using INHERITED::isValid; |
| 187 | |
| 188 | ResourceType resourceType() const { return this->domain(); } |
| 189 | |
| 190 | GrScratchKey& operator=(const GrScratchKey& that) { |
| 191 | this->INHERITED::operator=(that); |
| 192 | return *this; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 193 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 194 | |
| 195 | bool operator==(const GrScratchKey& that) const { |
| 196 | return this->INHERITED::operator==(that); |
| 197 | } |
| 198 | bool operator!=(const GrScratchKey& that) const { return !(*this == that); } |
| 199 | |
| 200 | class Builder : public INHERITED::Builder { |
| 201 | public: |
| 202 | Builder(GrScratchKey* key, ResourceType type, int data32Count) |
| 203 | : INHERITED::Builder(key, type, data32Count) {} |
| 204 | }; |
| 205 | }; |
| 206 | |
| 207 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 208 | * A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three |
| 209 | * rules governing the use of unique keys: |
| 210 | * * Only one resource can have a given unique key at a time. Hence, "unique". |
| 211 | * * A resource can have at most one unique key at a time. |
| 212 | * * Unlike scratch keys, multiple requests for a unique key will return the same |
| 213 | * resource even if the resource already has refs. |
| 214 | * This key type allows a code path to create cached resources for which it is the exclusive user. |
| 215 | * The code path creates a domain which it sets on its keys. This guarantees that there are no |
| 216 | * cross-domain collisions. |
| 217 | * |
| 218 | * Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its |
| 219 | * scratch key. It can become scratch again if the unique key is removed. |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 220 | */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 221 | class GrUniqueKey : public GrResourceKey { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 222 | private: |
| 223 | typedef GrResourceKey INHERITED; |
| 224 | |
| 225 | public: |
| 226 | typedef uint32_t Domain; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 227 | /** Generate a Domain for unique keys. */ |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 228 | static Domain GenerateDomain(); |
| 229 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 230 | /** Creates an invalid unique key. It must be initialized using a Builder object before use. */ |
| 231 | GrUniqueKey() {} |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 232 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 233 | GrUniqueKey(const GrUniqueKey& that) { *this = that; } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 234 | |
| 235 | /** reset() returns the key to the invalid state. */ |
| 236 | using INHERITED::reset; |
| 237 | |
| 238 | using INHERITED::isValid; |
| 239 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 240 | GrUniqueKey& operator=(const GrUniqueKey& that) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 241 | this->INHERITED::operator=(that); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 242 | this->setCustomData(sk_ref_sp(that.getCustomData())); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 243 | SkDEBUGCODE(fTag = that.fTag;) |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 244 | return *this; |
| 245 | } |
| 246 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 247 | bool operator==(const GrUniqueKey& that) const { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 248 | return this->INHERITED::operator==(that); |
| 249 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 250 | bool operator!=(const GrUniqueKey& that) const { return !(*this == that); } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 251 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 252 | void setCustomData(sk_sp<SkData> data) { |
| 253 | fData = std::move(data); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 254 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 255 | SkData* getCustomData() const { |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 256 | return fData.get(); |
| 257 | } |
| 258 | |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 259 | SkDEBUGCODE(const char* tag() const { return fTag.c_str(); }) |
| 260 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 261 | class Builder : public INHERITED::Builder { |
| 262 | public: |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 263 | Builder(GrUniqueKey* key, Domain type, int data32Count, const char* tag = nullptr) |
| 264 | : INHERITED::Builder(key, type, data32Count) { |
| 265 | SkDEBUGCODE(key->fTag = tag;) |
| 266 | (void) tag; // suppress unused named param warning. |
| 267 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 268 | |
| 269 | /** Used to build a key that wraps another key and adds additional data. */ |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 270 | Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain, int extraData32Cnt, |
| 271 | const char* tag = nullptr) |
| 272 | : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 273 | SkASSERT(&innerKey != key); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 274 | // 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] | 275 | uint32_t* innerKeyData = &this->operator[](extraData32Cnt); |
| 276 | const uint32_t* srcData = innerKey.data(); |
| 277 | (*innerKeyData++) = innerKey.domain(); |
| 278 | memcpy(innerKeyData, srcData, innerKey.dataSize()); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 279 | SkDEBUGCODE(key->fTag = tag;) |
| 280 | (void) tag; // suppress unused named param warning. |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 284 | static int Data32CntForInnerKey(const GrUniqueKey& innerKey) { |
bsalomon | 3bd12ef | 2015-01-28 11:39:48 -0800 | [diff] [blame] | 285 | // key data + domain |
| 286 | return SkToInt((innerKey.dataSize() >> 2) + 1); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 287 | } |
| 288 | }; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 289 | |
| 290 | private: |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 291 | sk_sp<SkData> fData; |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame^] | 292 | SkDEBUGCODE(SkString fTag;) |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 295 | /** |
| 296 | * It is common to need a frequently reused GrUniqueKey where the only requirement is that the key |
| 297 | * is unique. These macros create such a key in a thread safe manner so the key can be truly global |
| 298 | * and only constructed once. |
| 299 | */ |
| 300 | |
| 301 | /** Place outside of function/class definitions. */ |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 302 | #define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 303 | |
| 304 | /** Place inside function where the key is used. */ |
bsalomon | f0795ab | 2015-12-17 08:15:47 -0800 | [diff] [blame] | 305 | #define GR_DEFINE_STATIC_UNIQUE_KEY(name) \ |
| 306 | static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \ |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 307 | name##_once(gr_init_static_unique_key_once, &name##_storage); \ |
bsalomon | f0795ab | 2015-12-17 08:15:47 -0800 | [diff] [blame] | 308 | static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get()); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 309 | |
bsalomon | f0795ab | 2015-12-17 08:15:47 -0800 | [diff] [blame] | 310 | static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1,GrUniqueKey>* keyStorage) { |
| 311 | GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 312 | GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0); |
| 313 | } |
| 314 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 315 | // The cache listens for these messages to purge junk resources proactively. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 316 | class GrUniqueKeyInvalidatedMessage { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 317 | public: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 318 | explicit GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key) : fKey(key) {} |
| 319 | |
| 320 | GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage& that) : fKey(that.fKey) {} |
| 321 | |
| 322 | GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage& that) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 323 | fKey = that.fKey; |
| 324 | return *this; |
| 325 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 326 | |
| 327 | const GrUniqueKey& key() const { return fKey; } |
| 328 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 329 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 330 | GrUniqueKey fKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 331 | }; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 332 | #endif |