bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 9 | #include "GrResourceCache.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 10 | #include "GrGpuResourceCacheAccess.h" |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 11 | #include "GrTracing.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 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 42 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 43 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 44 | return SkChecksum::Murmur3(data, size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 47 | ////////////////////////////////////////////////////////////////////////////// |
| 48 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 49 | class GrResourceCache::AutoValidate : ::SkNoncopyable { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 50 | public: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 51 | AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 52 | ~AutoValidate() { fCache->validate(); } |
| 53 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 54 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | ////////////////////////////////////////////////////////////////////////////// |
| 58 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 59 | |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 60 | GrResourceCache::GrResourceCache(const GrCaps* caps) |
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) |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 64 | , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 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) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | , fOverBudgetCB(nullptr) |
| 75 | , fOverBudgetData(nullptr) |
| 76 | , fFlushTimestamps(nullptr) |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 77 | , fLastFlushTimestampIndex(0) |
| 78 | , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 79 | SkDEBUGCODE(fCount = 0;) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;) |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 81 | this->resetFlushTimestamps(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 82 | } |
| 83 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 84 | GrResourceCache::~GrResourceCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 85 | this->releaseAll(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 86 | delete[] fFlushTimestamps; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 87 | } |
| 88 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 89 | void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 90 | fMaxCount = count; |
| 91 | fMaxBytes = bytes; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 92 | fMaxUnusedFlushes = maxUnusedFlushes; |
| 93 | this->resetFlushTimestamps(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 94 | this->purgeAsNeeded(); |
| 95 | } |
| 96 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 97 | void GrResourceCache::resetFlushTimestamps() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 98 | delete[] fFlushTimestamps; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 99 | |
| 100 | // We assume this number is a power of two when wrapping indices into the timestamp array. |
| 101 | fMaxUnusedFlushes = SkNextPow2(fMaxUnusedFlushes); |
| 102 | |
| 103 | // Since our implementation is to store the timestamps of the last fMaxUnusedFlushes flush calls |
| 104 | // we just turn the feature off if that array would be large. |
| 105 | static const int kMaxSupportedTimestampHistory = 128; |
| 106 | |
| 107 | if (fMaxUnusedFlushes > kMaxSupportedTimestampHistory) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | fFlushTimestamps = nullptr; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 112 | fFlushTimestamps = new uint32_t[fMaxUnusedFlushes]; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 113 | fLastFlushTimestampIndex = 0; |
| 114 | // Set all the historical flush timestamps to initially be at the beginning of time (timestamp |
| 115 | // 0). |
| 116 | sk_bzero(fFlushTimestamps, fMaxUnusedFlushes * sizeof(uint32_t)); |
| 117 | } |
| 118 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 119 | void GrResourceCache::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 120 | SkASSERT(resource); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 121 | SkASSERT(!this->isInCache(resource)); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 122 | SkASSERT(!resource->wasDestroyed()); |
| 123 | SkASSERT(!resource->isPurgeable()); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 124 | |
| 125 | // We must set the timestamp before adding to the array in case the timestamp wraps and we wind |
| 126 | // up iterating over all the resources that already have timestamps. |
| 127 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
| 128 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 129 | this->addToNonpurgeableArray(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 130 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 131 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 132 | SkDEBUGCODE(++fCount;) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 133 | fBytes += size; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 134 | #if GR_CACHE_STATS |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 135 | fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount); |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 136 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 137 | #endif |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 138 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 139 | ++fBudgetedCount; |
| 140 | fBudgetedBytes += size; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 141 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 142 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 143 | #if GR_CACHE_STATS |
| 144 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 145 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 146 | #endif |
| 147 | } |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 148 | if (resource->resourcePriv().getScratchKey().isValid() && |
| 149 | !resource->getUniqueKey().isValid()) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 150 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 151 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 152 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 153 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 154 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 155 | } |
| 156 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 157 | void GrResourceCache::removeResource(GrGpuResource* resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 158 | this->validate(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 159 | SkASSERT(this->isInCache(resource)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 160 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 161 | if (resource->isPurgeable()) { |
| 162 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 163 | } else { |
| 164 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 165 | } |
| 166 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 167 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 168 | SkDEBUGCODE(--fCount;) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 169 | fBytes -= size; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 170 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 171 | --fBudgetedCount; |
| 172 | fBudgetedBytes -= size; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 173 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 174 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 175 | } |
| 176 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 177 | if (resource->resourcePriv().getScratchKey().isValid() && |
| 178 | !resource->getUniqueKey().isValid()) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 179 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 180 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 181 | if (resource->getUniqueKey().isValid()) { |
| 182 | fUniqueHash.remove(resource->getUniqueKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 183 | } |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 184 | this->validate(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 185 | } |
| 186 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 187 | void GrResourceCache::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 188 | AutoValidate av(this); |
| 189 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 190 | while (fNonpurgeableResources.count()) { |
| 191 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 192 | SkASSERT(!back->wasDestroyed()); |
| 193 | back->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 194 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 195 | |
| 196 | while (fPurgeableQueue.count()) { |
| 197 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 198 | SkASSERT(!top->wasDestroyed()); |
| 199 | top->cacheAccess().abandon(); |
| 200 | } |
| 201 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 202 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 203 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 204 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 205 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 206 | SkASSERT(!fBytes); |
| 207 | SkASSERT(!fBudgetedCount); |
| 208 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 209 | } |
| 210 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 211 | void GrResourceCache::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 212 | AutoValidate av(this); |
| 213 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 214 | while(fNonpurgeableResources.count()) { |
| 215 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 216 | SkASSERT(!back->wasDestroyed()); |
| 217 | back->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 218 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 219 | |
| 220 | while (fPurgeableQueue.count()) { |
| 221 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 222 | SkASSERT(!top->wasDestroyed()); |
| 223 | top->cacheAccess().release(); |
| 224 | } |
| 225 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 226 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 227 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 228 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 229 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 230 | SkASSERT(!fBytes); |
| 231 | SkASSERT(!fBudgetedCount); |
| 232 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 233 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 234 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 235 | class GrResourceCache::AvailableForScratchUse { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 236 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 237 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 238 | |
| 239 | bool operator()(const GrGpuResource* resource) const { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 240 | SkASSERT(!resource->getUniqueKey().isValid() && |
| 241 | resource->resourcePriv().getScratchKey().isValid()); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 242 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 243 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 244 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 245 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 246 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 247 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 248 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 249 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 252 | GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey, |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 253 | size_t resourceSize, |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 254 | uint32_t flags) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 255 | SkASSERT(scratchKey.isValid()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 256 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 257 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 258 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 259 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 260 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 261 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 262 | this->validate(); |
| 263 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 264 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 265 | return nullptr; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 266 | } |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 267 | // We would prefer to consume more available VRAM rather than flushing |
| 268 | // immediately, but on ANGLE this can lead to starving of the GPU. |
| 269 | if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 270 | // kPrefer is specified, we didn't find a resource without pending io, |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 271 | // but there is still space in our budget for the resource so force |
| 272 | // the caller to allocate a new resource. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 273 | return nullptr; |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 274 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 275 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 276 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 277 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 278 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 279 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 280 | } |
| 281 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 282 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 283 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 284 | void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 285 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 286 | if (!resource->getUniqueKey().isValid()) { |
| 287 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
| 288 | } |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 289 | } |
| 290 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 291 | void GrResourceCache::removeUniqueKey(GrGpuResource* resource) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 292 | // Someone has a ref to this resource in order to have removed the key. When the ref count |
| 293 | // reaches zero we will get a ref cnt notification and figure out what to do with it. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 294 | if (resource->getUniqueKey().isValid()) { |
| 295 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 296 | fUniqueHash.remove(resource->getUniqueKey()); |
| 297 | } |
| 298 | resource->cacheAccess().removeUniqueKey(); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 299 | |
| 300 | if (resource->resourcePriv().getScratchKey().isValid()) { |
| 301 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
| 302 | } |
| 303 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 304 | this->validate(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 305 | } |
| 306 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 307 | void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 308 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 309 | SkASSERT(this->isInCache(resource)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 310 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 311 | // If another resource has the new key, remove its key then install the key on this resource. |
| 312 | if (newKey.isValid()) { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 313 | // Remove the entry for this resource if it already has a unique key. |
| 314 | if (resource->getUniqueKey().isValid()) { |
| 315 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 316 | fUniqueHash.remove(resource->getUniqueKey()); |
| 317 | SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey())); |
| 318 | } else { |
| 319 | // 'resource' didn't have a valid unique key before so it is switching sides. Remove it |
| 320 | // from the ScratchMap |
| 321 | if (resource->resourcePriv().getScratchKey().isValid()) { |
| 322 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
| 323 | } |
| 324 | } |
| 325 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 326 | if (GrGpuResource* old = fUniqueHash.find(newKey)) { |
| 327 | // If the old resource using the key is purgeable and is unreachable, then remove it. |
| 328 | if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) { |
| 329 | // release may call validate() which will assert that resource is in fUniqueHash |
| 330 | // if it has a valid key. So in debug reset the key here before we assign it. |
| 331 | SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();) |
| 332 | old->cacheAccess().release(); |
| 333 | } else { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 334 | this->removeUniqueKey(old); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 337 | SkASSERT(nullptr == fUniqueHash.find(newKey)); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 338 | resource->cacheAccess().setUniqueKey(newKey); |
| 339 | fUniqueHash.add(resource); |
| 340 | } else { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 341 | this->removeUniqueKey(resource); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 342 | } |
| 343 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 344 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 345 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 346 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 347 | void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 348 | SkASSERT(resource); |
| 349 | SkASSERT(this->isInCache(resource)); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 350 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 351 | if (resource->isPurgeable()) { |
| 352 | // It's about to become unpurgeable. |
| 353 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 354 | this->addToNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 355 | } |
| 356 | resource->ref(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 357 | |
| 358 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 359 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 360 | } |
| 361 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 362 | void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 363 | SkASSERT(resource); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 364 | SkASSERT(!resource->wasDestroyed()); |
| 365 | SkASSERT(flags); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 366 | SkASSERT(this->isInCache(resource)); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 367 | // This resource should always be in the nonpurgeable array when this function is called. It |
| 368 | // will be moved to the queue if it is newly purgeable. |
| 369 | SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 370 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 371 | if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) { |
| 372 | #ifdef SK_DEBUG |
| 373 | // When the timestamp overflows validate() is called. validate() checks that resources in |
| 374 | // the nonpurgeable array are indeed not purgeable. However, the movement from the array to |
| 375 | // the purgeable queue happens just below in this function. So we mark it as an exception. |
| 376 | if (resource->isPurgeable()) { |
| 377 | fNewlyPurgeableResourceForValidation = resource; |
| 378 | } |
| 379 | #endif |
| 380 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 381 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) { |
| 385 | SkASSERT(!resource->isPurgeable()); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | SkASSERT(resource->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 390 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 391 | fPurgeableQueue.insert(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 392 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 393 | if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 394 | // Check whether this resource could still be used as a scratch resource. |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 395 | if (!resource->resourcePriv().refsWrappedObjects() && |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 396 | resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 397 | // We won't purge an existing resource to make room for this one. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 398 | if (fBudgetedCount < fMaxCount && |
| 399 | fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 400 | resource->resourcePriv().makeBudgeted(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 401 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 402 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 403 | } |
| 404 | } else { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 405 | // Purge the resource immediately if we're over budget |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 406 | // 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] | 407 | bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 408 | !resource->getUniqueKey().isValid(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 409 | if (!this->overBudget() && !noKey) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 410 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 413 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 414 | SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 415 | resource->cacheAccess().release(); |
| 416 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 417 | SkASSERT(this->getResourceCount() < beforeCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 418 | this->validate(); |
| 419 | } |
| 420 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 421 | void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 422 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 423 | SkASSERT(resource); |
| 424 | SkASSERT(this->isInCache(resource)); |
| 425 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 426 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 427 | |
| 428 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 429 | #if GR_CACHE_STATS |
| 430 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 431 | #endif |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 432 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 433 | fBudgetedBytes += delta; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 434 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 435 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 436 | #if GR_CACHE_STATS |
| 437 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 438 | #endif |
| 439 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 440 | |
| 441 | this->purgeAsNeeded(); |
| 442 | this->validate(); |
| 443 | } |
| 444 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 445 | void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 446 | SkASSERT(resource); |
| 447 | SkASSERT(this->isInCache(resource)); |
| 448 | |
| 449 | size_t size = resource->gpuMemorySize(); |
| 450 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 451 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 452 | ++fBudgetedCount; |
| 453 | fBudgetedBytes += size; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 454 | #if GR_CACHE_STATS |
| 455 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 456 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 457 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 458 | this->purgeAsNeeded(); |
| 459 | } else { |
| 460 | --fBudgetedCount; |
| 461 | fBudgetedBytes -= size; |
| 462 | } |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 463 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 464 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 465 | |
| 466 | this->validate(); |
| 467 | } |
| 468 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 469 | void GrResourceCache::purgeAsNeeded() { |
| 470 | SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs; |
| 471 | fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); |
| 472 | if (invalidKeyMsgs.count()) { |
| 473 | this->processInvalidUniqueKeys(invalidKeyMsgs); |
| 474 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 475 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 476 | if (fFlushTimestamps) { |
| 477 | // Assuming kNumFlushesToDeleteUnusedResource is a power of 2. |
| 478 | SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 479 | int oldestFlushIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1); |
| 480 | |
| 481 | uint32_t oldestAllowedTimestamp = fFlushTimestamps[oldestFlushIndex]; |
| 482 | while (fPurgeableQueue.count()) { |
| 483 | uint32_t oldestResourceTimestamp = fPurgeableQueue.peek()->cacheAccess().timestamp(); |
| 484 | if (oldestAllowedTimestamp < oldestResourceTimestamp) { |
| 485 | break; |
| 486 | } |
| 487 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 488 | SkASSERT(resource->isPurgeable()); |
| 489 | resource->cacheAccess().release(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | bool stillOverbudget = this->overBudget(); |
| 494 | while (stillOverbudget && fPurgeableQueue.count()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 495 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 496 | SkASSERT(resource->isPurgeable()); |
| 497 | resource->cacheAccess().release(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 498 | stillOverbudget = this->overBudget(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 499 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 500 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 501 | this->validate(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 502 | |
| 503 | if (stillOverbudget) { |
| 504 | // Despite the purge we're still over budget. Call our over budget callback. If this frees |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 505 | // any resources then we'll get notified and take appropriate action. |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 506 | (*fOverBudgetCB)(fOverBudgetData); |
| 507 | this->validate(); |
| 508 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 509 | } |
| 510 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 511 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 512 | // We could disable maintaining the heap property here, but it would add a lot of complexity. |
| 513 | // Moreover, this is rarely called. |
| 514 | while (fPurgeableQueue.count()) { |
| 515 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 516 | SkASSERT(resource->isPurgeable()); |
| 517 | resource->cacheAccess().release(); |
| 518 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 519 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 520 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 521 | } |
| 522 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 523 | void GrResourceCache::processInvalidUniqueKeys( |
| 524 | const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 525 | for (int i = 0; i < msgs.count(); ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 526 | GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 527 | if (resource) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 528 | resource->resourcePriv().removeUniqueKey(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 529 | resource->unref(); // If this resource is now purgeable, the cache will be notified. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 534 | void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
| 535 | int index = fNonpurgeableResources.count(); |
| 536 | *fNonpurgeableResources.append() = resource; |
| 537 | *resource->cacheAccess().accessCacheIndex() = index; |
| 538 | } |
| 539 | |
| 540 | void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) { |
| 541 | int* index = resource->cacheAccess().accessCacheIndex(); |
| 542 | // Fill the whole we will create in the array with the tail object, adjust its index, and |
| 543 | // then pop the array |
| 544 | GrGpuResource* tail = *(fNonpurgeableResources.end() - 1); |
| 545 | SkASSERT(fNonpurgeableResources[*index] == resource); |
| 546 | fNonpurgeableResources[*index] = tail; |
| 547 | *tail->cacheAccess().accessCacheIndex() = *index; |
| 548 | fNonpurgeableResources.pop(); |
| 549 | SkDEBUGCODE(*index = -1); |
| 550 | } |
| 551 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 552 | uint32_t GrResourceCache::getNextTimestamp() { |
| 553 | // If we wrap then all the existing resources will appear older than any resources that get |
| 554 | // a timestamp after the wrap. |
| 555 | if (0 == fTimestamp) { |
| 556 | int count = this->getResourceCount(); |
| 557 | if (count) { |
| 558 | // Reset all the timestamps. We sort the resources by timestamp and then assign |
| 559 | // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely |
| 560 | // rare. |
| 561 | SkTDArray<GrGpuResource*> sortedPurgeableResources; |
| 562 | sortedPurgeableResources.setReserve(fPurgeableQueue.count()); |
| 563 | |
| 564 | while (fPurgeableQueue.count()) { |
| 565 | *sortedPurgeableResources.append() = fPurgeableQueue.peek(); |
| 566 | fPurgeableQueue.pop(); |
| 567 | } |
| 568 | |
| 569 | struct Less { |
| 570 | bool operator()(GrGpuResource* a, GrGpuResource* b) { |
| 571 | return CompareTimestamp(a,b); |
| 572 | } |
| 573 | }; |
| 574 | Less less; |
| 575 | SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1, less); |
| 576 | |
| 577 | // Pick resources out of the purgeable and non-purgeable arrays based on lowest |
| 578 | // timestamp and assign new timestamps. |
| 579 | int currP = 0; |
| 580 | int currNP = 0; |
| 581 | while (currP < sortedPurgeableResources.count() && |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 582 | currNP < fNonpurgeableResources.count()) { |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 583 | uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp(); |
| 584 | uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp(); |
| 585 | SkASSERT(tsP != tsNP); |
| 586 | if (tsP < tsNP) { |
| 587 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 588 | } else { |
| 589 | // Correct the index in the nonpurgeable array stored on the resource post-sort. |
| 590 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 591 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // The above loop ended when we hit the end of one array. Finish the other one. |
| 596 | while (currP < sortedPurgeableResources.count()) { |
| 597 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 598 | } |
| 599 | while (currNP < fNonpurgeableResources.count()) { |
| 600 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 601 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 602 | } |
| 603 | |
| 604 | // Rebuild the queue. |
| 605 | for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 606 | fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 607 | } |
| 608 | |
| 609 | this->validate(); |
| 610 | SkASSERT(count == this->getResourceCount()); |
| 611 | |
| 612 | // count should be the next timestamp we return. |
| 613 | SkASSERT(fTimestamp == SkToU32(count)); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 614 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 615 | // The historical timestamps of flushes are now invalid. |
| 616 | this->resetFlushTimestamps(); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 617 | } |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 618 | } |
| 619 | return fTimestamp++; |
| 620 | } |
| 621 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 622 | void GrResourceCache::notifyFlushOccurred() { |
| 623 | if (fFlushTimestamps) { |
| 624 | SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 625 | fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1); |
| 626 | // get the timestamp before accessing fFlushTimestamps because getNextTimestamp will |
| 627 | // reallocate fFlushTimestamps on timestamp overflow. |
| 628 | uint32_t timestamp = this->getNextTimestamp(); |
| 629 | fFlushTimestamps[fLastFlushTimestampIndex] = timestamp; |
| 630 | this->purgeAsNeeded(); |
| 631 | } |
| 632 | } |
| 633 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 634 | void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 635 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 636 | fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump); |
| 637 | } |
| 638 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 639 | fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump); |
| 640 | } |
| 641 | } |
| 642 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 643 | #ifdef SK_DEBUG |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 644 | void GrResourceCache::validate() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 645 | // Reduce the frequency of validations for large resource counts. |
| 646 | static SkRandom gRandom; |
| 647 | int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 648 | if (~mask && (gRandom.nextU() & mask)) { |
| 649 | return; |
| 650 | } |
| 651 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 652 | struct Stats { |
| 653 | size_t fBytes; |
| 654 | int fBudgetedCount; |
| 655 | size_t fBudgetedBytes; |
| 656 | int fLocked; |
| 657 | int fScratch; |
| 658 | int fCouldBeScratch; |
| 659 | int fContent; |
| 660 | const ScratchMap* fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 661 | const UniqueHash* fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 662 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 663 | Stats(const GrResourceCache* cache) { |
| 664 | memset(this, 0, sizeof(*this)); |
| 665 | fScratchMap = &cache->fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 666 | fUniqueHash = &cache->fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 667 | } |
| 668 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 669 | void update(GrGpuResource* resource) { |
| 670 | fBytes += resource->gpuMemorySize(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 671 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 672 | if (!resource->isPurgeable()) { |
| 673 | ++fLocked; |
| 674 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 675 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 676 | const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey(); |
| 677 | const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
| 678 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 679 | if (resource->cacheAccess().isScratch()) { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 680 | SkASSERT(!uniqueKey.isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 681 | ++fScratch; |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 682 | SkASSERT(fScratchMap->countForKey(scratchKey)); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 683 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 684 | } else if (scratchKey.isValid()) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 685 | SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() || |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 686 | uniqueKey.isValid()); |
| 687 | if (!uniqueKey.isValid()) { |
| 688 | ++fCouldBeScratch; |
| 689 | SkASSERT(fScratchMap->countForKey(scratchKey)); |
| 690 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 691 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 692 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 693 | if (uniqueKey.isValid()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 694 | ++fContent; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 695 | SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 696 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 697 | SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted()); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 698 | |
| 699 | if (scratchKey.isValid()) { |
| 700 | SkASSERT(!fScratchMap->has(resource, scratchKey)); |
| 701 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 702 | } |
| 703 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 704 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 705 | ++fBudgetedCount; |
| 706 | fBudgetedBytes += resource->gpuMemorySize(); |
| 707 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 708 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 709 | }; |
| 710 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame^] | 711 | { |
| 712 | ScratchMap::ConstIter iter(&fScratchMap); |
| 713 | |
| 714 | int count = 0; |
| 715 | for ( ; !iter.done(); ++iter) { |
| 716 | const GrGpuResource* resource = *iter; |
| 717 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
| 718 | SkASSERT(!resource->getUniqueKey().isValid()); |
| 719 | count++; |
| 720 | } |
| 721 | SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly |
| 722 | } |
| 723 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 724 | Stats stats(this); |
| 725 | |
| 726 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 727 | SkASSERT(!fNonpurgeableResources[i]->isPurgeable() || |
| 728 | fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 729 | SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i); |
| 730 | SkASSERT(!fNonpurgeableResources[i]->wasDestroyed()); |
| 731 | stats.update(fNonpurgeableResources[i]); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 732 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 733 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 734 | SkASSERT(fPurgeableQueue.at(i)->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 735 | SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i); |
| 736 | SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed()); |
| 737 | stats.update(fPurgeableQueue.at(i)); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 738 | } |
| 739 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 740 | SkASSERT(fCount == this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 741 | SkASSERT(fBudgetedCount <= fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 742 | SkASSERT(fBudgetedBytes <= fBytes); |
| 743 | SkASSERT(stats.fBytes == fBytes); |
| 744 | SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
| 745 | SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 746 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 747 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 748 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 749 | SkASSERT(fBytes <= fHighWaterBytes); |
| 750 | SkASSERT(fCount <= fHighWaterCount); |
| 751 | SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
| 752 | SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 753 | #endif |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 754 | SkASSERT(stats.fContent == fUniqueHash.count()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 755 | SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 756 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 757 | // This assertion is not currently valid because we can be in recursive notifyCntReachedZero() |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 758 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 759 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 760 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 761 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 762 | |
| 763 | bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
| 764 | int index = *resource->cacheAccess().accessCacheIndex(); |
| 765 | if (index < 0) { |
| 766 | return false; |
| 767 | } |
| 768 | if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) { |
| 769 | return true; |
| 770 | } |
| 771 | if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { |
| 772 | return true; |
| 773 | } |
| 774 | SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache."); |
| 775 | return false; |
| 776 | } |
| 777 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 778 | #endif |