blob: 0ead35ea3f79a1b359766b995c335fdd1be39b16 [file] [log] [blame]
bsalomon744998e2014-08-28 09:54:34 -07001
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
bungemanf3c15b72015-08-19 11:56:48 -070012#include "../private/SkTemplates.h"
bsalomon744998e2014-08-28 09:54:34 -070013#include "GrTypes.h"
senorblanco84cd6212015-08-04 10:01:58 -070014#include "SkData.h"
herb62a69c22015-09-29 11:47:45 -070015#include "../private/SkOnce.h"
bsalomon744998e2014-08-28 09:54:34 -070016
bsalomon24db3b12015-01-23 04:24:04 -080017uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
bsalomon7775c852014-12-30 12:50:52 -080018
bsalomon8718aaf2015-02-19 07:24:21 -080019/**
20 * Base class for all GrGpuResource cache keys. There are two types of cache keys. Refer to the
21 * comments for each key type below.
22 */
bsalomon24db3b12015-01-23 04:24:04 -080023class GrResourceKey {
24public:
25 uint32_t hash() const {
26 this->validate();
27 return fKey[kHash_MetaDataIdx];
28 }
29
30 size_t size() const {
31 this->validate();
bsalomon3bd12ef2015-01-28 11:39:48 -080032 SkASSERT(this->isValid());
bsalomon24db3b12015-01-23 04:24:04 -080033 return this->internalSize();
34 }
35
bsalomon24db3b12015-01-23 04:24:04 -080036protected:
37 static const uint32_t kInvalidDomain = 0;
38
39 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080040
41 /** Reset to an invalid key. */
42 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080043 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080044 fKey.reset(kMetaDataCnt);
45 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080046 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080047 }
48
bsalomon24db3b12015-01-23 04:24:04 -080049 bool operator==(const GrResourceKey& that) const {
kkinnunen54b85112015-05-18 22:47:33 -070050 return this->hash() == that.hash() &&
51 0 == memcmp(&fKey[kHash_MetaDataIdx + 1],
52 &that.fKey[kHash_MetaDataIdx + 1],
53 this->internalSize() - sizeof(uint32_t));
bsalomon24db3b12015-01-23 04:24:04 -080054 }
bsalomon7775c852014-12-30 12:50:52 -080055
bsalomon24db3b12015-01-23 04:24:04 -080056 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon23e619c2015-02-06 11:54:28 -080057 SkASSERT(that.isValid());
bsalomon4dffede2015-01-23 07:17:55 -080058 if (this != &that) {
59 size_t bytes = that.size();
60 SkASSERT(SkIsAlign4(bytes));
61 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
62 memcpy(fKey.get(), that.fKey.get(), bytes);
bsalomon23e619c2015-02-06 11:54:28 -080063 this->validate();
bsalomon4dffede2015-01-23 07:17:55 -080064 }
bsalomon7775c852014-12-30 12:50:52 -080065 return *this;
66 }
67
bsalomon24db3b12015-01-23 04:24:04 -080068 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080069
bsalomon24db3b12015-01-23 04:24:04 -080070 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
71
bsalomon3bd12ef2015-01-28 11:39:48 -080072 /** size of the key data, excluding meta-data (hash, domain, etc). */
73 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
mtkleind9dd4282016-04-18 08:09:11 -070074
bsalomon3bd12ef2015-01-28 11:39:48 -080075 /** ptr to the key data, excluding meta-data (hash, domain, etc). */
76 const uint32_t* data() const {
77 this->validate();
78 return &fKey[kMetaDataCnt];
79 }
80
bsalomon24db3b12015-01-23 04:24:04 -080081 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080082 class Builder {
83 public:
bsalomon24db3b12015-01-23 04:24:04 -080084 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080085 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080086 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080087 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080088 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080089 SkASSERT(SkToU16(size) == size);
90 SkASSERT(SkToU16(domain) == domain);
91 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080092 }
93
94 ~Builder() { this->finish(); }
95
bsalomon24db3b12015-01-23 04:24:04 -080096 void finish() {
97 if (NULL == fKey) {
98 return;
99 }
100 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
101 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
102 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
103 fKey->validate();
104 fKey = NULL;
105 }
bsalomon7775c852014-12-30 12:50:52 -0800106
107 uint32_t& operator[](int dataIdx) {
108 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -0800109 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -0800110 SkASSERT(SkToU32(dataIdx) < dataCount);
111 return fKey->fKey[kMetaDataCnt + dataIdx];
112 }
113
114 private:
bsalomon24db3b12015-01-23 04:24:04 -0800115 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800116 };
117
118private:
119 enum MetaDataIdx {
120 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800121 // The key domain and size are packed into a single uint32_t.
122 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800123
bsalomon24db3b12015-01-23 04:24:04 -0800124 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800125 };
bsalomon7775c852014-12-30 12:50:52 -0800126 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
127
bsalomon3bd12ef2015-01-28 11:39:48 -0800128 size_t internalSize() const {
129 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
130 }
131
132 void validate() const {
133 SkASSERT(fKey[kHash_MetaDataIdx] ==
134 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
135 this->internalSize() - sizeof(uint32_t)));
136 SkASSERT(SkIsAlign4(this->internalSize()));
137 }
138
bsalomon1c60dfe2015-01-21 09:32:40 -0800139 friend class TestResource; // For unit test to access kMetaDataCnt.
140
bsalomon045802d2015-10-20 07:58:01 -0700141 // bmp textures require 5 uint32_t values.
142 SkAutoSTMalloc<kMetaDataCnt + 5, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800143};
144
bsalomon24db3b12015-01-23 04:24:04 -0800145/**
bsalomon8718aaf2015-02-19 07:24:21 -0800146 * A key used for scratch resources. There are three important rules about scratch keys:
147 * * Multiple resources can share the same scratch key. Therefore resources assigned the same
148 * scratch key should be interchangeable with respect to the code that uses them.
149 * * A resource can have at most one scratch key and it is set at resource creation by the
150 * resource itself.
151 * * When a scratch resource is ref'ed it will not be returned from the
152 * cache for a subsequent cache request until all refs are released. This facilitates using
153 * a scratch key for multiple render-to-texture scenarios. An example is a separable blur:
154 *
155 * GrTexture* texture[2];
156 * texture[0] = get_scratch_texture(scratchKey);
157 * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a
158 * // different one for texture[1]
159 * draw_mask(texture[0], path); // draws path mask to texture[0]
160 * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1]
161 * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0]
162 * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey
163 * consume_blur(texture[0]);
164 * texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey
bsalomon24db3b12015-01-23 04:24:04 -0800165 */
166class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700167private:
bsalomon24db3b12015-01-23 04:24:04 -0800168 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700169
bsalomon24db3b12015-01-23 04:24:04 -0800170public:
171 /** Uniquely identifies the type of resource that is cached as scratch. */
172 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700173
bsalomon24db3b12015-01-23 04:24:04 -0800174 /** Generate a unique ResourceType. */
175 static ResourceType GenerateResourceType();
176
177 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
178 GrScratchKey() {}
179
180 GrScratchKey(const GrScratchKey& that) { *this = that; }
181
182 /** reset() returns the key to the invalid state. */
183 using INHERITED::reset;
184
185 using INHERITED::isValid;
186
187 ResourceType resourceType() const { return this->domain(); }
188
189 GrScratchKey& operator=(const GrScratchKey& that) {
190 this->INHERITED::operator=(that);
191 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700192 }
bsalomon24db3b12015-01-23 04:24:04 -0800193
194 bool operator==(const GrScratchKey& that) const {
195 return this->INHERITED::operator==(that);
196 }
197 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
198
199 class Builder : public INHERITED::Builder {
200 public:
201 Builder(GrScratchKey* key, ResourceType type, int data32Count)
202 : INHERITED::Builder(key, type, data32Count) {}
203 };
204};
205
206/**
bsalomon8718aaf2015-02-19 07:24:21 -0800207 * A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three
208 * rules governing the use of unique keys:
209 * * Only one resource can have a given unique key at a time. Hence, "unique".
210 * * A resource can have at most one unique key at a time.
211 * * Unlike scratch keys, multiple requests for a unique key will return the same
212 * resource even if the resource already has refs.
213 * This key type allows a code path to create cached resources for which it is the exclusive user.
214 * The code path creates a domain which it sets on its keys. This guarantees that there are no
215 * cross-domain collisions.
216 *
217 * Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its
218 * scratch key. It can become scratch again if the unique key is removed.
bsalomon24db3b12015-01-23 04:24:04 -0800219 */
bsalomon8718aaf2015-02-19 07:24:21 -0800220class GrUniqueKey : public GrResourceKey {
bsalomon24db3b12015-01-23 04:24:04 -0800221private:
222 typedef GrResourceKey INHERITED;
223
224public:
225 typedef uint32_t Domain;
bsalomon8718aaf2015-02-19 07:24:21 -0800226 /** Generate a Domain for unique keys. */
bsalomon24db3b12015-01-23 04:24:04 -0800227 static Domain GenerateDomain();
228
bsalomon8718aaf2015-02-19 07:24:21 -0800229 /** Creates an invalid unique key. It must be initialized using a Builder object before use. */
230 GrUniqueKey() {}
bsalomon24db3b12015-01-23 04:24:04 -0800231
bsalomon8718aaf2015-02-19 07:24:21 -0800232 GrUniqueKey(const GrUniqueKey& that) { *this = that; }
bsalomon24db3b12015-01-23 04:24:04 -0800233
234 /** reset() returns the key to the invalid state. */
235 using INHERITED::reset;
236
237 using INHERITED::isValid;
238
bsalomon8718aaf2015-02-19 07:24:21 -0800239 GrUniqueKey& operator=(const GrUniqueKey& that) {
bsalomon24db3b12015-01-23 04:24:04 -0800240 this->INHERITED::operator=(that);
bungeman38d909e2016-08-02 14:40:46 -0700241 this->setCustomData(sk_ref_sp(that.getCustomData()));
bsalomon24db3b12015-01-23 04:24:04 -0800242 return *this;
243 }
244
bsalomon8718aaf2015-02-19 07:24:21 -0800245 bool operator==(const GrUniqueKey& that) const {
bsalomon24db3b12015-01-23 04:24:04 -0800246 return this->INHERITED::operator==(that);
247 }
bsalomon8718aaf2015-02-19 07:24:21 -0800248 bool operator!=(const GrUniqueKey& that) const { return !(*this == that); }
bsalomon24db3b12015-01-23 04:24:04 -0800249
bungeman38d909e2016-08-02 14:40:46 -0700250 void setCustomData(sk_sp<SkData> data) {
251 fData = std::move(data);
senorblanco84cd6212015-08-04 10:01:58 -0700252 }
bungeman38d909e2016-08-02 14:40:46 -0700253 SkData* getCustomData() const {
senorblanco84cd6212015-08-04 10:01:58 -0700254 return fData.get();
255 }
256
bsalomon24db3b12015-01-23 04:24:04 -0800257 class Builder : public INHERITED::Builder {
258 public:
bsalomon8718aaf2015-02-19 07:24:21 -0800259 Builder(GrUniqueKey* key, Domain domain, int data32Count)
bsalomon24db3b12015-01-23 04:24:04 -0800260 : INHERITED::Builder(key, domain, data32Count) {}
261
262 /** Used to build a key that wraps another key and adds additional data. */
bsalomon8718aaf2015-02-19 07:24:21 -0800263 Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain,
bsalomon24db3b12015-01-23 04:24:04 -0800264 int extraData32Cnt)
bsalomon3bd12ef2015-01-28 11:39:48 -0800265 : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
bsalomon37f9a262015-02-02 13:00:10 -0800266 SkASSERT(&innerKey != key);
bsalomon24db3b12015-01-23 04:24:04 -0800267 // add the inner key to the end of the key so that op[] can be indexed normally.
bsalomon3bd12ef2015-01-28 11:39:48 -0800268 uint32_t* innerKeyData = &this->operator[](extraData32Cnt);
269 const uint32_t* srcData = innerKey.data();
270 (*innerKeyData++) = innerKey.domain();
271 memcpy(innerKeyData, srcData, innerKey.dataSize());
272 }
273
274 private:
bsalomon8718aaf2015-02-19 07:24:21 -0800275 static int Data32CntForInnerKey(const GrUniqueKey& innerKey) {
bsalomon3bd12ef2015-01-28 11:39:48 -0800276 // key data + domain
277 return SkToInt((innerKey.dataSize() >> 2) + 1);
bsalomon24db3b12015-01-23 04:24:04 -0800278 }
279 };
senorblanco84cd6212015-08-04 10:01:58 -0700280
281private:
bungeman38d909e2016-08-02 14:40:46 -0700282 sk_sp<SkData> fData;
bsalomon744998e2014-08-28 09:54:34 -0700283};
284
bsalomoned0bcad2015-05-04 10:36:42 -0700285/**
286 * It is common to need a frequently reused GrUniqueKey where the only requirement is that the key
287 * is unique. These macros create such a key in a thread safe manner so the key can be truly global
288 * and only constructed once.
289 */
290
291/** Place outside of function/class definitions. */
mtkleind9dd4282016-04-18 08:09:11 -0700292#define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once
bsalomoned0bcad2015-05-04 10:36:42 -0700293
294/** Place inside function where the key is used. */
bsalomonf0795ab2015-12-17 08:15:47 -0800295#define GR_DEFINE_STATIC_UNIQUE_KEY(name) \
296 static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \
mtkleind9dd4282016-04-18 08:09:11 -0700297 name##_once(gr_init_static_unique_key_once, &name##_storage); \
bsalomonf0795ab2015-12-17 08:15:47 -0800298 static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get());
bsalomoned0bcad2015-05-04 10:36:42 -0700299
bsalomonf0795ab2015-12-17 08:15:47 -0800300static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1,GrUniqueKey>* keyStorage) {
301 GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey;
bsalomoned0bcad2015-05-04 10:36:42 -0700302 GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0);
303}
304
bsalomon23e619c2015-02-06 11:54:28 -0800305// The cache listens for these messages to purge junk resources proactively.
bsalomon8718aaf2015-02-19 07:24:21 -0800306class GrUniqueKeyInvalidatedMessage {
bsalomon23e619c2015-02-06 11:54:28 -0800307public:
bsalomon8718aaf2015-02-19 07:24:21 -0800308 explicit GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key) : fKey(key) {}
309
310 GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage& that) : fKey(that.fKey) {}
311
312 GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage& that) {
bsalomon23e619c2015-02-06 11:54:28 -0800313 fKey = that.fKey;
314 return *this;
315 }
bsalomon8718aaf2015-02-19 07:24:21 -0800316
317 const GrUniqueKey& key() const { return fKey; }
318
bsalomon23e619c2015-02-06 11:54:28 -0800319private:
bsalomon8718aaf2015-02-19 07:24:21 -0800320 GrUniqueKey fKey;
bsalomon23e619c2015-02-06 11:54:28 -0800321};
bsalomon744998e2014-08-28 09:54:34 -0700322#endif