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