blob: c0a08f8addb260e3ddabdabaa884557d91dfc124 [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
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"
bsalomon744998e2014-08-28 09:54:34 -070017
bsalomon24db3b12015-01-23 04:24:04 -080018uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
bsalomon7775c852014-12-30 12:50:52 -080019
bsalomon8718aaf2015-02-19 07:24:21 -080020/**
21 * Base class for all GrGpuResource cache keys. There are two types of cache keys. Refer to the
22 * comments for each key type below.
23 */
bsalomon24db3b12015-01-23 04:24:04 -080024class GrResourceKey {
25public:
26 uint32_t hash() const {
27 this->validate();
28 return fKey[kHash_MetaDataIdx];
29 }
30
31 size_t size() const {
32 this->validate();
bsalomon3bd12ef2015-01-28 11:39:48 -080033 SkASSERT(this->isValid());
bsalomon24db3b12015-01-23 04:24:04 -080034 return this->internalSize();
35 }
36
bsalomon24db3b12015-01-23 04:24:04 -080037protected:
38 static const uint32_t kInvalidDomain = 0;
39
40 GrResourceKey() { this->reset(); }
bsalomon7775c852014-12-30 12:50:52 -080041
42 /** Reset to an invalid key. */
43 void reset() {
bsalomon24db3b12015-01-23 04:24:04 -080044 GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080045 fKey.reset(kMetaDataCnt);
46 fKey[kHash_MetaDataIdx] = 0;
bsalomon24db3b12015-01-23 04:24:04 -080047 fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
bsalomon7775c852014-12-30 12:50:52 -080048 }
49
bsalomon24db3b12015-01-23 04:24:04 -080050 bool operator==(const GrResourceKey& that) const {
kkinnunen54b85112015-05-18 22:47:33 -070051 return this->hash() == that.hash() &&
52 0 == memcmp(&fKey[kHash_MetaDataIdx + 1],
53 &that.fKey[kHash_MetaDataIdx + 1],
54 this->internalSize() - sizeof(uint32_t));
bsalomon24db3b12015-01-23 04:24:04 -080055 }
bsalomon7775c852014-12-30 12:50:52 -080056
bsalomon24db3b12015-01-23 04:24:04 -080057 GrResourceKey& operator=(const GrResourceKey& that) {
bsalomon23e619c2015-02-06 11:54:28 -080058 SkASSERT(that.isValid());
bsalomon4dffede2015-01-23 07:17:55 -080059 if (this != &that) {
60 size_t bytes = that.size();
61 SkASSERT(SkIsAlign4(bytes));
62 fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
63 memcpy(fKey.get(), that.fKey.get(), bytes);
bsalomon23e619c2015-02-06 11:54:28 -080064 this->validate();
bsalomon4dffede2015-01-23 07:17:55 -080065 }
bsalomon7775c852014-12-30 12:50:52 -080066 return *this;
67 }
68
bsalomon24db3b12015-01-23 04:24:04 -080069 bool isValid() const { return kInvalidDomain != this->domain(); }
bsalomon7775c852014-12-30 12:50:52 -080070
bsalomon24db3b12015-01-23 04:24:04 -080071 uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
72
bsalomon3bd12ef2015-01-28 11:39:48 -080073 /** size of the key data, excluding meta-data (hash, domain, etc). */
74 size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
mtkleind9dd4282016-04-18 08:09:11 -070075
bsalomon3bd12ef2015-01-28 11:39:48 -080076 /** ptr to the key data, excluding meta-data (hash, domain, etc). */
77 const uint32_t* data() const {
78 this->validate();
79 return &fKey[kMetaDataCnt];
80 }
81
bsalomon24db3b12015-01-23 04:24:04 -080082 /** Used to initialize a key. */
bsalomon7775c852014-12-30 12:50:52 -080083 class Builder {
84 public:
bsalomon24db3b12015-01-23 04:24:04 -080085 Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
bsalomon7775c852014-12-30 12:50:52 -080086 SkASSERT(data32Count >= 0);
bsalomon24db3b12015-01-23 04:24:04 -080087 SkASSERT(domain != kInvalidDomain);
bsalomon7775c852014-12-30 12:50:52 -080088 key->fKey.reset(kMetaDataCnt + data32Count);
bsalomon7775c852014-12-30 12:50:52 -080089 int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
bsalomon24db3b12015-01-23 04:24:04 -080090 SkASSERT(SkToU16(size) == size);
91 SkASSERT(SkToU16(domain) == domain);
92 key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
bsalomon7775c852014-12-30 12:50:52 -080093 }
94
95 ~Builder() { this->finish(); }
96
bsalomon24db3b12015-01-23 04:24:04 -080097 void finish() {
Ben Wagnera93a14a2017-08-28 10:34:05 -040098 if (nullptr == fKey) {
bsalomon24db3b12015-01-23 04:24:04 -080099 return;
100 }
101 GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
102 uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
103 *hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
104 fKey->validate();
Ben Wagnera93a14a2017-08-28 10:34:05 -0400105 fKey = nullptr;
bsalomon24db3b12015-01-23 04:24:04 -0800106 }
bsalomon7775c852014-12-30 12:50:52 -0800107
108 uint32_t& operator[](int dataIdx) {
109 SkASSERT(fKey);
bsalomon24db3b12015-01-23 04:24:04 -0800110 SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
bsalomon7775c852014-12-30 12:50:52 -0800111 SkASSERT(SkToU32(dataIdx) < dataCount);
112 return fKey->fKey[kMetaDataCnt + dataIdx];
113 }
114
115 private:
bsalomon24db3b12015-01-23 04:24:04 -0800116 GrResourceKey* fKey;
bsalomon7775c852014-12-30 12:50:52 -0800117 };
118
119private:
120 enum MetaDataIdx {
121 kHash_MetaDataIdx,
bsalomon24db3b12015-01-23 04:24:04 -0800122 // The key domain and size are packed into a single uint32_t.
123 kDomainAndSize_MetaDataIdx,
bsalomon7775c852014-12-30 12:50:52 -0800124
bsalomon24db3b12015-01-23 04:24:04 -0800125 kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
bsalomon7775c852014-12-30 12:50:52 -0800126 };
bsalomon7775c852014-12-30 12:50:52 -0800127 static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
128
bsalomon3bd12ef2015-01-28 11:39:48 -0800129 size_t internalSize() const {
130 return fKey[kDomainAndSize_MetaDataIdx] >> 16;
131 }
132
133 void validate() const {
134 SkASSERT(fKey[kHash_MetaDataIdx] ==
135 GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
136 this->internalSize() - sizeof(uint32_t)));
137 SkASSERT(SkIsAlign4(this->internalSize()));
138 }
139
bsalomon1c60dfe2015-01-21 09:32:40 -0800140 friend class TestResource; // For unit test to access kMetaDataCnt.
141
Mike Reede53de482017-03-09 19:52:32 -0500142 // bmp textures require 7 uint32_t values (5 for the base key, and two more for image
143 // cacherator's decode format.
144 SkAutoSTMalloc<kMetaDataCnt + 7, uint32_t> fKey;
bsalomon7775c852014-12-30 12:50:52 -0800145};
146
bsalomon24db3b12015-01-23 04:24:04 -0800147/**
bsalomon8718aaf2015-02-19 07:24:21 -0800148 * A key used for scratch resources. There are three important rules about scratch keys:
149 * * Multiple resources can share the same scratch key. Therefore resources assigned the same
150 * scratch key should be interchangeable with respect to the code that uses them.
151 * * A resource can have at most one scratch key and it is set at resource creation by the
152 * resource itself.
153 * * When a scratch resource is ref'ed it will not be returned from the
154 * cache for a subsequent cache request until all refs are released. This facilitates using
155 * a scratch key for multiple render-to-texture scenarios. An example is a separable blur:
156 *
157 * GrTexture* texture[2];
158 * texture[0] = get_scratch_texture(scratchKey);
159 * texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a
160 * // different one for texture[1]
161 * draw_mask(texture[0], path); // draws path mask to texture[0]
162 * blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1]
163 * blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0]
164 * texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey
165 * consume_blur(texture[0]);
166 * texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey
bsalomon24db3b12015-01-23 04:24:04 -0800167 */
168class GrScratchKey : public GrResourceKey {
bsalomon744998e2014-08-28 09:54:34 -0700169private:
bsalomon24db3b12015-01-23 04:24:04 -0800170 typedef GrResourceKey INHERITED;
bsalomon744998e2014-08-28 09:54:34 -0700171
bsalomon24db3b12015-01-23 04:24:04 -0800172public:
173 /** Uniquely identifies the type of resource that is cached as scratch. */
174 typedef uint32_t ResourceType;
bsalomon744998e2014-08-28 09:54:34 -0700175
bsalomon24db3b12015-01-23 04:24:04 -0800176 /** Generate a unique ResourceType. */
177 static ResourceType GenerateResourceType();
178
179 /** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
180 GrScratchKey() {}
181
182 GrScratchKey(const GrScratchKey& that) { *this = that; }
183
184 /** reset() returns the key to the invalid state. */
185 using INHERITED::reset;
186
187 using INHERITED::isValid;
188
189 ResourceType resourceType() const { return this->domain(); }
190
191 GrScratchKey& operator=(const GrScratchKey& that) {
192 this->INHERITED::operator=(that);
193 return *this;
bsalomon744998e2014-08-28 09:54:34 -0700194 }
bsalomon24db3b12015-01-23 04:24:04 -0800195
196 bool operator==(const GrScratchKey& that) const {
197 return this->INHERITED::operator==(that);
198 }
199 bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
200
201 class Builder : public INHERITED::Builder {
202 public:
203 Builder(GrScratchKey* key, ResourceType type, int data32Count)
204 : INHERITED::Builder(key, type, data32Count) {}
205 };
206};
207
208/**
bsalomon8718aaf2015-02-19 07:24:21 -0800209 * A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three
210 * rules governing the use of unique keys:
211 * * Only one resource can have a given unique key at a time. Hence, "unique".
212 * * A resource can have at most one unique key at a time.
213 * * Unlike scratch keys, multiple requests for a unique key will return the same
214 * resource even if the resource already has refs.
215 * This key type allows a code path to create cached resources for which it is the exclusive user.
216 * The code path creates a domain which it sets on its keys. This guarantees that there are no
217 * cross-domain collisions.
218 *
219 * Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its
220 * scratch key. It can become scratch again if the unique key is removed.
bsalomon24db3b12015-01-23 04:24:04 -0800221 */
bsalomon8718aaf2015-02-19 07:24:21 -0800222class GrUniqueKey : public GrResourceKey {
bsalomon24db3b12015-01-23 04:24:04 -0800223private:
224 typedef GrResourceKey INHERITED;
225
226public:
227 typedef uint32_t Domain;
bsalomon8718aaf2015-02-19 07:24:21 -0800228 /** Generate a Domain for unique keys. */
bsalomon24db3b12015-01-23 04:24:04 -0800229 static Domain GenerateDomain();
230
bsalomon8718aaf2015-02-19 07:24:21 -0800231 /** Creates an invalid unique key. It must be initialized using a Builder object before use. */
232 GrUniqueKey() {}
bsalomon24db3b12015-01-23 04:24:04 -0800233
bsalomon8718aaf2015-02-19 07:24:21 -0800234 GrUniqueKey(const GrUniqueKey& that) { *this = that; }
bsalomon24db3b12015-01-23 04:24:04 -0800235
236 /** reset() returns the key to the invalid state. */
237 using INHERITED::reset;
238
239 using INHERITED::isValid;
240
bsalomon8718aaf2015-02-19 07:24:21 -0800241 GrUniqueKey& operator=(const GrUniqueKey& that) {
bsalomon24db3b12015-01-23 04:24:04 -0800242 this->INHERITED::operator=(that);
bungeman38d909e2016-08-02 14:40:46 -0700243 this->setCustomData(sk_ref_sp(that.getCustomData()));
Brian Salomon1090da62017-01-06 12:04:19 -0500244 SkDEBUGCODE(fTag = that.fTag;)
bsalomon24db3b12015-01-23 04:24:04 -0800245 return *this;
246 }
247
bsalomon8718aaf2015-02-19 07:24:21 -0800248 bool operator==(const GrUniqueKey& that) const {
bsalomon24db3b12015-01-23 04:24:04 -0800249 return this->INHERITED::operator==(that);
250 }
bsalomon8718aaf2015-02-19 07:24:21 -0800251 bool operator!=(const GrUniqueKey& that) const { return !(*this == that); }
bsalomon24db3b12015-01-23 04:24:04 -0800252
bungeman38d909e2016-08-02 14:40:46 -0700253 void setCustomData(sk_sp<SkData> data) {
254 fData = std::move(data);
senorblanco84cd6212015-08-04 10:01:58 -0700255 }
bungeman38d909e2016-08-02 14:40:46 -0700256 SkData* getCustomData() const {
senorblanco84cd6212015-08-04 10:01:58 -0700257 return fData.get();
258 }
259
Brian Salomon1090da62017-01-06 12:04:19 -0500260 SkDEBUGCODE(const char* tag() const { return fTag.c_str(); })
261
bsalomon24db3b12015-01-23 04:24:04 -0800262 class Builder : public INHERITED::Builder {
263 public:
Brian Salomon1090da62017-01-06 12:04:19 -0500264 Builder(GrUniqueKey* key, Domain type, int data32Count, const char* tag = nullptr)
265 : INHERITED::Builder(key, type, data32Count) {
266 SkDEBUGCODE(key->fTag = tag;)
267 (void) tag; // suppress unused named param warning.
268 }
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());
Brian Salomon1090da62017-01-06 12:04:19 -0500280 SkDEBUGCODE(key->fTag = tag;)
281 (void) tag; // suppress unused named param warning.
bsalomon3bd12ef2015-01-28 11:39:48 -0800282 }
283
284 private:
bsalomon8718aaf2015-02-19 07:24:21 -0800285 static int Data32CntForInnerKey(const GrUniqueKey& innerKey) {
bsalomon3bd12ef2015-01-28 11:39:48 -0800286 // key data + domain
287 return SkToInt((innerKey.dataSize() >> 2) + 1);
bsalomon24db3b12015-01-23 04:24:04 -0800288 }
289 };
senorblanco84cd6212015-08-04 10:01:58 -0700290
291private:
bungeman38d909e2016-08-02 14:40:46 -0700292 sk_sp<SkData> fData;
Brian Salomon1090da62017-01-06 12:04:19 -0500293 SkDEBUGCODE(SkString fTag;)
bsalomon744998e2014-08-28 09:54:34 -0700294};
295
bsalomoned0bcad2015-05-04 10:36:42 -0700296/**
297 * It is common to need a frequently reused GrUniqueKey where the only requirement is that the key
298 * is unique. These macros create such a key in a thread safe manner so the key can be truly global
299 * and only constructed once.
300 */
301
302/** Place outside of function/class definitions. */
mtkleind9dd4282016-04-18 08:09:11 -0700303#define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once
bsalomoned0bcad2015-05-04 10:36:42 -0700304
305/** Place inside function where the key is used. */
bsalomonf0795ab2015-12-17 08:15:47 -0800306#define GR_DEFINE_STATIC_UNIQUE_KEY(name) \
307 static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \
mtkleind9dd4282016-04-18 08:09:11 -0700308 name##_once(gr_init_static_unique_key_once, &name##_storage); \
bsalomonf0795ab2015-12-17 08:15:47 -0800309 static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get());
bsalomoned0bcad2015-05-04 10:36:42 -0700310
bsalomonf0795ab2015-12-17 08:15:47 -0800311static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1,GrUniqueKey>* keyStorage) {
312 GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey;
bsalomoned0bcad2015-05-04 10:36:42 -0700313 GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0);
314}
315
bsalomon23e619c2015-02-06 11:54:28 -0800316// The cache listens for these messages to purge junk resources proactively.
bsalomon8718aaf2015-02-19 07:24:21 -0800317class GrUniqueKeyInvalidatedMessage {
bsalomon23e619c2015-02-06 11:54:28 -0800318public:
bsalomon8718aaf2015-02-19 07:24:21 -0800319 explicit GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key) : fKey(key) {}
320
321 GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage& that) : fKey(that.fKey) {}
322
323 GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage& that) {
bsalomon23e619c2015-02-06 11:54:28 -0800324 fKey = that.fKey;
325 return *this;
326 }
bsalomon8718aaf2015-02-19 07:24:21 -0800327
328 const GrUniqueKey& key() const { return fKey; }
329
bsalomon23e619c2015-02-06 11:54:28 -0800330private:
bsalomon8718aaf2015-02-19 07:24:21 -0800331 GrUniqueKey fKey;
bsalomon23e619c2015-02-06 11:54:28 -0800332};
bsalomon744998e2014-08-28 09:54:34 -0700333#endif