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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 #endif | 67 #endif |
68 SkDELETE(pixels); | 68 SkDELETE(pixels); |
69 pixels = prev; | 69 pixels = prev; |
70 } | 70 } |
71 #ifdef SK_DEBUG | 71 #ifdef SK_DEBUG |
72 SkASSERT(fRamUsed == 0); | 72 SkASSERT(fRamUsed == 0); |
73 #endif | 73 #endif |
74 } | 74 } |
75 | 75 |
76 #ifdef SK_DEBUG | 76 #ifdef SK_DEBUG |
77 SkImageCache::CacheStatus SkLruImageCache::getCacheStatus(intptr_t ID) const { | 77 SkImageCache::PinStatus SkLruImageCache::getPinStatus(intptr_t ID) const { |
78 SkAutoMutexAcquire ac(&fMutex); | 78 SkAutoMutexAcquire ac(&fMutex); |
79 CachedPixels* pixels = this->findByID(ID); | 79 CachedPixels* pixels = this->findByID(ID); |
80 if (NULL == pixels) { | 80 if (NULL == pixels) { |
81 return SkImageCache::kThrownAway_CacheStatus; | 81 return SkImageCache::kThrownAway_PinStatus; |
82 } | 82 } |
83 if (pixels->isLocked()) { | 83 if (pixels->isLocked()) { |
84 return SkImageCache::kPinned_CacheStatus; | 84 return SkImageCache::kPinned_PinStatus; |
85 } | 85 } |
86 return SkImageCache::kUnpinned_CacheStatus; | 86 return SkImageCache::kNeedsPin_PinStatus; |
| 87 } |
| 88 |
| 89 void SkLruImageCache::purgeAllCaches() { |
| 90 SkAutoMutexAcquire ac(&fMutex); |
| 91 this->purgeTilAtOrBelow(0); |
87 } | 92 } |
88 #endif | 93 #endif |
89 | 94 |
90 size_t SkLruImageCache::setImageCacheLimit(size_t newLimit) { | 95 size_t SkLruImageCache::setImageCacheLimit(size_t newLimit) { |
91 size_t oldLimit = fRamBudget; | 96 size_t oldLimit = fRamBudget; |
92 SkAutoMutexAcquire ac(&fMutex); | 97 SkAutoMutexAcquire ac(&fMutex); |
93 fRamBudget = newLimit; | 98 fRamBudget = newLimit; |
94 this->purgeIfNeeded(); | 99 this->purgeIfNeeded(); |
95 return oldLimit; | 100 return oldLimit; |
96 } | 101 } |
97 | 102 |
98 void* SkLruImageCache::allocAndPinCache(size_t bytes, intptr_t* ID) { | 103 void* SkLruImageCache::allocAndPinCache(size_t bytes, intptr_t* ID) { |
99 SkAutoMutexAcquire ac(&fMutex); | 104 SkAutoMutexAcquire ac(&fMutex); |
100 CachedPixels* pixels = SkNEW_ARGS(CachedPixels, (bytes)); | 105 CachedPixels* pixels = SkNEW_ARGS(CachedPixels, (bytes)); |
101 if (ID != NULL) { | 106 if (ID != NULL) { |
102 *ID = pixels->getID(); | 107 *ID = pixels->getID(); |
103 } | 108 } |
104 pixels->lock(); | 109 pixels->lock(); |
105 fRamUsed += bytes; | 110 fRamUsed += bytes; |
106 fLRU.addToHead(pixels); | 111 fLRU.addToHead(pixels); |
107 this->purgeIfNeeded(); | 112 this->purgeIfNeeded(); |
108 return pixels->getData(); | 113 return pixels->getData(); |
109 } | 114 } |
110 | 115 |
111 void* SkLruImageCache::pinCache(intptr_t ID) { | 116 void* SkLruImageCache::pinCache(intptr_t ID, SkImageCache::PurgeStatus* status)
{ |
112 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); | 117 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); |
113 SkAutoMutexAcquire ac(&fMutex); | 118 SkAutoMutexAcquire ac(&fMutex); |
114 CachedPixels* pixels = this->findByID(ID); | 119 CachedPixels* pixels = this->findByID(ID); |
115 if (NULL == pixels) { | 120 if (NULL == pixels) { |
116 return NULL; | 121 return NULL; |
117 } | 122 } |
118 if (pixels != fLRU.head()) { | 123 if (pixels != fLRU.head()) { |
119 fLRU.remove(pixels); | 124 fLRU.remove(pixels); |
120 fLRU.addToHead(pixels); | 125 fLRU.addToHead(pixels); |
121 } | 126 } |
| 127 SkASSERT(status != NULL); |
| 128 // This cache will never return pinned memory whose data has been overwritte
n. |
| 129 *status = SkImageCache::kNotPurged_PurgeStatus; |
122 pixels->lock(); | 130 pixels->lock(); |
123 return pixels->getData(); | 131 return pixels->getData(); |
124 } | 132 } |
125 | 133 |
126 void SkLruImageCache::releaseCache(intptr_t ID) { | 134 void SkLruImageCache::releaseCache(intptr_t ID) { |
127 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); | 135 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); |
128 SkAutoMutexAcquire ac(&fMutex); | 136 SkAutoMutexAcquire ac(&fMutex); |
129 CachedPixels* pixels = this->findByID(ID); | 137 CachedPixels* pixels = this->findByID(ID); |
130 SkASSERT(pixels != NULL); | 138 SkASSERT(pixels != NULL); |
131 pixels->unlock(); | 139 pixels->unlock(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 CachedPixels* pixels = iter.init(fLRU, Iter::kTail_IterStart); | 193 CachedPixels* pixels = iter.init(fLRU, Iter::kTail_IterStart); |
186 while (pixels != NULL && fRamUsed > limit) { | 194 while (pixels != NULL && fRamUsed > limit) { |
187 CachedPixels* prev = iter.prev(); | 195 CachedPixels* prev = iter.prev(); |
188 if (!pixels->isLocked()) { | 196 if (!pixels->isLocked()) { |
189 this->removePixels(pixels); | 197 this->removePixels(pixels); |
190 } | 198 } |
191 pixels = prev; | 199 pixels = prev; |
192 } | 200 } |
193 } | 201 } |
194 } | 202 } |
OLD | NEW |