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 "SkMessageBus.h" | 9 #include "SkMessageBus.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 SkDiscardableMemory* dm, | 98 SkDiscardableMemory* dm, |
99 size_t rowBytes) | 99 size_t rowBytes) |
100 : INHERITED(info) | 100 : INHERITED(info) |
101 , fDM(dm) | 101 , fDM(dm) |
102 , fRB(rowBytes) | 102 , fRB(rowBytes) |
103 { | 103 { |
104 SkASSERT(dm->data()); | 104 SkASSERT(dm->data()); |
105 fFirstTime = true; | 105 fFirstTime = true; |
106 } | 106 } |
107 | 107 |
108 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { | 108 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { delete fDM; } |
109 SkDELETE(fDM); | |
110 } | |
111 | 109 |
112 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { | 110 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
113 if (fFirstTime) { | 111 if (fFirstTime) { |
114 // we're already locked | 112 // we're already locked |
115 SkASSERT(fDM->data()); | 113 SkASSERT(fDM->data()); |
116 fFirstTime = false; | 114 fFirstTime = false; |
117 goto SUCCESS; | 115 goto SUCCESS; |
118 } | 116 } |
119 | 117 |
120 // A previous call to onUnlock may have deleted our DM, so check for that | 118 // A previous call to onUnlock may have deleted our DM, so check for that |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 if (NULL == dm) { | 167 if (NULL == dm) { |
170 return false; | 168 return false; |
171 } | 169 } |
172 | 170 |
173 // can we relax this? | 171 // can we relax this? |
174 if (kN32_SkColorType != bitmap->colorType()) { | 172 if (kN32_SkColorType != bitmap->colorType()) { |
175 return false; | 173 return false; |
176 } | 174 } |
177 | 175 |
178 SkImageInfo info = bitmap->info(); | 176 SkImageInfo info = bitmap->info(); |
179 bitmap->setPixelRef(SkNEW_ARGS(SkOneShotDiscardablePixelRef, | 177 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy
tes()))->unref(); |
180 (info, dm, bitmap->rowBytes())))->unref(); | |
181 bitmap->lockPixels(); | 178 bitmap->lockPixels(); |
182 return bitmap->readyToDraw(); | 179 return bitmap->readyToDraw(); |
183 } | 180 } |
184 | 181 |
185 SkResourceCache::SkResourceCache(DiscardableFactory factory) { | 182 SkResourceCache::SkResourceCache(DiscardableFactory factory) { |
186 this->init(); | 183 this->init(); |
187 fDiscardableFactory = factory; | 184 fDiscardableFactory = factory; |
188 | 185 |
189 fAllocator = SkNEW_ARGS(SkResourceCacheDiscardableAllocator, (factory)); | 186 fAllocator = new SkResourceCacheDiscardableAllocator(factory); |
190 } | 187 } |
191 | 188 |
192 SkResourceCache::SkResourceCache(size_t byteLimit) { | 189 SkResourceCache::SkResourceCache(size_t byteLimit) { |
193 this->init(); | 190 this->init(); |
194 fTotalByteLimit = byteLimit; | 191 fTotalByteLimit = byteLimit; |
195 } | 192 } |
196 | 193 |
197 SkResourceCache::~SkResourceCache() { | 194 SkResourceCache::~SkResourceCache() { |
198 SkSafeUnref(fAllocator); | 195 SkSafeUnref(fAllocator); |
199 | 196 |
200 Rec* rec = fHead; | 197 Rec* rec = fHead; |
201 while (rec) { | 198 while (rec) { |
202 Rec* next = rec->fNext; | 199 Rec* next = rec->fNext; |
203 SkDELETE(rec); | 200 delete rec; |
204 rec = next; | 201 rec = next; |
205 } | 202 } |
206 delete fHash; | 203 delete fHash; |
207 } | 204 } |
208 | 205 |
209 //////////////////////////////////////////////////////////////////////////////// | 206 //////////////////////////////////////////////////////////////////////////////// |
210 | 207 |
211 bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) { | 208 bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) { |
212 this->checkMessages(); | 209 this->checkMessages(); |
213 | 210 |
(...skipping 22 matching lines...) Expand all Loading... |
236 | 233 |
237 static bool gDumpCacheTransactions; | 234 static bool gDumpCacheTransactions; |
238 | 235 |
239 void SkResourceCache::add(Rec* rec) { | 236 void SkResourceCache::add(Rec* rec) { |
240 this->checkMessages(); | 237 this->checkMessages(); |
241 | 238 |
242 SkASSERT(rec); | 239 SkASSERT(rec); |
243 // See if we already have this key (racy inserts, etc.) | 240 // See if we already have this key (racy inserts, etc.) |
244 Rec* existing = fHash->find(rec->getKey()); | 241 Rec* existing = fHash->find(rec->getKey()); |
245 if (existing) { | 242 if (existing) { |
246 SkDELETE(rec); | 243 delete rec; |
247 return; | 244 return; |
248 } | 245 } |
249 | 246 |
250 this->addToHead(rec); | 247 this->addToHead(rec); |
251 fHash->add(rec); | 248 fHash->add(rec); |
252 | 249 |
253 if (gDumpCacheTransactions) { | 250 if (gDumpCacheTransactions) { |
254 SkString bytesStr, totalStr; | 251 SkString bytesStr, totalStr; |
255 make_size_str(rec->bytesUsed(), &bytesStr); | 252 make_size_str(rec->bytesUsed(), &bytesStr); |
256 make_size_str(fTotalBytesUsed, &totalStr); | 253 make_size_str(fTotalBytesUsed, &totalStr); |
(...skipping 16 matching lines...) Expand all Loading... |
273 fCount -= 1; | 270 fCount -= 1; |
274 | 271 |
275 if (gDumpCacheTransactions) { | 272 if (gDumpCacheTransactions) { |
276 SkString bytesStr, totalStr; | 273 SkString bytesStr, totalStr; |
277 make_size_str(used, &bytesStr); | 274 make_size_str(used, &bytesStr); |
278 make_size_str(fTotalBytesUsed, &totalStr); | 275 make_size_str(fTotalBytesUsed, &totalStr); |
279 SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n", | 276 SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n", |
280 bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount
); | 277 bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount
); |
281 } | 278 } |
282 | 279 |
283 SkDELETE(rec); | 280 delete rec; |
284 } | 281 } |
285 | 282 |
286 void SkResourceCache::purgeAsNeeded(bool forcePurge) { | 283 void SkResourceCache::purgeAsNeeded(bool forcePurge) { |
287 size_t byteLimit; | 284 size_t byteLimit; |
288 int countLimit; | 285 int countLimit; |
289 | 286 |
290 if (fDiscardableFactory) { | 287 if (fDiscardableFactory) { |
291 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; | 288 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
292 byteLimit = SK_MaxU32; // no limit based on bytes | 289 byteLimit = SK_MaxU32; // no limit based on bytes |
293 } else { | 290 } else { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 this->purgeAsNeeded(); | 364 this->purgeAsNeeded(); |
368 } | 365 } |
369 return prevLimit; | 366 return prevLimit; |
370 } | 367 } |
371 | 368 |
372 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { | 369 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
373 this->checkMessages(); | 370 this->checkMessages(); |
374 | 371 |
375 if (fDiscardableFactory) { | 372 if (fDiscardableFactory) { |
376 SkDiscardableMemory* dm = fDiscardableFactory(bytes); | 373 SkDiscardableMemory* dm = fDiscardableFactory(bytes); |
377 return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL; | 374 return dm ? new SkCachedData(bytes, dm) : NULL; |
378 } else { | 375 } else { |
379 return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes)); | 376 return new SkCachedData(sk_malloc_throw(bytes), bytes); |
380 } | 377 } |
381 } | 378 } |
382 | 379 |
383 /////////////////////////////////////////////////////////////////////////////// | 380 /////////////////////////////////////////////////////////////////////////////// |
384 | 381 |
385 void SkResourceCache::detach(Rec* rec) { | 382 void SkResourceCache::detach(Rec* rec) { |
386 Rec* prev = rec->fPrev; | 383 Rec* prev = rec->fPrev; |
387 Rec* next = rec->fNext; | 384 Rec* next = rec->fNext; |
388 | 385 |
389 if (!prev) { | 386 if (!prev) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 /////////////////////////////////////////////////////////////////////////////// | 527 /////////////////////////////////////////////////////////////////////////////// |
531 | 528 |
532 SK_DECLARE_STATIC_MUTEX(gMutex); | 529 SK_DECLARE_STATIC_MUTEX(gMutex); |
533 static SkResourceCache* gResourceCache = NULL; | 530 static SkResourceCache* gResourceCache = NULL; |
534 static void cleanup_gResourceCache() { | 531 static void cleanup_gResourceCache() { |
535 // We'll clean this up in our own tests, but disable for clients. | 532 // We'll clean this up in our own tests, but disable for clients. |
536 // Chrome seems to have funky multi-process things going on in unit tests th
at | 533 // Chrome seems to have funky multi-process things going on in unit tests th
at |
537 // makes this unsafe to delete when the main process atexit()s. | 534 // makes this unsafe to delete when the main process atexit()s. |
538 // SkLazyPtr does the same sort of thing. | 535 // SkLazyPtr does the same sort of thing. |
539 #if SK_DEVELOPER | 536 #if SK_DEVELOPER |
540 SkDELETE(gResourceCache); | 537 delete gResourceCache; |
541 #endif | 538 #endif |
542 } | 539 } |
543 | 540 |
544 /** Must hold gMutex when calling. */ | 541 /** Must hold gMutex when calling. */ |
545 static SkResourceCache* get_cache() { | 542 static SkResourceCache* get_cache() { |
546 // gMutex is always held when this is called, so we don't need to be fancy i
n here. | 543 // gMutex is always held when this is called, so we don't need to be fancy i
n here. |
547 gMutex.assertHeld(); | 544 gMutex.assertHeld(); |
548 if (NULL == gResourceCache) { | 545 if (NULL == gResourceCache) { |
549 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 546 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
550 gResourceCache = SkNEW_ARGS(SkResourceCache, (SkDiscardableMemory::Creat
e)); | 547 gResourceCache = new SkResourceCache(SkDiscardableMemory::Create); |
551 #else | 548 #else |
552 gResourceCache = SkNEW_ARGS(SkResourceCache, (SK_DEFAULT_IMAGE_CACHE_LIM
IT)); | 549 gResourceCache = new SkResourceCache(SK_DEFAULT_IMAGE_CACHE_LIMIT); |
553 #endif | 550 #endif |
554 atexit(cleanup_gResourceCache); | 551 atexit(cleanup_gResourceCache); |
555 } | 552 } |
556 return gResourceCache; | 553 return gResourceCache; |
557 } | 554 } |
558 | 555 |
559 size_t SkResourceCache::GetTotalBytesUsed() { | 556 size_t SkResourceCache::GetTotalBytesUsed() { |
560 SkAutoMutexAcquire am(gMutex); | 557 SkAutoMutexAcquire am(gMutex); |
561 return get_cache()->getTotalBytesUsed(); | 558 return get_cache()->getTotalBytesUsed(); |
562 } | 559 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 ///////////// | 662 ///////////// |
666 | 663 |
667 static void dump_visitor(const SkResourceCache::Rec& rec, void*) { | 664 static void dump_visitor(const SkResourceCache::Rec& rec, void*) { |
668 SkDebugf("RC: %12s bytes %9lu discardable %p\n", | 665 SkDebugf("RC: %12s bytes %9lu discardable %p\n", |
669 rec.getCategory(), rec.bytesUsed(), rec.diagnostic_only_getDiscarda
ble()); | 666 rec.getCategory(), rec.bytesUsed(), rec.diagnostic_only_getDiscarda
ble()); |
670 } | 667 } |
671 | 668 |
672 void SkResourceCache::TestDumpMemoryStatistics() { | 669 void SkResourceCache::TestDumpMemoryStatistics() { |
673 VisitAll(dump_visitor, nullptr); | 670 VisitAll(dump_visitor, nullptr); |
674 } | 671 } |
OLD | NEW |