OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #ifndef GrResourceKey_DEFINED | 9 #ifndef GrResourceKey_DEFINED |
10 #define GrResourceKey_DEFINED | 10 #define GrResourceKey_DEFINED |
11 | 11 |
12 #include "GrTypes.h" | 12 #include "GrTypes.h" |
| 13 #include "SkTemplates.h" |
13 #include "GrBinHashKey.h" | 14 #include "GrBinHashKey.h" |
14 | 15 |
| 16 /** |
| 17 * A key used for scratch resources. The key consists of a resource type (subcla
ss) identifier, a |
| 18 * hash, a data length, and type-specific data. A Builder object is used to init
ialize 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) { |
| 49 size_t size = that.size(); |
| 50 fKey.reset(SkToInt(size)); |
| 51 memcpy(fKey.get(), that.fKey.get(), size); |
| 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(ke
y) { |
| 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) - kMe
taDataCnt;) |
| 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 |
| 99 // Stencil and textures each require 2 uint32_t values. |
| 100 SkAutoSTArray<kMetaDataCnt + 2, uint32_t> fKey; |
| 101 }; |
| 102 |
15 class GrResourceKey { | 103 class GrResourceKey { |
16 public: | 104 public: |
17 static GrCacheID::Domain ScratchDomain(); | |
18 | |
19 /** Uniquely identifies the GrGpuResource subclass in the key to avoid colli
sions | |
20 across resource types. */ | |
21 typedef uint8_t ResourceType; | |
22 | |
23 /** Flags set by the GrGpuResource subclass. */ | 105 /** Flags set by the GrGpuResource subclass. */ |
24 typedef uint8_t ResourceFlags; | 106 typedef uint8_t ResourceFlags; |
25 | 107 |
26 /** Generate a unique ResourceType */ | |
27 static ResourceType GenerateResourceType(); | |
28 | |
29 /** Creates a key for resource */ | 108 /** Creates a key for resource */ |
30 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 109 GrResourceKey(const GrCacheID& id, ResourceFlags flags) { |
31 this->init(id.getDomain(), id.getKey(), type, flags); | 110 this->init(id.getDomain(), id.getKey(), flags); |
32 }; | 111 }; |
33 | 112 |
34 GrResourceKey(const GrResourceKey& src) { fKey = src.fKey; } | 113 GrResourceKey(const GrResourceKey& src) { fKey = src.fKey; } |
35 | 114 |
36 GrResourceKey() { fKey.reset(); } | 115 GrResourceKey() { fKey.reset(); } |
37 | 116 |
38 void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 117 void reset(const GrCacheID& id, ResourceFlags flags) { |
39 this->init(id.getDomain(), id.getKey(), type, flags); | 118 this->init(id.getDomain(), id.getKey(), flags); |
40 } | 119 } |
41 | 120 |
42 uint32_t getHash() const { return fKey.getHash(); } | 121 uint32_t getHash() const { return fKey.getHash(); } |
43 | 122 |
44 bool isScratch() const { | |
45 return ScratchDomain() == | |
46 *reinterpret_cast<const GrCacheID::Domain*>(fKey.getData() + | |
47 kCacheIDDomainOffset); | |
48 } | |
49 | |
50 ResourceType getResourceType() const { | |
51 return *reinterpret_cast<const ResourceType*>(fKey.getData() + | |
52 kResourceTypeOffset); | |
53 } | |
54 | |
55 ResourceFlags getResourceFlags() const { | 123 ResourceFlags getResourceFlags() const { |
56 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + | 124 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + |
57 kResourceFlagsOffset); | 125 kResourceFlagsOffset); |
58 } | 126 } |
59 | 127 |
60 bool operator==(const GrResourceKey& other) const { return fKey == other.fKe
y; } | 128 bool operator==(const GrResourceKey& other) const { return fKey == other.fKe
y; } |
61 | 129 |
62 // A key indicating that the resource is not usable as a scratch resource. | 130 // A key indicating that the resource is not usable as a scratch resource. |
63 static GrResourceKey& NullScratchKey(); | 131 static GrResourceKey& NullScratchKey(); |
64 | 132 |
65 bool isNullScratch() const { | |
66 return this->isScratch() && NoneResourceType() == this->getResourceType(
); | |
67 } | |
68 | |
69 private: | 133 private: |
70 enum { | 134 enum { |
71 kCacheIDKeyOffset = 0, | 135 kCacheIDKeyOffset = 0, |
72 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), | 136 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), |
73 kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), | 137 kResourceFlagsOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), |
74 kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType), | |
75 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), | 138 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), |
76 kKeySize = SkAlign4(kPadOffset), | 139 kKeySize = SkAlign4(kPadOffset), |
77 kPadSize = kKeySize - kPadOffset | 140 kPadSize = kKeySize - kPadOffset |
78 }; | 141 }; |
79 | 142 |
80 static ResourceType NoneResourceType(); | 143 void init(const GrCacheID::Domain domain, const GrCacheID::Key& key, Resourc
eFlags flags) { |
81 | |
82 void init(const GrCacheID::Domain domain, | |
83 const GrCacheID::Key& key, | |
84 ResourceType type, | |
85 ResourceFlags flags) { | |
86 union { | 144 union { |
87 uint8_t fKey8[kKeySize]; | 145 uint8_t fKey8[kKeySize]; |
88 uint32_t fKey32[kKeySize / 4]; | 146 uint32_t fKey32[kKeySize / 4]; |
89 } keyData; | 147 } keyData; |
90 | 148 |
91 uint8_t* k = keyData.fKey8; | 149 uint8_t* k = keyData.fKey8; |
92 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); | 150 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); |
93 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); | 151 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); |
94 memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType)); | |
95 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); | 152 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); |
96 memset(k + kPadOffset, 0, kPadSize); | 153 memset(k + kPadOffset, 0, kPadSize); |
97 fKey.setKeyData(keyData.fKey32); | 154 fKey.setKeyData(keyData.fKey32); |
98 } | 155 } |
99 GrBinHashKey<kKeySize> fKey; | 156 GrBinHashKey<kKeySize> fKey; |
100 }; | 157 }; |
101 | 158 |
102 #endif | 159 #endif |
OLD | NEW |