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 | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 9 | #include "SkResourceCache.h" |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 10 | #include "SkMipMap.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 11 | #include "SkPixelRef.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 12 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 15 | // This can be defined by the caller's build system |
| 16 | //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
| 17 | |
| 18 | #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT |
| 19 | # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 |
| 20 | #endif |
| 21 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 22 | #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT |
| 23 | #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024) |
| 24 | #endif |
| 25 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 26 | void SkResourceCache::Key::init(void* nameSpace, size_t length) { |
reed | 4f987e9 | 2014-08-20 13:41:56 -0700 | [diff] [blame] | 27 | SkASSERT(SkAlign4(length) == length); |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 28 | |
| 29 | // fCount32 and fHash are not hashed |
| 30 | static const int kUnhashedLocal32s = 2; |
| 31 | static const int kLocal32s = kUnhashedLocal32s + (sizeof(fNamespace) >> 2); |
| 32 | |
| 33 | SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals); |
| 34 | SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespace), |
| 35 | namespace_field_must_be_last); |
| 36 | |
| 37 | fCount32 = SkToS32(kLocal32s + (length >> 2)); |
| 38 | fNamespace = nameSpace; |
| 39 | // skip unhashed fields when computing the murmur |
| 40 | fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s, |
| 41 | (fCount32 - kUnhashedLocal32s) << 2); |
reed | 4f987e9 | 2014-08-20 13:41:56 -0700 | [diff] [blame] | 42 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 43 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 44 | #include "SkTDynamicHash.h" |
| 45 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 46 | class SkResourceCache::Hash : |
| 47 | public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; |
commit-bot@chromium.org | 158f646 | 2014-04-02 17:03:09 +0000 | [diff] [blame] | 48 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 52 | void SkResourceCache::init() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 53 | fHead = NULL; |
| 54 | fTail = NULL; |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 55 | fHash = new Hash; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 56 | fTotalBytesUsed = 0; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 57 | fCount = 0; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 58 | fSingleAllocationByteLimit = 0; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 59 | fAllocator = NULL; |
| 60 | |
| 61 | // One of these should be explicit set by the caller after we return. |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 62 | fTotalByteLimit = 0; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 63 | fDiscardableFactory = NULL; |
| 64 | } |
| 65 | |
| 66 | #include "SkDiscardableMemory.h" |
| 67 | |
| 68 | class SkOneShotDiscardablePixelRef : public SkPixelRef { |
| 69 | public: |
commit-bot@chromium.org | 227c246 | 2014-01-24 18:33:07 +0000 | [diff] [blame] | 70 | SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef) |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 71 | // Ownership of the discardablememory is transfered to the pixelref |
| 72 | SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_t rowBytes); |
| 73 | ~SkOneShotDiscardablePixelRef(); |
| 74 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 75 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 76 | bool onNewLockPixels(LockRec*) SK_OVERRIDE; |
| 77 | void onUnlockPixels() SK_OVERRIDE; |
| 78 | size_t getAllocatedSizeInBytes() const SK_OVERRIDE; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 81 | SkDiscardableMemory* fDM; |
| 82 | size_t fRB; |
| 83 | bool fFirstTime; |
| 84 | |
| 85 | typedef SkPixelRef INHERITED; |
| 86 | }; |
| 87 | |
| 88 | SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& info, |
| 89 | SkDiscardableMemory* dm, |
| 90 | size_t rowBytes) |
| 91 | : INHERITED(info) |
| 92 | , fDM(dm) |
| 93 | , fRB(rowBytes) |
| 94 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 95 | SkASSERT(dm->data()); |
| 96 | fFirstTime = true; |
| 97 | } |
| 98 | |
| 99 | SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { |
| 100 | SkDELETE(fDM); |
| 101 | } |
| 102 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 103 | bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 104 | if (fFirstTime) { |
| 105 | // we're already locked |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 106 | SkASSERT(fDM->data()); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 107 | fFirstTime = false; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 108 | goto SUCCESS; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 109 | } |
commit-bot@chromium.org | 281713e | 2013-12-12 18:08:08 +0000 | [diff] [blame] | 110 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 111 | // A previous call to onUnlock may have deleted our DM, so check for that |
| 112 | if (NULL == fDM) { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 113 | return false; |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (!fDM->lock()) { |
| 117 | // since it failed, we delete it now, to free-up the resource |
| 118 | delete fDM; |
| 119 | fDM = NULL; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 120 | return false; |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 121 | } |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 122 | |
| 123 | SUCCESS: |
| 124 | rec->fPixels = fDM->data(); |
| 125 | rec->fColorTable = NULL; |
| 126 | rec->fRowBytes = fRB; |
| 127 | return true; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void SkOneShotDiscardablePixelRef::onUnlockPixels() { |
| 131 | SkASSERT(!fFirstTime); |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 132 | fDM->unlock(); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 136 | return this->info().getSafeSize(fRB); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 137 | } |
| 138 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 139 | class SkResourceCacheDiscardableAllocator : public SkBitmap::Allocator { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 140 | public: |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 141 | SkResourceCacheDiscardableAllocator(SkResourceCache::DiscardableFactory factory) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 142 | SkASSERT(factory); |
| 143 | fFactory = factory; |
| 144 | } |
| 145 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 146 | bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 147 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 148 | private: |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 149 | SkResourceCache::DiscardableFactory fFactory; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 152 | bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 153 | size_t size = bitmap->getSize(); |
sugoi | c197c8a | 2014-07-03 10:44:26 -0700 | [diff] [blame] | 154 | uint64_t size64 = bitmap->computeSize64(); |
| 155 | if (0 == size || size64 > (uint64_t)size) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
| 159 | SkDiscardableMemory* dm = fFactory(size); |
| 160 | if (NULL == dm) { |
| 161 | return false; |
| 162 | } |
| 163 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 164 | // can we relax this? |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 165 | if (kN32_SkColorType != bitmap->colorType()) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 169 | SkImageInfo info = bitmap->info(); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 170 | bitmap->setPixelRef(SkNEW_ARGS(SkOneShotDiscardablePixelRef, |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 171 | (info, dm, bitmap->rowBytes())))->unref(); |
| 172 | bitmap->lockPixels(); |
| 173 | return bitmap->readyToDraw(); |
| 174 | } |
| 175 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 176 | SkResourceCache::SkResourceCache(DiscardableFactory factory) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 177 | this->init(); |
| 178 | fDiscardableFactory = factory; |
| 179 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 180 | fAllocator = SkNEW_ARGS(SkResourceCacheDiscardableAllocator, (factory)); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 181 | } |
| 182 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 183 | SkResourceCache::SkResourceCache(size_t byteLimit) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 184 | this->init(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 185 | fTotalByteLimit = byteLimit; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 186 | } |
| 187 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 188 | SkResourceCache::~SkResourceCache() { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 189 | SkSafeUnref(fAllocator); |
| 190 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 191 | Rec* rec = fHead; |
| 192 | while (rec) { |
| 193 | Rec* next = rec->fNext; |
| 194 | SkDELETE(rec); |
| 195 | rec = next; |
| 196 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 197 | delete fHash; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 198 | } |
| 199 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 202 | bool SkResourceCache::find(const Key& key, VisitorProc visitor, void* context) { |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 203 | Rec* rec = fHash->find(key); |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 204 | if (rec) { |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 205 | if (visitor(*rec, context)) { |
| 206 | this->moveToHead(rec); // for our LRU |
| 207 | return true; |
| 208 | } else { |
| 209 | this->remove(rec); // stale |
| 210 | return false; |
| 211 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 212 | } |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 213 | return false; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 214 | } |
| 215 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 216 | static void make_size_str(size_t size, SkString* str) { |
| 217 | const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 }; |
| 218 | int i = 0; |
| 219 | while (suffix[i] && (size > 1024)) { |
| 220 | i += 1; |
| 221 | size >>= 10; |
| 222 | } |
| 223 | str->printf("%zu%c", size, suffix[i]); |
| 224 | } |
| 225 | |
| 226 | static bool gDumpCacheTransactions; |
| 227 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 228 | void SkResourceCache::add(Rec* rec) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 229 | SkASSERT(rec); |
| 230 | // See if we already have this key (racy inserts, etc.) |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 231 | Rec* existing = fHash->find(rec->getKey()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 232 | if (existing) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 233 | SkDELETE(rec); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
| 237 | this->addToHead(rec); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 238 | fHash->add(rec); |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 239 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 240 | if (gDumpCacheTransactions) { |
| 241 | SkString bytesStr, totalStr; |
| 242 | make_size_str(rec->bytesUsed(), &bytesStr); |
| 243 | make_size_str(fTotalBytesUsed, &totalStr); |
| 244 | SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n", |
| 245 | bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount); |
| 246 | } |
| 247 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 248 | // since the new rec may push us over-budget, we perform a purge check now |
| 249 | this->purgeAsNeeded(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 250 | } |
| 251 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 252 | void SkResourceCache::remove(Rec* rec) { |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 253 | size_t used = rec->bytesUsed(); |
| 254 | SkASSERT(used <= fTotalBytesUsed); |
| 255 | |
| 256 | this->detach(rec); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 257 | fHash->remove(rec->getKey()); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 258 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 259 | fTotalBytesUsed -= used; |
| 260 | fCount -= 1; |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 261 | |
| 262 | if (gDumpCacheTransactions) { |
| 263 | SkString bytesStr, totalStr; |
| 264 | make_size_str(used, &bytesStr); |
| 265 | make_size_str(fTotalBytesUsed, &totalStr); |
| 266 | SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n", |
| 267 | bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount); |
| 268 | } |
| 269 | |
| 270 | SkDELETE(rec); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 271 | } |
| 272 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 273 | void SkResourceCache::purgeAsNeeded(bool forcePurge) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 274 | size_t byteLimit; |
| 275 | int countLimit; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 276 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 277 | if (fDiscardableFactory) { |
| 278 | countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
| 279 | byteLimit = SK_MaxU32; // no limit based on bytes |
| 280 | } else { |
| 281 | countLimit = SK_MaxS32; // no limit based on count |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 282 | byteLimit = fTotalByteLimit; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 283 | } |
| 284 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 285 | Rec* rec = fTail; |
| 286 | while (rec) { |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 287 | if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 290 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 291 | Rec* prev = rec->fPrev; |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 292 | this->remove(rec); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 293 | rec = prev; |
| 294 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 295 | } |
| 296 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 297 | size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 298 | size_t prevLimit = fTotalByteLimit; |
| 299 | fTotalByteLimit = newLimit; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 300 | if (newLimit < prevLimit) { |
| 301 | this->purgeAsNeeded(); |
| 302 | } |
| 303 | return prevLimit; |
| 304 | } |
| 305 | |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 306 | SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
| 307 | if (fDiscardableFactory) { |
| 308 | SkDiscardableMemory* dm = fDiscardableFactory(bytes); |
| 309 | return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL; |
| 310 | } else { |
| 311 | return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes)); |
| 312 | } |
| 313 | } |
| 314 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 315 | /////////////////////////////////////////////////////////////////////////////// |
| 316 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 317 | void SkResourceCache::detach(Rec* rec) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 318 | Rec* prev = rec->fPrev; |
| 319 | Rec* next = rec->fNext; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 320 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 321 | if (!prev) { |
| 322 | SkASSERT(fHead == rec); |
| 323 | fHead = next; |
| 324 | } else { |
| 325 | prev->fNext = next; |
| 326 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 327 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 328 | if (!next) { |
| 329 | fTail = prev; |
| 330 | } else { |
| 331 | next->fPrev = prev; |
| 332 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 333 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 334 | rec->fNext = rec->fPrev = NULL; |
| 335 | } |
| 336 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 337 | void SkResourceCache::moveToHead(Rec* rec) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 338 | if (fHead == rec) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | SkASSERT(fHead); |
| 343 | SkASSERT(fTail); |
| 344 | |
| 345 | this->validate(); |
| 346 | |
| 347 | this->detach(rec); |
| 348 | |
| 349 | fHead->fPrev = rec; |
| 350 | rec->fNext = fHead; |
| 351 | fHead = rec; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 352 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 353 | this->validate(); |
| 354 | } |
| 355 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 356 | void SkResourceCache::addToHead(Rec* rec) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 357 | this->validate(); |
| 358 | |
| 359 | rec->fPrev = NULL; |
| 360 | rec->fNext = fHead; |
| 361 | if (fHead) { |
| 362 | fHead->fPrev = rec; |
| 363 | } |
| 364 | fHead = rec; |
| 365 | if (!fTail) { |
| 366 | fTail = rec; |
| 367 | } |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 368 | fTotalBytesUsed += rec->bytesUsed(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 369 | fCount += 1; |
| 370 | |
| 371 | this->validate(); |
| 372 | } |
| 373 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 374 | /////////////////////////////////////////////////////////////////////////////// |
| 375 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 376 | #ifdef SK_DEBUG |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 377 | void SkResourceCache::validate() const { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 378 | if (NULL == fHead) { |
| 379 | SkASSERT(NULL == fTail); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 380 | SkASSERT(0 == fTotalBytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | |
| 384 | if (fHead == fTail) { |
| 385 | SkASSERT(NULL == fHead->fPrev); |
| 386 | SkASSERT(NULL == fHead->fNext); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 387 | SkASSERT(fHead->bytesUsed() == fTotalBytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
| 391 | SkASSERT(NULL == fHead->fPrev); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 392 | SkASSERT(fHead->fNext); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 393 | SkASSERT(NULL == fTail->fNext); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 394 | SkASSERT(fTail->fPrev); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 395 | |
| 396 | size_t used = 0; |
| 397 | int count = 0; |
| 398 | const Rec* rec = fHead; |
| 399 | while (rec) { |
| 400 | count += 1; |
| 401 | used += rec->bytesUsed(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 402 | SkASSERT(used <= fTotalBytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 403 | rec = rec->fNext; |
| 404 | } |
| 405 | SkASSERT(fCount == count); |
| 406 | |
| 407 | rec = fTail; |
| 408 | while (rec) { |
| 409 | SkASSERT(count > 0); |
| 410 | count -= 1; |
| 411 | SkASSERT(used >= rec->bytesUsed()); |
| 412 | used -= rec->bytesUsed(); |
| 413 | rec = rec->fPrev; |
| 414 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 415 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 416 | SkASSERT(0 == count); |
| 417 | SkASSERT(0 == used); |
| 418 | } |
| 419 | #endif |
| 420 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 421 | void SkResourceCache::dump() const { |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 422 | this->validate(); |
| 423 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 424 | SkDebugf("SkResourceCache: count=%d bytes=%d %s\n", |
| 425 | fCount, fTotalBytesUsed, fDiscardableFactory ? "discardable" : "malloc"); |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 428 | size_t SkResourceCache::setSingleAllocationByteLimit(size_t newLimit) { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 429 | size_t oldLimit = fSingleAllocationByteLimit; |
| 430 | fSingleAllocationByteLimit = newLimit; |
| 431 | return oldLimit; |
| 432 | } |
| 433 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 434 | size_t SkResourceCache::getSingleAllocationByteLimit() const { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 435 | return fSingleAllocationByteLimit; |
| 436 | } |
| 437 | |
reed | 1d9e80f | 2015-01-26 11:24:37 -0800 | [diff] [blame^] | 438 | size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const { |
| 439 | // fSingleAllocationByteLimit == 0 means the caller is asking for our default |
| 440 | size_t limit = fSingleAllocationByteLimit; |
| 441 | |
| 442 | // if we're not discardable (i.e. we are fixed-budget) then cap the single-limit |
| 443 | // to our budget. |
| 444 | if (NULL == fDiscardableFactory) { |
| 445 | if (0 == limit) { |
| 446 | limit = fTotalByteLimit; |
| 447 | } else { |
| 448 | limit = SkTMin(limit, fTotalByteLimit); |
| 449 | } |
| 450 | } |
| 451 | return limit; |
| 452 | } |
| 453 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 454 | /////////////////////////////////////////////////////////////////////////////// |
| 455 | |
| 456 | #include "SkThread.h" |
| 457 | |
reed@google.com | be19dbe | 2013-07-24 15:06:34 +0000 | [diff] [blame] | 458 | SK_DECLARE_STATIC_MUTEX(gMutex); |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 459 | static SkResourceCache* gResourceCache = NULL; |
| 460 | static void cleanup_gResourceCache() { |
mtklein | 77ec7a6 | 2014-06-10 11:13:47 -0700 | [diff] [blame] | 461 | // 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 that |
| 463 | // makes this unsafe to delete when the main process atexit()s. |
| 464 | // SkLazyPtr does the same sort of thing. |
| 465 | #if SK_DEVELOPER |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 466 | SkDELETE(gResourceCache); |
mtklein | 77ec7a6 | 2014-06-10 11:13:47 -0700 | [diff] [blame] | 467 | #endif |
| 468 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 469 | |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 470 | /** Must hold gMutex when calling. */ |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 471 | static SkResourceCache* get_cache() { |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 472 | // 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] | 473 | gMutex.assertHeld(); |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 474 | if (NULL == gResourceCache) { |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 475 | #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 476 | gResourceCache = SkNEW_ARGS(SkResourceCache, (SkDiscardableMemory::Create)); |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 477 | #else |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 478 | gResourceCache = SkNEW_ARGS(SkResourceCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT)); |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 479 | #endif |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 480 | atexit(cleanup_gResourceCache); |
commit-bot@chromium.org | 60c8d24 | 2014-05-27 16:28:43 +0000 | [diff] [blame] | 481 | } |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 482 | return gResourceCache; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 483 | } |
| 484 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 485 | size_t SkResourceCache::GetTotalBytesUsed() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 486 | SkAutoMutexAcquire am(gMutex); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 487 | return get_cache()->getTotalBytesUsed(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 488 | } |
| 489 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 490 | size_t SkResourceCache::GetTotalByteLimit() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 491 | SkAutoMutexAcquire am(gMutex); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 492 | return get_cache()->getTotalByteLimit(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 493 | } |
| 494 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 495 | size_t SkResourceCache::SetTotalByteLimit(size_t newLimit) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 496 | SkAutoMutexAcquire am(gMutex); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 497 | return get_cache()->setTotalByteLimit(newLimit); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 498 | } |
| 499 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 500 | SkResourceCache::DiscardableFactory SkResourceCache::GetDiscardableFactory() { |
| 501 | SkAutoMutexAcquire am(gMutex); |
| 502 | return get_cache()->discardableFactory(); |
| 503 | } |
| 504 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 505 | SkBitmap::Allocator* SkResourceCache::GetAllocator() { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 506 | SkAutoMutexAcquire am(gMutex); |
| 507 | return get_cache()->allocator(); |
| 508 | } |
| 509 | |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 510 | SkCachedData* SkResourceCache::NewCachedData(size_t bytes) { |
| 511 | SkAutoMutexAcquire am(gMutex); |
| 512 | return get_cache()->newCachedData(bytes); |
| 513 | } |
| 514 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 515 | void SkResourceCache::Dump() { |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 516 | SkAutoMutexAcquire am(gMutex); |
| 517 | get_cache()->dump(); |
| 518 | } |
| 519 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 520 | size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 521 | SkAutoMutexAcquire am(gMutex); |
| 522 | return get_cache()->setSingleAllocationByteLimit(size); |
| 523 | } |
| 524 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 525 | size_t SkResourceCache::GetSingleAllocationByteLimit() { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 526 | SkAutoMutexAcquire am(gMutex); |
| 527 | return get_cache()->getSingleAllocationByteLimit(); |
| 528 | } |
| 529 | |
reed | 1d9e80f | 2015-01-26 11:24:37 -0800 | [diff] [blame^] | 530 | size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { |
| 531 | SkAutoMutexAcquire am(gMutex); |
| 532 | return get_cache()->getEffectiveSingleAllocationByteLimit(); |
| 533 | } |
| 534 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 535 | void SkResourceCache::PurgeAll() { |
| 536 | SkAutoMutexAcquire am(gMutex); |
| 537 | return get_cache()->purgeAll(); |
| 538 | } |
| 539 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 540 | bool SkResourceCache::Find(const Key& key, VisitorProc visitor, void* context) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 541 | SkAutoMutexAcquire am(gMutex); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 542 | return get_cache()->find(key, visitor, context); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 543 | } |
| 544 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 545 | void SkResourceCache::Add(Rec* rec) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 546 | SkAutoMutexAcquire am(gMutex); |
| 547 | get_cache()->add(rec); |
| 548 | } |
| 549 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 550 | /////////////////////////////////////////////////////////////////////////////// |
| 551 | |
| 552 | #include "SkGraphics.h" |
| 553 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 554 | size_t SkGraphics::GetResourceCacheTotalBytesUsed() { |
| 555 | return SkResourceCache::GetTotalBytesUsed(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 556 | } |
| 557 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 558 | size_t SkGraphics::GetResourceCacheTotalByteLimit() { |
| 559 | return SkResourceCache::GetTotalByteLimit(); |
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 SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) { |
| 563 | return SkResourceCache::SetTotalByteLimit(newLimit); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 564 | } |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 565 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 566 | size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() { |
| 567 | return SkResourceCache::GetSingleAllocationByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 568 | } |
| 569 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 570 | size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
| 571 | return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 572 | } |
| 573 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 574 | void SkGraphics::PurgeResourceCache() { |
| 575 | return SkResourceCache::PurgeAll(); |
| 576 | } |
| 577 | |