Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: src/core/SkResourceCache.cpp

Issue 825263005: notify resource caches when pixelref genID goes stale (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add test for purge notifications Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkResourceCache.h ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 fTail = NULL; 54 fTail = NULL;
55 fHash = new Hash; 55 fHash = new Hash;
56 fTotalBytesUsed = 0; 56 fTotalBytesUsed = 0;
57 fCount = 0; 57 fCount = 0;
58 fSingleAllocationByteLimit = 0; 58 fSingleAllocationByteLimit = 0;
59 fAllocator = NULL; 59 fAllocator = NULL;
60 60
61 // One of these should be explicit set by the caller after we return. 61 // One of these should be explicit set by the caller after we return.
62 fTotalByteLimit = 0; 62 fTotalByteLimit = 0;
63 fDiscardableFactory = NULL; 63 fDiscardableFactory = NULL;
64
65 fInsidePurgeAllCounter = 0;
64 } 66 }
65 67
66 #include "SkDiscardableMemory.h" 68 #include "SkDiscardableMemory.h"
67 69
68 class SkOneShotDiscardablePixelRef : public SkPixelRef { 70 class SkOneShotDiscardablePixelRef : public SkPixelRef {
69 public: 71 public:
70 SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef) 72 SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef)
71 // Ownership of the discardablememory is transfered to the pixelref 73 // Ownership of the discardablememory is transfered to the pixelref
72 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_ t rowBytes); 74 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_ t rowBytes);
73 ~SkOneShotDiscardablePixelRef(); 75 ~SkOneShotDiscardablePixelRef();
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 while (rec) { 194 while (rec) {
193 Rec* next = rec->fNext; 195 Rec* next = rec->fNext;
194 SkDELETE(rec); 196 SkDELETE(rec);
195 rec = next; 197 rec = next;
196 } 198 }
197 delete fHash; 199 delete fHash;
198 } 200 }
199 201
200 //////////////////////////////////////////////////////////////////////////////// 202 ////////////////////////////////////////////////////////////////////////////////
201 203
202 bool SkResourceCache::find(const Key& key, VisitorProc visitor, void* context) { 204 bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) {
203 Rec* rec = fHash->find(key); 205 Rec* rec = fHash->find(key);
204 if (rec) { 206 if (rec) {
205 if (visitor(*rec, context)) { 207 if (visitor(*rec, context)) {
206 this->moveToHead(rec); // for our LRU 208 this->moveToHead(rec); // for our LRU
207 return true; 209 return true;
208 } else { 210 } else {
209 this->remove(rec); // stale 211 this->remove(rec); // stale
210 return false; 212 return false;
211 } 213 }
212 } 214 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) { 289 if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) {
288 break; 290 break;
289 } 291 }
290 292
291 Rec* prev = rec->fPrev; 293 Rec* prev = rec->fPrev;
292 this->remove(rec); 294 this->remove(rec);
293 rec = prev; 295 rec = prev;
294 } 296 }
295 } 297 }
296 298
299 void SkResourceCache::purge(const void* nameSpace, PurgeVisitor proc, void* cont ext) {
300 if (this->insidePurgeAll()) {
301 return;
302 }
303
304 // go backwards, just like purgeAsNeeded, just to make the code similar.
305 // could iterate either direction and still be correct.
306 Rec* rec = fTail;
307 while (rec) {
308 Rec* prev = rec->fPrev;
309 if (rec->getKey().getNamespace() == nameSpace) {
310 switch (proc(*rec, context)) {
311 case kRetainAndContinue_PurgeVisitorResult:
312 break;
313 case kPurgeAndContinue_PurgeVisitorResult:
314 this->remove(rec);
315 break;
316 case kRetainAndStop_PurgeVisitorResult:
317 return;
318 case kPurgeAndStop_PurgeVisitorResult:
319 this->remove(rec);
320 return;
321 }
322 }
323 rec = prev;
324 }
325 }
326
297 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) { 327 size_t SkResourceCache::setTotalByteLimit(size_t newLimit) {
298 size_t prevLimit = fTotalByteLimit; 328 size_t prevLimit = fTotalByteLimit;
299 fTotalByteLimit = newLimit; 329 fTotalByteLimit = newLimit;
300 if (newLimit < prevLimit) { 330 if (newLimit < prevLimit) {
301 this->purgeAsNeeded(); 331 this->purgeAsNeeded();
302 } 332 }
303 return prevLimit; 333 return prevLimit;
304 } 334 }
305 335
306 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { 336 SkCachedData* SkResourceCache::newCachedData(size_t bytes) {
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 size_t SkResourceCache::GetSingleAllocationByteLimit() { 555 size_t SkResourceCache::GetSingleAllocationByteLimit() {
526 SkAutoMutexAcquire am(gMutex); 556 SkAutoMutexAcquire am(gMutex);
527 return get_cache()->getSingleAllocationByteLimit(); 557 return get_cache()->getSingleAllocationByteLimit();
528 } 558 }
529 559
530 size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() { 560 size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() {
531 SkAutoMutexAcquire am(gMutex); 561 SkAutoMutexAcquire am(gMutex);
532 return get_cache()->getEffectiveSingleAllocationByteLimit(); 562 return get_cache()->getEffectiveSingleAllocationByteLimit();
533 } 563 }
534 564
565 void SkResourceCache::Purge(const void* nameSpace, PurgeVisitor proc, void* cont ext) {
566 SkAutoMutexAcquire am(gMutex);
567 return get_cache()->purge(nameSpace, proc, context);
568 }
569
535 void SkResourceCache::PurgeAll() { 570 void SkResourceCache::PurgeAll() {
536 SkAutoMutexAcquire am(gMutex); 571 SkAutoMutexAcquire am(gMutex);
537 return get_cache()->purgeAll(); 572 return get_cache()->purgeAll();
538 } 573 }
539 574
540 bool SkResourceCache::Find(const Key& key, VisitorProc visitor, void* context) { 575 bool SkResourceCache::Find(const Key& key, FindVisitor visitor, void* context) {
541 SkAutoMutexAcquire am(gMutex); 576 SkAutoMutexAcquire am(gMutex);
542 return get_cache()->find(key, visitor, context); 577 return get_cache()->find(key, visitor, context);
543 } 578 }
544 579
545 void SkResourceCache::Add(Rec* rec) { 580 void SkResourceCache::Add(Rec* rec) {
546 SkAutoMutexAcquire am(gMutex); 581 SkAutoMutexAcquire am(gMutex);
547 get_cache()->add(rec); 582 get_cache()->add(rec);
548 } 583 }
549 584
550 /////////////////////////////////////////////////////////////////////////////// 585 ///////////////////////////////////////////////////////////////////////////////
(...skipping 17 matching lines...) Expand all
568 } 603 }
569 604
570 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) { 605 size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) {
571 return SkResourceCache::SetSingleAllocationByteLimit(newLimit); 606 return SkResourceCache::SetSingleAllocationByteLimit(newLimit);
572 } 607 }
573 608
574 void SkGraphics::PurgeResourceCache() { 609 void SkGraphics::PurgeResourceCache() {
575 return SkResourceCache::PurgeAll(); 610 return SkResourceCache::PurgeAll();
576 } 611 }
577 612
OLDNEW
« no previous file with comments | « src/core/SkResourceCache.h ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698