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