| 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 | |
| 151 /** | 144 /** |
| 152 * Search for an entry with the same Key. If found, return it. | 145 * Search for an entry with the same Key. If found, return it. |
| 153 * If not found, return null. | 146 * 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. | |
| 161 */ | 147 */ |
| 162 GrGpuResource* find(const GrResourceKey& key, | 148 GrGpuResource* find(const GrResourceKey& key); |
| 163 uint32_t ownershipFlags = 0); | 149 |
| 150 void makeResourceMRU(GrGpuResource*); |
| 164 | 151 |
| 165 /** | 152 /** |
| 166 * Add the new resource to the cache (by creating a new cache entry based | 153 * Add the new resource to the cache (by creating a new cache entry based |
| 167 * on the provided key and resource). | 154 * on the provided key and resource). |
| 168 * | 155 * |
| 169 * Ownership of the resource is transferred to the resource cache, | 156 * Ownership of the resource is transferred to the resource cache, |
| 170 * which will unref() it when it is purged or deleted. | 157 * 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. | |
| 175 */ | 158 */ |
| 176 void addResource(const GrResourceKey& key, | 159 void addResource(const GrResourceKey& key, GrGpuResource* resource); |
| 177 GrGpuResource* resource, | |
| 178 uint32_t ownershipFlags = 0); | |
| 179 | 160 |
| 180 /** | 161 /** |
| 181 * Determines if the cache contains an entry matching a key. If a matching | 162 * Determines if the cache contains an entry matching a key. If a matching |
| 182 * entry exists but was detached then it will not be found. | 163 * entry exists but was detached then it will not be found. |
| 183 */ | 164 */ |
| 184 bool hasKey(const GrResourceKey& key) const { return SkToBool(fCache.find(ke
y)); } | 165 bool hasKey(const GrResourceKey& key) const { return SkToBool(fCache.find(ke
y)); } |
| 185 | 166 |
| 186 /** | 167 /** |
| 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 /** | |
| 201 * Notify the cache that the size of a resource has changed. | 168 * Notify the cache that the size of a resource has changed. |
| 202 */ | 169 */ |
| 203 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); | 170 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); |
| 204 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); | 171 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); |
| 205 | 172 |
| 206 /** | 173 /** |
| 207 * Remove a resource from the cache and delete it! | 174 * Remove a resource from the cache and delete it! |
| 208 */ | 175 */ |
| 209 void deleteResource(GrResourceCacheEntry* entry); | 176 void deleteResource(GrResourceCacheEntry* entry); |
| 210 | 177 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 230 void validate() const; | 197 void validate() const; |
| 231 #else | 198 #else |
| 232 void validate() const {} | 199 void validate() const {} |
| 233 #endif | 200 #endif |
| 234 | 201 |
| 235 #if GR_CACHE_STATS | 202 #if GR_CACHE_STATS |
| 236 void printStats(); | 203 void printStats(); |
| 237 #endif | 204 #endif |
| 238 | 205 |
| 239 private: | 206 private: |
| 240 enum BudgetBehaviors { | 207 void internalDetach(GrResourceCacheEntry*); |
| 241 kAccountFor_BudgetBehavior, | 208 void attachToHead(GrResourceCacheEntry*); |
| 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); | |
| 249 | 209 |
| 250 SkTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache; | 210 SkTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache; |
| 251 | 211 |
| 252 // We're an internal doubly linked list | 212 // We're an internal doubly linked list |
| 253 typedef SkTInternalLList<GrResourceCacheEntry> EntryList; | 213 typedef SkTInternalLList<GrResourceCacheEntry> EntryList; |
| 254 EntryList fList; | 214 EntryList fList; |
| 255 | 215 |
| 256 #ifdef SK_DEBUG | |
| 257 // These objects cannot be returned by a search | |
| 258 EntryList fExclusiveList; | |
| 259 #endif | |
| 260 | |
| 261 // our budget, used in purgeAsNeeded() | 216 // our budget, used in purgeAsNeeded() |
| 262 int fMaxCount; | 217 int fMaxCount; |
| 263 size_t fMaxBytes; | 218 size_t fMaxBytes; |
| 264 | 219 |
| 265 // our current stats, related to our budget | 220 // our current stats, related to our budget |
| 266 #if GR_CACHE_STATS | 221 #if GR_CACHE_STATS |
| 267 int fHighWaterEntryCount; | 222 int fHighWaterEntryCount; |
| 268 size_t fHighWaterEntryBytes; | 223 size_t fHighWaterEntryBytes; |
| 269 int fHighWaterClientDetachedCount; | |
| 270 size_t fHighWaterClientDetachedBytes; | |
| 271 #endif | 224 #endif |
| 272 | 225 |
| 273 int fEntryCount; | 226 int fEntryCount; |
| 274 size_t fEntryBytes; | 227 size_t fEntryBytes; |
| 275 int fClientDetachedCount; | |
| 276 size_t fClientDetachedBytes; | |
| 277 | 228 |
| 278 // prevents recursive purging | 229 // prevents recursive purging |
| 279 bool fPurging; | 230 bool fPurging; |
| 280 | 231 |
| 281 PFOverbudgetCB fOverbudgetCB; | 232 PFOverbudgetCB fOverbudgetCB; |
| 282 void* fOverbudgetData; | 233 void* fOverbudgetData; |
| 283 | 234 |
| 284 void internalPurge(int extraCount, size_t extraBytes); | 235 void internalPurge(int extraCount, size_t extraBytes); |
| 285 | 236 |
| 286 // Listen for messages that a resource has been invalidated and purge cached
junk proactively. | 237 // Listen for messages that a resource has been invalidated and purge cached
junk proactively. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 307 GrResourceCache* fCache; | 258 GrResourceCache* fCache; |
| 308 }; | 259 }; |
| 309 #else | 260 #else |
| 310 class GrAutoResourceCacheValidate { | 261 class GrAutoResourceCacheValidate { |
| 311 public: | 262 public: |
| 312 GrAutoResourceCacheValidate(GrResourceCache*) {} | 263 GrAutoResourceCacheValidate(GrResourceCache*) {} |
| 313 }; | 264 }; |
| 314 #endif | 265 #endif |
| 315 | 266 |
| 316 #endif | 267 #endif |
| OLD | NEW |