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