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