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