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