blob: fb1b8920eba18508c71b10ce55a47b41a94c4cbb [file] [log] [blame]
bsalomon744998e2014-08-28 09:54:34 -07001/*
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 Salomon1090da62017-01-06 12:04:19 -050011#include "../private/SkOnce.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "../private/SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040013#include "../private/SkTo.h"
bsalomon744998e2014-08-28 09:54:34 -070014#include "GrTypes.h"
senorblanco84cd6212015-08-04 10:01:58 -070015#include "SkData.h"
Brian Salomon1090da62017-01-06 12:04:19 -050016#include "SkString.h"
bsalomon744998e2014-08-28 09:54:34 -070017
Hal Canaryc2d0fb12018-09-19 10:28:59 -040018#include <new>
19
bsalomon24db3b12015-01-23 04:24:04 -080020uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
bsalomon7775c852014-12-30 12:50:52 -080021
bsalomon8718aaf2015-02-19 07:24:21 -080022/**
23 * Base class for all GrGpuResource cache keys. There are two types of cache keys. Refer to the
24 * comments for each key type below.
25 */
bsalomon24db3b12015-01-23 04:24:04 -080026class GrResourceKey {
27public:
28 uint32_t hash() const {
29 this->validate();
30 return fKey[kHash_MetaDataIdx];
31 }
32
33 size_t size() const {
34 this->validate();
bsalomon3bd12ef2015-01-28 11:39:48 -080035 SkASSERT(this->isValid());
bsalomon24db3b12015-01-23 04:24:04 -080036 return this->internalSize();
37 }
38
bsalomon24db3b12015-01-23 04:24:04 -080039protected:
40 static const uint32_t kInvalidDomain = 0;
41
42 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080043
44 /** Reset to an invalid key. */
45 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080046 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080047 fKey.reset(kMetaDataCnt);
48 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080049 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080050 }
51
bsalomon24db3b12015-01-23 04:24:04 -080052 bool operator==(const GrResourceKey& that) const {
kkinnunen54b85112015-05-18 22:47:33 -070053 return this->hash() == that.hash() &&
54 0 == memcmp(&fKey[kHash_MetaDataIdx + 1],
55 &that.fKey[kHash_MetaDataIdx + 1],
56 this->internalSize() - sizeof(uint32_t));
bsalomon24db3b12015-01-23 04:24:04 -080057 }
bsalomon7775c852014-12-30 12:50:52 -080058
bsalomon24db3b12015-01-23 04:24:04 -080059 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon23e619c2015-02-06 11:54:28 -080060 SkASSERT(that.isValid());
bsalomon4dffede2015-01-23 07:17:55 -080061 if (this != &that) {
62 size_t bytes = that.size();
63 SkASSERT(SkIsAlign4(bytes));
64 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
65 memcpy(fKey.get(), that.fKey.get(), bytes);
bsalomon23e619c2015-02-06 11:54:28 -080066 this->validate();
bsalomon4dffede2015-01-23 07:17:55 -080067 }
bsalomon7775c852014-12-30 12:50:52 -080068 return *this;
69 }
70
bsalomon24db3b12015-01-23 04:24:04 -080071 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080072
bsalomon24db3b12015-01-23 04:24:04 -080073 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
74
bsalomon3bd12ef2015-01-28 11:39:48 -080075 /** size of the key data, excluding meta-data (hash, domain, etc). */
76 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
mtkleind9dd4282016-04-18 08:09:11 -070077
bsalomon3bd12ef2015-01-28 11:39:48 -080078 /** ptr to the key data, excluding meta-data (hash, domain, etc). */
79 const uint32_t* data() const {
80 this->validate();
81 return &fKey[kMetaDataCnt];
82 }
83
Robert Phillips0790f8a2018-09-18 13:11:03 -040084#ifdef SK_DEBUG
85 void dump() const {
86 if (!this->isValid()) {
87 SkDebugf("Invalid Key\n");
88 } else {
89 SkDebugf("hash: %d ", this->hash());
90 SkDebugf("domain: %d ", this->domain());
91 SkDebugf("size: %dB ", this->internalSize());
92 for (size_t i = 0; i < this->internalSize(); ++i) {
93 SkDebugf("%d ", fKey[i]);
94 }
95 SkDebugf("\n");
96 }
97 }
98#endif
99
bsalomon24db3b12015-01-23 04:24:04 -0800100 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -0800101 class Builder {
102 public:
bsalomon24db3b12015-01-23 04:24:04 -0800103 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -0800104 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -0800105 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -0800106 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -0800107 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -0800108 SkASSERT(SkToU16(size) == size);
109 SkASSERT(SkToU16(domain) == domain);
110 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -0800111 }
112
113 ~Builder() { this->finish(); }
114
bsalomon24db3b12015-01-23 04:24:04 -0800115 void finish() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400116 if (nullptr == fKey) {
bsalomon24db3b12015-01-23 04:24:04 -0800117 return;
118 }
119 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
120 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
121 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
122 fKey->validate();
Ben Wagnera93a14a2017-08-28 10:34:05 -0400123 fKey = nullptr;
bsalomon24db3b12015-01-23 04:24:04 -0800124 }
bsalomon7775c852014-12-30 12:50:52 -0800125
126 uint32_t& operator[](int dataIdx) {
127 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -0800128 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -0800129 SkASSERT(SkToU32(dataIdx) < dataCount);
130 return fKey->fKey[kMetaDataCnt + dataIdx];
131 }
132
133 private:
bsalomon24db3b12015-01-23 04:24:04 -0800134 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800135 };
136
137private:
138 enum MetaDataIdx {
139 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800140 // The key domain and size are packed into a single uint32_t.
141 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800142
bsalomon24db3b12015-01-23 04:24:04 -0800143 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800144 };
bsalomon7775c852014-12-30 12:50:52 -0800145 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
146
bsalomon3bd12ef2015-01-28 11:39:48 -0800147 size_t internalSize() const {
148 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
149 }
150
151 void validate() const {
152 SkASSERT(fKey[kHash_MetaDataIdx] ==
153 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
154 this->internalSize() - sizeof(uint32_t)));
155 SkASSERT(SkIsAlign4(this->internalSize()));
156 }
157
bsalomon1c60dfe2015-01-21 09:32:40 -0800158 friend class TestResource; // For unit test to access kMetaDataCnt.
159
Brian Osman086679b2018-09-10 16:26:51 -0400160 // bmp textures require 5 uint32_t values.
161 SkAutoSTMalloc<kMetaDataCnt + 5, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800162};
163
bsalomon24db3b12015-01-23 04:24:04 -0800164/**
bsalomon8718aaf2015-02-19 07:24:21 -0800165 * A key used for scratch resources. There are three important rules about scratch keys:
166 * * Multiple resources can share the same scratch key. Therefore resources assigned the same
167 * scratch key should be interchangeable with respect to the code that uses them.
168 * * A resource can have at most one scratch key and it is set at resource creation by the
169 * resource itself.
170 * * When a scratch resource is ref'ed it will not be returned from the
171 * cache for a subsequent cache request until all refs are released. This facilitates using
172 * a scratch key for multiple render-to-texture scenarios. An example is a separable blur:
173 *
174 * GrTexture* texture[2];
175 * texture[0] = get_scratch_texture(scratchKey);
176 * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a
177 * // different one for texture[1]
178 * draw_mask(texture[0], path); // draws path mask to texture[0]
179 * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1]
180 * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0]
181 * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey
182 * consume_blur(texture[0]);
183 * texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey
bsalomon24db3b12015-01-23 04:24:04 -0800184 */
185class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700186private:
bsalomon24db3b12015-01-23 04:24:04 -0800187 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700188
bsalomon24db3b12015-01-23 04:24:04 -0800189public:
190 /** Uniquely identifies the type of resource that is cached as scratch. */
191 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700192
bsalomon24db3b12015-01-23 04:24:04 -0800193 /** Generate a unique ResourceType. */
194 static ResourceType GenerateResourceType();
195
196 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
197 GrScratchKey() {}
198
199 GrScratchKey(const GrScratchKey& that) { *this = that; }
200
201 /** reset() returns the key to the invalid state. */
202 using INHERITED::reset;
203
204 using INHERITED::isValid;
205
206 ResourceType resourceType() const { return this->domain(); }
207
208 GrScratchKey& operator=(const GrScratchKey& that) {
209 this->INHERITED::operator=(that);
210 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700211 }
bsalomon24db3b12015-01-23 04:24:04 -0800212
213 bool operator==(const GrScratchKey& that) const {
214 return this->INHERITED::operator==(that);
215 }
216 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
217
218 class Builder : public INHERITED::Builder {
219 public:
220 Builder(GrScratchKey* key, ResourceType type, int data32Count)
221 : INHERITED::Builder(key, type, data32Count) {}
222 };
223};
224
225/**
bsalomon8718aaf2015-02-19 07:24:21 -0800226 * A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three
227 * rules governing the use of unique keys:
228 * * Only one resource can have a given unique key at a time. Hence, "unique".
229 * * A resource can have at most one unique key at a time.
230 * * Unlike scratch keys, multiple requests for a unique key will return the same
231 * resource even if the resource already has refs.
232 * This key type allows a code path to create cached resources for which it is the exclusive user.
233 * The code path creates a domain which it sets on its keys. This guarantees that there are no
234 * cross-domain collisions.
235 *
236 * Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its
237 * scratch key. It can become scratch again if the unique key is removed.
bsalomon24db3b12015-01-23 04:24:04 -0800238 */
bsalomon8718aaf2015-02-19 07:24:21 -0800239class GrUniqueKey : public GrResourceKey {
bsalomon24db3b12015-01-23 04:24:04 -0800240private:
241 typedef GrResourceKey INHERITED;
242
243public:
244 typedef uint32_t Domain;
bsalomon8718aaf2015-02-19 07:24:21 -0800245 /** Generate a Domain for unique keys. */
bsalomon24db3b12015-01-23 04:24:04 -0800246 static Domain GenerateDomain();
247
bsalomon8718aaf2015-02-19 07:24:21 -0800248 /** Creates an invalid unique key. It must be initialized using a Builder object before use. */
Robert Phillips0790f8a2018-09-18 13:11:03 -0400249 GrUniqueKey() : fTag(nullptr) {}
bsalomon24db3b12015-01-23 04:24:04 -0800250
bsalomon8718aaf2015-02-19 07:24:21 -0800251 GrUniqueKey(const GrUniqueKey& that) { *this = that; }
bsalomon24db3b12015-01-23 04:24:04 -0800252
253 /** reset() returns the key to the invalid state. */
254 using INHERITED::reset;
255
256 using INHERITED::isValid;
257
bsalomon8718aaf2015-02-19 07:24:21 -0800258 GrUniqueKey& operator=(const GrUniqueKey& that) {
bsalomon24db3b12015-01-23 04:24:04 -0800259 this->INHERITED::operator=(that);
bungeman38d909e2016-08-02 14:40:46 -0700260 this->setCustomData(sk_ref_sp(that.getCustomData()));
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400261 fTag = that.fTag;
bsalomon24db3b12015-01-23 04:24:04 -0800262 return *this;
263 }
264
bsalomon8718aaf2015-02-19 07:24:21 -0800265 bool operator==(const GrUniqueKey& that) const {
bsalomon24db3b12015-01-23 04:24:04 -0800266 return this->INHERITED::operator==(that);
267 }
bsalomon8718aaf2015-02-19 07:24:21 -0800268 bool operator!=(const GrUniqueKey& that) const { return !(*this == that); }
bsalomon24db3b12015-01-23 04:24:04 -0800269
bungeman38d909e2016-08-02 14:40:46 -0700270 void setCustomData(sk_sp<SkData> data) {
271 fData = std::move(data);
senorblanco84cd6212015-08-04 10:01:58 -0700272 }
bungeman38d909e2016-08-02 14:40:46 -0700273 SkData* getCustomData() const {
senorblanco84cd6212015-08-04 10:01:58 -0700274 return fData.get();
275 }
276
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400277 const char* tag() const { return fTag; }
Brian Salomon1090da62017-01-06 12:04:19 -0500278
Robert Phillips0790f8a2018-09-18 13:11:03 -0400279#ifdef SK_DEBUG
280 void dump(const char* label) const {
281 SkDebugf("%s tag: %s\n", label, fTag ? fTag : "None");
282 this->INHERITED::dump();
283 }
284#endif
285
bsalomon24db3b12015-01-23 04:24:04 -0800286 class Builder : public INHERITED::Builder {
287 public:
Brian Salomon1090da62017-01-06 12:04:19 -0500288 Builder(GrUniqueKey* key, Domain type, int data32Count, const char* tag = nullptr)
289 : INHERITED::Builder(key, type, data32Count) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400290 key->fTag = tag;
Brian Salomon1090da62017-01-06 12:04:19 -0500291 }
bsalomon24db3b12015-01-23 04:24:04 -0800292
293 /** Used to build a key that wraps another key and adds additional data. */
Brian Salomon1090da62017-01-06 12:04:19 -0500294 Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain, int extraData32Cnt,
295 const char* tag = nullptr)
296 : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
bsalomon37f9a262015-02-02 13:00:10 -0800297 SkASSERT(&innerKey != key);
bsalomon24db3b12015-01-23 04:24:04 -0800298 // add the inner key to the end of the key so that op[] can be indexed normally.
bsalomon3bd12ef2015-01-28 11:39:48 -0800299 uint32_t* innerKeyData = &this->operator[](extraData32Cnt);
300 const uint32_t* srcData = innerKey.data();
301 (*innerKeyData++) = innerKey.domain();
302 memcpy(innerKeyData, srcData, innerKey.dataSize());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400303 key->fTag = tag;
bsalomon3bd12ef2015-01-28 11:39:48 -0800304 }
305
306 private:
bsalomon8718aaf2015-02-19 07:24:21 -0800307 static int Data32CntForInnerKey(const GrUniqueKey& innerKey) {
bsalomon3bd12ef2015-01-28 11:39:48 -0800308 // key data + domain
309 return SkToInt((innerKey.dataSize() >> 2) + 1);
bsalomon24db3b12015-01-23 04:24:04 -0800310 }
311 };
senorblanco84cd6212015-08-04 10:01:58 -0700312
313private:
bungeman38d909e2016-08-02 14:40:46 -0700314 sk_sp<SkData> fData;
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400315 const char* fTag;
bsalomon744998e2014-08-28 09:54:34 -0700316};
317
bsalomoned0bcad2015-05-04 10:36:42 -0700318/**
319 * It is common to need a frequently reused GrUniqueKey where the only requirement is that the key
320 * is unique. These macros create such a key in a thread safe manner so the key can be truly global
321 * and only constructed once.
322 */
323
324/** Place outside of function/class definitions. */
mtkleind9dd4282016-04-18 08:09:11 -0700325#define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once
bsalomoned0bcad2015-05-04 10:36:42 -0700326
327/** Place inside function where the key is used. */
bsalomonf0795ab2015-12-17 08:15:47 -0800328#define GR_DEFINE_STATIC_UNIQUE_KEY(name) \
329 static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \
mtkleind9dd4282016-04-18 08:09:11 -0700330 name##_once(gr_init_static_unique_key_once, &name##_storage); \
bsalomonf0795ab2015-12-17 08:15:47 -0800331 static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get());
bsalomoned0bcad2015-05-04 10:36:42 -0700332
bsalomonf0795ab2015-12-17 08:15:47 -0800333static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1,GrUniqueKey>* keyStorage) {
334 GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey;
bsalomoned0bcad2015-05-04 10:36:42 -0700335 GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0);
336}
337
bsalomon23e619c2015-02-06 11:54:28 -0800338// The cache listens for these messages to purge junk resources proactively.
bsalomon8718aaf2015-02-19 07:24:21 -0800339class GrUniqueKeyInvalidatedMessage {
bsalomon23e619c2015-02-06 11:54:28 -0800340public:
Brian Salomon238069b2018-07-11 15:58:57 -0400341 GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key, uint32_t contextUniqueID)
342 : fKey(key), fContextID(contextUniqueID) {
343 SkASSERT(SK_InvalidUniqueID != contextUniqueID);
bsalomon23e619c2015-02-06 11:54:28 -0800344 }
bsalomon8718aaf2015-02-19 07:24:21 -0800345
Brian Salomon238069b2018-07-11 15:58:57 -0400346 GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage&) = default;
347
348 GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage&) = default;
349
bsalomon8718aaf2015-02-19 07:24:21 -0800350 const GrUniqueKey& key() const { return fKey; }
Chris Dalton9a986cf2018-10-18 15:27:59 -0600351 uint32_t contextID() const { return fContextID; }
Brian Salomon238069b2018-07-11 15:58:57 -0400352
bsalomon23e619c2015-02-06 11:54:28 -0800353private:
bsalomon8718aaf2015-02-19 07:24:21 -0800354 GrUniqueKey fKey;
Brian Salomon238069b2018-07-11 15:58:57 -0400355 uint32_t fContextID;
bsalomon23e619c2015-02-06 11:54:28 -0800356};
Brian Salomon238069b2018-07-11 15:58:57 -0400357
Chris Dalton9a986cf2018-10-18 15:27:59 -0600358static inline bool SkShouldPostMessageToBus(
359 const GrUniqueKeyInvalidatedMessage& msg, uint32_t msgBusUniqueID) {
360 return msg.contextID() == msgBusUniqueID;
361}
362
bsalomon744998e2014-08-28 09:54:34 -0700363#endif