2014-08-28 16:54:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrResourceKey_DEFINED
|
|
|
|
#define GrResourceKey_DEFINED
|
|
|
|
|
2017-01-06 17:04:19 +00:00
|
|
|
#include "../private/SkOnce.h"
|
2015-08-19 18:56:48 +00:00
|
|
|
#include "../private/SkTemplates.h"
|
2014-08-28 16:54:34 +00:00
|
|
|
#include "GrTypes.h"
|
2015-08-04 17:01:58 +00:00
|
|
|
#include "SkData.h"
|
2017-01-06 17:04:19 +00:00
|
|
|
#include "SkString.h"
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
uint32_t GrResourceKeyHash(const uint32_t* data, size_t size);
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
/**
|
|
|
|
* Base class for all GrGpuResource cache keys. There are two types of cache keys. Refer to the
|
|
|
|
* comments for each key type below.
|
|
|
|
*/
|
2015-01-23 12:24:04 +00:00
|
|
|
class GrResourceKey {
|
2014-08-28 16:54:34 +00:00
|
|
|
public:
|
2015-01-23 12:24:04 +00:00
|
|
|
uint32_t hash() const {
|
|
|
|
this->validate();
|
|
|
|
return fKey[kHash_MetaDataIdx];
|
|
|
|
}
|
2014-12-30 20:50:52 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
size_t size() const {
|
|
|
|
this->validate();
|
2015-01-28 19:39:48 +00:00
|
|
|
SkASSERT(this->isValid());
|
2015-01-23 12:24:04 +00:00
|
|
|
return this->internalSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static const uint32_t kInvalidDomain = 0;
|
|
|
|
|
|
|
|
GrResourceKey() { this->reset(); }
|
2014-12-30 20:50:52 +00:00
|
|
|
|
|
|
|
/** Reset to an invalid key. */
|
|
|
|
void reset() {
|
2015-01-23 12:24:04 +00:00
|
|
|
GR_STATIC_ASSERT((uint16_t)kInvalidDomain == kInvalidDomain);
|
2014-12-30 20:50:52 +00:00
|
|
|
fKey.reset(kMetaDataCnt);
|
|
|
|
fKey[kHash_MetaDataIdx] = 0;
|
2015-01-23 12:24:04 +00:00
|
|
|
fKey[kDomainAndSize_MetaDataIdx] = kInvalidDomain;
|
2014-12-30 20:50:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
bool operator==(const GrResourceKey& that) const {
|
2015-05-19 05:47:33 +00:00
|
|
|
return this->hash() == that.hash() &&
|
|
|
|
0 == memcmp(&fKey[kHash_MetaDataIdx + 1],
|
|
|
|
&that.fKey[kHash_MetaDataIdx + 1],
|
|
|
|
this->internalSize() - sizeof(uint32_t));
|
2015-01-23 12:24:04 +00:00
|
|
|
}
|
2014-12-30 20:50:52 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
GrResourceKey& operator=(const GrResourceKey& that) {
|
2015-02-06 19:54:28 +00:00
|
|
|
SkASSERT(that.isValid());
|
2015-01-23 15:17:55 +00:00
|
|
|
if (this != &that) {
|
|
|
|
size_t bytes = that.size();
|
|
|
|
SkASSERT(SkIsAlign4(bytes));
|
|
|
|
fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
|
|
|
|
memcpy(fKey.get(), that.fKey.get(), bytes);
|
2015-02-06 19:54:28 +00:00
|
|
|
this->validate();
|
2015-01-23 15:17:55 +00:00
|
|
|
}
|
2014-12-30 20:50:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
bool isValid() const { return kInvalidDomain != this->domain(); }
|
2014-12-30 20:50:52 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
uint32_t domain() const { return fKey[kDomainAndSize_MetaDataIdx] & 0xffff; }
|
|
|
|
|
2015-01-28 19:39:48 +00:00
|
|
|
/** size of the key data, excluding meta-data (hash, domain, etc). */
|
|
|
|
size_t dataSize() const { return this->size() - 4 * kMetaDataCnt; }
|
2016-04-18 15:09:11 +00:00
|
|
|
|
2015-01-28 19:39:48 +00:00
|
|
|
/** ptr to the key data, excluding meta-data (hash, domain, etc). */
|
|
|
|
const uint32_t* data() const {
|
|
|
|
this->validate();
|
|
|
|
return &fKey[kMetaDataCnt];
|
|
|
|
}
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/** Used to initialize a key. */
|
2014-12-30 20:50:52 +00:00
|
|
|
class Builder {
|
|
|
|
public:
|
2015-01-23 12:24:04 +00:00
|
|
|
Builder(GrResourceKey* key, uint32_t domain, int data32Count) : fKey(key) {
|
2014-12-30 20:50:52 +00:00
|
|
|
SkASSERT(data32Count >= 0);
|
2015-01-23 12:24:04 +00:00
|
|
|
SkASSERT(domain != kInvalidDomain);
|
2014-12-30 20:50:52 +00:00
|
|
|
key->fKey.reset(kMetaDataCnt + data32Count);
|
|
|
|
int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t);
|
2015-01-23 12:24:04 +00:00
|
|
|
SkASSERT(SkToU16(size) == size);
|
|
|
|
SkASSERT(SkToU16(domain) == domain);
|
|
|
|
key->fKey[kDomainAndSize_MetaDataIdx] = domain | (size << 16);
|
2014-12-30 20:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Builder() { this->finish(); }
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
void finish() {
|
2017-08-28 14:34:05 +00:00
|
|
|
if (nullptr == fKey) {
|
2015-01-23 12:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GR_STATIC_ASSERT(0 == kHash_MetaDataIdx);
|
|
|
|
uint32_t* hash = &fKey->fKey[kHash_MetaDataIdx];
|
|
|
|
*hash = GrResourceKeyHash(hash + 1, fKey->internalSize() - sizeof(uint32_t));
|
|
|
|
fKey->validate();
|
2017-08-28 14:34:05 +00:00
|
|
|
fKey = nullptr;
|
2015-01-23 12:24:04 +00:00
|
|
|
}
|
2014-12-30 20:50:52 +00:00
|
|
|
|
|
|
|
uint32_t& operator[](int dataIdx) {
|
|
|
|
SkASSERT(fKey);
|
2015-01-23 12:24:04 +00:00
|
|
|
SkDEBUGCODE(size_t dataCount = fKey->internalSize() / sizeof(uint32_t) - kMetaDataCnt;)
|
2014-12-30 20:50:52 +00:00
|
|
|
SkASSERT(SkToU32(dataIdx) < dataCount);
|
|
|
|
return fKey->fKey[kMetaDataCnt + dataIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-23 12:24:04 +00:00
|
|
|
GrResourceKey* fKey;
|
2014-12-30 20:50:52 +00:00
|
|
|
};
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2014-12-30 20:50:52 +00:00
|
|
|
private:
|
2015-01-28 19:39:48 +00:00
|
|
|
enum MetaDataIdx {
|
|
|
|
kHash_MetaDataIdx,
|
|
|
|
// The key domain and size are packed into a single uint32_t.
|
|
|
|
kDomainAndSize_MetaDataIdx,
|
|
|
|
|
|
|
|
kLastMetaDataIdx = kDomainAndSize_MetaDataIdx
|
|
|
|
};
|
|
|
|
static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1;
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
size_t internalSize() const {
|
|
|
|
return fKey[kDomainAndSize_MetaDataIdx] >> 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
void validate() const {
|
|
|
|
SkASSERT(fKey[kHash_MetaDataIdx] ==
|
|
|
|
GrResourceKeyHash(&fKey[kHash_MetaDataIdx] + 1,
|
|
|
|
this->internalSize() - sizeof(uint32_t)));
|
2015-01-28 19:39:48 +00:00
|
|
|
SkASSERT(SkIsAlign4(this->internalSize()));
|
2015-01-23 12:24:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 17:32:40 +00:00
|
|
|
friend class TestResource; // For unit test to access kMetaDataCnt.
|
|
|
|
|
2017-03-10 00:52:32 +00:00
|
|
|
// bmp textures require 7 uint32_t values (5 for the base key, and two more for image
|
|
|
|
// cacherator's decode format.
|
|
|
|
SkAutoSTMalloc<kMetaDataCnt + 7, uint32_t> fKey;
|
2014-12-30 20:50:52 +00:00
|
|
|
};
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/**
|
2015-02-19 15:24:21 +00:00
|
|
|
* A key used for scratch resources. There are three important rules about scratch keys:
|
|
|
|
* * Multiple resources can share the same scratch key. Therefore resources assigned the same
|
|
|
|
* scratch key should be interchangeable with respect to the code that uses them.
|
|
|
|
* * A resource can have at most one scratch key and it is set at resource creation by the
|
|
|
|
* resource itself.
|
|
|
|
* * When a scratch resource is ref'ed it will not be returned from the
|
|
|
|
* cache for a subsequent cache request until all refs are released. This facilitates using
|
|
|
|
* a scratch key for multiple render-to-texture scenarios. An example is a separable blur:
|
|
|
|
*
|
|
|
|
* GrTexture* texture[2];
|
|
|
|
* texture[0] = get_scratch_texture(scratchKey);
|
|
|
|
* texture[1] = get_scratch_texture(scratchKey); // texture[0] is already owned so we will get a
|
|
|
|
* // different one for texture[1]
|
|
|
|
* draw_mask(texture[0], path); // draws path mask to texture[0]
|
|
|
|
* blur_x(texture[0], texture[1]); // blurs texture[0] in y and stores result in texture[1]
|
|
|
|
* blur_y(texture[1], texture[0]); // blurs texture[1] in y and stores result in texture[0]
|
|
|
|
* texture[1]->unref(); // texture 1 can now be recycled for the next request with scratchKey
|
|
|
|
* consume_blur(texture[0]);
|
|
|
|
* texture[0]->unref(); // texture 0 can now be recycled for the next request with scratchKey
|
2015-01-23 12:24:04 +00:00
|
|
|
*/
|
|
|
|
class GrScratchKey : public GrResourceKey {
|
|
|
|
private:
|
|
|
|
typedef GrResourceKey INHERITED;
|
|
|
|
|
2014-12-30 20:50:52 +00:00
|
|
|
public:
|
2015-01-23 12:24:04 +00:00
|
|
|
/** Uniquely identifies the type of resource that is cached as scratch. */
|
|
|
|
typedef uint32_t ResourceType;
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/** Generate a unique ResourceType. */
|
|
|
|
static ResourceType GenerateResourceType();
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/** Creates an invalid scratch key. It must be initialized using a Builder object before use. */
|
|
|
|
GrScratchKey() {}
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
GrScratchKey(const GrScratchKey& that) { *this = that; }
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/** reset() returns the key to the invalid state. */
|
|
|
|
using INHERITED::reset;
|
|
|
|
|
|
|
|
using INHERITED::isValid;
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
ResourceType resourceType() const { return this->domain(); }
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
GrScratchKey& operator=(const GrScratchKey& that) {
|
|
|
|
this->INHERITED::operator=(that);
|
|
|
|
return *this;
|
2014-08-28 16:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
bool operator==(const GrScratchKey& that) const {
|
|
|
|
return this->INHERITED::operator==(that);
|
|
|
|
}
|
|
|
|
bool operator!=(const GrScratchKey& that) const { return !(*this == that); }
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
class Builder : public INHERITED::Builder {
|
|
|
|
public:
|
|
|
|
Builder(GrScratchKey* key, ResourceType type, int data32Count)
|
|
|
|
: INHERITED::Builder(key, type, data32Count) {}
|
|
|
|
};
|
|
|
|
};
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/**
|
2015-02-19 15:24:21 +00:00
|
|
|
* A key that allows for exclusive use of a resource for a use case (AKA "domain"). There are three
|
|
|
|
* rules governing the use of unique keys:
|
|
|
|
* * Only one resource can have a given unique key at a time. Hence, "unique".
|
|
|
|
* * A resource can have at most one unique key at a time.
|
|
|
|
* * Unlike scratch keys, multiple requests for a unique key will return the same
|
|
|
|
* resource even if the resource already has refs.
|
|
|
|
* This key type allows a code path to create cached resources for which it is the exclusive user.
|
|
|
|
* The code path creates a domain which it sets on its keys. This guarantees that there are no
|
|
|
|
* cross-domain collisions.
|
|
|
|
*
|
|
|
|
* Unique keys preempt scratch keys. While a resource has a unique key it is inaccessible via its
|
|
|
|
* scratch key. It can become scratch again if the unique key is removed.
|
2015-01-23 12:24:04 +00:00
|
|
|
*/
|
2015-02-19 15:24:21 +00:00
|
|
|
class GrUniqueKey : public GrResourceKey {
|
2014-08-28 16:54:34 +00:00
|
|
|
private:
|
2015-01-23 12:24:04 +00:00
|
|
|
typedef GrResourceKey INHERITED;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef uint32_t Domain;
|
2015-02-19 15:24:21 +00:00
|
|
|
/** Generate a Domain for unique keys. */
|
2015-01-23 12:24:04 +00:00
|
|
|
static Domain GenerateDomain();
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
/** Creates an invalid unique key. It must be initialized using a Builder object before use. */
|
|
|
|
GrUniqueKey() {}
|
2015-01-23 12:24:04 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey(const GrUniqueKey& that) { *this = that; }
|
2014-08-28 16:54:34 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
/** reset() returns the key to the invalid state. */
|
|
|
|
using INHERITED::reset;
|
|
|
|
|
|
|
|
using INHERITED::isValid;
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey& operator=(const GrUniqueKey& that) {
|
2015-01-23 12:24:04 +00:00
|
|
|
this->INHERITED::operator=(that);
|
2016-08-02 21:40:46 +00:00
|
|
|
this->setCustomData(sk_ref_sp(that.getCustomData()));
|
2018-03-29 17:40:02 +00:00
|
|
|
fTag = that.fTag;
|
2015-01-23 12:24:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
bool operator==(const GrUniqueKey& that) const {
|
2015-01-23 12:24:04 +00:00
|
|
|
return this->INHERITED::operator==(that);
|
2014-08-28 16:54:34 +00:00
|
|
|
}
|
2015-02-19 15:24:21 +00:00
|
|
|
bool operator!=(const GrUniqueKey& that) const { return !(*this == that); }
|
2015-01-23 12:24:04 +00:00
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
void setCustomData(sk_sp<SkData> data) {
|
|
|
|
fData = std::move(data);
|
2015-08-04 17:01:58 +00:00
|
|
|
}
|
2016-08-02 21:40:46 +00:00
|
|
|
SkData* getCustomData() const {
|
2015-08-04 17:01:58 +00:00
|
|
|
return fData.get();
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:40:02 +00:00
|
|
|
const char* tag() const { return fTag; }
|
2017-01-06 17:04:19 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
class Builder : public INHERITED::Builder {
|
|
|
|
public:
|
2017-01-06 17:04:19 +00:00
|
|
|
Builder(GrUniqueKey* key, Domain type, int data32Count, const char* tag = nullptr)
|
|
|
|
: INHERITED::Builder(key, type, data32Count) {
|
2018-03-29 17:40:02 +00:00
|
|
|
key->fTag = tag;
|
2017-01-06 17:04:19 +00:00
|
|
|
}
|
2015-01-23 12:24:04 +00:00
|
|
|
|
|
|
|
/** Used to build a key that wraps another key and adds additional data. */
|
2017-01-06 17:04:19 +00:00
|
|
|
Builder(GrUniqueKey* key, const GrUniqueKey& innerKey, Domain domain, int extraData32Cnt,
|
|
|
|
const char* tag = nullptr)
|
|
|
|
: INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
|
2015-02-02 21:00:10 +00:00
|
|
|
SkASSERT(&innerKey != key);
|
2015-01-23 12:24:04 +00:00
|
|
|
// add the inner key to the end of the key so that op[] can be indexed normally.
|
2015-01-28 19:39:48 +00:00
|
|
|
uint32_t* innerKeyData = &this->operator[](extraData32Cnt);
|
|
|
|
const uint32_t* srcData = innerKey.data();
|
|
|
|
(*innerKeyData++) = innerKey.domain();
|
|
|
|
memcpy(innerKeyData, srcData, innerKey.dataSize());
|
2018-03-29 17:40:02 +00:00
|
|
|
key->fTag = tag;
|
2015-01-28 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-19 15:24:21 +00:00
|
|
|
static int Data32CntForInnerKey(const GrUniqueKey& innerKey) {
|
2015-01-28 19:39:48 +00:00
|
|
|
// key data + domain
|
|
|
|
return SkToInt((innerKey.dataSize() >> 2) + 1);
|
2015-01-23 12:24:04 +00:00
|
|
|
}
|
|
|
|
};
|
2015-08-04 17:01:58 +00:00
|
|
|
|
|
|
|
private:
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> fData;
|
2018-03-29 17:40:02 +00:00
|
|
|
const char* fTag;
|
2014-08-28 16:54:34 +00:00
|
|
|
};
|
|
|
|
|
2015-05-04 17:36:42 +00:00
|
|
|
/**
|
|
|
|
* It is common to need a frequently reused GrUniqueKey where the only requirement is that the key
|
|
|
|
* is unique. These macros create such a key in a thread safe manner so the key can be truly global
|
|
|
|
* and only constructed once.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Place outside of function/class definitions. */
|
2016-04-18 15:09:11 +00:00
|
|
|
#define GR_DECLARE_STATIC_UNIQUE_KEY(name) static SkOnce name##_once
|
2015-05-04 17:36:42 +00:00
|
|
|
|
|
|
|
/** Place inside function where the key is used. */
|
2015-12-17 16:15:47 +00:00
|
|
|
#define GR_DEFINE_STATIC_UNIQUE_KEY(name) \
|
|
|
|
static SkAlignedSTStorage<1, GrUniqueKey> name##_storage; \
|
2016-04-18 15:09:11 +00:00
|
|
|
name##_once(gr_init_static_unique_key_once, &name##_storage); \
|
2015-12-17 16:15:47 +00:00
|
|
|
static const GrUniqueKey& name = *reinterpret_cast<GrUniqueKey*>(name##_storage.get());
|
2015-05-04 17:36:42 +00:00
|
|
|
|
2015-12-17 16:15:47 +00:00
|
|
|
static inline void gr_init_static_unique_key_once(SkAlignedSTStorage<1,GrUniqueKey>* keyStorage) {
|
|
|
|
GrUniqueKey* key = new (keyStorage->get()) GrUniqueKey;
|
2015-05-04 17:36:42 +00:00
|
|
|
GrUniqueKey::Builder builder(key, GrUniqueKey::GenerateDomain(), 0);
|
|
|
|
}
|
|
|
|
|
2015-02-06 19:54:28 +00:00
|
|
|
// The cache listens for these messages to purge junk resources proactively.
|
2015-02-19 15:24:21 +00:00
|
|
|
class GrUniqueKeyInvalidatedMessage {
|
2015-02-06 19:54:28 +00:00
|
|
|
public:
|
2015-02-19 15:24:21 +00:00
|
|
|
explicit GrUniqueKeyInvalidatedMessage(const GrUniqueKey& key) : fKey(key) {}
|
|
|
|
|
|
|
|
GrUniqueKeyInvalidatedMessage(const GrUniqueKeyInvalidatedMessage& that) : fKey(that.fKey) {}
|
|
|
|
|
|
|
|
GrUniqueKeyInvalidatedMessage& operator=(const GrUniqueKeyInvalidatedMessage& that) {
|
2015-02-06 19:54:28 +00:00
|
|
|
fKey = that.fKey;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-02-19 15:24:21 +00:00
|
|
|
|
|
|
|
const GrUniqueKey& key() const { return fKey; }
|
|
|
|
|
2015-02-06 19:54:28 +00:00
|
|
|
private:
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey fKey;
|
2015-02-06 19:54:28 +00:00
|
|
|
};
|
2014-08-28 16:54:34 +00:00
|
|
|
#endif
|