| 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(Rec* rec) { |
| 290 SkASSERT(0 == rec->fLockCount); |
| 291 |
| 292 size_t used = rec->bytesUsed(); |
| 293 SkASSERT(used <= fTotalBytesUsed); |
| 294 |
| 295 this->detach(rec); |
| 296 #ifdef USE_HASH |
| 297 fHash->remove(rec->getKey()); |
| 298 #endif |
| 299 |
| 300 SkDELETE(rec); |
| 301 |
| 302 fTotalBytesUsed -= used; |
| 303 fCount -= 1; |
| 304 } |
| 305 |
| 289 void SkResourceCache::purgeAsNeeded() { | 306 void SkResourceCache::purgeAsNeeded() { |
| 290 size_t byteLimit; | 307 size_t byteLimit; |
| 291 int countLimit; | 308 int countLimit; |
| 292 | 309 |
| 293 if (fDiscardableFactory) { | 310 if (fDiscardableFactory) { |
| 294 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; | 311 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
| 295 byteLimit = SK_MaxU32; // no limit based on bytes | 312 byteLimit = SK_MaxU32; // no limit based on bytes |
| 296 } else { | 313 } else { |
| 297 countLimit = SK_MaxS32; // no limit based on count | 314 countLimit = SK_MaxS32; // no limit based on count |
| 298 byteLimit = fTotalByteLimit; | 315 byteLimit = fTotalByteLimit; |
| 299 } | 316 } |
| 300 | 317 |
| 301 size_t bytesUsed = fTotalBytesUsed; | |
| 302 int countUsed = fCount; | |
| 303 | |
| 304 Rec* rec = fTail; | 318 Rec* rec = fTail; |
| 305 while (rec) { | 319 while (rec) { |
| 306 if (bytesUsed < byteLimit && countUsed < countLimit) { | 320 if (fTotalBytesUsed < byteLimit && fCount < countLimit) { |
| 307 break; | 321 break; |
| 308 } | 322 } |
| 309 | 323 |
| 310 Rec* prev = rec->fPrev; | 324 Rec* prev = rec->fPrev; |
| 311 if (0 == rec->fLockCount) { | 325 if (0 == rec->fLockCount) { |
| 312 size_t used = rec->bytesUsed(); | 326 this->remove(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 } | 327 } |
| 324 rec = prev; | 328 rec = prev; |
| 325 } | 329 } |
| 326 | |
| 327 fTotalBytesUsed = bytesUsed; | |
| 328 fCount = countUsed; | |
| 329 } | 330 } |
| 330 | 331 |
| 331 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { | 332 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { |
| 332 size_t prevLimit = fTotalByteLimit; | 333 size_t prevLimit = fTotalByteLimit; |
| 333 fTotalByteLimit = newLimit; | 334 fTotalByteLimit = newLimit; |
| 334 if (newLimit < prevLimit) { | 335 if (newLimit < prevLimit) { |
| 335 this->purgeAsNeeded(); | 336 this->purgeAsNeeded(); |
| 336 } | 337 } |
| 337 return prevLimit; | 338 return prevLimit; |
| 338 } | 339 } |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 return gResourceCache; | 500 return gResourceCache; |
| 500 } | 501 } |
| 501 | 502 |
| 502 void SkResourceCache::Unlock(SkResourceCache::ID id) { | 503 void SkResourceCache::Unlock(SkResourceCache::ID id) { |
| 503 SkAutoMutexAcquire am(gMutex); | 504 SkAutoMutexAcquire am(gMutex); |
| 504 get_cache()->unlock(id); | 505 get_cache()->unlock(id); |
| 505 | 506 |
| 506 // get_cache()->dump(); | 507 // get_cache()->dump(); |
| 507 } | 508 } |
| 508 | 509 |
| 510 void SkResourceCache::Remove(SkResourceCache::ID id) { |
| 511 SkAutoMutexAcquire am(gMutex); |
| 512 SkASSERT(id); |
| 513 |
| 514 #ifdef SK_DEBUG |
| 515 { |
| 516 bool found = false; |
| 517 Rec* rec = get_cache()->fHead; |
| 518 while (rec != NULL) { |
| 519 if (rec == id) { |
| 520 found = true; |
| 521 break; |
| 522 } |
| 523 rec = rec->fNext; |
| 524 } |
| 525 SkASSERT(found); |
| 526 } |
| 527 #endif |
| 528 const Rec* rec = id; |
| 529 get_cache()->remove(const_cast<Rec*>(rec)); |
| 530 } |
| 531 |
| 509 size_t SkResourceCache::GetTotalBytesUsed() { | 532 size_t SkResourceCache::GetTotalBytesUsed() { |
| 510 SkAutoMutexAcquire am(gMutex); | 533 SkAutoMutexAcquire am(gMutex); |
| 511 return get_cache()->getTotalBytesUsed(); | 534 return get_cache()->getTotalBytesUsed(); |
| 512 } | 535 } |
| 513 | 536 |
| 514 size_t SkResourceCache::GetTotalByteLimit() { | 537 size_t SkResourceCache::GetTotalByteLimit() { |
| 515 SkAutoMutexAcquire am(gMutex); | 538 SkAutoMutexAcquire am(gMutex); |
| 516 return get_cache()->getTotalByteLimit(); | 539 return get_cache()->getTotalByteLimit(); |
| 517 } | 540 } |
| 518 | 541 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 } | 596 } |
| 574 | 597 |
| 575 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { | 598 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { |
| 576 return SkResourceCache::GetSingleAllocationByteLimit(); | 599 return SkResourceCache::GetSingleAllocationByteLimit(); |
| 577 } | 600 } |
| 578 | 601 |
| 579 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { | 602 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
| 580 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); | 603 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
| 581 } | 604 } |
| 582 | 605 |
| OLD | NEW |