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

Side by Side Diff: src/gpu/GrResourceCache.cpp

Issue 2361093002: Change implementation of flush-count based GrGpuResource purging (Closed)
Patch Set: minor Created 4 years, 2 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/gpu/GrResourceCache.h ('k') | tests/ResourceCacheTest.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 2014 Google Inc. 2 * Copyright 2014 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 8
9 #include "GrResourceCache.h" 9 #include "GrResourceCache.h"
10 10
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 #if GR_CACHE_STATS 67 #if GR_CACHE_STATS
68 , fHighWaterCount(0) 68 , fHighWaterCount(0)
69 , fHighWaterBytes(0) 69 , fHighWaterBytes(0)
70 , fBudgetedHighWaterCount(0) 70 , fBudgetedHighWaterCount(0)
71 , fBudgetedHighWaterBytes(0) 71 , fBudgetedHighWaterBytes(0)
72 #endif 72 #endif
73 , fBytes(0) 73 , fBytes(0)
74 , fBudgetedCount(0) 74 , fBudgetedCount(0)
75 , fBudgetedBytes(0) 75 , fBudgetedBytes(0)
76 , fRequestFlush(false) 76 , fRequestFlush(false)
77 , fFlushTimestamps(nullptr) 77 , fExternalFlushCnt(0)
78 , fLastFlushTimestampIndex(0)
79 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) { 78 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
80 SkDEBUGCODE(fCount = 0;) 79 SkDEBUGCODE(fCount = 0;)
81 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;) 80 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;)
82 this->resetFlushTimestamps();
83 } 81 }
84 82
85 GrResourceCache::~GrResourceCache() { 83 GrResourceCache::~GrResourceCache() {
86 this->releaseAll(); 84 this->releaseAll();
87 delete[] fFlushTimestamps;
88 } 85 }
89 86
90 void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) { 87 void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) {
91 fMaxCount = count; 88 fMaxCount = count;
92 fMaxBytes = bytes; 89 fMaxBytes = bytes;
93 fMaxUnusedFlushes = maxUnusedFlushes; 90 fMaxUnusedFlushes = maxUnusedFlushes;
94 this->resetFlushTimestamps();
95 this->purgeAsNeeded(); 91 this->purgeAsNeeded();
96 } 92 }
97 93
98 void GrResourceCache::resetFlushTimestamps() {
99 delete[] fFlushTimestamps;
100
101 // We assume this number is a power of two when wrapping indices into the ti mestamp array.
102 fMaxUnusedFlushes = SkNextPow2(fMaxUnusedFlushes);
103
104 // Since our implementation is to store the timestamps of the last fMaxUnuse dFlushes flush calls
105 // we just turn the feature off if that array would be large.
106 static const int kMaxSupportedTimestampHistory = 128;
107
108 if (fMaxUnusedFlushes > kMaxSupportedTimestampHistory) {
109 fFlushTimestamps = nullptr;
110 return;
111 }
112
113 fFlushTimestamps = new uint32_t[fMaxUnusedFlushes];
114 fLastFlushTimestampIndex = 0;
115 // Set all the historical flush timestamps to initially be at the beginning of time (timestamp
116 // 0).
117 sk_bzero(fFlushTimestamps, fMaxUnusedFlushes * sizeof(uint32_t));
118 }
119
120 void GrResourceCache::insertResource(GrGpuResource* resource) { 94 void GrResourceCache::insertResource(GrGpuResource* resource) {
121 SkASSERT(resource); 95 SkASSERT(resource);
122 SkASSERT(!this->isInCache(resource)); 96 SkASSERT(!this->isInCache(resource));
123 SkASSERT(!resource->wasDestroyed()); 97 SkASSERT(!resource->wasDestroyed());
124 SkASSERT(!resource->isPurgeable()); 98 SkASSERT(!resource->isPurgeable());
125 99
126 // We must set the timestamp before adding to the array in case the timestam p wraps and we wind 100 // We must set the timestamp before adding to the array in case the timestam p wraps and we wind
127 // up iterating over all the resources that already have timestamps. 101 // up iterating over all the resources that already have timestamps.
128 resource->cacheAccess().setTimestamp(this->getNextTimestamp()); 102 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
129 103
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 } 357 }
384 358
385 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flag s)) { 359 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flag s)) {
386 SkASSERT(!resource->isPurgeable()); 360 SkASSERT(!resource->isPurgeable());
387 return; 361 return;
388 } 362 }
389 363
390 SkASSERT(resource->isPurgeable()); 364 SkASSERT(resource->isPurgeable());
391 this->removeFromNonpurgeableArray(resource); 365 this->removeFromNonpurgeableArray(resource);
392 fPurgeableQueue.insert(resource); 366 fPurgeableQueue.insert(resource);
367 resource->cacheAccess().setFlushCntWhenResourceBecamePurgeable(fExternalFlus hCnt);
393 368
394 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) { 369 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
395 // Check whether this resource could still be used as a scratch resource . 370 // Check whether this resource could still be used as a scratch resource .
396 if (!resource->resourcePriv().refsWrappedObjects() && 371 if (!resource->resourcePriv().refsWrappedObjects() &&
397 resource->resourcePriv().getScratchKey().isValid()) { 372 resource->resourcePriv().getScratchKey().isValid()) {
398 // We won't purge an existing resource to make room for this one. 373 // We won't purge an existing resource to make room for this one.
399 if (fBudgetedCount < fMaxCount && 374 if (fBudgetedCount < fMaxCount &&
400 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { 375 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
401 resource->resourcePriv().makeBudgeted(); 376 resource->resourcePriv().makeBudgeted();
402 return; 377 return;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 this->validate(); 442 this->validate();
468 } 443 }
469 444
470 void GrResourceCache::purgeAsNeeded() { 445 void GrResourceCache::purgeAsNeeded() {
471 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs; 446 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
472 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); 447 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
473 if (invalidKeyMsgs.count()) { 448 if (invalidKeyMsgs.count()) {
474 this->processInvalidUniqueKeys(invalidKeyMsgs); 449 this->processInvalidUniqueKeys(invalidKeyMsgs);
475 } 450 }
476 451
477 if (fFlushTimestamps) { 452 if (fMaxUnusedFlushes > 0) {
478 // Assuming kNumFlushesToDeleteUnusedResource is a power of 2. 453 // We want to know how many complete flushes have occurred without the r esource being used.
479 SkASSERT(SkIsPow2(fMaxUnusedFlushes)); 454 // If the resource was tagged when fExternalFlushCnt was N then this mea ns it became
480 int oldestFlushIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlush es - 1); 455 // purgeable during activity that became the N+1th flush. So when the fl ush count is N+2
481 456 // it has sat in the purgeable queue for a one entire flush.
482 uint32_t oldestAllowedTimestamp = fFlushTimestamps[oldestFlushIndex]; 457 uint32_t oldestAllowedFlushCnt = fExternalFlushCnt - fMaxUnusedFlushes - 1;
483 while (fPurgeableQueue.count()) { 458 // check for underflow
484 uint32_t oldestResourceTimestamp = fPurgeableQueue.peek()->cacheAcce ss().timestamp(); 459 if (oldestAllowedFlushCnt < fExternalFlushCnt) {
485 if (oldestAllowedTimestamp < oldestResourceTimestamp) { 460 while (fPurgeableQueue.count()) {
robertphillips 2016/09/22 17:55:56 oldestResourceFlushCnt doesn't seem like the right
bsalomon 2016/09/22 18:09:55 Done.
486 break; 461 uint32_t oldestResourceFlushCnt =
462 fPurgeableQueue.peek()->cacheAccess().flushCntWhenResour ceBecamePurgeable();
463 if (oldestAllowedFlushCnt < oldestResourceFlushCnt) {
robertphillips 2016/09/22 17:55:56 // comment why this is break and not continue ?
bsalomon 2016/09/22 18:09:55 Done.
464 break;
465 }
466 GrGpuResource* resource = fPurgeableQueue.peek();
467 SkASSERT(resource->isPurgeable());
468 resource->cacheAccess().release();
487 } 469 }
488 GrGpuResource* resource = fPurgeableQueue.peek();
489 SkASSERT(resource->isPurgeable());
490 resource->cacheAccess().release();
491 } 470 }
492 } 471 }
493 472
494 bool stillOverbudget = this->overBudget(); 473 bool stillOverbudget = this->overBudget();
495 while (stillOverbudget && fPurgeableQueue.count()) { 474 while (stillOverbudget && fPurgeableQueue.count()) {
496 GrGpuResource* resource = fPurgeableQueue.peek(); 475 GrGpuResource* resource = fPurgeableQueue.peek();
497 SkASSERT(resource->isPurgeable()); 476 SkASSERT(resource->isPurgeable());
498 resource->cacheAccess().release(); 477 resource->cacheAccess().release();
499 stillOverbudget = this->overBudget(); 478 stillOverbudget = this->overBudget();
500 } 479 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely 538 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
560 // rare. 539 // rare.
561 SkTDArray<GrGpuResource*> sortedPurgeableResources; 540 SkTDArray<GrGpuResource*> sortedPurgeableResources;
562 sortedPurgeableResources.setReserve(fPurgeableQueue.count()); 541 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
563 542
564 while (fPurgeableQueue.count()) { 543 while (fPurgeableQueue.count()) {
565 *sortedPurgeableResources.append() = fPurgeableQueue.peek(); 544 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
566 fPurgeableQueue.pop(); 545 fPurgeableQueue.pop();
567 } 546 }
568 547
569 struct Less { 548 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end( ) - 1,
570 bool operator()(GrGpuResource* a, GrGpuResource* b) { 549 CompareTimestamp);
571 return CompareTimestamp(a,b);
572 }
573 };
574 Less less;
575 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end( ) - 1, less);
576 550
577 // Pick resources out of the purgeable and non-purgeable arrays base d on lowest 551 // Pick resources out of the purgeable and non-purgeable arrays base d on lowest
578 // timestamp and assign new timestamps. 552 // timestamp and assign new timestamps.
579 int currP = 0; 553 int currP = 0;
580 int currNP = 0; 554 int currNP = 0;
581 while (currP < sortedPurgeableResources.count() && 555 while (currP < sortedPurgeableResources.count() &&
582 currNP < fNonpurgeableResources.count()) { 556 currNP < fNonpurgeableResources.count()) {
583 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().ti mestamp(); 557 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().ti mestamp();
584 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().ti mestamp(); 558 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().ti mestamp();
585 SkASSERT(tsP != tsNP); 559 SkASSERT(tsP != tsNP);
(...skipping 18 matching lines...) Expand all
604 // Rebuild the queue. 578 // Rebuild the queue.
605 for (int i = 0; i < sortedPurgeableResources.count(); ++i) { 579 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
606 fPurgeableQueue.insert(sortedPurgeableResources[i]); 580 fPurgeableQueue.insert(sortedPurgeableResources[i]);
607 } 581 }
608 582
609 this->validate(); 583 this->validate();
610 SkASSERT(count == this->getResourceCount()); 584 SkASSERT(count == this->getResourceCount());
611 585
612 // count should be the next timestamp we return. 586 // count should be the next timestamp we return.
613 SkASSERT(fTimestamp == SkToU32(count)); 587 SkASSERT(fTimestamp == SkToU32(count));
614
615 // The historical timestamps of flushes are now invalid.
616 this->resetFlushTimestamps();
617 } 588 }
618 } 589 }
619 return fTimestamp++; 590 return fTimestamp++;
620 } 591 }
621 592
622 void GrResourceCache::notifyFlushOccurred(FlushType type) { 593 void GrResourceCache::notifyFlushOccurred(FlushType type) {
623 switch (type) { 594 switch (type) {
624 case FlushType::kImmediateMode: 595 case FlushType::kImmediateMode:
625 break; 596 break;
626 case FlushType::kCacheRequested: 597 case FlushType::kCacheRequested:
627 SkASSERT(fRequestFlush); 598 SkASSERT(fRequestFlush);
628 fRequestFlush = false; 599 fRequestFlush = false;
629 break; 600 break;
630 case FlushType::kExternal: 601 case FlushType::kExternal:
631 if (fFlushTimestamps) { 602 ++fExternalFlushCnt;
632 SkASSERT(SkIsPow2(fMaxUnusedFlushes)); 603 if (0 == fExternalFlushCnt) {
633 fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMa xUnusedFlushes - 1); 604 // When this wraps just reset all the purgeable resources' last used flush state.
634 // get the timestamp before accessing fFlushTimestamps because g etNextTimestamp will 605 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
635 // reallocate fFlushTimestamps on timestamp overflow. 606 fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResource BecamePurgeable(0);
636 uint32_t timestamp = this->getNextTimestamp(); 607 }
637 fFlushTimestamps[fLastFlushTimestampIndex] = timestamp;
638 } 608 }
639 break; 609 break;
640 } 610 }
641 this->purgeAsNeeded(); 611 this->purgeAsNeeded();
642 } 612 }
643 613
644 void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) c onst { 614 void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) c onst {
645 for (int i = 0; i < fNonpurgeableResources.count(); ++i) { 615 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
646 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump); 616 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
647 } 617 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 return true; 749 return true;
780 } 750 }
781 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { 751 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
782 return true; 752 return true;
783 } 753 }
784 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the ca che."); 754 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the ca che.");
785 return false; 755 return false;
786 } 756 }
787 757
788 #endif 758 #endif
OLDNEW
« no previous file with comments | « src/gpu/GrResourceCache.h ('k') | tests/ResourceCacheTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698