blob: d67fa7d226bfccf794f6a92b11e8cd49498c9840 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkData.h"
12#include "include/core/SkString.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkOnce.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.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 {
Brian Salomonb4ba8262018-12-20 20:37:55 -050053 return this->hash() == that.hash() && 0 == memcmp(&fKey[kHash_MetaDataIdx + 1],
54 &that.fKey[kHash_MetaDataIdx + 1],
55 this->internalSize() - sizeof(uint32_t));
bsalomon24db3b12015-01-23 04:24:04 -080056 }
bsalomon7775c852014-12-30 12:50:52 -080057
bsalomon24db3b12015-01-23 04:24:04 -080058 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon4dffede2015-01-23 07:17:55 -080059 if (this != &that) {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050060 if (!that.isValid()) {
61 this->reset();
62 } else {
63 size_t bytes = that.size();
64 SkASSERT(SkIsAlign4(bytes));
65 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
66 memcpy(fKey.get(), that.fKey.get(), bytes);
67 this->validate();
68 }
bsalomon4dffede2015-01-23 07:17:55 -080069 }
bsalomon7775c852014-12-30 12:50:52 -080070 return *this;
71 }
72
bsalomon24db3b12015-01-23 04:24:04 -080073 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080074
bsalomon24db3b12015-01-23 04:24:04 -080075 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
76
bsalomon3bd12ef2015-01-28 11:39:48 -080077 /** size of the key data, excluding meta-data (hash, domain, etc). */
78 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
mtkleind9dd4282016-04-18 08:09:11 -070079
bsalomon3bd12ef2015-01-28 11:39:48 -080080 /** ptr to the key data, excluding meta-data (hash, domain, etc). */
81 const uint32_t* data() const {
82 this->validate();
83 return &fKey[kMetaDataCnt];
84 }
85
Robert Phillips0790f8a2018-09-18 13:11:03 -040086#ifdef SK_DEBUG
87 void dump() const {
88 if (!this->isValid()) {
89 SkDebugf("Invalid Key\n");
90 } else {
91 SkDebugf("hash: %d ", this->hash());
92 SkDebugf("domain: %d ", this->domain());
93 SkDebugf("size: %dB ", this->internalSize());
94 for (size_t i = 0; i < this->internalSize(); ++i) {
Herb Derby880979f2018-12-17 17:37:28 -050095 SkDebugf("%d ", fKey[SkTo<int>(i)]);
Robert Phillips0790f8a2018-09-18 13:11:03 -040096 }
97 SkDebugf("\n");
98 }
99 }
100#endif
101
bsalomon24db3b12015-01-23 04:24:04 -0800102 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -0800103 class Builder {
104 public:
bsalomon24db3b12015-01-23 04:24:04 -0800105 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -0800106 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -0800107 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -0800108 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -0800109 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -0800110 SkASSERT(SkToU16(size) == size);
111 SkASSERT(SkToU16(domain) == domain);
112 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -0800113 }
114
115 ~Builder() { this->finish(); }
116
bsalomon24db3b12015-01-23 04:24:04 -0800117 void finish() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400118 if (nullptr == fKey) {
bsalomon24db3b12015-01-23 04:24:04 -0800119 return;
120 }
121 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
122 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
123 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
124 fKey->validate();
Ben Wagnera93a14a2017-08-28 10:34:05 -0400125 fKey = nullptr;
bsalomon24db3b12015-01-23 04:24:04 -0800126 }
bsalomon7775c852014-12-30 12:50:52 -0800127
128 uint32_t& operator[](int dataIdx) {
129 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -0800130 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -0800131 SkASSERT(SkToU32(dataIdx) < dataCount);
132 return fKey->fKey[kMetaDataCnt + dataIdx];
133 }
134
135 private:
bsalomon24db3b12015-01-23 04:24:04 -0800136 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800137 };
138
139private:
140 enum MetaDataIdx {
141 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800142 // The key domain and size are packed into a single uint32_t.
143 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800144
bsalomon24db3b12015-01-23 04:24:04 -0800145 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800146 };
bsalomon7775c852014-12-30 12:50:52 -0800147 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
148
Brian Salomonb4ba8262018-12-20 20:37:55 -0500149 size_t internalSize() const { return fKey[kDomainAndSize_MetaDataIdx] >> 16; }
bsalomon3bd12ef2015-01-28 11:39:48 -0800150
151 void validate() const {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500152 SkASSERT(this->isValid());
bsalomon3bd12ef2015-01-28 11:39:48 -0800153 SkASSERT(fKey[kHash_MetaDataIdx] ==
154 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
155 this->internalSize() - sizeof(uint32_t)));
156 SkASSERT(SkIsAlign4(this->internalSize()));
157 }
158
Brian Salomonb4ba8262018-12-20 20:37:55 -0500159 friend class TestResource; // For unit test to access kMetaDataCnt.
bsalomon1c60dfe2015-01-21 09:32:40 -0800160
Brian Osman086679b2018-09-10 16:26:51 -0400161 // bmp textures require 5 uint32_t values.
162 SkAutoSTMalloc<kMetaDataCnt + 5, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800163};
164
bsalomon24db3b12015-01-23 04:24:04 -0800165/**
bsalomon8718aaf2015-02-19 07:24:21 -0800166 * A key used for scratch resources. There are three important rules about scratch keys:
167 * * Multiple resources can share the same scratch key. Therefore resources assigned the same
168 * scratch key should be interchangeable with respect to the code that uses them.
169 * * A resource can have at most one scratch key and it is set at resource creation by the
170 * resource itself.
171 * * When a scratch resource is ref'ed it will not be returned from the
172 * cache for a subsequent cache request until all refs are released. This facilitates using
173 * a scratch key for multiple render-to-texture scenarios. An example is a separable blur:
174 *
175 * GrTexture* texture[2];
176 * texture[0] = get_scratch_texture(scratchKey);
177 * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a
178 * // different one for texture[1]
179 * draw_mask(texture[0], path); // draws path mask to texture[0]
180 * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1]
181 * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0]
182 * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey
183 * consume_blur(texture[0]);
184 * texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey
bsalomon24db3b12015-01-23 04:24:04 -0800185 */
186class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700187private:
bsalomon24db3b12015-01-23 04:24:04 -0800188 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700189
bsalomon24db3b12015-01-23 04:24:04 -0800190public:
191 /** Uniquely identifies the type of resource that is cached as scratch. */
192 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700193
bsalomon24db3b12015-01-23 04:24:04 -0800194 /** Generate a unique ResourceType. */
195 static ResourceType GenerateResourceType();
196
197 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
198 GrScratchKey() {}
199
200 GrScratchKey(const GrScratchKey& that) { *this = that; }
201
202 /** reset() returns the key to the invalid state. */
203 using INHERITED::reset;
204
205 using INHERITED::isValid;
206
207 ResourceType resourceType() const { return this->domain(); }
208
209 GrScratchKey& operator=(const GrScratchKey& that) {
210 this->INHERITED::operator=(that);
211 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700212 }
bsalomon24db3b12015-01-23 04:24:04 -0800213
Brian Salomonb4ba8262018-12-20 20:37:55 -0500214 bool operator==(const GrScratchKey& that) const { return this->INHERITED::operator==(that); }
bsalomon24db3b12015-01-23 04:24:04 -0800215 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)
Brian Salomonb4ba8262018-12-20 20:37:55 -0500220 : INHERITED::Builder(key, type, data32Count) {}
bsalomon24db3b12015-01-23 04:24:04 -0800221 };
222};
223
224/**
bsalomon8718aaf2015-02-19 07:24:21 -0800225 * 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.
bsalomon24db3b12015-01-23 04:24:04 -0800237 */
bsalomon8718aaf2015-02-19 07:24:21 -0800238class GrUniqueKey : public GrResourceKey {
bsalomon24db3b12015-01-23 04:24:04 -0800239private:
240 typedef GrResourceKey INHERITED;
241
242public:
243 typedef uint32_t Domain;
bsalomon8718aaf2015-02-19 07:24:21 -0800244 /** Generate a Domain for unique keys. */
bsalomon24db3b12015-01-23 04:24:04 -0800245 static Domain GenerateDomain();
246
bsalomon8718aaf2015-02-19 07:24:21 -0800247 /** Creates an invalid unique key. It must be initialized using a Builder object before use. */
Robert Phillips0790f8a2018-09-18 13:11:03 -0400248 GrUniqueKey() : fTag(nullptr) {}
bsalomon24db3b12015-01-23 04:24:04 -0800249
bsalomon8718aaf2015-02-19 07:24:21 -0800250 GrUniqueKey(const GrUniqueKey& that) { *this = that; }
bsalomon24db3b12015-01-23 04:24:04 -0800251
252 /** reset() returns the key to the invalid state. */
253 using INHERITED::reset;
254
255 using INHERITED::isValid;
256
bsalomon8718aaf2015-02-19 07:24:21 -0800257 GrUniqueKey& operator=(const GrUniqueKey& that) {
bsalomon24db3b12015-01-23 04:24:04 -0800258 this->INHERITED::operator=(that);
bungeman38d909e2016-08-02 14:40:46 -0700259 this->setCustomData(sk_ref_sp(that.getCustomData()));
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400260 fTag = that.fTag;
bsalomon24db3b12015-01-23 04:24:04 -0800261 return *this;
262 }
263
Brian Salomonb4ba8262018-12-20 20:37:55 -0500264 bool operator==(const GrUniqueKey& that) const { return this->INHERITED::operator==(that); }
bsalomon8718aaf2015-02-19 07:24:21 -0800265 bool operator!=(const GrUniqueKey& that) const { return !(*this == that); }
bsalomon24db3b12015-01-23 04:24:04 -0800266
Brian Salomonb4ba8262018-12-20 20:37:55 -0500267 void setCustomData(sk_sp<SkData> data) { fData = std::move(data); }
268 SkData* getCustomData() const { return fData.get(); }
senorblanco84cd6212015-08-04 10:01:58 -0700269
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400270 const char* tag() const { return fTag; }
Brian Salomon1090da62017-01-06 12:04:19 -0500271
Robert Phillips0790f8a2018-09-18 13:11:03 -0400272#ifdef SK_DEBUG
273 void dump(const char* label) const {
274 SkDebugf("%s tag: %s\n", label, fTag ? fTag : "None");
275 this->INHERITED::dump();
276 }
277#endif
278
bsalomon24db3b12015-01-23 04:24:04 -0800279 class Builder : public INHERITED::Builder {
280 public:
Brian Salomon1090da62017-01-06 12:04:19 -0500281 Builder(GrUniqueKey* key, Domain type, int data32Count, const char* tag = nullptr)
282 : INHERITED::Builder(key, type, data32Count) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400283 key->fTag = tag;
Brian Salomon1090da62017-01-06 12:04:19 -0500284 }
bsalomon24db3b12015-01-23 04:24:04 -0800285
286 /** Used to build a key that wraps another key and adds additional data. */
Brian Salomon1090da62017-01-06 12:04:19 -0500287 Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain, int extraData32Cnt,
288 const char* tag = nullptr)
289 : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
bsalomon37f9a262015-02-02 13:00:10 -0800290 SkASSERT(&innerKey != key);
bsalomon24db3b12015-01-23 04:24:04 -0800291 // add the inner key to the end of the key so that op[] can be indexed normally.
bsalomon3bd12ef2015-01-28 11:39:48 -0800292 uint32_t* innerKeyData = &this->operator[](extraData32Cnt);
293 const uint32_t* srcData = innerKey.data();
294 (*innerKeyData++) = innerKey.domain();
295 memcpy(innerKeyData, srcData, innerKey.dataSize());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400296 key->fTag = tag;
bsalomon3bd12ef2015-01-28 11:39:48 -0800297 }
298
299 private:
bsalomon8718aaf2015-02-19 07:24:21 -0800300 static int Data32CntForInnerKey(const GrUniqueKey& innerKey) {
bsalomon3bd12ef2015-01-28 11:39:48 -0800301 // key data + domain
302 return SkToInt((innerKey.dataSize() >> 2) + 1);
bsalomon24db3b12015-01-23 04:24:04 -0800303 }
304 };
senorblanco84cd6212015-08-04 10:01:58 -0700305
306private:
bungeman38d909e2016-08-02 14:40:46 -0700307 sk_sp<SkData> fData;
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400308 const char* fTag;
bsalomon744998e2014-08-28 09:54:34 -0700309};
310
bsalomoned0bcad2015-05-04 10:36:42 -0700311/**
312 * It is common to need a frequently reused GrUniqueKey where the only requirement is that the key
313 * is unique. These macros create such a key in a thread safe manner so the key can be truly global
314 * and only constructed once.
315 */
316
317/** Place outside of function/class definitions. */
mtkleind9dd4282016-04-18 08:09:11 -0700318#define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once
bsalomoned0bcad2015-05-04 10:36:42 -0700319
320/** Place inside function where the key is used. */
Brian Salomon23356442018-11-30 15:33:19 -0500321#define GR_DEFINE_STATIC_UNIQUE_KEY(name) \
322 static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \
323 name##_once(gr_init_static_unique_key_once, &name##_storage); \
324 static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get())
bsalomoned0bcad2015-05-04 10:36:42 -0700325
Brian Salomonb4ba8262018-12-20 20:37:55 -0500326static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1, GrUniqueKey>* keyStorage) {
bsalomonf0795ab2015-12-17 08:15:47 -0800327 GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey;
bsalomoned0bcad2015-05-04 10:36:42 -0700328 GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0);
329}
330
bsalomon23e619c2015-02-06 11:54:28 -0800331// The cache listens for these messages to purge junk resources proactively.
bsalomon8718aaf2015-02-19 07:24:21 -0800332class GrUniqueKeyInvalidatedMessage {
bsalomon23e619c2015-02-06 11:54:28 -0800333public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500334 GrUniqueKeyInvalidatedMessage() = default;
Brian Salomon238069b2018-07-11 15:58:57 -0400335 GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key, uint32_t contextUniqueID)
336 : fKey(key), fContextID(contextUniqueID) {
337 SkASSERT(SK_InvalidUniqueID != contextUniqueID);
bsalomon23e619c2015-02-06 11:54:28 -0800338 }
bsalomon8718aaf2015-02-19 07:24:21 -0800339
Brian Salomon238069b2018-07-11 15:58:57 -0400340 GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage&) = default;
341
342 GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage&) = default;
343
bsalomon8718aaf2015-02-19 07:24:21 -0800344 const GrUniqueKey& key() const { return fKey; }
Chris Dalton9a986cf2018-10-18 15:27:59 -0600345 uint32_t contextID() const { return fContextID; }
Brian Salomon238069b2018-07-11 15:58:57 -0400346
bsalomon23e619c2015-02-06 11:54:28 -0800347private:
bsalomon8718aaf2015-02-19 07:24:21 -0800348 GrUniqueKey fKey;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500349 uint32_t fContextID = SK_InvalidUniqueID;
bsalomon23e619c2015-02-06 11:54:28 -0800350};
Brian Salomon238069b2018-07-11 15:58:57 -0400351
Brian Salomonb4ba8262018-12-20 20:37:55 -0500352static inline bool SkShouldPostMessageToBus(const GrUniqueKeyInvalidatedMessage& msg,
353 uint32_t msgBusUniqueID) {
Chris Dalton9a986cf2018-10-18 15:27:59 -0600354 return msg.contextID() == msgBusUniqueID;
355}
356
bsalomon744998e2014-08-28 09:54:34 -0700357#endif