| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkLruImageCache.h" | 8 #include "SkLruImageCache.h" |
| 9 | 9 |
| 10 static intptr_t NextGenerationID() { | 10 static intptr_t NextGenerationID() { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 if (NULL == pixels) { | 80 if (NULL == pixels) { |
| 81 return SkImageCache::kThrownAway_CacheStatus; | 81 return SkImageCache::kThrownAway_CacheStatus; |
| 82 } | 82 } |
| 83 if (pixels->isLocked()) { | 83 if (pixels->isLocked()) { |
| 84 return SkImageCache::kPinned_CacheStatus; | 84 return SkImageCache::kPinned_CacheStatus; |
| 85 } | 85 } |
| 86 return SkImageCache::kUnpinned_CacheStatus; | 86 return SkImageCache::kUnpinned_CacheStatus; |
| 87 } | 87 } |
| 88 #endif | 88 #endif |
| 89 | 89 |
| 90 void SkLruImageCache::setBudget(size_t newBudget) { | 90 size_t SkLruImageCache::setImageCacheLimit(size_t newLimit) { |
| 91 size_t oldLimit = fRamBudget; |
| 91 SkAutoMutexAcquire ac(&fMutex); | 92 SkAutoMutexAcquire ac(&fMutex); |
| 92 fRamBudget = newBudget; | 93 fRamBudget = newLimit; |
| 93 this->purgeIfNeeded(); | 94 this->purgeIfNeeded(); |
| 95 return oldLimit; |
| 94 } | 96 } |
| 95 | 97 |
| 96 void* SkLruImageCache::allocAndPinCache(size_t bytes, intptr_t* ID) { | 98 void* SkLruImageCache::allocAndPinCache(size_t bytes, intptr_t* ID) { |
| 97 SkAutoMutexAcquire ac(&fMutex); | 99 SkAutoMutexAcquire ac(&fMutex); |
| 98 CachedPixels* pixels = SkNEW_ARGS(CachedPixels, (bytes)); | 100 CachedPixels* pixels = SkNEW_ARGS(CachedPixels, (bytes)); |
| 99 if (ID != NULL) { | 101 if (ID != NULL) { |
| 100 *ID = pixels->getID(); | 102 *ID = pixels->getID(); |
| 101 } | 103 } |
| 102 pixels->lock(); | 104 pixels->lock(); |
| 103 fRamUsed += bytes; | 105 fRamUsed += bytes; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 if (pixels->getID() == ID) { | 163 if (pixels->getID() == ID) { |
| 162 return pixels; | 164 return pixels; |
| 163 } | 165 } |
| 164 pixels = iter.next(); | 166 pixels = iter.next(); |
| 165 } | 167 } |
| 166 return NULL; | 168 return NULL; |
| 167 } | 169 } |
| 168 | 170 |
| 169 void SkLruImageCache::purgeIfNeeded() { | 171 void SkLruImageCache::purgeIfNeeded() { |
| 170 // Mutex is already locked. | 172 // Mutex is already locked. |
| 171 this->purgeTilAtOrBelow(fRamBudget); | 173 if (fRamBudget > 0) { |
| 174 this->purgeTilAtOrBelow(fRamBudget); |
| 175 } |
| 172 } | 176 } |
| 173 | 177 |
| 174 void SkLruImageCache::purgeTilAtOrBelow(size_t limit) { | 178 void SkLruImageCache::purgeTilAtOrBelow(size_t limit) { |
| 175 // Mutex is already locked. | 179 // Mutex is already locked. |
| 176 if (fRamUsed > limit) { | 180 if (fRamUsed > limit) { |
| 177 Iter iter; | 181 Iter iter; |
| 178 // Start from the tail, least recently used. | 182 // Start from the tail, least recently used. |
| 179 CachedPixels* pixels = iter.init(fLRU, Iter::kTail_IterStart); | 183 CachedPixels* pixels = iter.init(fLRU, Iter::kTail_IterStart); |
| 180 while (pixels != NULL && fRamUsed > limit) { | 184 while (pixels != NULL && fRamUsed > limit) { |
| 181 CachedPixels* prev = iter.prev(); | 185 CachedPixels* prev = iter.prev(); |
| 182 if (!pixels->isLocked()) { | 186 if (!pixels->isLocked()) { |
| 183 this->removePixels(pixels); | 187 this->removePixels(pixels); |
| 184 } | 188 } |
| 185 pixels = prev; | 189 pixels = prev; |
| 186 } | 190 } |
| 187 } | 191 } |
| 188 } | 192 } |
| OLD | NEW |