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