| 1 |
/* |
| 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 skgpu_ResourceKey_DEFINED |
| 9 |
#define skgpu_ResourceKey_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkAlign.h" |
| 15 |
#include "include/private/base/SkAlignedStorage.h" |
| 16 |
#include "include/private/base/SkDebug.h" |
| 17 |
#include "include/private/base/SkTemplates.h" |
| 18 |
#include "include/private/base/SkTo.h" |
| 19 |
|
| 20 |
#include <cstdint> |
| 21 |
#include <cstring> |
| 22 |
#include <limits> |
| 23 |
#include <new> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
class TestResource; |
| 27 |
|
| 28 |
namespace skgpu { |
| 29 |
|
| 30 |
uint32_t ResourceKeyHash(const uint32_t* data, size_t size); |
| 31 |
|
| 32 |
/** |
| 33 |
* Base class for all gpu Resource cache keys. There are two types of cache keys. Refer to the |
| 34 |
* comments for each key type below. |
| 35 |
*/ |
| 36 |
class ResourceKey { |
| 37 |
public: |
| 38 |
uint32_t hash() const { |
| 39 |
this->validate(); |
| 40 |
return fKey[kHash_MetaDataIdx]; |
| 41 |
} |
| 42 |
|
| 43 |
size_t size() const { |
| 44 |
this->validate(); |
| 45 |
SkASSERT(this->isValid()); |
| 46 |
return this->internalSize(); |
| 47 |
} |
| 48 |
|
| 49 |
/** Reset to an invalid key. */ |
| 50 |
void reset() { |
| 51 |
fKey.reset(kMetaDataCnt); |
| 52 |
fKey[kHash_MetaDataIdx] = 0; |
| 53 |
fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain; |
| 54 |
} |
| 55 |
|
| 56 |
bool isValid() const { return kInvalidDomain != this->domain(); } |
| 57 |
|
| 58 |
/** Used to initialize a key. */ |
| 59 |
class Builder { |
| 60 |
public: |
| 61 |
~Builder() { this->finish(); } |
| 62 |
|
| 63 |
void finish() { |
| 64 |
if (nullptr == fKey) { |
| 65 |
return; |
| 66 |
} |
| 67 |
uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx]; |
| 68 |
*hash = ResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t)); |
| 69 |
fKey->validate(); |
| 70 |
fKey = nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
uint32_t& operator[](int dataIdx) { |
| 74 |
SkASSERT(fKey); |
| 75 |
SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;) |
| 76 |
SkASSERT(SkToU32(dataIdx) < dataCount); |
| 77 |
return fKey->fKey[(int)kMetaDataCnt + dataIdx]; |
| 78 |
} |
| 79 |
|
| 80 |
protected: |
| 81 |
Builder(ResourceKey* key, uint16_t domain, uint16_t data32Count) : fKey(key) { |
| 82 |
SkASSERT(domain != kInvalidDomain); |
| 83 |
key->fKey.reset(kMetaDataCnt + data32Count); |
| 84 |
key->fKey[kDomainAndSize_MetaDataIdx] = domain | (data32Count << 16); |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
ResourceKey* fKey; |
| 89 |
}; |
| 90 |
|
| 91 |
protected: |
| 92 |
static const uint16_t kInvalidDomain = 0; |
| 93 |
|
| 94 |
ResourceKey() { this->reset(); } |
| 95 |
|
| 96 |
bool operator==(const ResourceKey& that) const { |
| 97 |
// Both keys should be sized to at least contain the meta data. The metadata contains each |
| 98 |
// key's length. So the second memcmp should only run if the keys have the same length. |
| 99 |
return 0 == memcmp(fKey.get(), that.fKey.get(), kMetaDataCnt*sizeof(uint32_t)) && |
| 100 |
0 == memcmp(&fKey[kMetaDataCnt], &that.fKey[kMetaDataCnt], this->dataSize()); |
| 101 |
} |
| 102 |
|
| 103 |
ResourceKey& operator=(const ResourceKey& that) { |
| 104 |
if (this != &that) { |
| 105 |
if (!that.isValid()) { |
| 106 |
this->reset(); |
| 107 |
} else { |
| 108 |
size_t bytes = that.size(); |
| 109 |
SkASSERT(SkIsAlign4(bytes)); |
| 110 |
fKey.reset(bytes / sizeof(uint32_t)); |
| 111 |
memcpy(fKey.get(), that.fKey.get(), bytes); |
| 112 |
this->validate(); |
| 113 |
} |
| 114 |
} |
| 115 |
return *this; |
| 116 |
} |
| 117 |
|
| 118 |
uint16_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; } |
| 119 |
|
| 120 |
/** size of the key data, excluding meta-data (hash, domain, etc). */ |
| 121 |
size_t dataSize() const { return (fKey[kDomainAndSize_MetaDataIdx] >> 16) * sizeof(uint32_t); } |
| 122 |
|
| 123 |
/** ptr to the key data, excluding meta-data (hash, domain, etc). */ |
| 124 |
const uint32_t* data() const { |
| 125 |
this->validate(); |
| 126 |
return &fKey[kMetaDataCnt]; |
| 127 |
} |
| 128 |
|
| 129 |
#ifdef SK_DEBUG |
| 130 |
void dump() const { |
| 131 |
if (!this->isValid()) { |
| 132 |
SkDebugf("Invalid Key\n"); |
| 133 |
} else { |
| 134 |
SkDebugf("hash: %u ", this->hash()); |
| 135 |
SkDebugf("domain: %u ", this->domain()); |
| 136 |
SkDebugf("size: %zuB ", this->internalSize()); |
| 137 |
size_t dataCount = this->internalSize() / sizeof(uint32_t) - kMetaDataCnt; |
| 138 |
for (size_t i = 0; i < dataCount; ++i) { |
| 139 |
SkDebugf("%u ", fKey[SkTo<int>(kMetaDataCnt+i)]); |
| 140 |
} |
| 141 |
SkDebugf("\n"); |
| 142 |
} |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
private: |
| 147 |
enum MetaDataIdx { |
| 148 |
kHash_MetaDataIdx, |
| 149 |
// The key domain and size are packed into a single uint32_t. The stored size is in units |
| 150 |
// of uint32_t and does not include the metadata, i.e. it stores the data32Count provided |
| 151 |
// to the original key builder. |
| 152 |
kDomainAndSize_MetaDataIdx, |
| 153 |
|
| 154 |
kLastMetaDataIdx = kDomainAndSize_MetaDataIdx |
| 155 |
}; |
| 156 |
static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 157 |
|
| 158 |
// Total size in bytes, including metadata |
| 159 |
size_t internalSize() const { return this->dataSize() + sizeof(uint32_t) * kMetaDataCnt; } |
| 160 |
|
| 161 |
void validate() const { |
| 162 |
SkASSERT(this->isValid()); |
| 163 |
SkASSERT(fKey[kHash_MetaDataIdx] == |
| 164 |
ResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1, |
| 165 |
this->internalSize() - sizeof(uint32_t))); |
| 166 |
SkASSERT(SkIsAlign4(this->internalSize())); |
| 167 |
} |
| 168 |
|
| 169 |
friend class ::TestResource; // For unit test to access kMetaDataCnt. |
| 170 |
|
| 171 |
// For Ganesh, bmp textures require 5 uint32_t values. Graphite requires 6 (due to |
| 172 |
// storing mipmap status as part of the key). |
| 173 |
skia_private::AutoSTMalloc<kMetaDataCnt + 6, uint32_t> fKey; |
| 174 |
}; |
| 175 |
|
| 176 |
/** |
| 177 |
* A key used for scratch resources. There are three important rules about scratch keys: |
| 178 |
* * Multiple resources can share the same scratch key. Therefore resources assigned the same |
| 179 |
* scratch key should be interchangeable with respect to the code that uses them. |
| 180 |
* * A resource can have at most one scratch key and it is set at resource creation by the |
| 181 |
* resource itself. |
| 182 |
* * When a scratch resource is ref'ed it will not be returned from the |
| 183 |
* cache for a subsequent cache request until all refs are released. This facilitates using |
| 184 |
* a scratch key for multiple render-to-texture scenarios. An example is a separable blur: |
| 185 |
* |
| 186 |
* GrTexture* texture[2]; |
| 187 |
* texture[0] = get_scratch_texture(scratchKey); |
| 188 |
* texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a |
| 189 |
* // different one for texture[1] |
| 190 |
* draw_mask(texture[0], path); // draws path mask to texture[0] |
| 191 |
* blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1] |
| 192 |
* blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0] |
| 193 |
* texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey |
| 194 |
* consume_blur(texture[0]); |
| 195 |
* texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey |
| 196 |
*/ |
| 197 |
class ScratchKey : public ResourceKey { |
| 198 |
public: |
| 199 |
/** Uniquely identifies the type of resource that is cached as scratch. */ |
| 200 |
typedef uint16_t ResourceType; |
| 201 |
|
| 202 |
/** Generate a unique ResourceType. */ |
| 203 |
static ResourceType GenerateResourceType(); |
| 204 |
|
| 205 |
/** Creates an invalid scratch key. It must be initialized using a Builder object before use. */ |
| 206 |
ScratchKey() {} |
| 207 |
|
| 208 |
ScratchKey(const ScratchKey& that) { *this = that; } |
| 209 |
|
| 210 |
ResourceType resourceType() const { return this->domain(); } |
| 211 |
|
| 212 |
ScratchKey& operator=(const ScratchKey& that) { |
| 213 |
this->ResourceKey::operator=(that); |
| 214 |
return *this; |
| 215 |
} |
| 216 |
|
| 217 |
bool operator==(const ScratchKey& that) const { return this->ResourceKey::operator==(that); } |
| 218 |
bool operator!=(const ScratchKey& that) const { return !(*this == that); } |
| 219 |
|
| 220 |
class Builder : public ResourceKey::Builder { |
| 221 |
public: |
| 222 |
Builder(ScratchKey* key, ResourceType type, uint16_t data32Count) |
| 223 |
: ResourceKey::Builder(key, type, data32Count) {} |
| 224 |
}; |
| 225 |
}; |
| 226 |
|
| 227 |
/** |
| 228 |
* A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three |
| 229 |
* rules governing the use of unique keys: |
| 230 |
* * Only one resource can have a given unique key at a time. Hence, "unique". |
| 231 |
* * A resource can have at most one unique key at a time. |
| 232 |
* * Unlike scratch keys, multiple requests for a unique key will return the same |
| 233 |
* resource even if the resource already has refs. |
| 234 |
* This key type allows a code path to create cached resources for which it is the exclusive user. |
| 235 |
* The code path creates a domain which it sets on its keys. This guarantees that there are no |
| 236 |
* cross-domain collisions. |
| 237 |
* |
| 238 |
* Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its |
| 239 |
* scratch key. It can become scratch again if the unique key is removed. |
| 240 |
*/ |
| 241 |
class UniqueKey : public ResourceKey { |
| 242 |
public: |
| 243 |
typedef uint16_t Domain; |
| 244 |
/** Generate a Domain for unique keys. */ |
| 245 |
static Domain GenerateDomain(); |
| 246 |
|
| 247 |
/** Creates an invalid unique key. It must be initialized using a Builder object before use. */ |
| 248 |
UniqueKey() : fTag(nullptr) {} |
| 249 |
|
| 250 |
UniqueKey(const UniqueKey& that) { *this = that; } |
| 251 |
|
| 252 |
UniqueKey& operator=(const UniqueKey& that) { |
| 253 |
this->ResourceKey::operator=(that); |
| 254 |
this->setCustomData(sk_ref_sp(that.getCustomData())); |
| 255 |
fTag = that.fTag; |
| 256 |
return *this; |
| 257 |
} |
| 258 |
|
| 259 |
bool operator==(const UniqueKey& that) const { return this->ResourceKey::operator==(that); } |
| 260 |
bool operator!=(const UniqueKey& that) const { return !(*this == that); } |
| 261 |
|
| 262 |
void setCustomData(sk_sp<SkData> data) { fData = std::move(data); } |
| 263 |
SkData* getCustomData() const { return fData.get(); } |
| 264 |
sk_sp<SkData> refCustomData() const { return fData; } |
| 265 |
|
| 266 |
const char* tag() const { return fTag; } |
| 267 |
|
| 268 |
const uint32_t* data() const { return this->ResourceKey::data(); } |
| 269 |
|
| 270 |
#ifdef SK_DEBUG |
| 271 |
uint32_t domain() const { return this->ResourceKey::domain(); } |
| 272 |
size_t dataSize() const { return this->ResourceKey::dataSize(); } |
| 273 |
|
| 274 |
void dump(const char* label) const { |
| 275 |
SkDebugf("%s tag: %s\n", label, fTag ? fTag : "None"); |
| 276 |
this->ResourceKey::dump(); |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
class Builder : public ResourceKey::Builder { |
| 281 |
public: |
| 282 |
Builder(UniqueKey* key, Domain type, uint16_t data32Count, const char* tag = nullptr) |
| 283 |
: ResourceKey::Builder(key, type, data32Count) { |
| 284 |
key->fTag = tag; |
| 285 |
} |
| 286 |
|
| 287 |
/** Used to build a key that wraps another key and adds additional data. */ |
| 288 |
Builder(UniqueKey* key, const UniqueKey& innerKey, Domain domain, uint16_t extraData32Cnt, |
| 289 |
const char* tag = nullptr) |
| 290 |
: ResourceKey::Builder(key, |
| 291 |
domain, |
| 292 |
Data32CntForInnerKey(innerKey, extraData32Cnt)) { |
| 293 |
SkASSERT(&innerKey != key); |
| 294 |
// add the inner key to the end of the key so that op[] can be indexed normally. |
| 295 |
uint32_t* innerKeyData = &this->operator[](extraData32Cnt); |
| 296 |
const uint32_t* srcData = innerKey.data(); |
| 297 |
(*innerKeyData++) = innerKey.domain(); |
| 298 |
memcpy(innerKeyData, srcData, innerKey.dataSize()); |
| 299 |
key->fTag = tag; |
| 300 |
} |
| 301 |
|
| 302 |
private: |
| 303 |
static uint16_t Data32CntForInnerKey(const UniqueKey& innerKey, uint16_t extraData32Cnt) { |
| 304 |
// key data + domain + extraData32Cnt needs to fit into a uint16_t. This key builder is |
| 305 |
// only used in Ganesh for wrapping textures |
| 306 |
uint16_t innerData32Cnt = innerKey.dataSize() >> 2; |
| 307 |
// The Builder API doesn't have a way to return a failure, so if this is somehow |
| 308 |
// exceeded, then we have no way to recover. |
| 309 |
SkASSERT_RELEASE((uint32_t) extraData32Cnt + (uint32_t) innerData32Cnt + 1 <= |
| 310 |
(uint32_t) std::numeric_limits<uint16_t>::max()); |
| 311 |
return innerData32Cnt + extraData32Cnt + 1; |
| 312 |
} |
| 313 |
}; |
| 314 |
|
| 315 |
private: |
| 316 |
sk_sp<SkData> fData; |
| 317 |
const char* fTag; |
| 318 |
}; |
| 319 |
|
| 320 |
/** |
| 321 |
* It is common to need a frequently reused UniqueKey where the only requirement is that the key |
| 322 |
* is unique. These macros create such a key in a thread safe manner so the key can be truly global |
| 323 |
* and only constructed once. |
| 324 |
*/ |
| 325 |
|
| 326 |
/** Place outside of function/class definitions. */ |
| 327 |
#define SKGPU_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once |
| 328 |
|
| 329 |
/** Place inside function where the key is used. */ |
| 330 |
#define SKGPU_DEFINE_STATIC_UNIQUE_KEY(name) \ |
| 331 |
static SkAlignedSTStorage<1, skgpu::UniqueKey> name##_storage; \ |
| 332 |
name##_once(skgpu::skgpu_init_static_unique_key_once, &name##_storage); \ |
| 333 |
static const skgpu::UniqueKey& name = \ |
| 334 |
*reinterpret_cast<skgpu::UniqueKey*>(name##_storage.get()) |
| 335 |
|
| 336 |
static inline void skgpu_init_static_unique_key_once(SkAlignedSTStorage<1, UniqueKey>* keyStorage) { |
| 337 |
UniqueKey* key = new (keyStorage->get()) UniqueKey; |
| 338 |
UniqueKey::Builder builder(key, UniqueKey::GenerateDomain(), 0); |
| 339 |
} |
| 340 |
|
| 341 |
// The cache listens for these messages to purge junk resources proactively. |
| 342 |
class UniqueKeyInvalidatedMessage { |
| 343 |
public: |
| 344 |
UniqueKeyInvalidatedMessage() = default; |
| 345 |
UniqueKeyInvalidatedMessage(const UniqueKey& key, |
| 346 |
uint32_t contextUniqueID, |
| 347 |
bool inThreadSafeCache = false) |
| 348 |
: fKey(key), fContextID(contextUniqueID), fInThreadSafeCache(inThreadSafeCache) { |
| 349 |
SkASSERT(SK_InvalidUniqueID != contextUniqueID); |
| 350 |
} |
| 351 |
|
| 352 |
UniqueKeyInvalidatedMessage(const UniqueKeyInvalidatedMessage&) = default; |
| 353 |
|
| 354 |
UniqueKeyInvalidatedMessage& operator=(const UniqueKeyInvalidatedMessage&) = default; |
| 355 |
|
| 356 |
const UniqueKey& key() const { return fKey; } |
| 357 |
uint32_t contextID() const { return fContextID; } |
| 358 |
bool inThreadSafeCache() const { return fInThreadSafeCache; } |
| 359 |
|
| 360 |
private: |
| 361 |
UniqueKey fKey; |
| 362 |
uint32_t fContextID = SK_InvalidUniqueID; |
| 363 |
bool fInThreadSafeCache = false; |
| 364 |
}; |
| 365 |
|
| 366 |
static inline bool SkShouldPostMessageToBus(const UniqueKeyInvalidatedMessage& msg, |
| 367 |
uint32_t msgBusUniqueID) { |
| 368 |
return msg.contextID() == msgBusUniqueID; |
| 369 |
} |
| 370 |
|
| 371 |
class UniqueKeyInvalidatedMsg_Graphite { |
| 372 |
public: |
| 373 |
UniqueKeyInvalidatedMsg_Graphite() = default; |
| 374 |
UniqueKeyInvalidatedMsg_Graphite(const UniqueKey& key, uint32_t recorderID) |
| 375 |
: fKey(key), fRecorderID(recorderID) { |
| 376 |
SkASSERT(SK_InvalidUniqueID != fRecorderID); |
| 377 |
} |
| 378 |
|
| 379 |
UniqueKeyInvalidatedMsg_Graphite(const UniqueKeyInvalidatedMsg_Graphite&) = default; |
| 380 |
|
| 381 |
UniqueKeyInvalidatedMsg_Graphite& operator=(const UniqueKeyInvalidatedMsg_Graphite&) = default; |
| 382 |
|
| 383 |
const UniqueKey& key() const { return fKey; } |
| 384 |
uint32_t recorderID() const { return fRecorderID; } |
| 385 |
|
| 386 |
private: |
| 387 |
UniqueKey fKey; |
| 388 |
uint32_t fRecorderID = SK_InvalidUniqueID; |
| 389 |
}; |
| 390 |
|
| 391 |
static inline bool SkShouldPostMessageToBus(const UniqueKeyInvalidatedMsg_Graphite& msg, |
| 392 |
uint32_t msgBusUniqueID) { |
| 393 |
return msg.recorderID() == msgBusUniqueID; |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* This is a special key that doesn't have domain and can only be used in a dedicated cache. |
| 398 |
* Unlike UniqueKey & ScratchKey, this key has compile time size (in number of uint32_t) |
| 399 |
* and doesn't need dynamic allocations. In comparison, UniqueKey & ScratchKey will need |
| 400 |
* dynamic allocation if a key is larger than 6 uint32_ts. |
| 401 |
*/ |
| 402 |
template <size_t SizeInUInt32> |
| 403 |
class FixedSizeKey { |
| 404 |
public: |
| 405 |
uint32_t hash() const { return fHash; } |
| 406 |
|
| 407 |
bool operator==(const FixedSizeKey& that) const { |
| 408 |
return fHash == that.fHash && |
| 409 |
0 == memcmp(fPackedData, that.fPackedData, sizeof(fPackedData)); |
| 410 |
} |
| 411 |
|
| 412 |
class Builder { |
| 413 |
public: |
| 414 |
Builder(FixedSizeKey* key) : fKey(key) {} |
| 415 |
|
| 416 |
void finish() { |
| 417 |
SkASSERT(fKey); |
| 418 |
fKey->fHash = ResourceKeyHash(fKey->fPackedData, sizeof(fKey->fPackedData)); |
| 419 |
fKey = nullptr; |
| 420 |
} |
| 421 |
|
| 422 |
uint32_t& operator[](int dataIdx) { |
| 423 |
SkASSERT(fKey); |
| 424 |
SkASSERT(SkToU32(dataIdx) < SizeInUInt32); |
| 425 |
return fKey->fPackedData[dataIdx]; |
| 426 |
} |
| 427 |
|
| 428 |
private: |
| 429 |
FixedSizeKey* fKey = nullptr; |
| 430 |
}; |
| 431 |
|
| 432 |
struct Hash { |
| 433 |
uint32_t operator()(const FixedSizeKey& key) const { return key.hash(); } |
| 434 |
}; |
| 435 |
|
| 436 |
private: |
| 437 |
uint32_t fHash = 0; |
| 438 |
uint32_t fPackedData[SizeInUInt32] = {}; |
| 439 |
}; |
| 440 |
|
| 441 |
} // namespace skgpu |
| 442 |
|
| 443 |
#endif // skgpu_ResourceKey_DEFINED |