OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 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 | 9 |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 /** Creates a key for resource */ | 47 /** Creates a key for resource */ |
48 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 48 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
49 this->init(id.getDomain(), id.getKey(), type, flags); | 49 this->init(id.getDomain(), id.getKey(), type, flags); |
50 }; | 50 }; |
51 | 51 |
52 GrResourceKey(const GrResourceKey& src) { | 52 GrResourceKey(const GrResourceKey& src) { |
53 fKey = src.fKey; | 53 fKey = src.fKey; |
54 } | 54 } |
55 | 55 |
56 GrResourceKey() { | 56 GrResourceKey() { |
57 fKey.fHashedKey.reset(); | 57 fKey.reset(); |
58 } | 58 } |
59 | 59 |
60 void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 60 void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
61 this->init(id.getDomain(), id.getKey(), type, flags); | 61 this->init(id.getDomain(), id.getKey(), type, flags); |
62 } | 62 } |
63 | 63 |
64 //!< returns hash value [0..kHashMask] for the key | 64 //!< returns hash value [0..kHashMask] for the key |
65 int getHash() const { | 65 int getHash() const { |
66 return fKey.fHashedKey.getHash() & kHashMask; | 66 return fKey.getHash() & kHashMask; |
67 } | 67 } |
68 | 68 |
69 bool isScratch() const { | 69 bool isScratch() const { |
70 return ScratchDomain() == | 70 return ScratchDomain() == |
71 *reinterpret_cast<const GrCacheID::Domain*>(fKey.fHashedKey.getData(
) + | 71 *reinterpret_cast<const GrCacheID::Domain*>(fKey.getData() + |
72 kCacheIDDomainOffset); | 72 kCacheIDDomainOffset); |
73 } | 73 } |
74 | 74 |
75 ResourceType getResourceType() const { | 75 ResourceType getResourceType() const { |
76 return *reinterpret_cast<const ResourceType*>(fKey.fHashedKey.getData()
+ | 76 return *reinterpret_cast<const ResourceType*>(fKey.getData() + |
77 kResourceTypeOffset); | 77 kResourceTypeOffset); |
78 } | 78 } |
79 | 79 |
80 ResourceFlags getResourceFlags() const { | 80 ResourceFlags getResourceFlags() const { |
81 return *reinterpret_cast<const ResourceFlags*>(fKey.fHashedKey.getData()
+ | 81 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + |
82 kResourceFlagsOffset); | 82 kResourceFlagsOffset); |
83 } | 83 } |
84 | 84 |
85 int compare(const GrResourceKey& other) const { | 85 bool operator==(const GrResourceKey& other) const { return fKey == other.fKe
y; } |
86 return fKey.fHashedKey.compare(other.fKey.fHashedKey); | 86 bool operator<(const GrResourceKey& other) const { return fKey < other.fKey;
} |
87 } | |
88 | 87 |
89 static bool LT(const GrResourceKey& a, const GrResourceKey& b) { | 88 static bool LessThan(const GrResourceEntry& entry, const GrResourceKey& key)
; |
90 return a.compare(b) < 0; | 89 static bool Equals(const GrResourceEntry& entry, const GrResourceKey& key); |
91 } | 90 #ifdef SK_DEBUG |
92 | 91 static bool LessThan(const GrResourceEntry& a, const GrResourceEntry& b); |
93 static bool EQ(const GrResourceKey& a, const GrResourceKey& b) { | 92 static bool Equals(const GrResourceEntry& a, const GrResourceEntry& b); |
94 return 0 == a.compare(b); | 93 #endif |
95 } | |
96 | |
97 inline static bool LT(const GrResourceEntry& entry, const GrResourceKey& key
); | |
98 inline static bool EQ(const GrResourceEntry& entry, const GrResourceKey& key
); | |
99 inline static bool LT(const GrResourceEntry& a, const GrResourceEntry& b); | |
100 inline static bool EQ(const GrResourceEntry& a, const GrResourceEntry& b); | |
101 | 94 |
102 private: | 95 private: |
103 enum { | 96 enum { |
104 kCacheIDKeyOffset = 0, | 97 kCacheIDKeyOffset = 0, |
105 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), | 98 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), |
106 kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), | 99 kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), |
107 kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType), | 100 kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType), |
108 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), | 101 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), |
109 kKeySize = SkAlign4(kPadOffset), | 102 kKeySize = SkAlign4(kPadOffset), |
110 kPadSize = kKeySize - kPadOffset | 103 kPadSize = kKeySize - kPadOffset |
111 }; | 104 }; |
112 | 105 |
113 void init(const GrCacheID::Domain domain, | 106 void init(const GrCacheID::Domain domain, |
114 const GrCacheID::Key& key, | 107 const GrCacheID::Key& key, |
115 ResourceType type, | 108 ResourceType type, |
116 ResourceFlags flags) { | 109 ResourceFlags flags) { |
117 union { | 110 union { |
118 uint8_t fKey8[kKeySize]; | 111 uint8_t fKey8[kKeySize]; |
119 uint32_t fKey32[kKeySize / 4]; | 112 uint32_t fKey32[kKeySize / 4]; |
120 } keyData; | 113 } keyData; |
121 | 114 |
122 uint8_t* k = keyData.fKey8; | 115 uint8_t* k = keyData.fKey8; |
123 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); | 116 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); |
124 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); | 117 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); |
125 memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType)); | 118 memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType)); |
126 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); | 119 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); |
127 memset(k + kPadOffset, 0, kPadSize); | 120 memset(k + kPadOffset, 0, kPadSize); |
128 fKey.fHashedKey.setKeyData(keyData.fKey32); | 121 fKey.setKeyData(keyData.fKey32); |
129 } | 122 } |
130 | 123 GrBinHashKey<kKeySize> fKey; |
131 struct Key; | |
132 typedef GrTBinHashKey<Key, kKeySize> HashedKey; | |
133 | |
134 struct Key { | |
135 int compare(const HashedKey& hashedKey) const { | |
136 return fHashedKey.compare(hashedKey); | |
137 } | |
138 | |
139 HashedKey fHashedKey; | |
140 }; | |
141 | |
142 Key fKey; | |
143 }; | 124 }; |
144 | 125 |
145 // The cache listens for these messages to purge junk resources proactively. | 126 // The cache listens for these messages to purge junk resources proactively. |
146 struct GrResourceInvalidatedMessage { | 127 struct GrResourceInvalidatedMessage { |
147 GrResourceKey key; | 128 GrResourceKey key; |
148 }; | 129 }; |
149 | 130 |
150 /////////////////////////////////////////////////////////////////////////////// | 131 /////////////////////////////////////////////////////////////////////////////// |
151 | 132 |
152 class GrResourceEntry { | 133 class GrResourceEntry { |
(...skipping 14 matching lines...) Expand all Loading... |
167 GrResourceKey fKey; | 148 GrResourceKey fKey; |
168 GrResource* fResource; | 149 GrResource* fResource; |
169 | 150 |
170 // we're a linked list | 151 // we're a linked list |
171 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceEntry); | 152 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceEntry); |
172 | 153 |
173 friend class GrResourceCache; | 154 friend class GrResourceCache; |
174 friend class GrDLinkedList; | 155 friend class GrDLinkedList; |
175 }; | 156 }; |
176 | 157 |
177 bool GrResourceKey::LT(const GrResourceEntry& entry, const GrResourceKey& key) { | 158 inline bool GrResourceKey::LessThan(const GrResourceEntry& entry, const GrResour
ceKey& key) { |
178 return LT(entry.key(), key); | 159 return entry.key() < key; |
179 } | 160 } |
180 | 161 |
181 bool GrResourceKey::EQ(const GrResourceEntry& entry, const GrResourceKey& key) { | 162 inline bool GrResourceKey::Equals(const GrResourceEntry& entry, const GrResource
Key& key) { |
182 return EQ(entry.key(), key); | 163 return entry.key() == key; |
183 } | 164 } |
184 | 165 |
185 bool GrResourceKey::LT(const GrResourceEntry& a, const GrResourceEntry& b) { | 166 #ifdef SK_DEBUG |
186 return LT(a.key(), b.key()); | 167 inline bool GrResourceKey::LessThan(const GrResourceEntry& a, const GrResourceEn
try& b) { |
| 168 return a.key() < b.key(); |
187 } | 169 } |
188 | 170 |
189 bool GrResourceKey::EQ(const GrResourceEntry& a, const GrResourceEntry& b) { | 171 inline bool GrResourceKey::Equals(const GrResourceEntry& a, const GrResourceEntr
y& b) { |
190 return EQ(a.key(), b.key()); | 172 return a.key() == b.key(); |
191 } | 173 } |
| 174 #endif |
192 | 175 |
193 /////////////////////////////////////////////////////////////////////////////// | 176 /////////////////////////////////////////////////////////////////////////////// |
194 | 177 |
195 /** | 178 /** |
196 * Cache of GrResource objects. | 179 * Cache of GrResource objects. |
197 * | 180 * |
198 * These have a corresponding GrResourceKey, built from 128bits identifying the | 181 * These have a corresponding GrResourceKey, built from 128bits identifying the |
199 * resource. | 182 * resource. |
200 * | 183 * |
201 * The cache stores the entries in a double-linked list, which is its LRU. | 184 * The cache stores the entries in a double-linked list, which is its LRU. |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 GrResourceCache* fCache; | 408 GrResourceCache* fCache; |
426 }; | 409 }; |
427 #else | 410 #else |
428 class GrAutoResourceCacheValidate { | 411 class GrAutoResourceCacheValidate { |
429 public: | 412 public: |
430 GrAutoResourceCacheValidate(GrResourceCache*) {} | 413 GrAutoResourceCacheValidate(GrResourceCache*) {} |
431 }; | 414 }; |
432 #endif | 415 #endif |
433 | 416 |
434 #endif | 417 #endif |
OLD | NEW |