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 "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkResourceCache.h" | 9 #include "SkResourceCache.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 this->moveToHead(rec); // for our LRU | 194 this->moveToHead(rec); // for our LRU |
195 return true; | 195 return true; |
196 } else { | 196 } else { |
197 this->remove(rec); // stale | 197 this->remove(rec); // stale |
198 return false; | 198 return false; |
199 } | 199 } |
200 } | 200 } |
201 return false; | 201 return false; |
202 } | 202 } |
203 | 203 |
| 204 static void make_size_str(size_t size, SkString* str) { |
| 205 const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 }; |
| 206 int i = 0; |
| 207 while (suffix[i] && (size > 1024)) { |
| 208 i += 1; |
| 209 size >>= 10; |
| 210 } |
| 211 str->printf("%zu%c", size, suffix[i]); |
| 212 } |
| 213 |
| 214 static bool gDumpCacheTransactions; |
| 215 |
204 void SkResourceCache::add(Rec* rec) { | 216 void SkResourceCache::add(Rec* rec) { |
205 SkASSERT(rec); | 217 SkASSERT(rec); |
206 // See if we already have this key (racy inserts, etc.) | 218 // See if we already have this key (racy inserts, etc.) |
207 Rec* existing = fHash->find(rec->getKey()); | 219 Rec* existing = fHash->find(rec->getKey()); |
208 if (existing) { | 220 if (existing) { |
209 SkDELETE(rec); | 221 SkDELETE(rec); |
210 return; | 222 return; |
211 } | 223 } |
212 | 224 |
213 this->addToHead(rec); | 225 this->addToHead(rec); |
214 fHash->add(rec); | 226 fHash->add(rec); |
215 | 227 |
| 228 if (gDumpCacheTransactions) { |
| 229 SkString bytesStr, totalStr; |
| 230 make_size_str(rec->bytesUsed(), &bytesStr); |
| 231 make_size_str(fTotalBytesUsed, &totalStr); |
| 232 SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n", |
| 233 bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount
); |
| 234 } |
| 235 |
216 // since the new rec may push us over-budget, we perform a purge check now | 236 // since the new rec may push us over-budget, we perform a purge check now |
217 this->purgeAsNeeded(); | 237 this->purgeAsNeeded(); |
218 } | 238 } |
219 | 239 |
220 void SkResourceCache::remove(Rec* rec) { | 240 void SkResourceCache::remove(Rec* rec) { |
221 size_t used = rec->bytesUsed(); | 241 size_t used = rec->bytesUsed(); |
222 SkASSERT(used <= fTotalBytesUsed); | 242 SkASSERT(used <= fTotalBytesUsed); |
223 | 243 |
224 this->detach(rec); | 244 this->detach(rec); |
225 fHash->remove(rec->getKey()); | 245 fHash->remove(rec->getKey()); |
226 | 246 |
227 SkDELETE(rec); | |
228 | |
229 fTotalBytesUsed -= used; | 247 fTotalBytesUsed -= used; |
230 fCount -= 1; | 248 fCount -= 1; |
| 249 |
| 250 if (gDumpCacheTransactions) { |
| 251 SkString bytesStr, totalStr; |
| 252 make_size_str(used, &bytesStr); |
| 253 make_size_str(fTotalBytesUsed, &totalStr); |
| 254 SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n", |
| 255 bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount
); |
| 256 } |
| 257 |
| 258 SkDELETE(rec); |
231 } | 259 } |
232 | 260 |
233 void SkResourceCache::purgeAsNeeded(bool forcePurge) { | 261 void SkResourceCache::purgeAsNeeded(bool forcePurge) { |
234 size_t byteLimit; | 262 size_t byteLimit; |
235 int countLimit; | 263 int countLimit; |
236 | 264 |
237 if (fDiscardableFactory) { | 265 if (fDiscardableFactory) { |
238 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; | 266 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
239 byteLimit = SK_MaxU32; // no limit based on bytes | 267 byteLimit = SK_MaxU32; // no limit based on bytes |
240 } else { | 268 } else { |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 } | 521 } |
494 | 522 |
495 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { | 523 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { |
496 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); | 524 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); |
497 } | 525 } |
498 | 526 |
499 void SkGraphics::PurgeResourceCache() { | 527 void SkGraphics::PurgeResourceCache() { |
500 return SkResourceCache::PurgeAll(); | 528 return SkResourceCache::PurgeAll(); |
501 } | 529 } |
502 | 530 |
OLD | NEW |