bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 10 | #include "GrResourceCache.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 11 | #include "GrGpuResourceCacheAccess.h" |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 12 | #include "SkChecksum.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 13 | #include "SkGr.h" |
| 14 | #include "SkMessageBus.h" |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame^] | 15 | #include "SkTSort.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 16 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 17 | DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 18 | |
| 19 | ////////////////////////////////////////////////////////////////////////////// |
| 20 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 21 | GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 22 | static int32_t gType = INHERITED::kInvalidDomain + 1; |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 23 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 24 | int32_t type = sk_atomic_inc(&gType); |
robertphillips | 9790a7b | 2015-01-05 12:29:15 -0800 | [diff] [blame] | 25 | if (type > SK_MaxU16) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 26 | SkFAIL("Too many Resource Types"); |
| 27 | } |
| 28 | |
| 29 | return static_cast<ResourceType>(type); |
| 30 | } |
| 31 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 32 | GrUniqueKey::Domain GrUniqueKey::GenerateDomain() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 33 | static int32_t gDomain = INHERITED::kInvalidDomain + 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 34 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 35 | int32_t domain = sk_atomic_inc(&gDomain); |
kkinnunen | 016dffb | 2015-01-23 06:43:05 -0800 | [diff] [blame] | 36 | if (domain > SK_MaxU16) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 37 | SkFAIL("Too many GrUniqueKey Domains"); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 38 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 39 | |
| 40 | return static_cast<Domain>(domain); |
| 41 | } |
| 42 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { |
| 43 | return SkChecksum::Compute(data, size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 46 | ////////////////////////////////////////////////////////////////////////////// |
| 47 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 48 | class GrResourceCache::AutoValidate : ::SkNoncopyable { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 49 | public: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 50 | AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 51 | ~AutoValidate() { fCache->validate(); } |
| 52 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 53 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | ////////////////////////////////////////////////////////////////////////////// |
| 57 | |
| 58 | static const int kDefaultMaxCount = 2 * (1 << 10); |
| 59 | static const size_t kDefaultMaxSize = 96 * (1 << 20); |
| 60 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 61 | GrResourceCache::GrResourceCache() |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 62 | : fTimestamp(0) |
| 63 | , fMaxCount(kDefaultMaxCount) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 64 | , fMaxBytes(kDefaultMaxSize) |
| 65 | #if GR_CACHE_STATS |
| 66 | , fHighWaterCount(0) |
| 67 | , fHighWaterBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 68 | , fBudgetedHighWaterCount(0) |
| 69 | , fBudgetedHighWaterBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 70 | #endif |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 71 | , fBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 72 | , fBudgetedCount(0) |
| 73 | , fBudgetedBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 74 | , fOverBudgetCB(NULL) |
| 75 | , fOverBudgetData(NULL) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 76 | SkDEBUGCODE(fCount = 0;) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 77 | } |
| 78 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 79 | GrResourceCache::~GrResourceCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 80 | this->releaseAll(); |
| 81 | } |
| 82 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 83 | void GrResourceCache::setLimits(int count, size_t bytes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 84 | fMaxCount = count; |
| 85 | fMaxBytes = bytes; |
| 86 | this->purgeAsNeeded(); |
| 87 | } |
| 88 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 89 | void GrResourceCache::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 90 | SkASSERT(resource); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 91 | SkASSERT(!this->isInCache(resource)); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 92 | SkASSERT(!resource->wasDestroyed()); |
| 93 | SkASSERT(!resource->isPurgeable()); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame^] | 94 | |
| 95 | // We must set the timestamp before adding to the array in case the timestamp wraps and we wind |
| 96 | // up iterating over all the resources that already have timestamps. |
| 97 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
| 98 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 99 | this->addToNonpurgeableArray(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 100 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 101 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 102 | SkDEBUGCODE(++fCount;) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 103 | fBytes += size; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 104 | #if GR_CACHE_STATS |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 105 | fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount); |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 106 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 107 | #endif |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 108 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 109 | ++fBudgetedCount; |
| 110 | fBudgetedBytes += size; |
| 111 | #if GR_CACHE_STATS |
| 112 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 113 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 114 | #endif |
| 115 | } |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 116 | if (resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 117 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 118 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 119 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 120 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 121 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 122 | } |
| 123 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 124 | void GrResourceCache::removeResource(GrGpuResource* resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 125 | this->validate(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 126 | SkASSERT(this->isInCache(resource)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 127 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 128 | if (resource->isPurgeable()) { |
| 129 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 130 | } else { |
| 131 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 132 | } |
| 133 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 134 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 135 | SkDEBUGCODE(--fCount;) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 136 | fBytes -= size; |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 137 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 138 | --fBudgetedCount; |
| 139 | fBudgetedBytes -= size; |
| 140 | } |
| 141 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 142 | if (resource->resourcePriv().getScratchKey().isValid()) { |
| 143 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 144 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 145 | if (resource->getUniqueKey().isValid()) { |
| 146 | fUniqueHash.remove(resource->getUniqueKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 147 | } |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 148 | this->validate(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 149 | } |
| 150 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 151 | void GrResourceCache::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 152 | AutoValidate av(this); |
| 153 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 154 | while (fNonpurgeableResources.count()) { |
| 155 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 156 | SkASSERT(!back->wasDestroyed()); |
| 157 | back->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 158 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 159 | |
| 160 | while (fPurgeableQueue.count()) { |
| 161 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 162 | SkASSERT(!top->wasDestroyed()); |
| 163 | top->cacheAccess().abandon(); |
| 164 | } |
| 165 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 166 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 167 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 168 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 169 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 170 | SkASSERT(!fBytes); |
| 171 | SkASSERT(!fBudgetedCount); |
| 172 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 175 | void GrResourceCache::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 176 | AutoValidate av(this); |
| 177 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 178 | while(fNonpurgeableResources.count()) { |
| 179 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 180 | SkASSERT(!back->wasDestroyed()); |
| 181 | back->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 182 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 183 | |
| 184 | while (fPurgeableQueue.count()) { |
| 185 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 186 | SkASSERT(!top->wasDestroyed()); |
| 187 | top->cacheAccess().release(); |
| 188 | } |
| 189 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 190 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 191 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 192 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 193 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 194 | SkASSERT(!fBytes); |
| 195 | SkASSERT(!fBudgetedCount); |
| 196 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 197 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 198 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 199 | class GrResourceCache::AvailableForScratchUse { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 200 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 201 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 202 | |
| 203 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 204 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 205 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 206 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 207 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 208 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 209 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 210 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 211 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 214 | GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey, |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 215 | uint32_t flags) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 216 | SkASSERT(scratchKey.isValid()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 217 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 218 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 219 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 220 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 221 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 222 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 223 | this->validate(); |
| 224 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 225 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 226 | return NULL; |
| 227 | } |
| 228 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 229 | // but there is still space in our budget for the resource. |
| 230 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 231 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 232 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 233 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 234 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 235 | } |
| 236 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 237 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 238 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 239 | void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 240 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
| 241 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 242 | } |
| 243 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 244 | void GrResourceCache::removeUniqueKey(GrGpuResource* resource) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 245 | // Someone has a ref to this resource in order to invalidate it. When the ref count reaches |
| 246 | // zero we will get a notifyPurgable() and figure out what to do with it. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 247 | if (resource->getUniqueKey().isValid()) { |
| 248 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 249 | fUniqueHash.remove(resource->getUniqueKey()); |
| 250 | } |
| 251 | resource->cacheAccess().removeUniqueKey(); |
| 252 | this->validate(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 253 | } |
| 254 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 255 | void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 256 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 257 | SkASSERT(this->isInCache(resource)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 258 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 259 | // Remove the entry for this resource if it already has a unique key. |
| 260 | if (resource->getUniqueKey().isValid()) { |
| 261 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 262 | fUniqueHash.remove(resource->getUniqueKey()); |
| 263 | SkASSERT(NULL == fUniqueHash.find(resource->getUniqueKey())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 264 | } |
| 265 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 266 | // If another resource has the new key, remove its key then install the key on this resource. |
| 267 | if (newKey.isValid()) { |
| 268 | if (GrGpuResource* old = fUniqueHash.find(newKey)) { |
| 269 | // If the old resource using the key is purgeable and is unreachable, then remove it. |
| 270 | if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) { |
| 271 | // release may call validate() which will assert that resource is in fUniqueHash |
| 272 | // if it has a valid key. So in debug reset the key here before we assign it. |
| 273 | SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();) |
| 274 | old->cacheAccess().release(); |
| 275 | } else { |
| 276 | fUniqueHash.remove(newKey); |
| 277 | old->cacheAccess().removeUniqueKey(); |
| 278 | } |
| 279 | } |
| 280 | SkASSERT(NULL == fUniqueHash.find(newKey)); |
| 281 | resource->cacheAccess().setUniqueKey(newKey); |
| 282 | fUniqueHash.add(resource); |
| 283 | } else { |
| 284 | resource->cacheAccess().removeUniqueKey(); |
| 285 | } |
| 286 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 287 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 288 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 289 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 290 | void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 291 | SkASSERT(resource); |
| 292 | SkASSERT(this->isInCache(resource)); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame^] | 293 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 294 | if (resource->isPurgeable()) { |
| 295 | // It's about to become unpurgeable. |
| 296 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 297 | this->addToNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 298 | } |
| 299 | resource->ref(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame^] | 300 | |
| 301 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 302 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 303 | } |
| 304 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 305 | void GrResourceCache::notifyPurgeable(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 306 | SkASSERT(resource); |
| 307 | SkASSERT(this->isInCache(resource)); |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 308 | SkASSERT(resource->isPurgeable()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 309 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 310 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 311 | fPurgeableQueue.insert(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 312 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 313 | if (!resource->resourcePriv().isBudgeted()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 314 | // Check whether this resource could still be used as a scratch resource. |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 315 | if (!resource->cacheAccess().isWrapped() && |
| 316 | resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 317 | // We won't purge an existing resource to make room for this one. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 318 | if (fBudgetedCount < fMaxCount && |
| 319 | fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 320 | resource->resourcePriv().makeBudgeted(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 321 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 322 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 323 | } |
| 324 | } else { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 325 | // Purge the resource immediately if we're over budget |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 326 | // Also purge if the resource has neither a valid scratch key nor a unique key. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 327 | bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 328 | !resource->getUniqueKey().isValid(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 329 | if (!this->overBudget() && !noKey) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 330 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 333 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 334 | SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 335 | resource->cacheAccess().release(); |
| 336 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 337 | SkASSERT(this->getResourceCount() < beforeCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 338 | this->validate(); |
| 339 | } |
| 340 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 341 | void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 342 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 343 | SkASSERT(resource); |
| 344 | SkASSERT(this->isInCache(resource)); |
| 345 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 346 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 347 | |
| 348 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 349 | #if GR_CACHE_STATS |
| 350 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 351 | #endif |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 352 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 353 | fBudgetedBytes += delta; |
| 354 | #if GR_CACHE_STATS |
| 355 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 356 | #endif |
| 357 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 358 | |
| 359 | this->purgeAsNeeded(); |
| 360 | this->validate(); |
| 361 | } |
| 362 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 363 | void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 364 | SkASSERT(resource); |
| 365 | SkASSERT(this->isInCache(resource)); |
| 366 | |
| 367 | size_t size = resource->gpuMemorySize(); |
| 368 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 369 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 370 | ++fBudgetedCount; |
| 371 | fBudgetedBytes += size; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 372 | #if GR_CACHE_STATS |
| 373 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 374 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 375 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 376 | this->purgeAsNeeded(); |
| 377 | } else { |
| 378 | --fBudgetedCount; |
| 379 | fBudgetedBytes -= size; |
| 380 | } |
| 381 | |
| 382 | this->validate(); |
| 383 | } |
| 384 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 385 | void GrResourceCache::internalPurgeAsNeeded() { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 386 | SkASSERT(this->overBudget()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 387 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 388 | bool stillOverbudget = true; |
| 389 | while (fPurgeableQueue.count()) { |
| 390 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 391 | SkASSERT(resource->isPurgeable()); |
| 392 | resource->cacheAccess().release(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 393 | if (!this->overBudget()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 394 | stillOverbudget = false; |
| 395 | break; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 396 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 397 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 398 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 399 | this->validate(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 400 | |
| 401 | if (stillOverbudget) { |
| 402 | // Despite the purge we're still over budget. Call our over budget callback. If this frees |
| 403 | // any resources then we'll get notifyPurgeable() calls and take appropriate action. |
| 404 | (*fOverBudgetCB)(fOverBudgetData); |
| 405 | this->validate(); |
| 406 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 407 | } |
| 408 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 409 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 410 | // We could disable maintaining the heap property here, but it would add a lot of complexity. |
| 411 | // Moreover, this is rarely called. |
| 412 | while (fPurgeableQueue.count()) { |
| 413 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 414 | SkASSERT(resource->isPurgeable()); |
| 415 | resource->cacheAccess().release(); |
| 416 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 417 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 418 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 419 | } |
| 420 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 421 | void GrResourceCache::processInvalidUniqueKeys( |
| 422 | const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 423 | for (int i = 0; i < msgs.count(); ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 424 | GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 425 | if (resource) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 426 | resource->resourcePriv().removeUniqueKey(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 427 | resource->unref(); // will call notifyPurgeable, if it is indeed now purgeable. |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 432 | void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
| 433 | int index = fNonpurgeableResources.count(); |
| 434 | *fNonpurgeableResources.append() = resource; |
| 435 | *resource->cacheAccess().accessCacheIndex() = index; |
| 436 | } |
| 437 | |
| 438 | void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) { |
| 439 | int* index = resource->cacheAccess().accessCacheIndex(); |
| 440 | // Fill the whole we will create in the array with the tail object, adjust its index, and |
| 441 | // then pop the array |
| 442 | GrGpuResource* tail = *(fNonpurgeableResources.end() - 1); |
| 443 | SkASSERT(fNonpurgeableResources[*index] == resource); |
| 444 | fNonpurgeableResources[*index] = tail; |
| 445 | *tail->cacheAccess().accessCacheIndex() = *index; |
| 446 | fNonpurgeableResources.pop(); |
| 447 | SkDEBUGCODE(*index = -1); |
| 448 | } |
| 449 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame^] | 450 | uint32_t GrResourceCache::getNextTimestamp() { |
| 451 | // If we wrap then all the existing resources will appear older than any resources that get |
| 452 | // a timestamp after the wrap. |
| 453 | if (0 == fTimestamp) { |
| 454 | int count = this->getResourceCount(); |
| 455 | if (count) { |
| 456 | // Reset all the timestamps. We sort the resources by timestamp and then assign |
| 457 | // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely |
| 458 | // rare. |
| 459 | SkTDArray<GrGpuResource*> sortedPurgeableResources; |
| 460 | sortedPurgeableResources.setReserve(fPurgeableQueue.count()); |
| 461 | |
| 462 | while (fPurgeableQueue.count()) { |
| 463 | *sortedPurgeableResources.append() = fPurgeableQueue.peek(); |
| 464 | fPurgeableQueue.pop(); |
| 465 | } |
| 466 | |
| 467 | struct Less { |
| 468 | bool operator()(GrGpuResource* a, GrGpuResource* b) { |
| 469 | return CompareTimestamp(a,b); |
| 470 | } |
| 471 | }; |
| 472 | Less less; |
| 473 | SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1, less); |
| 474 | |
| 475 | // Pick resources out of the purgeable and non-purgeable arrays based on lowest |
| 476 | // timestamp and assign new timestamps. |
| 477 | int currP = 0; |
| 478 | int currNP = 0; |
| 479 | while (currP < sortedPurgeableResources.count() && |
| 480 | currNP < fNonpurgeableResources.count()) { |
| 481 | uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp(); |
| 482 | uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp(); |
| 483 | SkASSERT(tsP != tsNP); |
| 484 | if (tsP < tsNP) { |
| 485 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 486 | } else { |
| 487 | // Correct the index in the nonpurgeable array stored on the resource post-sort. |
| 488 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 489 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // The above loop ended when we hit the end of one array. Finish the other one. |
| 494 | while (currP < sortedPurgeableResources.count()) { |
| 495 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 496 | } |
| 497 | while (currNP < fNonpurgeableResources.count()) { |
| 498 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 499 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 500 | } |
| 501 | |
| 502 | // Rebuild the queue. |
| 503 | for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 504 | fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 505 | } |
| 506 | |
| 507 | this->validate(); |
| 508 | SkASSERT(count == this->getResourceCount()); |
| 509 | |
| 510 | // count should be the next timestamp we return. |
| 511 | SkASSERT(fTimestamp == SkToU32(count)); |
| 512 | } |
| 513 | } |
| 514 | return fTimestamp++; |
| 515 | } |
| 516 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 517 | #ifdef SK_DEBUG |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 518 | void GrResourceCache::validate() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 519 | // Reduce the frequency of validations for large resource counts. |
| 520 | static SkRandom gRandom; |
| 521 | int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 522 | if (~mask && (gRandom.nextU() & mask)) { |
| 523 | return; |
| 524 | } |
| 525 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 526 | struct Stats { |
| 527 | size_t fBytes; |
| 528 | int fBudgetedCount; |
| 529 | size_t fBudgetedBytes; |
| 530 | int fLocked; |
| 531 | int fScratch; |
| 532 | int fCouldBeScratch; |
| 533 | int fContent; |
| 534 | const ScratchMap* fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 535 | const UniqueHash* fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 536 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 537 | Stats(const GrResourceCache* cache) { |
| 538 | memset(this, 0, sizeof(*this)); |
| 539 | fScratchMap = &cache->fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 540 | fUniqueHash = &cache->fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 541 | } |
| 542 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 543 | void update(GrGpuResource* resource) { |
| 544 | fBytes += resource->gpuMemorySize(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 545 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 546 | if (!resource->isPurgeable()) { |
| 547 | ++fLocked; |
| 548 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 549 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 550 | if (resource->cacheAccess().isScratch()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 551 | SkASSERT(!resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 552 | ++fScratch; |
| 553 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
| 554 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 555 | } else if (resource->resourcePriv().getScratchKey().isValid()) { |
| 556 | SkASSERT(!resource->resourcePriv().isBudgeted() || |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 557 | resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 558 | ++fCouldBeScratch; |
| 559 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
| 560 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 561 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 562 | const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
| 563 | if (uniqueKey.isValid()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 564 | ++fContent; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 565 | SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 566 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 567 | SkASSERT(resource->resourcePriv().isBudgeted()); |
| 568 | } |
| 569 | |
| 570 | if (resource->resourcePriv().isBudgeted()) { |
| 571 | ++fBudgetedCount; |
| 572 | fBudgetedBytes += resource->gpuMemorySize(); |
| 573 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 574 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | Stats stats(this); |
| 578 | |
| 579 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 580 | SkASSERT(!fNonpurgeableResources[i]->isPurgeable()); |
| 581 | SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i); |
| 582 | SkASSERT(!fNonpurgeableResources[i]->wasDestroyed()); |
| 583 | stats.update(fNonpurgeableResources[i]); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 584 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 585 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 586 | SkASSERT(fPurgeableQueue.at(i)->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 587 | SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i); |
| 588 | SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed()); |
| 589 | stats.update(fPurgeableQueue.at(i)); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 590 | } |
| 591 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 592 | SkASSERT(fCount == this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 593 | SkASSERT(fBudgetedCount <= fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 594 | SkASSERT(fBudgetedBytes <= fBytes); |
| 595 | SkASSERT(stats.fBytes == fBytes); |
| 596 | SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
| 597 | SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 598 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 599 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 600 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 601 | SkASSERT(fBytes <= fHighWaterBytes); |
| 602 | SkASSERT(fCount <= fHighWaterCount); |
| 603 | SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
| 604 | SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 605 | #endif |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 606 | SkASSERT(stats.fContent == fUniqueHash.count()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 607 | SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 608 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 609 | // This assertion is not currently valid because we can be in recursive notifyIsPurgeable() |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 610 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 611 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 612 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 613 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 614 | |
| 615 | bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
| 616 | int index = *resource->cacheAccess().accessCacheIndex(); |
| 617 | if (index < 0) { |
| 618 | return false; |
| 619 | } |
| 620 | if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) { |
| 621 | return true; |
| 622 | } |
| 623 | if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { |
| 624 | return true; |
| 625 | } |
| 626 | SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache."); |
| 627 | return false; |
| 628 | } |
| 629 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 630 | #endif |