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
|
|
|
|
|
|
|
|
#include "GrTypes.h"
|
2014-12-30 20:50:52 +00:00
|
|
|
#include "SkTemplates.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);
|
|
|
|
|
|
|
|
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-01-27 17:56:04 +00:00
|
|
|
SkASSERT(this->isValid() && that.isValid());
|
2015-01-23 12:24:04 +00:00
|
|
|
return 0 == memcmp(fKey.get(), that.fKey.get(), this->size());
|
|
|
|
}
|
2014-12-30 20:50:52 +00:00
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
GrResourceKey& operator=(const GrResourceKey& that) {
|
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);
|
|
|
|
}
|
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; }
|
|
|
|
|
|
|
|
/** 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() {
|
|
|
|
if (NULL == fKey) {
|
|
|
|
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();
|
|
|
|
fKey = NULL;
|
|
|
|
}
|
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.
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
// bmp textures require 4 uint32_t values.
|
|
|
|
SkAutoSTArray<kMetaDataCnt + 4, 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
|
|
|
/**
|
|
|
|
* A key used for scratch resources. The key consists of a resource type (subclass) identifier, a
|
|
|
|
* hash, a data length, and type-specific data. A Builder object is used to initialize the
|
|
|
|
* key contents. The contents must be initialized before the key can be used.
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* A key used to cache resources based on their content. The key consists of a domain type (use
|
|
|
|
* case for the cache), a hash, a data length, and domain-specific data. A Builder object is used to
|
|
|
|
* initialize the key contents. The contents must be initialized before the key can be used.
|
|
|
|
*/
|
|
|
|
class GrContentKey : 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;
|
|
|
|
/** Generate a unique Domain of content keys. */
|
|
|
|
static Domain GenerateDomain();
|
|
|
|
|
|
|
|
/** Creates an invalid content key. It must be initialized using a Builder object before use. */
|
|
|
|
GrContentKey() {}
|
|
|
|
|
|
|
|
GrContentKey(const GrContentKey& 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;
|
|
|
|
|
|
|
|
GrContentKey& operator=(const GrContentKey& that) {
|
|
|
|
this->INHERITED::operator=(that);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const GrContentKey& that) const {
|
|
|
|
return this->INHERITED::operator==(that);
|
2014-08-28 16:54:34 +00:00
|
|
|
}
|
2015-01-23 12:24:04 +00:00
|
|
|
bool operator!=(const GrContentKey& that) const { return !(*this == that); }
|
|
|
|
|
|
|
|
class Builder : public INHERITED::Builder {
|
|
|
|
public:
|
|
|
|
Builder(GrContentKey* key, Domain domain, int data32Count)
|
|
|
|
: INHERITED::Builder(key, domain, data32Count) {}
|
|
|
|
|
|
|
|
/** Used to build a key that wraps another key and adds additional data. */
|
|
|
|
Builder(GrContentKey* key, const GrContentKey& innerKey, Domain domain,
|
|
|
|
int extraData32Cnt)
|
2015-01-28 19:39:48 +00:00
|
|
|
: INHERITED::Builder(key, domain, Data32CntForInnerKey(innerKey) + extraData32Cnt) {
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int Data32CntForInnerKey(const GrContentKey& innerKey) {
|
|
|
|
// key data + domain
|
|
|
|
return SkToInt((innerKey.dataSize() >> 2) + 1);
|
2015-01-23 12:24:04 +00:00
|
|
|
}
|
|
|
|
};
|
2014-08-28 16:54:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|