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