OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkResourceCache.h" | 9 #include "SkResourceCache.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 // We're under our lock, and we're the only possible mutator, so unconsting is fine. | 279 // We're under our lock, and we're the only possible mutator, so unconsting is fine. |
280 const_cast<Rec*>(rec)->fLockCount -= 1; | 280 const_cast<Rec*>(rec)->fLockCount -= 1; |
281 | 281 |
282 // we may have been over-budget, but now have released something, so check | 282 // we may have been over-budget, but now have released something, so check |
283 // if we should purge. | 283 // if we should purge. |
284 if (0 == rec->fLockCount) { | 284 if (0 == rec->fLockCount) { |
285 this->purgeAsNeeded(); | 285 this->purgeAsNeeded(); |
286 } | 286 } |
287 } | 287 } |
288 | 288 |
289 void SkResourceCache::remove(SkResourceCache::ID id) { | |
reed1
2014/09/11 13:40:30
I think we can just have remove(), and move the im
reed1
2014/09/11 14:33:32
Additionally, we probably need to actually check t
danakj
2014/09/11 14:42:29
IIUC you're suggesting that Remove() do the cast f
danakj
2014/09/11 14:42:30
I considered that, but then I ended up with "marki
| |
290 SkASSERT(id); | |
291 | |
292 #ifdef SK_DEBUG | |
293 { | |
294 bool found = false; | |
295 Rec* rec = fHead; | |
296 while (rec != NULL) { | |
297 if (rec == id) { | |
298 found = true; | |
299 break; | |
300 } | |
301 rec = rec->fNext; | |
302 } | |
303 SkASSERT(found); | |
304 } | |
305 #endif | |
306 const Rec* rec = id; | |
307 SkASSERT(0 == rec->fLockCount); | |
308 this->purgeRec(const_cast<Rec*>(rec)); | |
309 } | |
310 | |
311 void SkResourceCache::purgeRec(Rec* rec) { | |
312 SkASSERT(0 == rec->fLockCount); | |
313 | |
314 size_t used = rec->bytesUsed(); | |
315 SkASSERT(used <= fTotalBytesUsed); | |
316 | |
317 this->detach(rec); | |
318 #ifdef USE_HASH | |
319 fHash->remove(rec->getKey()); | |
320 #endif | |
321 | |
322 SkDELETE(rec); | |
323 | |
324 fTotalBytesUsed -= used; | |
325 fCount -= 1; | |
326 } | |
327 | |
289 void SkResourceCache::purgeAsNeeded() { | 328 void SkResourceCache::purgeAsNeeded() { |
290 size_t byteLimit; | 329 size_t byteLimit; |
291 int countLimit; | 330 int countLimit; |
292 | 331 |
293 if (fDiscardableFactory) { | 332 if (fDiscardableFactory) { |
294 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; | 333 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
295 byteLimit = SK_MaxU32; // no limit based on bytes | 334 byteLimit = SK_MaxU32; // no limit based on bytes |
296 } else { | 335 } else { |
297 countLimit = SK_MaxS32; // no limit based on count | 336 countLimit = SK_MaxS32; // no limit based on count |
298 byteLimit = fTotalByteLimit; | 337 byteLimit = fTotalByteLimit; |
299 } | 338 } |
300 | 339 |
301 size_t bytesUsed = fTotalBytesUsed; | |
302 int countUsed = fCount; | |
303 | |
304 Rec* rec = fTail; | 340 Rec* rec = fTail; |
305 while (rec) { | 341 while (rec) { |
306 if (bytesUsed < byteLimit && countUsed < countLimit) { | 342 if (fTotalBytesUsed < byteLimit && fCount < countLimit) { |
307 break; | 343 break; |
308 } | 344 } |
309 | 345 |
310 Rec* prev = rec->fPrev; | 346 Rec* prev = rec->fPrev; |
311 if (0 == rec->fLockCount) { | 347 if (0 == rec->fLockCount) { |
312 size_t used = rec->bytesUsed(); | 348 purgeRec(rec); |
313 SkASSERT(used <= bytesUsed); | |
314 this->detach(rec); | |
315 #ifdef USE_HASH | |
316 fHash->remove(rec->getKey()); | |
317 #endif | |
318 | |
319 SkDELETE(rec); | |
320 | |
321 bytesUsed -= used; | |
322 countUsed -= 1; | |
323 } | 349 } |
324 rec = prev; | 350 rec = prev; |
325 } | 351 } |
326 | |
327 fTotalBytesUsed = bytesUsed; | |
328 fCount = countUsed; | |
329 } | 352 } |
330 | 353 |
331 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { | 354 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { |
332 size_t prevLimit = fTotalByteLimit; | 355 size_t prevLimit = fTotalByteLimit; |
333 fTotalByteLimit = newLimit; | 356 fTotalByteLimit = newLimit; |
334 if (newLimit < prevLimit) { | 357 if (newLimit < prevLimit) { |
335 this->purgeAsNeeded(); | 358 this->purgeAsNeeded(); |
336 } | 359 } |
337 return prevLimit; | 360 return prevLimit; |
338 } | 361 } |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 return gResourceCache; | 522 return gResourceCache; |
500 } | 523 } |
501 | 524 |
502 void SkResourceCache::Unlock(SkResourceCache::ID id) { | 525 void SkResourceCache::Unlock(SkResourceCache::ID id) { |
503 SkAutoMutexAcquire am(gMutex); | 526 SkAutoMutexAcquire am(gMutex); |
504 get_cache()->unlock(id); | 527 get_cache()->unlock(id); |
505 | 528 |
506 // get_cache()->dump(); | 529 // get_cache()->dump(); |
507 } | 530 } |
508 | 531 |
532 void SkResourceCache::Remove(SkResourceCache::ID id) { | |
533 SkAutoMutexAcquire am(gMutex); | |
534 get_cache()->remove(id); | |
535 } | |
536 | |
509 size_t SkResourceCache::GetTotalBytesUsed() { | 537 size_t SkResourceCache::GetTotalBytesUsed() { |
510 SkAutoMutexAcquire am(gMutex); | 538 SkAutoMutexAcquire am(gMutex); |
511 return get_cache()->getTotalBytesUsed(); | 539 return get_cache()->getTotalBytesUsed(); |
512 } | 540 } |
513 | 541 |
514 size_t SkResourceCache::GetTotalByteLimit() { | 542 size_t SkResourceCache::GetTotalByteLimit() { |
515 SkAutoMutexAcquire am(gMutex); | 543 SkAutoMutexAcquire am(gMutex); |
516 return get_cache()->getTotalByteLimit(); | 544 return get_cache()->getTotalByteLimit(); |
517 } | 545 } |
518 | 546 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
573 } | 601 } |
574 | 602 |
575 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { | 603 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { |
576 return SkResourceCache::GetSingleAllocationByteLimit(); | 604 return SkResourceCache::GetSingleAllocationByteLimit(); |
577 } | 605 } |
578 | 606 |
579 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { | 607 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
580 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); | 608 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
581 } | 609 } |
582 | 610 |
OLD | NEW |