| 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 "SkMessageBus.h" |
| 10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
| 11 #include "SkPixelRef.h" | 11 #include "SkPixelRef.h" |
| 12 #include "SkResourceCache.h" |
| 12 | 13 |
| 13 #include <stddef.h> | 14 #include <stddef.h> |
| 14 | 15 |
| 16 DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage) |
| 17 |
| 15 // This can be defined by the caller's build system | 18 // This can be defined by the caller's build system |
| 16 //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 19 //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
| 17 | 20 |
| 18 #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT | 21 #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT |
| 19 # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 | 22 # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 |
| 20 #endif | 23 #endif |
| 21 | 24 |
| 22 #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT | 25 #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT |
| 23 #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024) | 26 #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024) |
| 24 #endif | 27 #endif |
| 25 | 28 |
| 26 void SkResourceCache::Key::init(void* nameSpace, size_t length) { | 29 void SkResourceCache::Key::init(void* nameSpace, uint64_t sharedID, size_t lengt
h) { |
| 27 SkASSERT(SkAlign4(length) == length); | 30 SkASSERT(SkAlign4(length) == length); |
| 28 | 31 |
| 29 // fCount32 and fHash are not hashed | 32 // fCount32 and fHash are not hashed |
| 30 static const int kUnhashedLocal32s = 2; | 33 static const int kUnhashedLocal32s = 2; // fCache32 + fHash |
| 31 static const int kLocal32s = kUnhashedLocal32s + (sizeof(fNamespace) >> 2); | 34 static const int kSharedIDLocal32s = 2; // fSharedID_lo + fSharedID_hi |
| 35 static const int kHashedLocal32s = kSharedIDLocal32s + (sizeof(fNamespace) >
> 2); |
| 36 static const int kLocal32s = kUnhashedLocal32s + kHashedLocal32s; |
| 32 | 37 |
| 33 SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals); | 38 SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals); |
| 34 SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespa
ce), | 39 SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespa
ce), |
| 35 namespace_field_must_be_last); | 40 namespace_field_must_be_last); |
| 36 | 41 |
| 37 fCount32 = SkToS32(kLocal32s + (length >> 2)); | 42 fCount32 = SkToS32(kLocal32s + (length >> 2)); |
| 43 fSharedID_lo = (uint32_t)sharedID; |
| 44 fSharedID_hi = (uint32_t)(sharedID >> 32); |
| 38 fNamespace = nameSpace; | 45 fNamespace = nameSpace; |
| 39 // skip unhashed fields when computing the murmur | 46 // skip unhashed fields when computing the murmur |
| 40 fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s, | 47 fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s, |
| 41 (fCount32 - kUnhashedLocal32s) << 2); | 48 (fCount32 - kUnhashedLocal32s) << 2); |
| 42 } | 49 } |
| 43 | 50 |
| 44 #include "SkTDynamicHash.h" | 51 #include "SkTDynamicHash.h" |
| 45 | 52 |
| 46 class SkResourceCache::Hash : | 53 class SkResourceCache::Hash : |
| 47 public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; | 54 public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 while (rec) { | 199 while (rec) { |
| 193 Rec* next = rec->fNext; | 200 Rec* next = rec->fNext; |
| 194 SkDELETE(rec); | 201 SkDELETE(rec); |
| 195 rec = next; | 202 rec = next; |
| 196 } | 203 } |
| 197 delete fHash; | 204 delete fHash; |
| 198 } | 205 } |
| 199 | 206 |
| 200 //////////////////////////////////////////////////////////////////////////////// | 207 //////////////////////////////////////////////////////////////////////////////// |
| 201 | 208 |
| 202 bool SkResourceCache::find(const Key& key, VisitorProc visitor, void* context) { | 209 bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) { |
| 210 this->checkMessages(); |
| 211 |
| 203 Rec* rec = fHash->find(key); | 212 Rec* rec = fHash->find(key); |
| 204 if (rec) { | 213 if (rec) { |
| 205 if (visitor(*rec, context)) { | 214 if (visitor(*rec, context)) { |
| 206 this->moveToHead(rec); // for our LRU | 215 this->moveToHead(rec); // for our LRU |
| 207 return true; | 216 return true; |
| 208 } else { | 217 } else { |
| 209 this->remove(rec); // stale | 218 this->remove(rec); // stale |
| 210 return false; | 219 return false; |
| 211 } | 220 } |
| 212 } | 221 } |
| 213 return false; | 222 return false; |
| 214 } | 223 } |
| 215 | 224 |
| 216 static void make_size_str(size_t size, SkString* str) { | 225 static void make_size_str(size_t size, SkString* str) { |
| 217 const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 }; | 226 const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 }; |
| 218 int i = 0; | 227 int i = 0; |
| 219 while (suffix[i] && (size > 1024)) { | 228 while (suffix[i] && (size > 1024)) { |
| 220 i += 1; | 229 i += 1; |
| 221 size >>= 10; | 230 size >>= 10; |
| 222 } | 231 } |
| 223 str->printf("%zu%c", size, suffix[i]); | 232 str->printf("%zu%c", size, suffix[i]); |
| 224 } | 233 } |
| 225 | 234 |
| 226 static bool gDumpCacheTransactions; | 235 static bool gDumpCacheTransactions; |
| 227 | 236 |
| 228 void SkResourceCache::add(Rec* rec) { | 237 void SkResourceCache::add(Rec* rec) { |
| 238 this->checkMessages(); |
| 239 |
| 229 SkASSERT(rec); | 240 SkASSERT(rec); |
| 230 // See if we already have this key (racy inserts, etc.) | 241 // See if we already have this key (racy inserts, etc.) |
| 231 Rec* existing = fHash->find(rec->getKey()); | 242 Rec* existing = fHash->find(rec->getKey()); |
| 232 if (existing) { | 243 if (existing) { |
| 233 SkDELETE(rec); | 244 SkDELETE(rec); |
| 234 return; | 245 return; |
| 235 } | 246 } |
| 236 | 247 |
| 237 this->addToHead(rec); | 248 this->addToHead(rec); |
| 238 fHash->add(rec); | 249 fHash->add(rec); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { | 298 if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { |
| 288 break; | 299 break; |
| 289 } | 300 } |
| 290 | 301 |
| 291 Rec* prev = rec->fPrev; | 302 Rec* prev = rec->fPrev; |
| 292 this->remove(rec); | 303 this->remove(rec); |
| 293 rec = prev; | 304 rec = prev; |
| 294 } | 305 } |
| 295 } | 306 } |
| 296 | 307 |
| 308 void SkResourceCache::purgeSharedID(uint64_t sharedID) { |
| 309 if (0 == sharedID) { |
| 310 return; |
| 311 } |
| 312 |
| 313 // go backwards, just like purgeAsNeeded, just to make the code similar. |
| 314 // could iterate either direction and still be correct. |
| 315 Rec* rec = fTail; |
| 316 while (rec) { |
| 317 Rec* prev = rec->fPrev; |
| 318 if (rec->getKey().getSharedID() == sharedID) { |
| 319 // SkDebugf("purgeSharedID id=%llx rec=%p\n", sharedID, rec); |
| 320 this->remove(rec); |
| 321 } |
| 322 rec = prev; |
| 323 } |
| 324 } |
| 325 |
| 297 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { | 326 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { |
| 298 size_t prevLimit = fTotalByteLimit; | 327 size_t prevLimit = fTotalByteLimit; |
| 299 fTotalByteLimit = newLimit; | 328 fTotalByteLimit = newLimit; |
| 300 if (newLimit < prevLimit) { | 329 if (newLimit < prevLimit) { |
| 301 this->purgeAsNeeded(); | 330 this->purgeAsNeeded(); |
| 302 } | 331 } |
| 303 return prevLimit; | 332 return prevLimit; |
| 304 } | 333 } |
| 305 | 334 |
| 306 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { | 335 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
| 336 this->checkMessages(); |
| 337 |
| 307 if (fDiscardableFactory) { | 338 if (fDiscardableFactory) { |
| 308 SkDiscardableMemory* dm = fDiscardableFactory(bytes); | 339 SkDiscardableMemory* dm = fDiscardableFactory(bytes); |
| 309 return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL; | 340 return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL; |
| 310 } else { | 341 } else { |
| 311 return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes)); | 342 return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes)); |
| 312 } | 343 } |
| 313 } | 344 } |
| 314 | 345 |
| 315 /////////////////////////////////////////////////////////////////////////////// | 346 /////////////////////////////////////////////////////////////////////////////// |
| 316 | 347 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 if (NULL == fDiscardableFactory) { | 475 if (NULL == fDiscardableFactory) { |
| 445 if (0 == limit) { | 476 if (0 == limit) { |
| 446 limit = fTotalByteLimit; | 477 limit = fTotalByteLimit; |
| 447 } else { | 478 } else { |
| 448 limit = SkTMin(limit, fTotalByteLimit); | 479 limit = SkTMin(limit, fTotalByteLimit); |
| 449 } | 480 } |
| 450 } | 481 } |
| 451 return limit; | 482 return limit; |
| 452 } | 483 } |
| 453 | 484 |
| 485 void SkResourceCache::checkMessages() { |
| 486 SkTArray<PurgeSharedIDMessage> msgs; |
| 487 fPurgeSharedIDInbox.poll(&msgs); |
| 488 for (int i = 0; i < msgs.count(); ++i) { |
| 489 this->purgeSharedID(msgs[i].fSharedID); |
| 490 } |
| 491 } |
| 492 |
| 454 /////////////////////////////////////////////////////////////////////////////// | 493 /////////////////////////////////////////////////////////////////////////////// |
| 455 | 494 |
| 456 #include "SkThread.h" | 495 #include "SkThread.h" |
| 457 | 496 |
| 458 SK_DECLARE_STATIC_MUTEX(gMutex); | 497 SK_DECLARE_STATIC_MUTEX(gMutex); |
| 459 static SkResourceCache* gResourceCache = NULL; | 498 static SkResourceCache* gResourceCache = NULL; |
| 460 static void cleanup_gResourceCache() { | 499 static void cleanup_gResourceCache() { |
| 461 // We'll clean this up in our own tests, but disable for clients. | 500 // 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 | 501 // 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. | 502 // makes this unsafe to delete when the main process atexit()s. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { | 569 size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { |
| 531 SkAutoMutexAcquire am(gMutex); | 570 SkAutoMutexAcquire am(gMutex); |
| 532 return get_cache()->getEffectiveSingleAllocationByteLimit(); | 571 return get_cache()->getEffectiveSingleAllocationByteLimit(); |
| 533 } | 572 } |
| 534 | 573 |
| 535 void SkResourceCache::PurgeAll() { | 574 void SkResourceCache::PurgeAll() { |
| 536 SkAutoMutexAcquire am(gMutex); | 575 SkAutoMutexAcquire am(gMutex); |
| 537 return get_cache()->purgeAll(); | 576 return get_cache()->purgeAll(); |
| 538 } | 577 } |
| 539 | 578 |
| 540 bool SkResourceCache::Find(const Key& key, VisitorProc visitor, void* context) { | 579 bool SkResourceCache::Find(const Key& key, FindVisitor visitor, void* context) { |
| 541 SkAutoMutexAcquire am(gMutex); | 580 SkAutoMutexAcquire am(gMutex); |
| 542 return get_cache()->find(key, visitor, context); | 581 return get_cache()->find(key, visitor, context); |
| 543 } | 582 } |
| 544 | 583 |
| 545 void SkResourceCache::Add(Rec* rec) { | 584 void SkResourceCache::Add(Rec* rec) { |
| 546 SkAutoMutexAcquire am(gMutex); | 585 SkAutoMutexAcquire am(gMutex); |
| 547 get_cache()->add(rec); | 586 get_cache()->add(rec); |
| 548 } | 587 } |
| 549 | 588 |
| 589 void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) { |
| 590 if (sharedID) { |
| 591 SkMessageBus<PurgeSharedIDMessage>::Post(PurgeSharedIDMessage(sharedID))
; |
| 592 } |
| 593 } |
| 594 |
| 550 /////////////////////////////////////////////////////////////////////////////// | 595 /////////////////////////////////////////////////////////////////////////////// |
| 551 | 596 |
| 552 #include "SkGraphics.h" | 597 #include "SkGraphics.h" |
| 553 | 598 |
| 554 size_t SkGraphics::GetResourceCacheTotalBytesUsed() { | 599 size_t SkGraphics::GetResourceCacheTotalBytesUsed() { |
| 555 return SkResourceCache::GetTotalBytesUsed(); | 600 return SkResourceCache::GetTotalBytesUsed(); |
| 556 } | 601 } |
| 557 | 602 |
| 558 size_t SkGraphics::GetResourceCacheTotalByteLimit() { | 603 size_t SkGraphics::GetResourceCacheTotalByteLimit() { |
| 559 return SkResourceCache::GetTotalByteLimit(); | 604 return SkResourceCache::GetTotalByteLimit(); |
| 560 } | 605 } |
| 561 | 606 |
| 562 size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) { | 607 size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) { |
| 563 return SkResourceCache::SetTotalByteLimit(newLimit); | 608 return SkResourceCache::SetTotalByteLimit(newLimit); |
| 564 } | 609 } |
| 565 | 610 |
| 566 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { | 611 size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { |
| 567 return SkResourceCache::GetSingleAllocationByteLimit(); | 612 return SkResourceCache::GetSingleAllocationByteLimit(); |
| 568 } | 613 } |
| 569 | 614 |
| 570 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { | 615 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
| 571 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); | 616 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
| 572 } | 617 } |
| 573 | 618 |
| 574 void SkGraphics::PurgeResourceCache() { | 619 void SkGraphics::PurgeResourceCache() { |
| 575 return SkResourceCache::PurgeAll(); | 620 return SkResourceCache::PurgeAll(); |
| 576 } | 621 } |
| 577 | 622 |
| OLD | NEW |