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