bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 10 | #include "GrResourceCache.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 11 | #include "GrGpuResourceCacheAccess.h" |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame^] | 12 | #include "GrTracing.h" |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 13 | #include "SkChecksum.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 14 | #include "SkGr.h" |
| 15 | #include "SkMessageBus.h" |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 16 | #include "SkTSort.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 17 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 18 | DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 19 | |
| 20 | ////////////////////////////////////////////////////////////////////////////// |
| 21 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 22 | GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 23 | static int32_t gType = INHERITED::kInvalidDomain + 1; |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 24 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 25 | int32_t type = sk_atomic_inc(&gType); |
robertphillips | 9790a7b | 2015-01-05 12:29:15 -0800 | [diff] [blame] | 26 | if (type > SK_MaxU16) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 27 | SkFAIL("Too many Resource Types"); |
| 28 | } |
| 29 | |
| 30 | return static_cast<ResourceType>(type); |
| 31 | } |
| 32 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 33 | GrUniqueKey::Domain GrUniqueKey::GenerateDomain() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 34 | static int32_t gDomain = INHERITED::kInvalidDomain + 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 35 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 36 | int32_t domain = sk_atomic_inc(&gDomain); |
kkinnunen | 016dffb | 2015-01-23 06:43:05 -0800 | [diff] [blame] | 37 | if (domain > SK_MaxU16) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 38 | SkFAIL("Too many GrUniqueKey Domains"); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 39 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 40 | |
| 41 | return static_cast<Domain>(domain); |
| 42 | } |
| 43 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { |
| 44 | return SkChecksum::Compute(data, size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 47 | ////////////////////////////////////////////////////////////////////////////// |
| 48 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 49 | class GrResourceCache::AutoValidate : ::SkNoncopyable { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 50 | public: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 51 | AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 52 | ~AutoValidate() { fCache->validate(); } |
| 53 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 54 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | ////////////////////////////////////////////////////////////////////////////// |
| 58 | |
| 59 | static const int kDefaultMaxCount = 2 * (1 << 10); |
| 60 | static const size_t kDefaultMaxSize = 96 * (1 << 20); |
| 61 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 62 | GrResourceCache::GrResourceCache() |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 63 | : fTimestamp(0) |
| 64 | , fMaxCount(kDefaultMaxCount) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 65 | , fMaxBytes(kDefaultMaxSize) |
| 66 | #if GR_CACHE_STATS |
| 67 | , fHighWaterCount(0) |
| 68 | , fHighWaterBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 69 | , fBudgetedHighWaterCount(0) |
| 70 | , fBudgetedHighWaterBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 71 | #endif |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 72 | , fBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 73 | , fBudgetedCount(0) |
| 74 | , fBudgetedBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 75 | , fOverBudgetCB(NULL) |
| 76 | , fOverBudgetData(NULL) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 77 | SkDEBUGCODE(fCount = 0;) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 80 | GrResourceCache::~GrResourceCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 81 | this->releaseAll(); |
| 82 | } |
| 83 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 84 | void GrResourceCache::setLimits(int count, size_t bytes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 85 | fMaxCount = count; |
| 86 | fMaxBytes = bytes; |
| 87 | this->purgeAsNeeded(); |
| 88 | } |
| 89 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 90 | void GrResourceCache::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 91 | SkASSERT(resource); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 92 | SkASSERT(!this->isInCache(resource)); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 93 | SkASSERT(!resource->wasDestroyed()); |
| 94 | SkASSERT(!resource->isPurgeable()); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 95 | |
| 96 | // We must set the timestamp before adding to the array in case the timestamp wraps and we wind |
| 97 | // up iterating over all the resources that already have timestamps. |
| 98 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
| 99 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 100 | this->addToNonpurgeableArray(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 101 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 102 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 103 | SkDEBUGCODE(++fCount;) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 104 | fBytes += size; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 105 | #if GR_CACHE_STATS |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 106 | fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount); |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 107 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 108 | #endif |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 109 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 110 | ++fBudgetedCount; |
| 111 | fBudgetedBytes += size; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame^] | 112 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 113 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 114 | #if GR_CACHE_STATS |
| 115 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 116 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 117 | #endif |
| 118 | } |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 119 | if (resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 120 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 121 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 122 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 123 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 124 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 125 | } |
| 126 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 127 | void GrResourceCache::removeResource(GrGpuResource* resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 128 | this->validate(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 129 | SkASSERT(this->isInCache(resource)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 130 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 131 | if (resource->isPurgeable()) { |
| 132 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 133 | } else { |
| 134 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 135 | } |
| 136 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 137 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 138 | SkDEBUGCODE(--fCount;) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 139 | fBytes -= size; |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 140 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 141 | --fBudgetedCount; |
| 142 | fBudgetedBytes -= size; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame^] | 143 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 144 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 145 | } |
| 146 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 147 | if (resource->resourcePriv().getScratchKey().isValid()) { |
| 148 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 149 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 150 | if (resource->getUniqueKey().isValid()) { |
| 151 | fUniqueHash.remove(resource->getUniqueKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 152 | } |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 153 | this->validate(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 154 | } |
| 155 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 156 | void GrResourceCache::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 157 | AutoValidate av(this); |
| 158 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 159 | while (fNonpurgeableResources.count()) { |
| 160 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 161 | SkASSERT(!back->wasDestroyed()); |
| 162 | back->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 163 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 164 | |
| 165 | while (fPurgeableQueue.count()) { |
| 166 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 167 | SkASSERT(!top->wasDestroyed()); |
| 168 | top->cacheAccess().abandon(); |
| 169 | } |
| 170 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 171 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 172 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 173 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 174 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 175 | SkASSERT(!fBytes); |
| 176 | SkASSERT(!fBudgetedCount); |
| 177 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 178 | } |
| 179 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 180 | void GrResourceCache::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 181 | AutoValidate av(this); |
| 182 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 183 | while(fNonpurgeableResources.count()) { |
| 184 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 185 | SkASSERT(!back->wasDestroyed()); |
| 186 | back->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 187 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 188 | |
| 189 | while (fPurgeableQueue.count()) { |
| 190 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 191 | SkASSERT(!top->wasDestroyed()); |
| 192 | top->cacheAccess().release(); |
| 193 | } |
| 194 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 195 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 196 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 197 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 198 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 199 | SkASSERT(!fBytes); |
| 200 | SkASSERT(!fBudgetedCount); |
| 201 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 202 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 203 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 204 | class GrResourceCache::AvailableForScratchUse { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 205 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 206 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 207 | |
| 208 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 209 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 210 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 211 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 212 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 213 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 214 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 215 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 216 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 219 | GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey, |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 220 | uint32_t flags) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 221 | SkASSERT(scratchKey.isValid()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 222 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 223 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 224 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 225 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 226 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 227 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 228 | this->validate(); |
| 229 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 230 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 231 | return NULL; |
| 232 | } |
| 233 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 234 | // but there is still space in our budget for the resource. |
| 235 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 236 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 237 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 238 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 239 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 240 | } |
| 241 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 242 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 243 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 244 | void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 245 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
| 246 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 247 | } |
| 248 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 249 | void GrResourceCache::removeUniqueKey(GrGpuResource* resource) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 250 | // Someone has a ref to this resource in order to invalidate it. When the ref count reaches |
| 251 | // zero we will get a notifyPurgable() and figure out what to do with it. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 252 | if (resource->getUniqueKey().isValid()) { |
| 253 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 254 | fUniqueHash.remove(resource->getUniqueKey()); |
| 255 | } |
| 256 | resource->cacheAccess().removeUniqueKey(); |
| 257 | this->validate(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 258 | } |
| 259 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 260 | void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 261 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 262 | SkASSERT(this->isInCache(resource)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 263 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 264 | // Remove the entry for this resource if it already has a unique key. |
| 265 | if (resource->getUniqueKey().isValid()) { |
| 266 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 267 | fUniqueHash.remove(resource->getUniqueKey()); |
| 268 | SkASSERT(NULL == fUniqueHash.find(resource->getUniqueKey())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 269 | } |
| 270 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 271 | // If another resource has the new key, remove its key then install the key on this resource. |
| 272 | if (newKey.isValid()) { |
| 273 | if (GrGpuResource* old = fUniqueHash.find(newKey)) { |
| 274 | // If the old resource using the key is purgeable and is unreachable, then remove it. |
| 275 | if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) { |
| 276 | // release may call validate() which will assert that resource is in fUniqueHash |
| 277 | // if it has a valid key. So in debug reset the key here before we assign it. |
| 278 | SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();) |
| 279 | old->cacheAccess().release(); |
| 280 | } else { |
| 281 | fUniqueHash.remove(newKey); |
| 282 | old->cacheAccess().removeUniqueKey(); |
| 283 | } |
| 284 | } |
| 285 | SkASSERT(NULL == fUniqueHash.find(newKey)); |
| 286 | resource->cacheAccess().setUniqueKey(newKey); |
| 287 | fUniqueHash.add(resource); |
| 288 | } else { |
| 289 | resource->cacheAccess().removeUniqueKey(); |
| 290 | } |
| 291 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 292 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 293 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 294 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 295 | void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 296 | SkASSERT(resource); |
| 297 | SkASSERT(this->isInCache(resource)); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 298 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 299 | if (resource->isPurgeable()) { |
| 300 | // It's about to become unpurgeable. |
| 301 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 302 | this->addToNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 303 | } |
| 304 | resource->ref(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 305 | |
| 306 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 307 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 308 | } |
| 309 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 310 | void GrResourceCache::notifyPurgeable(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 311 | SkASSERT(resource); |
| 312 | SkASSERT(this->isInCache(resource)); |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 313 | SkASSERT(resource->isPurgeable()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 314 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 315 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 316 | fPurgeableQueue.insert(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 317 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 318 | if (!resource->resourcePriv().isBudgeted()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 319 | // Check whether this resource could still be used as a scratch resource. |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 320 | if (!resource->cacheAccess().isWrapped() && |
| 321 | resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 322 | // We won't purge an existing resource to make room for this one. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 323 | if (fBudgetedCount < fMaxCount && |
| 324 | fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 325 | resource->resourcePriv().makeBudgeted(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 326 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 327 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 328 | } |
| 329 | } else { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 330 | // Purge the resource immediately if we're over budget |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 331 | // Also purge if the resource has neither a valid scratch key nor a unique key. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 332 | bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 333 | !resource->getUniqueKey().isValid(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 334 | if (!this->overBudget() && !noKey) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 335 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 338 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 339 | SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 340 | resource->cacheAccess().release(); |
| 341 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 342 | SkASSERT(this->getResourceCount() < beforeCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 343 | this->validate(); |
| 344 | } |
| 345 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 346 | void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 347 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 348 | SkASSERT(resource); |
| 349 | SkASSERT(this->isInCache(resource)); |
| 350 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 351 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 352 | |
| 353 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 354 | #if GR_CACHE_STATS |
| 355 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 356 | #endif |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 357 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 358 | fBudgetedBytes += delta; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame^] | 359 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 360 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 361 | #if GR_CACHE_STATS |
| 362 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 363 | #endif |
| 364 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 365 | |
| 366 | this->purgeAsNeeded(); |
| 367 | this->validate(); |
| 368 | } |
| 369 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 370 | void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 371 | SkASSERT(resource); |
| 372 | SkASSERT(this->isInCache(resource)); |
| 373 | |
| 374 | size_t size = resource->gpuMemorySize(); |
| 375 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 376 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 377 | ++fBudgetedCount; |
| 378 | fBudgetedBytes += size; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 379 | #if GR_CACHE_STATS |
| 380 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 381 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 382 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 383 | this->purgeAsNeeded(); |
| 384 | } else { |
| 385 | --fBudgetedCount; |
| 386 | fBudgetedBytes -= size; |
| 387 | } |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame^] | 388 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 389 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 390 | |
| 391 | this->validate(); |
| 392 | } |
| 393 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 394 | void GrResourceCache::internalPurgeAsNeeded() { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 395 | SkASSERT(this->overBudget()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 396 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 397 | bool stillOverbudget = true; |
| 398 | while (fPurgeableQueue.count()) { |
| 399 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 400 | SkASSERT(resource->isPurgeable()); |
| 401 | resource->cacheAccess().release(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 402 | if (!this->overBudget()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 403 | stillOverbudget = false; |
| 404 | break; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 405 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 406 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 407 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 408 | this->validate(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 409 | |
| 410 | if (stillOverbudget) { |
| 411 | // Despite the purge we're still over budget. Call our over budget callback. If this frees |
| 412 | // any resources then we'll get notifyPurgeable() calls and take appropriate action. |
| 413 | (*fOverBudgetCB)(fOverBudgetData); |
| 414 | this->validate(); |
| 415 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 416 | } |
| 417 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 418 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 419 | // We could disable maintaining the heap property here, but it would add a lot of complexity. |
| 420 | // Moreover, this is rarely called. |
| 421 | while (fPurgeableQueue.count()) { |
| 422 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 423 | SkASSERT(resource->isPurgeable()); |
| 424 | resource->cacheAccess().release(); |
| 425 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 426 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 427 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 428 | } |
| 429 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 430 | void GrResourceCache::processInvalidUniqueKeys( |
| 431 | const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 432 | for (int i = 0; i < msgs.count(); ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 433 | GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 434 | if (resource) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 435 | resource->resourcePriv().removeUniqueKey(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 436 | resource->unref(); // will call notifyPurgeable, if it is indeed now purgeable. |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 441 | void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
| 442 | int index = fNonpurgeableResources.count(); |
| 443 | *fNonpurgeableResources.append() = resource; |
| 444 | *resource->cacheAccess().accessCacheIndex() = index; |
| 445 | } |
| 446 | |
| 447 | void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) { |
| 448 | int* index = resource->cacheAccess().accessCacheIndex(); |
| 449 | // Fill the whole we will create in the array with the tail object, adjust its index, and |
| 450 | // then pop the array |
| 451 | GrGpuResource* tail = *(fNonpurgeableResources.end() - 1); |
| 452 | SkASSERT(fNonpurgeableResources[*index] == resource); |
| 453 | fNonpurgeableResources[*index] = tail; |
| 454 | *tail->cacheAccess().accessCacheIndex() = *index; |
| 455 | fNonpurgeableResources.pop(); |
| 456 | SkDEBUGCODE(*index = -1); |
| 457 | } |
| 458 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 459 | uint32_t GrResourceCache::getNextTimestamp() { |
| 460 | // If we wrap then all the existing resources will appear older than any resources that get |
| 461 | // a timestamp after the wrap. |
| 462 | if (0 == fTimestamp) { |
| 463 | int count = this->getResourceCount(); |
| 464 | if (count) { |
| 465 | // Reset all the timestamps. We sort the resources by timestamp and then assign |
| 466 | // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely |
| 467 | // rare. |
| 468 | SkTDArray<GrGpuResource*> sortedPurgeableResources; |
| 469 | sortedPurgeableResources.setReserve(fPurgeableQueue.count()); |
| 470 | |
| 471 | while (fPurgeableQueue.count()) { |
| 472 | *sortedPurgeableResources.append() = fPurgeableQueue.peek(); |
| 473 | fPurgeableQueue.pop(); |
| 474 | } |
| 475 | |
| 476 | struct Less { |
| 477 | bool operator()(GrGpuResource* a, GrGpuResource* b) { |
| 478 | return CompareTimestamp(a,b); |
| 479 | } |
| 480 | }; |
| 481 | Less less; |
| 482 | SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1, less); |
| 483 | |
| 484 | // Pick resources out of the purgeable and non-purgeable arrays based on lowest |
| 485 | // timestamp and assign new timestamps. |
| 486 | int currP = 0; |
| 487 | int currNP = 0; |
| 488 | while (currP < sortedPurgeableResources.count() && |
| 489 | currNP < fNonpurgeableResources.count()) { |
| 490 | uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp(); |
| 491 | uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp(); |
| 492 | SkASSERT(tsP != tsNP); |
| 493 | if (tsP < tsNP) { |
| 494 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 495 | } else { |
| 496 | // Correct the index in the nonpurgeable array stored on the resource post-sort. |
| 497 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 498 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // The above loop ended when we hit the end of one array. Finish the other one. |
| 503 | while (currP < sortedPurgeableResources.count()) { |
| 504 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 505 | } |
| 506 | while (currNP < fNonpurgeableResources.count()) { |
| 507 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 508 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 509 | } |
| 510 | |
| 511 | // Rebuild the queue. |
| 512 | for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 513 | fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 514 | } |
| 515 | |
| 516 | this->validate(); |
| 517 | SkASSERT(count == this->getResourceCount()); |
| 518 | |
| 519 | // count should be the next timestamp we return. |
| 520 | SkASSERT(fTimestamp == SkToU32(count)); |
| 521 | } |
| 522 | } |
| 523 | return fTimestamp++; |
| 524 | } |
| 525 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 526 | #ifdef SK_DEBUG |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 527 | void GrResourceCache::validate() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 528 | // Reduce the frequency of validations for large resource counts. |
| 529 | static SkRandom gRandom; |
| 530 | int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 531 | if (~mask && (gRandom.nextU() & mask)) { |
| 532 | return; |
| 533 | } |
| 534 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 535 | struct Stats { |
| 536 | size_t fBytes; |
| 537 | int fBudgetedCount; |
| 538 | size_t fBudgetedBytes; |
| 539 | int fLocked; |
| 540 | int fScratch; |
| 541 | int fCouldBeScratch; |
| 542 | int fContent; |
| 543 | const ScratchMap* fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 544 | const UniqueHash* fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 545 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 546 | Stats(const GrResourceCache* cache) { |
| 547 | memset(this, 0, sizeof(*this)); |
| 548 | fScratchMap = &cache->fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 549 | fUniqueHash = &cache->fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 550 | } |
| 551 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 552 | void update(GrGpuResource* resource) { |
| 553 | fBytes += resource->gpuMemorySize(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 554 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 555 | if (!resource->isPurgeable()) { |
| 556 | ++fLocked; |
| 557 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 558 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 559 | if (resource->cacheAccess().isScratch()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 560 | SkASSERT(!resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 561 | ++fScratch; |
| 562 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
| 563 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 564 | } else if (resource->resourcePriv().getScratchKey().isValid()) { |
| 565 | SkASSERT(!resource->resourcePriv().isBudgeted() || |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 566 | resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 567 | ++fCouldBeScratch; |
| 568 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
| 569 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 570 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 571 | const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
| 572 | if (uniqueKey.isValid()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 573 | ++fContent; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 574 | SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 575 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 576 | SkASSERT(resource->resourcePriv().isBudgeted()); |
| 577 | } |
| 578 | |
| 579 | if (resource->resourcePriv().isBudgeted()) { |
| 580 | ++fBudgetedCount; |
| 581 | fBudgetedBytes += resource->gpuMemorySize(); |
| 582 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 583 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | Stats stats(this); |
| 587 | |
| 588 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 589 | SkASSERT(!fNonpurgeableResources[i]->isPurgeable()); |
| 590 | SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i); |
| 591 | SkASSERT(!fNonpurgeableResources[i]->wasDestroyed()); |
| 592 | stats.update(fNonpurgeableResources[i]); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 593 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 594 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 595 | SkASSERT(fPurgeableQueue.at(i)->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 596 | SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i); |
| 597 | SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed()); |
| 598 | stats.update(fPurgeableQueue.at(i)); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 599 | } |
| 600 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 601 | SkASSERT(fCount == this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 602 | SkASSERT(fBudgetedCount <= fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 603 | SkASSERT(fBudgetedBytes <= fBytes); |
| 604 | SkASSERT(stats.fBytes == fBytes); |
| 605 | SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
| 606 | SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 607 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 608 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 609 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 610 | SkASSERT(fBytes <= fHighWaterBytes); |
| 611 | SkASSERT(fCount <= fHighWaterCount); |
| 612 | SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
| 613 | SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 614 | #endif |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 615 | SkASSERT(stats.fContent == fUniqueHash.count()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 616 | SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 617 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 618 | // This assertion is not currently valid because we can be in recursive notifyIsPurgeable() |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 619 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 620 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 621 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 622 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 623 | |
| 624 | bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
| 625 | int index = *resource->cacheAccess().accessCacheIndex(); |
| 626 | if (index < 0) { |
| 627 | return false; |
| 628 | } |
| 629 | if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) { |
| 630 | return true; |
| 631 | } |
| 632 | if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { |
| 633 | return true; |
| 634 | } |
| 635 | SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache."); |
| 636 | return false; |
| 637 | } |
| 638 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 639 | #endif |