reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 67a3271 | 2014-07-10 06:03:46 -0700 | [diff] [blame] | 8 | #include "SkChecksum.h" |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 9 | #include "SkMessageBus.h" |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 10 | #include "SkMipMap.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 11 | #include "SkMutex.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 13 | #include "SkResourceCache.h" |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 14 | #include "SkTraceMemoryDump.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 15 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 16 | #include <stddef.h> |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 18 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 19 | DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage) |
| 20 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 21 | // This can be defined by the caller's build system |
| 22 | //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
| 23 | |
| 24 | #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT |
| 25 | # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 |
| 26 | #endif |
| 27 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 28 | #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT |
reed | ae48877 | 2015-09-11 08:19:23 -0700 | [diff] [blame] | 29 | #define SK_DEFAULT_IMAGE_CACHE_LIMIT (32 * 1024 * 1024) |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
bungeman | 70bb808 | 2016-02-17 10:13:49 -0800 | [diff] [blame] | 32 | void SkResourceCache::Key::init(void* nameSpace, uint64_t sharedID, size_t dataSize) { |
| 33 | SkASSERT(SkAlign4(dataSize) == dataSize); |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 34 | |
| 35 | // fCount32 and fHash are not hashed |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 36 | static const int kUnhashedLocal32s = 2; // fCache32 + fHash |
| 37 | static const int kSharedIDLocal32s = 2; // fSharedID_lo + fSharedID_hi |
| 38 | static const int kHashedLocal32s = kSharedIDLocal32s + (sizeof(fNamespace) >> 2); |
| 39 | static const int kLocal32s = kUnhashedLocal32s + kHashedLocal32s; |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 40 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 41 | static_assert(sizeof(Key) == (kLocal32s << 2), "unaccounted_key_locals"); |
| 42 | static_assert(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespace), |
| 43 | "namespace_field_must_be_last"); |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 44 | |
bungeman | 70bb808 | 2016-02-17 10:13:49 -0800 | [diff] [blame] | 45 | fCount32 = SkToS32(kLocal32s + (dataSize >> 2)); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 46 | fSharedID_lo = (uint32_t)sharedID; |
| 47 | fSharedID_hi = (uint32_t)(sharedID >> 32); |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 48 | fNamespace = nameSpace; |
| 49 | // skip unhashed fields when computing the murmur |
| 50 | fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s, |
| 51 | (fCount32 - kUnhashedLocal32s) << 2); |
reed | 4f987e9 | 2014-08-20 13:41:56 -0700 | [diff] [blame] | 52 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 53 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 54 | #include "SkTDynamicHash.h" |
| 55 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 56 | class SkResourceCache::Hash : |
| 57 | public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; |
commit-bot@chromium.org | 158f646 | 2014-04-02 17:03:09 +0000 | [diff] [blame] | 58 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 62 | void SkResourceCache::init() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | fHead = nullptr; |
| 64 | fTail = nullptr; |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 65 | fHash = new Hash; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 66 | fTotalBytesUsed = 0; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 67 | fCount = 0; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 68 | fSingleAllocationByteLimit = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | fAllocator = nullptr; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 70 | |
| 71 | // One of these should be explicit set by the caller after we return. |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 72 | fTotalByteLimit = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | fDiscardableFactory = nullptr; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | #include "SkDiscardableMemory.h" |
| 77 | |
| 78 | class SkOneShotDiscardablePixelRef : public SkPixelRef { |
| 79 | public: |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 80 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 81 | // Ownership of the discardablememory is transfered to the pixelref |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 82 | // The pixelref will ref() the colortable (if not NULL), and unref() in destructor |
| 83 | SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_t rowBytes, |
| 84 | SkColorTable*); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 85 | ~SkOneShotDiscardablePixelRef(); |
| 86 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 87 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | bool onNewLockPixels(LockRec*) override; |
| 89 | void onUnlockPixels() override; |
| 90 | size_t getAllocatedSizeInBytes() const override; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 91 | |
ssid | 8523782 | 2015-09-04 08:29:24 -0700 | [diff] [blame] | 92 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return fDM; } |
| 93 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 94 | private: |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 95 | SkDiscardableMemory* fDM; |
| 96 | size_t fRB; |
| 97 | bool fFirstTime; |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 98 | SkColorTable* fCTable; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 99 | |
| 100 | typedef SkPixelRef INHERITED; |
| 101 | }; |
| 102 | |
| 103 | SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& info, |
| 104 | SkDiscardableMemory* dm, |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 105 | size_t rowBytes, |
| 106 | SkColorTable* ctable) |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 107 | : INHERITED(info) |
| 108 | , fDM(dm) |
| 109 | , fRB(rowBytes) |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 110 | , fCTable(ctable) |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 111 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 112 | SkASSERT(dm->data()); |
| 113 | fFirstTime = true; |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 114 | SkSafeRef(ctable); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 115 | } |
| 116 | |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 117 | SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { |
| 118 | delete fDM; |
| 119 | SkSafeUnref(fCTable); |
| 120 | } |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 121 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 122 | bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 123 | if (fFirstTime) { |
| 124 | // we're already locked |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 125 | SkASSERT(fDM->data()); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 126 | fFirstTime = false; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 127 | goto SUCCESS; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 128 | } |
commit-bot@chromium.org | 281713e | 2013-12-12 18:08:08 +0000 | [diff] [blame] | 129 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 130 | // A previous call to onUnlock may have deleted our DM, so check for that |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 131 | if (nullptr == fDM) { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 132 | return false; |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (!fDM->lock()) { |
| 136 | // since it failed, we delete it now, to free-up the resource |
| 137 | delete fDM; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | fDM = nullptr; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 139 | return false; |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 140 | } |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 141 | |
| 142 | SUCCESS: |
| 143 | rec->fPixels = fDM->data(); |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 144 | rec->fColorTable = fCTable; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 145 | rec->fRowBytes = fRB; |
| 146 | return true; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void SkOneShotDiscardablePixelRef::onUnlockPixels() { |
| 150 | SkASSERT(!fFirstTime); |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 151 | fDM->unlock(); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 155 | return this->info().getSafeSize(fRB); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 156 | } |
| 157 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 158 | class SkResourceCacheDiscardableAllocator : public SkBitmap::Allocator { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 159 | public: |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 160 | SkResourceCacheDiscardableAllocator(SkResourceCache::DiscardableFactory factory) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 161 | SkASSERT(factory); |
| 162 | fFactory = factory; |
| 163 | } |
| 164 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 165 | bool allocPixelRef(SkBitmap*, SkColorTable*) override; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 166 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 167 | private: |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 168 | SkResourceCache::DiscardableFactory fFactory; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 171 | bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 172 | size_t size = bitmap->getSize(); |
sugoi | c197c8a | 2014-07-03 10:44:26 -0700 | [diff] [blame] | 173 | uint64_t size64 = bitmap->computeSize64(); |
| 174 | if (0 == size || size64 > (uint64_t)size) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 178 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
| 179 | if (!ctable) { |
| 180 | return false; |
| 181 | } |
| 182 | } else { |
| 183 | ctable = nullptr; |
| 184 | } |
| 185 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 186 | SkDiscardableMemory* dm = fFactory(size); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | if (nullptr == dm) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 191 | SkImageInfo info = bitmap->info(); |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 192 | bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBytes(), |
| 193 | ctable))->unref(); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 194 | bitmap->lockPixels(); |
| 195 | return bitmap->readyToDraw(); |
| 196 | } |
| 197 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 198 | SkResourceCache::SkResourceCache(DiscardableFactory factory) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 199 | this->init(); |
| 200 | fDiscardableFactory = factory; |
| 201 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 202 | fAllocator = new SkResourceCacheDiscardableAllocator(factory); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 203 | } |
| 204 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 205 | SkResourceCache::SkResourceCache(size_t byteLimit) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 206 | this->init(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 207 | fTotalByteLimit = byteLimit; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 208 | } |
| 209 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 210 | SkResourceCache::~SkResourceCache() { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 211 | SkSafeUnref(fAllocator); |
| 212 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 213 | Rec* rec = fHead; |
| 214 | while (rec) { |
| 215 | Rec* next = rec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 216 | delete rec; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 217 | rec = next; |
| 218 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 219 | delete fHash; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 220 | } |
| 221 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 222 | //////////////////////////////////////////////////////////////////////////////// |
| 223 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 224 | bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) { |
| 225 | this->checkMessages(); |
| 226 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 227 | Rec* rec = fHash->find(key); |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 228 | if (rec) { |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 229 | if (visitor(*rec, context)) { |
| 230 | this->moveToHead(rec); // for our LRU |
| 231 | return true; |
| 232 | } else { |
| 233 | this->remove(rec); // stale |
| 234 | return false; |
| 235 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 236 | } |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 237 | return false; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 238 | } |
| 239 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 240 | static void make_size_str(size_t size, SkString* str) { |
| 241 | const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 }; |
| 242 | int i = 0; |
| 243 | while (suffix[i] && (size > 1024)) { |
| 244 | i += 1; |
| 245 | size >>= 10; |
| 246 | } |
| 247 | str->printf("%zu%c", size, suffix[i]); |
| 248 | } |
| 249 | |
| 250 | static bool gDumpCacheTransactions; |
| 251 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 252 | void SkResourceCache::add(Rec* rec) { |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 253 | this->checkMessages(); |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 254 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 255 | SkASSERT(rec); |
| 256 | // See if we already have this key (racy inserts, etc.) |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 257 | Rec* existing = fHash->find(rec->getKey()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 258 | if (existing) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 259 | delete rec; |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 260 | return; |
| 261 | } |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 262 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 263 | this->addToHead(rec); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 264 | fHash->add(rec); |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 265 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 266 | if (gDumpCacheTransactions) { |
| 267 | SkString bytesStr, totalStr; |
| 268 | make_size_str(rec->bytesUsed(), &bytesStr); |
| 269 | make_size_str(fTotalBytesUsed, &totalStr); |
| 270 | SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n", |
| 271 | bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount); |
| 272 | } |
| 273 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 274 | // since the new rec may push us over-budget, we perform a purge check now |
| 275 | this->purgeAsNeeded(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 278 | void SkResourceCache::remove(Rec* rec) { |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 279 | size_t used = rec->bytesUsed(); |
| 280 | SkASSERT(used <= fTotalBytesUsed); |
| 281 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 282 | this->release(rec); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 283 | fHash->remove(rec->getKey()); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 284 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 285 | fTotalBytesUsed -= used; |
| 286 | fCount -= 1; |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 287 | |
| 288 | if (gDumpCacheTransactions) { |
| 289 | SkString bytesStr, totalStr; |
| 290 | make_size_str(used, &bytesStr); |
| 291 | make_size_str(fTotalBytesUsed, &totalStr); |
| 292 | SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n", |
| 293 | bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount); |
| 294 | } |
| 295 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 296 | delete rec; |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 297 | } |
| 298 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 299 | void SkResourceCache::purgeAsNeeded(bool forcePurge) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 300 | size_t byteLimit; |
| 301 | int countLimit; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 302 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 303 | if (fDiscardableFactory) { |
| 304 | countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
| 305 | byteLimit = SK_MaxU32; // no limit based on bytes |
| 306 | } else { |
| 307 | countLimit = SK_MaxS32; // no limit based on count |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 308 | byteLimit = fTotalByteLimit; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 309 | } |
| 310 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 311 | Rec* rec = fTail; |
| 312 | while (rec) { |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 313 | if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 316 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 317 | Rec* prev = rec->fPrev; |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 318 | this->remove(rec); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 319 | rec = prev; |
| 320 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 321 | } |
| 322 | |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 323 | //#define SK_TRACK_PURGE_SHAREDID_HITRATE |
| 324 | |
| 325 | #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE |
| 326 | static int gPurgeCallCounter; |
| 327 | static int gPurgeHitCounter; |
| 328 | #endif |
| 329 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 330 | void SkResourceCache::purgeSharedID(uint64_t sharedID) { |
| 331 | if (0 == sharedID) { |
| 332 | return; |
| 333 | } |
| 334 | |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 335 | #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE |
| 336 | gPurgeCallCounter += 1; |
| 337 | bool found = false; |
| 338 | #endif |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 339 | // go backwards, just like purgeAsNeeded, just to make the code similar. |
| 340 | // could iterate either direction and still be correct. |
| 341 | Rec* rec = fTail; |
| 342 | while (rec) { |
| 343 | Rec* prev = rec->fPrev; |
| 344 | if (rec->getKey().getSharedID() == sharedID) { |
| 345 | // SkDebugf("purgeSharedID id=%llx rec=%p\n", sharedID, rec); |
| 346 | this->remove(rec); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 347 | #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE |
| 348 | found = true; |
| 349 | #endif |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 350 | } |
| 351 | rec = prev; |
| 352 | } |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 353 | |
| 354 | #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE |
| 355 | if (found) { |
| 356 | gPurgeHitCounter += 1; |
| 357 | } |
| 358 | |
| 359 | SkDebugf("PurgeShared calls=%d hits=%d rate=%g\n", gPurgeCallCounter, gPurgeHitCounter, |
| 360 | gPurgeHitCounter * 100.0 / gPurgeCallCounter); |
| 361 | #endif |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 362 | } |
| 363 | |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 364 | void SkResourceCache::visitAll(Visitor visitor, void* context) { |
| 365 | // go backwards, just like purgeAsNeeded, just to make the code similar. |
| 366 | // could iterate either direction and still be correct. |
| 367 | Rec* rec = fTail; |
| 368 | while (rec) { |
| 369 | visitor(*rec, context); |
| 370 | rec = rec->fPrev; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 375 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 376 | size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 377 | size_t prevLimit = fTotalByteLimit; |
| 378 | fTotalByteLimit = newLimit; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 379 | if (newLimit < prevLimit) { |
| 380 | this->purgeAsNeeded(); |
| 381 | } |
| 382 | return prevLimit; |
| 383 | } |
| 384 | |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 385 | SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 386 | this->checkMessages(); |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 387 | |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 388 | if (fDiscardableFactory) { |
| 389 | SkDiscardableMemory* dm = fDiscardableFactory(bytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 390 | return dm ? new SkCachedData(bytes, dm) : nullptr; |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 391 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 392 | return new SkCachedData(sk_malloc_throw(bytes), bytes); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 396 | /////////////////////////////////////////////////////////////////////////////// |
| 397 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 398 | void SkResourceCache::release(Rec* rec) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 399 | Rec* prev = rec->fPrev; |
| 400 | Rec* next = rec->fNext; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 401 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 402 | if (!prev) { |
| 403 | SkASSERT(fHead == rec); |
| 404 | fHead = next; |
| 405 | } else { |
| 406 | prev->fNext = next; |
| 407 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 408 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 409 | if (!next) { |
| 410 | fTail = prev; |
| 411 | } else { |
| 412 | next->fPrev = prev; |
| 413 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 414 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 415 | rec->fNext = rec->fPrev = nullptr; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 416 | } |
| 417 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 418 | void SkResourceCache::moveToHead(Rec* rec) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 419 | if (fHead == rec) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | SkASSERT(fHead); |
| 424 | SkASSERT(fTail); |
| 425 | |
| 426 | this->validate(); |
| 427 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 428 | this->release(rec); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 429 | |
| 430 | fHead->fPrev = rec; |
| 431 | rec->fNext = fHead; |
| 432 | fHead = rec; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 433 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 434 | this->validate(); |
| 435 | } |
| 436 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 437 | void SkResourceCache::addToHead(Rec* rec) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 438 | this->validate(); |
| 439 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | rec->fPrev = nullptr; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 441 | rec->fNext = fHead; |
| 442 | if (fHead) { |
| 443 | fHead->fPrev = rec; |
| 444 | } |
| 445 | fHead = rec; |
| 446 | if (!fTail) { |
| 447 | fTail = rec; |
| 448 | } |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 449 | fTotalBytesUsed += rec->bytesUsed(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 450 | fCount += 1; |
| 451 | |
| 452 | this->validate(); |
| 453 | } |
| 454 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 455 | /////////////////////////////////////////////////////////////////////////////// |
| 456 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 457 | #ifdef SK_DEBUG |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 458 | void SkResourceCache::validate() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 459 | if (nullptr == fHead) { |
| 460 | SkASSERT(nullptr == fTail); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 461 | SkASSERT(0 == fTotalBytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | |
| 465 | if (fHead == fTail) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 466 | SkASSERT(nullptr == fHead->fPrev); |
| 467 | SkASSERT(nullptr == fHead->fNext); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 468 | SkASSERT(fHead->bytesUsed() == fTotalBytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 472 | SkASSERT(nullptr == fHead->fPrev); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 473 | SkASSERT(fHead->fNext); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 474 | SkASSERT(nullptr == fTail->fNext); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 475 | SkASSERT(fTail->fPrev); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 476 | |
| 477 | size_t used = 0; |
| 478 | int count = 0; |
| 479 | const Rec* rec = fHead; |
| 480 | while (rec) { |
| 481 | count += 1; |
| 482 | used += rec->bytesUsed(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 483 | SkASSERT(used <= fTotalBytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 484 | rec = rec->fNext; |
| 485 | } |
| 486 | SkASSERT(fCount == count); |
| 487 | |
| 488 | rec = fTail; |
| 489 | while (rec) { |
| 490 | SkASSERT(count > 0); |
| 491 | count -= 1; |
| 492 | SkASSERT(used >= rec->bytesUsed()); |
| 493 | used -= rec->bytesUsed(); |
| 494 | rec = rec->fPrev; |
| 495 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 496 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 497 | SkASSERT(0 == count); |
| 498 | SkASSERT(0 == used); |
| 499 | } |
| 500 | #endif |
| 501 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 502 | void SkResourceCache::dump() const { |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 503 | this->validate(); |
| 504 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 505 | SkDebugf("SkResourceCache: count=%d bytes=%d %s\n", |
| 506 | fCount, fTotalBytesUsed, fDiscardableFactory ? "discardable" : "malloc"); |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 507 | } |
| 508 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 509 | size_t SkResourceCache::setSingleAllocationByteLimit(size_t newLimit) { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 510 | size_t oldLimit = fSingleAllocationByteLimit; |
| 511 | fSingleAllocationByteLimit = newLimit; |
| 512 | return oldLimit; |
| 513 | } |
| 514 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 515 | size_t SkResourceCache::getSingleAllocationByteLimit() const { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 516 | return fSingleAllocationByteLimit; |
| 517 | } |
| 518 | |
reed | 1d9e80f | 2015-01-26 11:24:37 -0800 | [diff] [blame] | 519 | size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const { |
| 520 | // fSingleAllocationByteLimit == 0 means the caller is asking for our default |
| 521 | size_t limit = fSingleAllocationByteLimit; |
| 522 | |
| 523 | // if we're not discardable (i.e. we are fixed-budget) then cap the single-limit |
| 524 | // to our budget. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 525 | if (nullptr == fDiscardableFactory) { |
reed | 1d9e80f | 2015-01-26 11:24:37 -0800 | [diff] [blame] | 526 | if (0 == limit) { |
| 527 | limit = fTotalByteLimit; |
| 528 | } else { |
| 529 | limit = SkTMin(limit, fTotalByteLimit); |
| 530 | } |
| 531 | } |
| 532 | return limit; |
| 533 | } |
| 534 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 535 | void SkResourceCache::checkMessages() { |
| 536 | SkTArray<PurgeSharedIDMessage> msgs; |
| 537 | fPurgeSharedIDInbox.poll(&msgs); |
| 538 | for (int i = 0; i < msgs.count(); ++i) { |
| 539 | this->purgeSharedID(msgs[i].fSharedID); |
| 540 | } |
| 541 | } |
| 542 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 543 | /////////////////////////////////////////////////////////////////////////////// |
| 544 | |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 545 | SK_DECLARE_STATIC_MUTEX(gMutex); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 546 | static SkResourceCache* gResourceCache = nullptr; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 547 | |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 548 | /** Must hold gMutex when calling. */ |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 549 | static SkResourceCache* get_cache() { |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 550 | // gMutex is always held when this is called, so we don't need to be fancy in here. |
mtklein | b83f6c3 | 2014-06-09 14:18:02 -0700 | [diff] [blame] | 551 | gMutex.assertHeld(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 552 | if (nullptr == gResourceCache) { |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 553 | #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 554 | gResourceCache = new SkResourceCache(SkDiscardableMemory::Create); |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 555 | #else |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 556 | gResourceCache = new SkResourceCache(SK_DEFAULT_IMAGE_CACHE_LIMIT); |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 557 | #endif |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 558 | } |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 559 | return gResourceCache; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 560 | } |
| 561 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 562 | size_t SkResourceCache::GetTotalBytesUsed() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 563 | SkAutoMutexAcquire am(gMutex); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 564 | return get_cache()->getTotalBytesUsed(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 565 | } |
| 566 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 567 | size_t SkResourceCache::GetTotalByteLimit() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 568 | SkAutoMutexAcquire am(gMutex); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 569 | return get_cache()->getTotalByteLimit(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 570 | } |
| 571 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 572 | size_t SkResourceCache::SetTotalByteLimit(size_t newLimit) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 573 | SkAutoMutexAcquire am(gMutex); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 574 | return get_cache()->setTotalByteLimit(newLimit); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 575 | } |
| 576 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 577 | SkResourceCache::DiscardableFactory SkResourceCache::GetDiscardableFactory() { |
| 578 | SkAutoMutexAcquire am(gMutex); |
| 579 | return get_cache()->discardableFactory(); |
| 580 | } |
| 581 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 582 | SkBitmap::Allocator* SkResourceCache::GetAllocator() { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 583 | SkAutoMutexAcquire am(gMutex); |
| 584 | return get_cache()->allocator(); |
| 585 | } |
| 586 | |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 587 | SkCachedData* SkResourceCache::NewCachedData(size_t bytes) { |
| 588 | SkAutoMutexAcquire am(gMutex); |
| 589 | return get_cache()->newCachedData(bytes); |
| 590 | } |
| 591 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 592 | void SkResourceCache::Dump() { |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 593 | SkAutoMutexAcquire am(gMutex); |
| 594 | get_cache()->dump(); |
| 595 | } |
| 596 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 597 | size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 598 | SkAutoMutexAcquire am(gMutex); |
| 599 | return get_cache()->setSingleAllocationByteLimit(size); |
| 600 | } |
| 601 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 602 | size_t SkResourceCache::GetSingleAllocationByteLimit() { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 603 | SkAutoMutexAcquire am(gMutex); |
| 604 | return get_cache()->getSingleAllocationByteLimit(); |
| 605 | } |
| 606 | |
reed | 1d9e80f | 2015-01-26 11:24:37 -0800 | [diff] [blame] | 607 | size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { |
| 608 | SkAutoMutexAcquire am(gMutex); |
| 609 | return get_cache()->getEffectiveSingleAllocationByteLimit(); |
| 610 | } |
| 611 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 612 | void SkResourceCache::PurgeAll() { |
| 613 | SkAutoMutexAcquire am(gMutex); |
| 614 | return get_cache()->purgeAll(); |
| 615 | } |
| 616 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 617 | bool SkResourceCache::Find(const Key& key, FindVisitor visitor, void* context) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 618 | SkAutoMutexAcquire am(gMutex); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 619 | return get_cache()->find(key, visitor, context); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 620 | } |
| 621 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 622 | void SkResourceCache::Add(Rec* rec) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 623 | SkAutoMutexAcquire am(gMutex); |
| 624 | get_cache()->add(rec); |
| 625 | } |
| 626 | |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 627 | void SkResourceCache::VisitAll(Visitor visitor, void* context) { |
| 628 | SkAutoMutexAcquire am(gMutex); |
| 629 | get_cache()->visitAll(visitor, context); |
| 630 | } |
| 631 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 632 | void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) { |
| 633 | if (sharedID) { |
| 634 | SkMessageBus<PurgeSharedIDMessage>::Post(PurgeSharedIDMessage(sharedID)); |
| 635 | } |
| 636 | } |
| 637 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 638 | /////////////////////////////////////////////////////////////////////////////// |
| 639 | |
| 640 | #include "SkGraphics.h" |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 641 | #include "SkImageFilter.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 642 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 643 | size_t SkGraphics::GetResourceCacheTotalBytesUsed() { |
| 644 | return SkResourceCache::GetTotalBytesUsed(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 645 | } |
| 646 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 647 | size_t SkGraphics::GetResourceCacheTotalByteLimit() { |
| 648 | return SkResourceCache::GetTotalByteLimit(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 649 | } |
| 650 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 651 | size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) { |
| 652 | return SkResourceCache::SetTotalByteLimit(newLimit); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 653 | } |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 654 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 655 | size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { |
| 656 | return SkResourceCache::GetSingleAllocationByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 657 | } |
| 658 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 659 | size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
| 660 | return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 661 | } |
| 662 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 663 | void SkGraphics::PurgeResourceCache() { |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 664 | SkImageFilter::PurgeCache(); |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 665 | return SkResourceCache::PurgeAll(); |
| 666 | } |
| 667 | |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 668 | ///////////// |
| 669 | |
| 670 | static void dump_visitor(const SkResourceCache::Rec& rec, void*) { |
| 671 | SkDebugf("RC: %12s bytes %9lu discardable %p\n", |
| 672 | rec.getCategory(), rec.bytesUsed(), rec.diagnostic_only_getDiscardable()); |
| 673 | } |
| 674 | |
| 675 | void SkResourceCache::TestDumpMemoryStatistics() { |
| 676 | VisitAll(dump_visitor, nullptr); |
| 677 | } |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 678 | |
| 679 | static void sk_trace_dump_visitor(const SkResourceCache::Rec& rec, void* context) { |
| 680 | SkTraceMemoryDump* dump = static_cast<SkTraceMemoryDump*>(context); |
ssid | f0c9865 | 2015-09-30 04:31:23 -0700 | [diff] [blame] | 681 | SkString dumpName = SkStringPrintf("skia/sk_resource_cache/%s_%p", rec.getCategory(), &rec); |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 682 | SkDiscardableMemory* discardable = rec.diagnostic_only_getDiscardable(); |
| 683 | if (discardable) { |
ssid | f0c9865 | 2015-09-30 04:31:23 -0700 | [diff] [blame] | 684 | dump->setDiscardableMemoryBacking(dumpName.c_str(), *discardable); |
ssid | 60df542 | 2015-10-01 12:41:35 -0700 | [diff] [blame] | 685 | |
| 686 | // The discardable memory size will be calculated by dumper, but we also dump what we think |
| 687 | // the size of object in memory is irrespective of whether object is live or dead. |
| 688 | dump->dumpNumericValue(dumpName.c_str(), "discardable_size", "bytes", rec.bytesUsed()); |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 689 | } else { |
ssid | f0c9865 | 2015-09-30 04:31:23 -0700 | [diff] [blame] | 690 | dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", rec.bytesUsed()); |
| 691 | dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr); |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | void SkResourceCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) { |
ssid | f0c9865 | 2015-09-30 04:31:23 -0700 | [diff] [blame] | 696 | // Since resource could be backed by malloc or discardable, the cache always dumps detailed |
| 697 | // stats to be accurate. |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame] | 698 | VisitAll(sk_trace_dump_visitor, dump); |
| 699 | } |