| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "GrResourceCache.h" | 10 #include "GrResourceCache.h" |
| 11 #include "GrGpuResourceCacheAccess.h" | 11 #include "GrGpuResourceCacheAccess.h" |
| 12 #include "GrTracing.h" | 12 #include "GrTracing.h" |
| 13 #include "SkChecksum.h" | 13 #include "SkChecksum.h" |
| 14 #include "SkGr.h" | 14 #include "SkGr.h" |
| 15 #include "SkMessageBus.h" | 15 #include "SkMessageBus.h" |
| 16 #include "SkTraceMemoryDump.h" |
| 16 #include "SkTSort.h" | 17 #include "SkTSort.h" |
| 17 | 18 |
| 18 DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); | 19 DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); |
| 19 | 20 |
| 20 ////////////////////////////////////////////////////////////////////////////// | 21 ////////////////////////////////////////////////////////////////////////////// |
| 21 | 22 |
| 22 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { | 23 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
| 23 static int32_t gType = INHERITED::kInvalidDomain + 1; | 24 static int32_t gType = INHERITED::kInvalidDomain + 1; |
| 24 | 25 |
| 25 int32_t type = sk_atomic_inc(&gType); | 26 int32_t type = sk_atomic_inc(&gType); |
| (...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 } | 554 } |
| 554 }; | 555 }; |
| 555 Less less; | 556 Less less; |
| 556 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(
) - 1, less); | 557 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(
) - 1, less); |
| 557 | 558 |
| 558 // Pick resources out of the purgeable and non-purgeable arrays base
d on lowest | 559 // Pick resources out of the purgeable and non-purgeable arrays base
d on lowest |
| 559 // timestamp and assign new timestamps. | 560 // timestamp and assign new timestamps. |
| 560 int currP = 0; | 561 int currP = 0; |
| 561 int currNP = 0; | 562 int currNP = 0; |
| 562 while (currP < sortedPurgeableResources.count() && | 563 while (currP < sortedPurgeableResources.count() && |
| 563 currNP < fNonpurgeableResources.count()) { | 564 currNP < fNonpurgeableResources.count()) { |
| 564 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().ti
mestamp(); | 565 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().ti
mestamp(); |
| 565 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().ti
mestamp(); | 566 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().ti
mestamp(); |
| 566 SkASSERT(tsP != tsNP); | 567 SkASSERT(tsP != tsNP); |
| 567 if (tsP < tsNP) { | 568 if (tsP < tsNP) { |
| 568 sortedPurgeableResources[currP++]->cacheAccess().setTimestam
p(fTimestamp++); | 569 sortedPurgeableResources[currP++]->cacheAccess().setTimestam
p(fTimestamp++); |
| 569 } else { | 570 } else { |
| 570 // Correct the index in the nonpurgeable array stored on the
resource post-sort. | 571 // Correct the index in the nonpurgeable array stored on the
resource post-sort. |
| 571 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIn
dex() = currNP; | 572 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIn
dex() = currNP; |
| 572 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp
(fTimestamp++); | 573 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp
(fTimestamp++); |
| 573 } | 574 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 585 // Rebuild the queue. | 586 // Rebuild the queue. |
| 586 for (int i = 0; i < sortedPurgeableResources.count(); ++i) { | 587 for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 587 fPurgeableQueue.insert(sortedPurgeableResources[i]); | 588 fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 588 } | 589 } |
| 589 | 590 |
| 590 this->validate(); | 591 this->validate(); |
| 591 SkASSERT(count == this->getResourceCount()); | 592 SkASSERT(count == this->getResourceCount()); |
| 592 | 593 |
| 593 // count should be the next timestamp we return. | 594 // count should be the next timestamp we return. |
| 594 SkASSERT(fTimestamp == SkToU32(count)); | 595 SkASSERT(fTimestamp == SkToU32(count)); |
| 595 | 596 |
| 596 // The historical timestamps of flushes are now invalid. | 597 // The historical timestamps of flushes are now invalid. |
| 597 this->resetFlushTimestamps(); | 598 this->resetFlushTimestamps(); |
| 598 } | 599 } |
| 599 } | 600 } |
| 600 return fTimestamp++; | 601 return fTimestamp++; |
| 601 } | 602 } |
| 602 | 603 |
| 603 void GrResourceCache::notifyFlushOccurred() { | 604 void GrResourceCache::notifyFlushOccurred() { |
| 604 if (fFlushTimestamps) { | 605 if (fFlushTimestamps) { |
| 605 SkASSERT(SkIsPow2(fMaxUnusedFlushes)); | 606 SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 606 fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedF
lushes - 1); | 607 fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedF
lushes - 1); |
| 607 // get the timestamp before accessing fFlushTimestamps because getNextTi
mestamp will | 608 // get the timestamp before accessing fFlushTimestamps because getNextTi
mestamp will |
| 608 // reallocate fFlushTimestamps on timestamp overflow. | 609 // reallocate fFlushTimestamps on timestamp overflow. |
| 609 uint32_t timestamp = this->getNextTimestamp(); | 610 uint32_t timestamp = this->getNextTimestamp(); |
| 610 fFlushTimestamps[fLastFlushTimestampIndex] = timestamp; | 611 fFlushTimestamps[fLastFlushTimestampIndex] = timestamp; |
| 611 this->purgeAsNeeded(); | 612 this->purgeAsNeeded(); |
| 612 } | 613 } |
| 613 } | 614 } |
| 614 | 615 |
| 616 void GrResourceCache::onMemoryDump(SkTraceMemoryDump *traceMemoryDump) const { |
| 617 for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 618 fNonpurgeableResources[i]->onMemoryDump(traceMemoryDump); |
| 619 } |
| 620 for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 621 fPurgeableQueue.at(i)->onMemoryDump(traceMemoryDump); |
| 622 } |
| 623 } |
| 624 |
| 615 #ifdef SK_DEBUG | 625 #ifdef SK_DEBUG |
| 616 void GrResourceCache::validate() const { | 626 void GrResourceCache::validate() const { |
| 617 // Reduce the frequency of validations for large resource counts. | 627 // Reduce the frequency of validations for large resource counts. |
| 618 static SkRandom gRandom; | 628 static SkRandom gRandom; |
| 619 int mask = (SkNextPow2(fCount + 1) >> 5) - 1; | 629 int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 620 if (~mask && (gRandom.nextU() & mask)) { | 630 if (~mask && (gRandom.nextU() & mask)) { |
| 621 return; | 631 return; |
| 622 } | 632 } |
| 623 | 633 |
| 624 struct Stats { | 634 struct Stats { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 return true; | 730 return true; |
| 721 } | 731 } |
| 722 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index]
== resource) { | 732 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index]
== resource) { |
| 723 return true; | 733 return true; |
| 724 } | 734 } |
| 725 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the ca
che."); | 735 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the ca
che."); |
| 726 return false; | 736 return false; |
| 727 } | 737 } |
| 728 | 738 |
| 729 #endif | 739 #endif |
| OLD | NEW |