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