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