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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 /** | 134 /** |
135 * Returns the number of bytes consumed by cached resources. | 135 * Returns the number of bytes consumed by cached resources. |
136 */ | 136 */ |
137 size_t getCachedResourceBytes() const { return fEntryBytes; } | 137 size_t getCachedResourceBytes() const { return fEntryBytes; } |
138 | 138 |
139 /** | 139 /** |
140 * Returns the number of cached resources. | 140 * Returns the number of cached resources. |
141 */ | 141 */ |
142 int getCachedResourceCount() const { return fEntryCount; } | 142 int getCachedResourceCount() const { return fEntryCount; } |
143 | 143 |
| 144 // For a found or added resource to be completely exclusive to the caller |
| 145 // both the kNoOtherOwners and kHide flags need to be specified |
| 146 enum OwnershipFlags { |
| 147 kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other
owners |
| 148 kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future
'find's |
| 149 }; |
| 150 |
144 /** | 151 /** |
145 * Search for an entry with the same Key. If found, return it. | 152 * Search for an entry with the same Key. If found, return it. |
146 * If not found, return null. | 153 * If not found, return null. |
| 154 * If ownershipFlags includes kNoOtherOwners and a resource is returned |
| 155 * then that resource has no other refs to it. |
| 156 * If ownershipFlags includes kHide and a resource is returned then that |
| 157 * resource will not be returned from future 'find' calls until it is |
| 158 * 'freed' (and recycled) or makeNonExclusive is called. |
| 159 * For a resource to be completely exclusive to a caller both kNoOtherOwner
s |
| 160 * and kHide must be specified. |
147 */ | 161 */ |
148 GrGpuResource* find(const GrResourceKey& key); | 162 GrGpuResource* find(const GrResourceKey& key, |
149 | 163 uint32_t ownershipFlags = 0); |
150 void makeResourceMRU(GrGpuResource*); | |
151 | 164 |
152 /** | 165 /** |
153 * Add the new resource to the cache (by creating a new cache entry based | 166 * Add the new resource to the cache (by creating a new cache entry based |
154 * on the provided key and resource). | 167 * on the provided key and resource). |
155 * | 168 * |
156 * Ownership of the resource is transferred to the resource cache, | 169 * Ownership of the resource is transferred to the resource cache, |
157 * which will unref() it when it is purged or deleted. | 170 * which will unref() it when it is purged or deleted. |
| 171 * |
| 172 * If ownershipFlags includes kHide, subsequent calls to 'find' will not |
| 173 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive |
| 174 * is called. |
158 */ | 175 */ |
159 void addResource(const GrResourceKey& key, GrGpuResource* resource); | 176 void addResource(const GrResourceKey& key, |
| 177 GrGpuResource* resource, |
| 178 uint32_t ownershipFlags = 0); |
160 | 179 |
161 /** | 180 /** |
162 * Determines if the cache contains an entry matching a key. If a matching | 181 * Determines if the cache contains an entry matching a key. If a matching |
163 * entry exists but was detached then it will not be found. | 182 * entry exists but was detached then it will not be found. |
164 */ | 183 */ |
165 bool hasKey(const GrResourceKey& key) const { return SkToBool(fCache.find(ke
y)); } | 184 bool hasKey(const GrResourceKey& key) const { return SkToBool(fCache.find(ke
y)); } |
166 | 185 |
167 /** | 186 /** |
| 187 * Hide 'entry' so that future searches will not find it. Such |
| 188 * hidden entries will not be purged. The entry still counts against |
| 189 * the cache's budget and should be made non-exclusive when exclusive access |
| 190 * is no longer needed. |
| 191 */ |
| 192 void makeExclusive(GrResourceCacheEntry* entry); |
| 193 |
| 194 /** |
| 195 * Restore 'entry' so that it can be found by future searches. 'entry' |
| 196 * will also be purgeable (provided its lock count is now 0.) |
| 197 */ |
| 198 void makeNonExclusive(GrResourceCacheEntry* entry); |
| 199 |
| 200 /** |
168 * Notify the cache that the size of a resource has changed. | 201 * Notify the cache that the size of a resource has changed. |
169 */ | 202 */ |
170 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); | 203 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); |
171 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); | 204 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); |
172 | 205 |
173 /** | 206 /** |
174 * Remove a resource from the cache and delete it! | 207 * Remove a resource from the cache and delete it! |
175 */ | 208 */ |
176 void deleteResource(GrResourceCacheEntry* entry); | 209 void deleteResource(GrResourceCacheEntry* entry); |
177 | 210 |
(...skipping 19 matching lines...) Expand all Loading... |
197 void validate() const; | 230 void validate() const; |
198 #else | 231 #else |
199 void validate() const {} | 232 void validate() const {} |
200 #endif | 233 #endif |
201 | 234 |
202 #if GR_CACHE_STATS | 235 #if GR_CACHE_STATS |
203 void printStats(); | 236 void printStats(); |
204 #endif | 237 #endif |
205 | 238 |
206 private: | 239 private: |
207 void internalDetach(GrResourceCacheEntry*); | 240 enum BudgetBehaviors { |
208 void attachToHead(GrResourceCacheEntry*); | 241 kAccountFor_BudgetBehavior, |
| 242 kIgnore_BudgetBehavior |
| 243 }; |
| 244 |
| 245 void internalDetach(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccou
ntFor_BudgetBehavior); |
| 246 void attachToHead(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccount
For_BudgetBehavior); |
| 247 |
| 248 void removeInvalidResource(GrResourceCacheEntry* entry); |
209 | 249 |
210 SkTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache; | 250 SkTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache; |
211 | 251 |
212 // We're an internal doubly linked list | 252 // We're an internal doubly linked list |
213 typedef SkTInternalLList<GrResourceCacheEntry> EntryList; | 253 typedef SkTInternalLList<GrResourceCacheEntry> EntryList; |
214 EntryList fList; | 254 EntryList fList; |
215 | 255 |
| 256 #ifdef SK_DEBUG |
| 257 // These objects cannot be returned by a search |
| 258 EntryList fExclusiveList; |
| 259 #endif |
| 260 |
216 // our budget, used in purgeAsNeeded() | 261 // our budget, used in purgeAsNeeded() |
217 int fMaxCount; | 262 int fMaxCount; |
218 size_t fMaxBytes; | 263 size_t fMaxBytes; |
219 | 264 |
220 // our current stats, related to our budget | 265 // our current stats, related to our budget |
221 #if GR_CACHE_STATS | 266 #if GR_CACHE_STATS |
222 int fHighWaterEntryCount; | 267 int fHighWaterEntryCount; |
223 size_t fHighWaterEntryBytes; | 268 size_t fHighWaterEntryBytes; |
| 269 int fHighWaterClientDetachedCount; |
| 270 size_t fHighWaterClientDetachedBytes; |
224 #endif | 271 #endif |
225 | 272 |
226 int fEntryCount; | 273 int fEntryCount; |
227 size_t fEntryBytes; | 274 size_t fEntryBytes; |
| 275 int fClientDetachedCount; |
| 276 size_t fClientDetachedBytes; |
228 | 277 |
229 // prevents recursive purging | 278 // prevents recursive purging |
230 bool fPurging; | 279 bool fPurging; |
231 | 280 |
232 PFOverbudgetCB fOverbudgetCB; | 281 PFOverbudgetCB fOverbudgetCB; |
233 void* fOverbudgetData; | 282 void* fOverbudgetData; |
234 | 283 |
235 void internalPurge(int extraCount, size_t extraBytes); | 284 void internalPurge(int extraCount, size_t extraBytes); |
236 | 285 |
237 // Listen for messages that a resource has been invalidated and purge cached
junk proactively. | 286 // Listen for messages that a resource has been invalidated and purge cached
junk proactively. |
(...skipping 20 matching lines...) Expand all Loading... |
258 GrResourceCache* fCache; | 307 GrResourceCache* fCache; |
259 }; | 308 }; |
260 #else | 309 #else |
261 class GrAutoResourceCacheValidate { | 310 class GrAutoResourceCacheValidate { |
262 public: | 311 public: |
263 GrAutoResourceCacheValidate(GrResourceCache*) {} | 312 GrAutoResourceCacheValidate(GrResourceCache*) {} |
264 }; | 313 }; |
265 #endif | 314 #endif |
266 | 315 |
267 #endif | 316 #endif |
OLD | NEW |