bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 12 | #include "GrTypes.h" |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 13 | #include "SkTemplates.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 14 | #include "GrBinHashKey.h" |
| 15 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 16 | /** |
| 17 | * A key used for scratch resources. The key consists of a resource type (subclass) identifier, a |
| 18 | * hash, a data length, and type-specific data. A Builder object is used to initialize the |
| 19 | * key contents. The contents must be initialized before the key can be used. |
| 20 | */ |
| 21 | class GrScratchKey { |
| 22 | public: |
| 23 | /** Uniquely identifies the type of resource that is cached as scratch. */ |
| 24 | typedef uint32_t ResourceType; |
| 25 | /** Generate a unique ResourceType. */ |
| 26 | static ResourceType GenerateResourceType(); |
| 27 | |
| 28 | GrScratchKey() { this->reset(); } |
| 29 | GrScratchKey(const GrScratchKey& that) { *this = that; } |
| 30 | |
| 31 | /** Reset to an invalid key. */ |
| 32 | void reset() { |
| 33 | fKey.reset(kMetaDataCnt); |
| 34 | fKey[kHash_MetaDataIdx] = 0; |
| 35 | fKey[kTypeAndSize_MetaDataIdx] = kInvalidResourceType; |
| 36 | } |
| 37 | |
| 38 | bool isValid() const { return kInvalidResourceType != this->resourceType(); } |
| 39 | |
| 40 | ResourceType resourceType() const { return fKey[kTypeAndSize_MetaDataIdx] & 0xffff; } |
| 41 | |
| 42 | uint32_t hash() const { return fKey[kHash_MetaDataIdx]; } |
| 43 | |
| 44 | size_t size() const { return SkToInt(fKey[kTypeAndSize_MetaDataIdx] >> 16); } |
| 45 | |
| 46 | const uint32_t* data() const { return &fKey[kMetaDataCnt]; } |
| 47 | |
| 48 | GrScratchKey& operator=(const GrScratchKey& that) { |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 49 | size_t bytes = that.size(); |
| 50 | fKey.reset(SkToInt(bytes / sizeof(uint32_t))); |
| 51 | memcpy(fKey.get(), that.fKey.get(), bytes); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | bool operator==(const GrScratchKey& that) const { |
| 56 | return 0 == memcmp(fKey.get(), that.fKey.get(), this->size()); |
| 57 | } |
| 58 | bool operator!=(const GrScratchKey& that) const { return !(*this == that); } |
| 59 | |
| 60 | /** Used to initialize scratch key. */ |
| 61 | class Builder { |
| 62 | public: |
| 63 | Builder(GrScratchKey* key, ResourceType type, int data32Count) : fKey(key) { |
| 64 | SkASSERT(data32Count >= 0); |
| 65 | SkASSERT(type != kInvalidResourceType); |
| 66 | key->fKey.reset(kMetaDataCnt + data32Count); |
| 67 | SkASSERT(type <= SK_MaxU16); |
| 68 | int size = (data32Count + kMetaDataCnt) * sizeof(uint32_t); |
| 69 | SkASSERT(size <= SK_MaxU16); |
| 70 | key->fKey[kTypeAndSize_MetaDataIdx] = type | (size << 16); |
| 71 | } |
| 72 | |
| 73 | ~Builder() { this->finish(); } |
| 74 | |
| 75 | void finish(); |
| 76 | |
| 77 | uint32_t& operator[](int dataIdx) { |
| 78 | SkASSERT(fKey); |
| 79 | SkDEBUGCODE(size_t dataCount = fKey->size() / sizeof(uint32_t) - kMetaDataCnt;) |
| 80 | SkASSERT(SkToU32(dataIdx) < dataCount); |
| 81 | return fKey->fKey[kMetaDataCnt + dataIdx]; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | GrScratchKey* fKey; |
| 86 | }; |
| 87 | |
| 88 | private: |
| 89 | enum MetaDataIdx { |
| 90 | kHash_MetaDataIdx, |
| 91 | // The resource type and size are packed into a single uint32_t. |
| 92 | kTypeAndSize_MetaDataIdx, |
| 93 | |
| 94 | kLastMetaDataIdx = kTypeAndSize_MetaDataIdx |
| 95 | }; |
| 96 | static const uint32_t kInvalidResourceType = 0; |
| 97 | static const uint32_t kMetaDataCnt = kLastMetaDataIdx + 1; |
| 98 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 99 | friend class TestResource; // For unit test to access kMetaDataCnt. |
| 100 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 101 | // Stencil and textures each require 2 uint32_t values. |
| 102 | SkAutoSTArray<kMetaDataCnt + 2, uint32_t> fKey; |
| 103 | }; |
| 104 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 105 | class GrResourceKey { |
| 106 | public: |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 107 | /** Flags set by the GrGpuResource subclass. */ |
| 108 | typedef uint8_t ResourceFlags; |
| 109 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 110 | /** Creates a key for resource */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 111 | GrResourceKey(const GrCacheID& id, ResourceFlags flags) { |
| 112 | this->init(id.getDomain(), id.getKey(), flags); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | GrResourceKey(const GrResourceKey& src) { fKey = src.fKey; } |
| 116 | |
| 117 | GrResourceKey() { fKey.reset(); } |
| 118 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 119 | void reset(const GrCacheID& id, ResourceFlags flags) { |
| 120 | this->init(id.getDomain(), id.getKey(), flags); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | uint32_t getHash() const { return fKey.getHash(); } |
| 124 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 125 | ResourceFlags getResourceFlags() const { |
| 126 | return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + |
| 127 | kResourceFlagsOffset); |
| 128 | } |
| 129 | |
| 130 | bool operator==(const GrResourceKey& other) const { return fKey == other.fKey; } |
| 131 | |
| 132 | // A key indicating that the resource is not usable as a scratch resource. |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 133 | static GrResourceKey& NullScratchKey(); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 134 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 135 | private: |
| 136 | enum { |
| 137 | kCacheIDKeyOffset = 0, |
| 138 | kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 139 | kResourceFlagsOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 140 | kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), |
| 141 | kKeySize = SkAlign4(kPadOffset), |
| 142 | kPadSize = kKeySize - kPadOffset |
| 143 | }; |
| 144 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 145 | void init(const GrCacheID::Domain domain, const GrCacheID::Key& key, ResourceFlags flags) { |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 146 | union { |
| 147 | uint8_t fKey8[kKeySize]; |
| 148 | uint32_t fKey32[kKeySize / 4]; |
| 149 | } keyData; |
| 150 | |
| 151 | uint8_t* k = keyData.fKey8; |
| 152 | memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); |
| 153 | memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 154 | memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); |
| 155 | memset(k + kPadOffset, 0, kPadSize); |
| 156 | fKey.setKeyData(keyData.fKey32); |
| 157 | } |
| 158 | GrBinHashKey<kKeySize> fKey; |
| 159 | }; |
| 160 | |
| 161 | #endif |