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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 void SkLruImageCache::releaseCache(intptr_t ID) { | 126 void SkLruImageCache::releaseCache(intptr_t ID) { |
127 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); | 127 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); |
128 SkAutoMutexAcquire ac(&fMutex); | 128 SkAutoMutexAcquire ac(&fMutex); |
129 CachedPixels* pixels = this->findByID(ID); | 129 CachedPixels* pixels = this->findByID(ID); |
130 SkASSERT(pixels != NULL); | 130 SkASSERT(pixels != NULL); |
131 pixels->unlock(); | 131 pixels->unlock(); |
132 this->purgeIfNeeded(); | 132 this->purgeIfNeeded(); |
133 } | 133 } |
134 | 134 |
135 void SkLruImageCache::throwAwayCache(intptr_t ID) { | 135 void SkLruImageCache::throwAwayCache(intptr_t ID) { |
136 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); | |
137 SkAutoMutexAcquire ac(&fMutex); | 136 SkAutoMutexAcquire ac(&fMutex); |
138 CachedPixels* pixels = this->findByID(ID); | 137 CachedPixels* pixels = this->findByID(ID); |
139 if (pixels != NULL) { | 138 if (pixels != NULL) { |
140 if (pixels->isLocked()) { | 139 if (pixels->isLocked()) { |
141 pixels->unlock(); | 140 pixels->unlock(); |
142 } | 141 } |
143 this->removePixels(pixels); | 142 this->removePixels(pixels); |
144 } | 143 } |
145 } | 144 } |
146 | 145 |
147 void SkLruImageCache::removePixels(CachedPixels* pixels) { | 146 void SkLruImageCache::removePixels(CachedPixels* pixels) { |
148 // Mutex is already locked. | 147 // Mutex is already locked. |
149 SkASSERT(!pixels->isLocked()); | 148 SkASSERT(!pixels->isLocked()); |
150 const size_t size = pixels->getLength(); | 149 const size_t size = pixels->getLength(); |
151 SkASSERT(size <= fRamUsed); | 150 SkASSERT(size <= fRamUsed); |
152 fLRU.remove(pixels); | 151 fLRU.remove(pixels); |
153 SkDELETE(pixels); | 152 SkDELETE(pixels); |
154 fRamUsed -= size; | 153 fRamUsed -= size; |
155 } | 154 } |
156 | 155 |
157 CachedPixels* SkLruImageCache::findByID(intptr_t ID) const { | 156 CachedPixels* SkLruImageCache::findByID(intptr_t ID) const { |
158 // Mutex is already locked. | 157 // Mutex is already locked. |
| 158 if (SkImageCache::UNINITIALIZED_ID == ID) { |
| 159 return NULL; |
| 160 } |
159 Iter iter; | 161 Iter iter; |
160 // Start from the head, most recently used. | 162 // Start from the head, most recently used. |
161 CachedPixels* pixels = iter.init(fLRU, Iter::kHead_IterStart); | 163 CachedPixels* pixels = iter.init(fLRU, Iter::kHead_IterStart); |
162 while (pixels != NULL) { | 164 while (pixels != NULL) { |
163 if (pixels->getID() == ID) { | 165 if (pixels->getID() == ID) { |
164 return pixels; | 166 return pixels; |
165 } | 167 } |
166 pixels = iter.next(); | 168 pixels = iter.next(); |
167 } | 169 } |
168 return NULL; | 170 return NULL; |
(...skipping 14 matching lines...) Expand all Loading... |
183 CachedPixels* pixels = iter.init(fLRU, Iter::kTail_IterStart); | 185 CachedPixels* pixels = iter.init(fLRU, Iter::kTail_IterStart); |
184 while (pixels != NULL && fRamUsed > limit) { | 186 while (pixels != NULL && fRamUsed > limit) { |
185 CachedPixels* prev = iter.prev(); | 187 CachedPixels* prev = iter.prev(); |
186 if (!pixels->isLocked()) { | 188 if (!pixels->isLocked()) { |
187 this->removePixels(pixels); | 189 this->removePixels(pixels); |
188 } | 190 } |
189 pixels = prev; | 191 pixels = prev; |
190 } | 192 } |
191 } | 193 } |
192 } | 194 } |
OLD | NEW |