blob: 0240387a6b33f0cceef51038c84372023c0ebb1f [file] [log] [blame]
Hal Canaryfdcfb8b2018-06-13 09:42:32 -04001
bsalomon744998e2014-08-28 09:54:34 -07002/*
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 Salomon1090da62017-01-06 12:04:19 -050012#include "../private/SkOnce.h"
bungemanf3c15b72015-08-19 11:56:48 -070013#include "../private/SkTemplates.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"
Mike Klein79aea6a2018-06-11 10:45:26 -040017#include <new>
bsalomon744998e2014-08-28 09:54:34 -070018
bsalomon24db3b12015-01-23 04:24:04 -080019uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
bsalomon7775c852014-12-30 12:50:52 -080020
bsalomon8718aaf2015-02-19 07:24:21 -080021/**
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 */
bsalomon24db3b12015-01-23 04:24:04 -080025class GrResourceKey {
26public:
27 uint32_t hash() const {
28 this->validate();
29 return fKey[kHash_MetaDataIdx];
30 }
31
32 size_t size() const {
33 this->validate();
bsalomon3bd12ef2015-01-28 11:39:48 -080034 SkASSERT(this->isValid());
bsalomon24db3b12015-01-23 04:24:04 -080035 return this->internalSize();
36 }
37
bsalomon24db3b12015-01-23 04:24:04 -080038protected:
39 static const uint32_t kInvalidDomain = 0;
40
41 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080042
43 /** Reset to an invalid key. */
44 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080045 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080046 fKey.reset(kMetaDataCnt);
47 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080048 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080049 }
50
bsalomon24db3b12015-01-23 04:24:04 -080051 bool operator==(const GrResourceKey& that) const {
kkinnunen54b85112015-05-18 22:47:33 -070052 return this->hash() == that.hash() &&
53 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) {
bsalomon23e619c2015-02-06 11:54:28 -080059 SkASSERT(that.isValid());
bsalomon4dffede2015-01-23 07:17:55 -080060 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);
bsalomon23e619c2015-02-06 11:54:28 -080065 this->validate();
bsalomon4dffede2015-01-23 07:17:55 -080066 }
bsalomon7775c852014-12-30 12:50:52 -080067 return *this;
68 }
69
bsalomon24db3b12015-01-23 04:24:04 -080070 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080071
bsalomon24db3b12015-01-23 04:24:04 -080072 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
73
bsalomon3bd12ef2015-01-28 11:39:48 -080074 /** size of the key data, excluding meta-data (hash, domain, etc). */
75 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
mtkleind9dd4282016-04-18 08:09:11 -070076
bsalomon3bd12ef2015-01-28 11:39:48 -080077 /** 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
bsalomon24db3b12015-01-23 04:24:04 -080083 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080084 class Builder {
85 public:
bsalomon24db3b12015-01-23 04:24:04 -080086 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080087 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080088 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080089 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080090 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080091 SkASSERT(SkToU16(size) == size);
92 SkASSERT(SkToU16(domain) == domain);
93 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080094 }
95
96 ~Builder() { this->finish(); }
97
bsalomon24db3b12015-01-23 04:24:04 -080098 void finish() {
Ben Wagnera93a14a2017-08-28 10:34:05 -040099 if (nullptr == fKey) {
bsalomon24db3b12015-01-23 04:24:04 -0800100 return;
101 }
102 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
103 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
104 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
105 fKey->validate();
Ben Wagnera93a14a2017-08-28 10:34:05 -0400106 fKey = nullptr;
bsalomon24db3b12015-01-23 04:24:04 -0800107 }
bsalomon7775c852014-12-30 12:50:52 -0800108
109 uint32_t& operator[](int dataIdx) {
110 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -0800111 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -0800112 SkASSERT(SkToU32(dataIdx) < dataCount);
113 return fKey->fKey[kMetaDataCnt + dataIdx];
114 }
115
116 private:
bsalomon24db3b12015-01-23 04:24:04 -0800117 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800118 };
119
120private:
121 enum MetaDataIdx {
122 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800123 // The key domain and size are packed into a single uint32_t.
124 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800125
bsalomon24db3b12015-01-23 04:24:04 -0800126 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800127 };
bsalomon7775c852014-12-30 12:50:52 -0800128 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
129
bsalomon3bd12ef2015-01-28 11:39:48 -0800130 size_t internalSize() const {
131 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
132 }
133
134 void validate() const {
135 SkASSERT(fKey[kHash_MetaDataIdx] ==
136 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
137 this->internalSize() - sizeof(uint32_t)));
138 SkASSERT(SkIsAlign4(this->internalSize()));
139 }
140
bsalomon1c60dfe2015-01-21 09:32:40 -0800141 friend class TestResource; // For unit test to access kMetaDataCnt.
142
Mike Reede53de482017-03-09 19:52:32 -0500143 // bmp textures require 7 uint32_t values (5 for the base key, and two more for image
144 // cacherator's decode format.
145 SkAutoSTMalloc<kMetaDataCnt + 7, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800146};
147
bsalomon24db3b12015-01-23 04:24:04 -0800148/**
bsalomon8718aaf2015-02-19 07:24:21 -0800149 * A key used for scratch resources. There are three important rules about scratch keys:
150 * * Multiple resources can share the same scratch key. Therefore resources assigned the same
151 * scratch key should be interchangeable with respect to the code that uses them.
152 * * A resource can have at most one scratch key and it is set at resource creation by the
153 * resource itself.
154 * * When a scratch resource is ref'ed it will not be returned from the
155 * cache for a subsequent cache request until all refs are released. This facilitates using
156 * a scratch key for multiple render-to-texture scenarios. An example is a separable blur:
157 *
158 * GrTexture* texture[2];
159 * texture[0] = get_scratch_texture(scratchKey);
160 * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a
161 * // different one for texture[1]
162 * draw_mask(texture[0], path); // draws path mask to texture[0]
163 * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1]
164 * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0]
165 * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey
166 * consume_blur(texture[0]);
167 * texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey
bsalomon24db3b12015-01-23 04:24:04 -0800168 */
169class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700170private:
bsalomon24db3b12015-01-23 04:24:04 -0800171 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700172
bsalomon24db3b12015-01-23 04:24:04 -0800173public:
174 /** Uniquely identifies the type of resource that is cached as scratch. */
175 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700176
bsalomon24db3b12015-01-23 04:24:04 -0800177 /** Generate a unique ResourceType. */
178 static ResourceType GenerateResourceType();
179
180 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
181 GrScratchKey() {}
182
183 GrScratchKey(const GrScratchKey& that) { *this = that; }
184
185 /** reset() returns the key to the invalid state. */
186 using INHERITED::reset;
187
188 using INHERITED::isValid;
189
190 ResourceType resourceType() const { return this->domain(); }
191
192 GrScratchKey& operator=(const GrScratchKey& that) {
193 this->INHERITED::operator=(that);
194 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700195 }
bsalomon24db3b12015-01-23 04:24:04 -0800196
197 bool operator==(const GrScratchKey& that) const {
198 return this->INHERITED::operator==(that);
199 }
200 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
201
202 class Builder : public INHERITED::Builder {
203 public:
204 Builder(GrScratchKey* key, ResourceType type, int data32Count)
205 : INHERITED::Builder(key, type, data32Count) {}
206 };
207};
208
209/**
bsalomon8718aaf2015-02-19 07:24:21 -0800210 * A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three
211 * rules governing the use of unique keys:
212 * * Only one resource can have a given unique key at a time. Hence, "unique".
213 * * A resource can have at most one unique key at a time.
214 * * Unlike scratch keys, multiple requests for a unique key will return the same
215 * resource even if the resource already has refs.
216 * This key type allows a code path to create cached resources for which it is the exclusive user.
217 * The code path creates a domain which it sets on its keys. This guarantees that there are no
218 * cross-domain collisions.
219 *
220 * Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its
221 * scratch key. It can become scratch again if the unique key is removed.
bsalomon24db3b12015-01-23 04:24:04 -0800222 */
bsalomon8718aaf2015-02-19 07:24:21 -0800223class GrUniqueKey : public GrResourceKey {
bsalomon24db3b12015-01-23 04:24:04 -0800224private:
225 typedef GrResourceKey INHERITED;
226
227public:
228 typedef uint32_t Domain;
bsalomon8718aaf2015-02-19 07:24:21 -0800229 /** Generate a Domain for unique keys. */
bsalomon24db3b12015-01-23 04:24:04 -0800230 static Domain GenerateDomain();
231
bsalomon8718aaf2015-02-19 07:24:21 -0800232 /** Creates an invalid unique key. It must be initialized using a Builder object before use. */
233 GrUniqueKey() {}
bsalomon24db3b12015-01-23 04:24:04 -0800234
bsalomon8718aaf2015-02-19 07:24:21 -0800235 GrUniqueKey(const GrUniqueKey& that) { *this = that; }
bsalomon24db3b12015-01-23 04:24:04 -0800236
237 /** reset() returns the key to the invalid state. */
238 using INHERITED::reset;
239
240 using INHERITED::isValid;
241
bsalomon8718aaf2015-02-19 07:24:21 -0800242 GrUniqueKey& operator=(const GrUniqueKey& that) {
bsalomon24db3b12015-01-23 04:24:04 -0800243 this->INHERITED::operator=(that);
bungeman38d909e2016-08-02 14:40:46 -0700244 this->setCustomData(sk_ref_sp(that.getCustomData()));
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400245 fTag = that.fTag;
bsalomon24db3b12015-01-23 04:24:04 -0800246 return *this;
247 }
248
bsalomon8718aaf2015-02-19 07:24:21 -0800249 bool operator==(const GrUniqueKey& that) const {
bsalomon24db3b12015-01-23 04:24:04 -0800250 return this->INHERITED::operator==(that);
251 }
bsalomon8718aaf2015-02-19 07:24:21 -0800252 bool operator!=(const GrUniqueKey& that) const { return !(*this == that); }
bsalomon24db3b12015-01-23 04:24:04 -0800253
bungeman38d909e2016-08-02 14:40:46 -0700254 void setCustomData(sk_sp<SkData> data) {
255 fData = std::move(data);
senorblanco84cd6212015-08-04 10:01:58 -0700256 }
bungeman38d909e2016-08-02 14:40:46 -0700257 SkData* getCustomData() const {
senorblanco84cd6212015-08-04 10:01:58 -0700258 return fData.get();
259 }
260
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400261 const char* tag() const { return fTag; }
Brian Salomon1090da62017-01-06 12:04:19 -0500262
bsalomon24db3b12015-01-23 04:24:04 -0800263 class Builder : public INHERITED::Builder {
264 public:
Brian Salomon1090da62017-01-06 12:04:19 -0500265 Builder(GrUniqueKey* key, Domain type, int data32Count, const char* tag = nullptr)
266 : INHERITED::Builder(key, type, data32Count) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400267 key->fTag = tag;
Brian Salomon1090da62017-01-06 12:04:19 -0500268 }
bsalomon24db3b12015-01-23 04:24:04 -0800269
270 /** Used to build a key that wraps another key and adds additional data. */
Brian Salomon1090da62017-01-06 12:04:19 -0500271 Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain, int extraData32Cnt,
272 const char* tag = nullptr)
273 : INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
bsalomon37f9a262015-02-02 13:00:10 -0800274 SkASSERT(&innerKey != key);
bsalomon24db3b12015-01-23 04:24:04 -0800275 // add the inner key to the end of the key so that op[] can be indexed normally.
bsalomon3bd12ef2015-01-28 11:39:48 -0800276 uint32_t* innerKeyData = &this->operator[](extraData32Cnt);
277 const uint32_t* srcData = innerKey.data();
278 (*innerKeyData++) = innerKey.domain();
279 memcpy(innerKeyData, srcData, innerKey.dataSize());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400280 key->fTag = tag;
bsalomon3bd12ef2015-01-28 11:39:48 -0800281 }
282
283 private:
bsalomon8718aaf2015-02-19 07:24:21 -0800284 static int Data32CntForInnerKey(const GrUniqueKey& innerKey) {
bsalomon3bd12ef2015-01-28 11:39:48 -0800285 // key data + domain
286 return SkToInt((innerKey.dataSize() >> 2) + 1);
bsalomon24db3b12015-01-23 04:24:04 -0800287 }
288 };
senorblanco84cd6212015-08-04 10:01:58 -0700289
290private:
bungeman38d909e2016-08-02 14:40:46 -0700291 sk_sp<SkData> fData;
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400292 const char* fTag;
bsalomon744998e2014-08-28 09:54:34 -0700293};
294
bsalomoned0bcad2015-05-04 10:36:42 -0700295/**
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. */
mtkleind9dd4282016-04-18 08:09:11 -0700302#define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once
bsalomoned0bcad2015-05-04 10:36:42 -0700303
304/** Place inside function where the key is used. */
bsalomonf0795ab2015-12-17 08:15:47 -0800305#define GR_DEFINE_STATIC_UNIQUE_KEY(name) \
306 static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \
mtkleind9dd4282016-04-18 08:09:11 -0700307 name##_once(gr_init_static_unique_key_once, &name##_storage); \
bsalomonf0795ab2015-12-17 08:15:47 -0800308 static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get());
bsalomoned0bcad2015-05-04 10:36:42 -0700309
bsalomonf0795ab2015-12-17 08:15:47 -0800310static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1,GrUniqueKey>* keyStorage) {
311 GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey;
bsalomoned0bcad2015-05-04 10:36:42 -0700312 GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0);
313}
314
bsalomon23e619c2015-02-06 11:54:28 -0800315// The cache listens for these messages to purge junk resources proactively.
bsalomon8718aaf2015-02-19 07:24:21 -0800316class GrUniqueKeyInvalidatedMessage {
bsalomon23e619c2015-02-06 11:54:28 -0800317public:
bsalomon8718aaf2015-02-19 07:24:21 -0800318 explicit GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key) : fKey(key) {}
319
320 GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage& that) : fKey(that.fKey) {}
321
322 GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage& that) {
bsalomon23e619c2015-02-06 11:54:28 -0800323 fKey = that.fKey;
324 return *this;
325 }
bsalomon8718aaf2015-02-19 07:24:21 -0800326
327 const GrUniqueKey& key() const { return fKey; }
328
bsalomon23e619c2015-02-06 11:54:28 -0800329private:
bsalomon8718aaf2015-02-19 07:24:21 -0800330 GrUniqueKey fKey;
bsalomon23e619c2015-02-06 11:54:28 -0800331};
bsalomon744998e2014-08-28 09:54:34 -0700332#endif