| 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" |
| 11 #include "SkPixelRef.h" | 11 #include "SkPixelRef.h" |
| 12 | 12 |
| 13 #include <stddef.h> | 13 #include <stddef.h> |
| 14 | 14 |
| 15 // This can be defined by the caller's build system | 15 // This can be defined by the caller's build system |
| 16 //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 16 //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
| 17 | 17 |
| 18 #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT | 18 #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT |
| 19 # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 | 19 # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT | 22 #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT |
| 23 #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024) | 23 #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024) |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 void SkResourceCache::Key::init(void* nameSpace, size_t length) { | 26 void SkResourceCache::Key::init(void* nameSpace, uint64_t sharedID, size_t lengt
h) { |
| 27 SkASSERT(SkAlign4(length) == length); | 27 SkASSERT(SkAlign4(length) == length); |
| 28 | 28 |
| 29 // fCount32 and fHash are not hashed | 29 // fCount32 and fHash are not hashed |
| 30 static const int kUnhashedLocal32s = 2; | 30 static const int kUnhashedLocal32s = 2; |
| 31 static const int kLocal32s = kUnhashedLocal32s + (sizeof(fNamespace) >> 2); | 31 static const int kHashedLocal32s = (sizeof(fSharedID) + sizeof(fNamespace))
>> 2; |
| 32 static const int kLocal32s = kUnhashedLocal32s + kHashedLocal32s; |
| 32 | 33 |
| 33 SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals); | 34 SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals); |
| 34 SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespa
ce), | 35 SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespa
ce), |
| 35 namespace_field_must_be_last); | 36 namespace_field_must_be_last); |
| 36 | 37 |
| 37 fCount32 = SkToS32(kLocal32s + (length >> 2)); | 38 fCount32 = SkToS32(kLocal32s + (length >> 2)); |
| 39 fSharedID = sharedID; |
| 38 fNamespace = nameSpace; | 40 fNamespace = nameSpace; |
| 39 // skip unhashed fields when computing the murmur | 41 // skip unhashed fields when computing the murmur |
| 40 fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s, | 42 fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s, |
| 41 (fCount32 - kUnhashedLocal32s) << 2); | 43 (fCount32 - kUnhashedLocal32s) << 2); |
| 42 } | 44 } |
| 43 | 45 |
| 44 #include "SkTDynamicHash.h" | 46 #include "SkTDynamicHash.h" |
| 45 | 47 |
| 46 class SkResourceCache::Hash : | 48 class SkResourceCache::Hash : |
| 47 public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; | 49 public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 while (rec) { | 194 while (rec) { |
| 193 Rec* next = rec->fNext; | 195 Rec* next = rec->fNext; |
| 194 SkDELETE(rec); | 196 SkDELETE(rec); |
| 195 rec = next; | 197 rec = next; |
| 196 } | 198 } |
| 197 delete fHash; | 199 delete fHash; |
| 198 } | 200 } |
| 199 | 201 |
| 200 //////////////////////////////////////////////////////////////////////////////// | 202 //////////////////////////////////////////////////////////////////////////////// |
| 201 | 203 |
| 202 bool SkResourceCache::find(const Key& key, VisitorProc visitor, void* context) { | 204 bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) { |
| 203 Rec* rec = fHash->find(key); | 205 Rec* rec = fHash->find(key); |
| 204 if (rec) { | 206 if (rec) { |
| 205 if (visitor(*rec, context)) { | 207 if (visitor(*rec, context)) { |
| 206 this->moveToHead(rec); // for our LRU | 208 this->moveToHead(rec); // for our LRU |
| 207 return true; | 209 return true; |
| 208 } else { | 210 } else { |
| 209 this->remove(rec); // stale | 211 this->remove(rec); // stale |
| 210 return false; | 212 return false; |
| 211 } | 213 } |
| 212 } | 214 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { | 289 if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { |
| 288 break; | 290 break; |
| 289 } | 291 } |
| 290 | 292 |
| 291 Rec* prev = rec->fPrev; | 293 Rec* prev = rec->fPrev; |
| 292 this->remove(rec); | 294 this->remove(rec); |
| 293 rec = prev; | 295 rec = prev; |
| 294 } | 296 } |
| 295 } | 297 } |
| 296 | 298 |
| 299 void SkResourceCache::purgeSharedID(uint64_t sharedID) { |
| 300 if (0 == sharedID) { |
| 301 return; |
| 302 } |
| 303 |
| 304 // go backwards, just like purgeAsNeeded, just to make the code similar. |
| 305 // could iterate either direction and still be correct. |
| 306 Rec* rec = fTail; |
| 307 while (rec) { |
| 308 Rec* prev = rec->fPrev; |
| 309 if (rec->getKey().getSharedID() == sharedID) { |
| 310 this->remove(rec); |
| 311 } |
| 312 rec = prev; |
| 313 } |
| 314 } |
| 315 |
| 297 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { | 316 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { |
| 298 size_t prevLimit = fTotalByteLimit; | 317 size_t prevLimit = fTotalByteLimit; |
| 299 fTotalByteLimit = newLimit; | 318 fTotalByteLimit = newLimit; |
| 300 if (newLimit < prevLimit) { | 319 if (newLimit < prevLimit) { |
| 301 this->purgeAsNeeded(); | 320 this->purgeAsNeeded(); |
| 302 } | 321 } |
| 303 return prevLimit; | 322 return prevLimit; |
| 304 } | 323 } |
| 305 | 324 |
| 306 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { | 325 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 if (NULL == fDiscardableFactory) { | 463 if (NULL == fDiscardableFactory) { |
| 445 if (0 == limit) { | 464 if (0 == limit) { |
| 446 limit = fTotalByteLimit; | 465 limit = fTotalByteLimit; |
| 447 } else { | 466 } else { |
| 448 limit = SkTMin(limit, fTotalByteLimit); | 467 limit = SkTMin(limit, fTotalByteLimit); |
| 449 } | 468 } |
| 450 } | 469 } |
| 451 return limit; | 470 return limit; |
| 452 } | 471 } |
| 453 | 472 |
| 473 void SkResourceCache::checkMessages() { |
| 474 SkTDArray<uint64_t>* msgs = DetachPurgeMessages(); |
| 475 if (msgs) { |
| 476 for (int i = 0; i < msgs->count(); ++i) { |
| 477 this->purgeSharedID(msgs->getAt(i)); |
| 478 } |
| 479 delete msgs; |
| 480 } |
| 481 } |
| 482 |
| 454 /////////////////////////////////////////////////////////////////////////////// | 483 /////////////////////////////////////////////////////////////////////////////// |
| 455 | 484 |
| 456 #include "SkThread.h" | 485 #include "SkThread.h" |
| 457 | 486 |
| 458 SK_DECLARE_STATIC_MUTEX(gMutex); | 487 SK_DECLARE_STATIC_MUTEX(gMutex); |
| 459 static SkResourceCache* gResourceCache = NULL; | 488 static SkResourceCache* gResourceCache = NULL; |
| 460 static void cleanup_gResourceCache() { | 489 static void cleanup_gResourceCache() { |
| 461 // We'll clean this up in our own tests, but disable for clients. | 490 // We'll clean this up in our own tests, but disable for clients. |
| 462 // Chrome seems to have funky multi-process things going on in unit tests th
at | 491 // Chrome seems to have funky multi-process things going on in unit tests th
at |
| 463 // makes this unsafe to delete when the main process atexit()s. | 492 // makes this unsafe to delete when the main process atexit()s. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 return get_cache()->discardableFactory(); | 531 return get_cache()->discardableFactory(); |
| 503 } | 532 } |
| 504 | 533 |
| 505 SkBitmap::Allocator* SkResourceCache::GetAllocator() { | 534 SkBitmap::Allocator* SkResourceCache::GetAllocator() { |
| 506 SkAutoMutexAcquire am(gMutex); | 535 SkAutoMutexAcquire am(gMutex); |
| 507 return get_cache()->allocator(); | 536 return get_cache()->allocator(); |
| 508 } | 537 } |
| 509 | 538 |
| 510 SkCachedData* SkResourceCache::NewCachedData(size_t bytes) { | 539 SkCachedData* SkResourceCache::NewCachedData(size_t bytes) { |
| 511 SkAutoMutexAcquire am(gMutex); | 540 SkAutoMutexAcquire am(gMutex); |
| 541 get_cache()->checkMessages(); |
| 512 return get_cache()->newCachedData(bytes); | 542 return get_cache()->newCachedData(bytes); |
| 513 } | 543 } |
| 514 | 544 |
| 515 void SkResourceCache::Dump() { | 545 void SkResourceCache::Dump() { |
| 516 SkAutoMutexAcquire am(gMutex); | 546 SkAutoMutexAcquire am(gMutex); |
| 517 get_cache()->dump(); | 547 get_cache()->dump(); |
| 518 } | 548 } |
| 519 | 549 |
| 520 size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) { | 550 size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) { |
| 521 SkAutoMutexAcquire am(gMutex); | 551 SkAutoMutexAcquire am(gMutex); |
| 522 return get_cache()->setSingleAllocationByteLimit(size); | 552 return get_cache()->setSingleAllocationByteLimit(size); |
| 523 } | 553 } |
| 524 | 554 |
| 525 size_t SkResourceCache::GetSingleAllocationByteLimit() { | 555 size_t SkResourceCache::GetSingleAllocationByteLimit() { |
| 526 SkAutoMutexAcquire am(gMutex); | 556 SkAutoMutexAcquire am(gMutex); |
| 527 return get_cache()->getSingleAllocationByteLimit(); | 557 return get_cache()->getSingleAllocationByteLimit(); |
| 528 } | 558 } |
| 529 | 559 |
| 530 size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { | 560 size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { |
| 531 SkAutoMutexAcquire am(gMutex); | 561 SkAutoMutexAcquire am(gMutex); |
| 532 return get_cache()->getEffectiveSingleAllocationByteLimit(); | 562 return get_cache()->getEffectiveSingleAllocationByteLimit(); |
| 533 } | 563 } |
| 534 | 564 |
| 565 void SkResourceCache::PurgeSharedID(uint64_t sharedID) { |
| 566 if (sharedID) { |
| 567 SkAutoMutexAcquire am(gMutex); |
| 568 return get_cache()->purgeSharedID(sharedID); |
| 569 } |
| 570 } |
| 571 |
| 535 void SkResourceCache::PurgeAll() { | 572 void SkResourceCache::PurgeAll() { |
| 536 SkAutoMutexAcquire am(gMutex); | 573 SkAutoMutexAcquire am(gMutex); |
| 537 return get_cache()->purgeAll(); | 574 return get_cache()->purgeAll(); |
| 538 } | 575 } |
| 539 | 576 |
| 540 bool SkResourceCache::Find(const Key& key, VisitorProc visitor, void* context) { | 577 bool SkResourceCache::Find(const Key& key, FindVisitor visitor, void* context) { |
| 541 SkAutoMutexAcquire am(gMutex); | 578 SkAutoMutexAcquire am(gMutex); |
| 579 get_cache()->checkMessages(); |
| 542 return get_cache()->find(key, visitor, context); | 580 return get_cache()->find(key, visitor, context); |
| 543 } | 581 } |
| 544 | 582 |
| 545 void SkResourceCache::Add(Rec* rec) { | 583 void SkResourceCache::Add(Rec* rec) { |
| 546 SkAutoMutexAcquire am(gMutex); | 584 SkAutoMutexAcquire am(gMutex); |
| 585 get_cache()->checkMessages(); |
| 547 get_cache()->add(rec); | 586 get_cache()->add(rec); |
| 548 } | 587 } |
| 549 | 588 |
| 550 /////////////////////////////////////////////////////////////////////////////// | 589 /////////////////////////////////////////////////////////////////////////////// |
| 551 | 590 |
| 591 static SkTDArray<uint64_t>* gPurgePayloads; |
| 592 static SkMutex gPuragePayloadMutex; |
| 593 |
| 594 void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) { |
| 595 if (sharedID) { |
| 596 SkAutoMutexAcquire am(gPuragePayloadMutex); |
| 597 if (NULL == gPurgePayloads) { |
| 598 gPurgePayloads = new SkTDArray<uint64_t>; |
| 599 } |
| 600 *gPurgePayloads->append() = sharedID; |
| 601 } |
| 602 } |
| 603 |
| 604 SkTDArray<uint64_t>* SkResourceCache::DetachPurgeMessages() { |
| 605 SkAutoMutexAcquire am(gPuragePayloadMutex); |
| 606 SkTDArray<uint64_t>* payloads = gPurgePayloads; |
| 607 gPurgePayloads = NULL; |
| 608 return payloads; |
| 609 } |
| 610 |
| 611 /////////////////////////////////////////////////////////////////////////////// |
| 612 |
| 552 #include "SkGraphics.h" | 613 #include "SkGraphics.h" |
| 553 | 614 |
| 554 size_t SkGraphics::GetResourceCacheTotalBytesUsed() { | 615 size_t SkGraphics::GetResourceCacheTotalBytesUsed() { |
| 555 return SkResourceCache::GetTotalBytesUsed(); | 616 return SkResourceCache::GetTotalBytesUsed(); |
| 556 } | 617 } |
| 557 | 618 |
| 558 size_t SkGraphics::GetResourceCacheTotalByteLimit() { | 619 size_t SkGraphics::GetResourceCacheTotalByteLimit() { |
| 559 return SkResourceCache::GetTotalByteLimit(); | 620 return SkResourceCache::GetTotalByteLimit(); |
| 560 } | 621 } |
| 561 | 622 |
| 562 size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) { | 623 size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) { |
| 563 return SkResourceCache::SetTotalByteLimit(newLimit); | 624 return SkResourceCache::SetTotalByteLimit(newLimit); |
| 564 } | 625 } |
| 565 | 626 |
| 566 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { | 627 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { |
| 567 return SkResourceCache::GetSingleAllocationByteLimit(); | 628 return SkResourceCache::GetSingleAllocationByteLimit(); |
| 568 } | 629 } |
| 569 | 630 |
| 570 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { | 631 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
| 571 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); | 632 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
| 572 } | 633 } |
| 573 | 634 |
| 574 void SkGraphics::PurgeResourceCache() { | 635 void SkGraphics::PurgeResourceCache() { |
| 575 return SkResourceCache::PurgeAll(); | 636 return SkResourceCache::PurgeAll(); |
| 576 } | 637 } |
| 577 | 638 |
| OLD | NEW |