| 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 "SkMessageBus.h" | 9 #include "SkMessageBus.h" |
| 10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
| 11 #include "SkMutex.h" |
| 11 #include "SkPixelRef.h" | 12 #include "SkPixelRef.h" |
| 12 #include "SkResourceCache.h" | 13 #include "SkResourceCache.h" |
| 13 | 14 |
| 14 #include <stddef.h> | 15 #include <stddef.h> |
| 15 | 16 |
| 16 DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage) | 17 DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage) |
| 17 | 18 |
| 18 // This can be defined by the caller's build system | 19 // This can be defined by the caller's build system |
| 19 //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 20 //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
| 20 | 21 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 68 |
| 68 // One of these should be explicit set by the caller after we return. | 69 // One of these should be explicit set by the caller after we return. |
| 69 fTotalByteLimit = 0; | 70 fTotalByteLimit = 0; |
| 70 fDiscardableFactory = NULL; | 71 fDiscardableFactory = NULL; |
| 71 } | 72 } |
| 72 | 73 |
| 73 #include "SkDiscardableMemory.h" | 74 #include "SkDiscardableMemory.h" |
| 74 | 75 |
| 75 class SkOneShotDiscardablePixelRef : public SkPixelRef { | 76 class SkOneShotDiscardablePixelRef : public SkPixelRef { |
| 76 public: | 77 public: |
| 77 | 78 |
| 78 // Ownership of the discardablememory is transfered to the pixelref | 79 // Ownership of the discardablememory is transfered to the pixelref |
| 79 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_
t rowBytes); | 80 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_
t rowBytes); |
| 80 ~SkOneShotDiscardablePixelRef(); | 81 ~SkOneShotDiscardablePixelRef(); |
| 81 | 82 |
| 82 protected: | 83 protected: |
| 83 bool onNewLockPixels(LockRec*) override; | 84 bool onNewLockPixels(LockRec*) override; |
| 84 void onUnlockPixels() override; | 85 void onUnlockPixels() override; |
| 85 size_t getAllocatedSizeInBytes() const override; | 86 size_t getAllocatedSizeInBytes() const override; |
| 86 | 87 |
| 87 private: | 88 private: |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 i += 1; | 230 i += 1; |
| 230 size >>= 10; | 231 size >>= 10; |
| 231 } | 232 } |
| 232 str->printf("%zu%c", size, suffix[i]); | 233 str->printf("%zu%c", size, suffix[i]); |
| 233 } | 234 } |
| 234 | 235 |
| 235 static bool gDumpCacheTransactions; | 236 static bool gDumpCacheTransactions; |
| 236 | 237 |
| 237 void SkResourceCache::add(Rec* rec) { | 238 void SkResourceCache::add(Rec* rec) { |
| 238 this->checkMessages(); | 239 this->checkMessages(); |
| 239 | 240 |
| 240 SkASSERT(rec); | 241 SkASSERT(rec); |
| 241 // See if we already have this key (racy inserts, etc.) | 242 // See if we already have this key (racy inserts, etc.) |
| 242 Rec* existing = fHash->find(rec->getKey()); | 243 Rec* existing = fHash->find(rec->getKey()); |
| 243 if (existing) { | 244 if (existing) { |
| 244 SkDELETE(rec); | 245 SkDELETE(rec); |
| 245 return; | 246 return; |
| 246 } | 247 } |
| 247 | 248 |
| 248 this->addToHead(rec); | 249 this->addToHead(rec); |
| 249 fHash->add(rec); | 250 fHash->add(rec); |
| 250 | 251 |
| 251 if (gDumpCacheTransactions) { | 252 if (gDumpCacheTransactions) { |
| 252 SkString bytesStr, totalStr; | 253 SkString bytesStr, totalStr; |
| 253 make_size_str(rec->bytesUsed(), &bytesStr); | 254 make_size_str(rec->bytesUsed(), &bytesStr); |
| 254 make_size_str(fTotalBytesUsed, &totalStr); | 255 make_size_str(fTotalBytesUsed, &totalStr); |
| 255 SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n", | 256 SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n", |
| 256 bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount
); | 257 bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount
); |
| 257 } | 258 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 size_t prevLimit = fTotalByteLimit; | 351 size_t prevLimit = fTotalByteLimit; |
| 351 fTotalByteLimit = newLimit; | 352 fTotalByteLimit = newLimit; |
| 352 if (newLimit < prevLimit) { | 353 if (newLimit < prevLimit) { |
| 353 this->purgeAsNeeded(); | 354 this->purgeAsNeeded(); |
| 354 } | 355 } |
| 355 return prevLimit; | 356 return prevLimit; |
| 356 } | 357 } |
| 357 | 358 |
| 358 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { | 359 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
| 359 this->checkMessages(); | 360 this->checkMessages(); |
| 360 | 361 |
| 361 if (fDiscardableFactory) { | 362 if (fDiscardableFactory) { |
| 362 SkDiscardableMemory* dm = fDiscardableFactory(bytes); | 363 SkDiscardableMemory* dm = fDiscardableFactory(bytes); |
| 363 return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL; | 364 return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL; |
| 364 } else { | 365 } else { |
| 365 return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes)); | 366 return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes)); |
| 366 } | 367 } |
| 367 } | 368 } |
| 368 | 369 |
| 369 /////////////////////////////////////////////////////////////////////////////// | 370 /////////////////////////////////////////////////////////////////////////////// |
| 370 | 371 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 void SkResourceCache::checkMessages() { | 509 void SkResourceCache::checkMessages() { |
| 509 SkTArray<PurgeSharedIDMessage> msgs; | 510 SkTArray<PurgeSharedIDMessage> msgs; |
| 510 fPurgeSharedIDInbox.poll(&msgs); | 511 fPurgeSharedIDInbox.poll(&msgs); |
| 511 for (int i = 0; i < msgs.count(); ++i) { | 512 for (int i = 0; i < msgs.count(); ++i) { |
| 512 this->purgeSharedID(msgs[i].fSharedID); | 513 this->purgeSharedID(msgs[i].fSharedID); |
| 513 } | 514 } |
| 514 } | 515 } |
| 515 | 516 |
| 516 /////////////////////////////////////////////////////////////////////////////// | 517 /////////////////////////////////////////////////////////////////////////////// |
| 517 | 518 |
| 518 #include "SkThread.h" | |
| 519 | |
| 520 SK_DECLARE_STATIC_MUTEX(gMutex); | 519 SK_DECLARE_STATIC_MUTEX(gMutex); |
| 521 static SkResourceCache* gResourceCache = NULL; | 520 static SkResourceCache* gResourceCache = NULL; |
| 522 static void cleanup_gResourceCache() { | 521 static void cleanup_gResourceCache() { |
| 523 // We'll clean this up in our own tests, but disable for clients. | 522 // We'll clean this up in our own tests, but disable for clients. |
| 524 // Chrome seems to have funky multi-process things going on in unit tests th
at | 523 // Chrome seems to have funky multi-process things going on in unit tests th
at |
| 525 // makes this unsafe to delete when the main process atexit()s. | 524 // makes this unsafe to delete when the main process atexit()s. |
| 526 // SkLazyPtr does the same sort of thing. | 525 // SkLazyPtr does the same sort of thing. |
| 527 #if SK_DEVELOPER | 526 #if SK_DEVELOPER |
| 528 SkDELETE(gResourceCache); | 527 SkDELETE(gResourceCache); |
| 529 #endif | 528 #endif |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 | 637 |
| 639 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { | 638 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
| 640 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); | 639 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
| 641 } | 640 } |
| 642 | 641 |
| 643 void SkGraphics::PurgeResourceCache() { | 642 void SkGraphics::PurgeResourceCache() { |
| 644 SkImageFilter::PurgeCache(); | 643 SkImageFilter::PurgeCache(); |
| 645 return SkResourceCache::PurgeAll(); | 644 return SkResourceCache::PurgeAll(); |
| 646 } | 645 } |
| 647 | 646 |
| OLD | NEW |