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 | |
| 10 | #include "GrResourceCache2.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 11 | #include "GrGpuResource.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 12 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 13 | #include "SkGr.h" |
| 14 | #include "SkMessageBus.h" |
| 15 | |
| 16 | DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); |
| 17 | |
| 18 | ////////////////////////////////////////////////////////////////////////////// |
| 19 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 20 | GrResourceKey& GrResourceKey::NullScratchKey() { |
| 21 | static const GrCacheID::Key kBogusKey = { { {0} } }; |
| 22 | static GrCacheID kBogusID(ScratchDomain(), kBogusKey); |
| 23 | static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0); |
| 24 | return kNullScratchKey; |
| 25 | } |
| 26 | |
| 27 | GrResourceKey::ResourceType GrResourceKey::NoneResourceType() { |
| 28 | static const ResourceType gNoneResourceType = GenerateResourceType(); |
| 29 | return gNoneResourceType; |
| 30 | } |
| 31 | |
| 32 | GrCacheID::Domain GrResourceKey::ScratchDomain() { |
| 33 | static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
| 34 | return gDomain; |
| 35 | } |
| 36 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 37 | GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() { |
| 38 | static int32_t gNextType = 0; |
| 39 | |
| 40 | int32_t type = sk_atomic_inc(&gNextType); |
| 41 | if (type >= (1 << 8 * sizeof(ResourceType))) { |
| 42 | SkFAIL("Too many Resource Types"); |
| 43 | } |
| 44 | |
| 45 | return static_cast<ResourceType>(type); |
| 46 | } |
| 47 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 48 | ////////////////////////////////////////////////////////////////////////////// |
| 49 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 50 | class GrResourceCache2::AutoValidate : ::SkNoncopyable { |
| 51 | public: |
| 52 | AutoValidate(GrResourceCache2* cache) : fCache(cache) { cache->validate(); } |
| 53 | ~AutoValidate() { fCache->validate(); } |
| 54 | private: |
| 55 | GrResourceCache2* fCache; |
| 56 | }; |
| 57 | |
| 58 | ////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | static const int kDefaultMaxCount = 2 * (1 << 10); |
| 61 | static const size_t kDefaultMaxSize = 96 * (1 << 20); |
| 62 | |
| 63 | GrResourceCache2::GrResourceCache2() |
| 64 | : fMaxCount(kDefaultMaxCount) |
| 65 | , fMaxBytes(kDefaultMaxSize) |
| 66 | #if GR_CACHE_STATS |
| 67 | , fHighWaterCount(0) |
| 68 | , fHighWaterBytes(0) |
| 69 | #endif |
| 70 | , fCount(0) |
| 71 | , fBytes(0) |
| 72 | , fPurging(false) |
| 73 | , fNewlyPurgableResourceWhilePurging(false) |
| 74 | , fOverBudgetCB(NULL) |
| 75 | , fOverBudgetData(NULL) { |
| 76 | } |
| 77 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 78 | GrResourceCache2::~GrResourceCache2() { |
| 79 | this->releaseAll(); |
| 80 | } |
| 81 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 82 | void GrResourceCache2::setLimits(int count, size_t bytes) { |
| 83 | fMaxCount = count; |
| 84 | fMaxBytes = bytes; |
| 85 | this->purgeAsNeeded(); |
| 86 | } |
| 87 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 88 | void GrResourceCache2::insertResource(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 89 | AutoValidate av(this); |
| 90 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 91 | SkASSERT(resource); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 92 | SkASSERT(!resource->wasDestroyed()); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 93 | SkASSERT(!this->isInCache(resource)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 94 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 95 | fResources.addToHead(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 96 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 97 | ++fCount; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 98 | fBytes += resource->gpuMemorySize(); |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame^] | 99 | #if GR_CACHE_STATS |
| 100 | fHighWaterCount = SkTMax(fCount, fHighWaterCount); |
| 101 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 102 | #endif |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 103 | if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 104 | // TODO(bsalomon): Make this assertion possible. |
| 105 | // SkASSERT(!resource->isWrapped()); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 106 | fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 107 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 108 | |
| 109 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 113 | AutoValidate av(this); |
| 114 | |
| 115 | --fCount; |
| 116 | fBytes -= resource->gpuMemorySize(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 117 | SkASSERT(this->isInCache(resource)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 118 | fResources.remove(resource); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 119 | if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 120 | fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 121 | } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 122 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 123 | fContentHash.remove(*contentKey); |
| 124 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void GrResourceCache2::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 128 | AutoValidate av(this); |
| 129 | |
| 130 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 131 | while (GrGpuResource* head = fResources.head()) { |
| 132 | SkASSERT(!head->wasDestroyed()); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 133 | head->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 134 | // abandon should have already removed this from the list. |
| 135 | SkASSERT(head != fResources.head()); |
| 136 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 137 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 138 | SkASSERT(!fContentHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 139 | SkASSERT(!fCount); |
| 140 | } |
| 141 | |
| 142 | void GrResourceCache2::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 143 | AutoValidate av(this); |
| 144 | |
| 145 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 146 | while (GrGpuResource* head = fResources.head()) { |
| 147 | SkASSERT(!head->wasDestroyed()); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 148 | head->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 149 | // release should have already removed this from the list. |
| 150 | SkASSERT(head != fResources.head()); |
| 151 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 152 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 153 | SkASSERT(!fCount); |
| 154 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 155 | |
| 156 | class GrResourceCache2::AvailableForScratchUse { |
| 157 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 158 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 159 | |
| 160 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 161 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 162 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 163 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 164 | |
| 165 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 166 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 167 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 168 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 169 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 173 | uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 174 | AutoValidate av(this); |
| 175 | |
| 176 | SkASSERT(!fPurging); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 177 | SkASSERT(scratchKey.isScratch()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 178 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 179 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 180 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 181 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 182 | if (resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 183 | this->makeResourceMRU(resource); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 184 | return SkRef(resource); |
| 185 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 186 | return NULL; |
| 187 | } |
| 188 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 189 | // but there is still space in our budget for the resource. |
| 190 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 191 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 192 | if (resource) { |
| 193 | resource->ref(); |
| 194 | this->makeResourceMRU(resource); |
| 195 | } |
| 196 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 197 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 198 | |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 199 | bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 200 | SkASSERT(!fPurging); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 201 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 202 | SkASSERT(this->isInCache(resource)); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 203 | SkASSERT(resource->cacheAccess().getContentKey()); |
| 204 | SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 205 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 206 | GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 207 | if (NULL != res) { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | fContentHash.add(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 212 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 213 | return true; |
| 214 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 215 | |
| 216 | void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) { |
| 217 | AutoValidate av(this); |
| 218 | |
| 219 | SkASSERT(!fPurging); |
| 220 | SkASSERT(resource); |
| 221 | SkASSERT(this->isInCache(resource)); |
| 222 | fResources.remove(resource); |
| 223 | fResources.addToHead(resource); |
| 224 | } |
| 225 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 226 | void GrResourceCache2::notifyPurgable(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 227 | SkASSERT(resource); |
| 228 | SkASSERT(this->isInCache(resource)); |
| 229 | SkASSERT(resource->isPurgable()); |
| 230 | |
| 231 | // We can't purge if in the middle of purging because purge is iterating. Instead record |
| 232 | // that additional resources became purgable. |
| 233 | if (fPurging) { |
| 234 | fNewlyPurgableResourceWhilePurging = true; |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Purge the resource if we're over budget |
| 239 | bool overBudget = fCount > fMaxCount || fBytes > fMaxBytes; |
| 240 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 241 | // Also purge if the resource has neither a valid scratch key nor a content key. |
| 242 | bool noKey = !resource->cacheAccess().isScratch() && |
| 243 | (NULL == resource->cacheAccess().getContentKey()); |
| 244 | |
| 245 | if (overBudget || noKey) { |
| 246 | SkDEBUGCODE(int beforeCount = fCount;) |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 247 | resource->cacheAccess().release(); |
| 248 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 249 | SkASSERT(fCount < beforeCount); |
| 250 | } |
| 251 | |
| 252 | this->validate(); |
| 253 | } |
| 254 | |
| 255 | void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 256 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 257 | SkASSERT(resource); |
| 258 | SkASSERT(this->isInCache(resource)); |
| 259 | |
| 260 | fBytes += resource->gpuMemorySize() - oldSize; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame^] | 261 | #if GR_CACHE_STATS |
| 262 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 263 | #endif |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 264 | |
| 265 | this->purgeAsNeeded(); |
| 266 | this->validate(); |
| 267 | } |
| 268 | |
| 269 | void GrResourceCache2::internalPurgeAsNeeded() { |
| 270 | SkASSERT(!fPurging); |
| 271 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 272 | SkASSERT(fCount > fMaxCount || fBytes > fMaxBytes); |
| 273 | |
| 274 | fPurging = true; |
| 275 | |
| 276 | AutoValidate av(this); // Put this after setting fPurging so we're allowed to be over budget. |
| 277 | |
| 278 | bool overBudget = true; |
| 279 | do { |
| 280 | fNewlyPurgableResourceWhilePurging = false; |
| 281 | ResourceList::Iter resourceIter; |
| 282 | GrGpuResource* resource = resourceIter.init(fResources, |
| 283 | ResourceList::Iter::kTail_IterStart); |
| 284 | |
| 285 | while (resource) { |
| 286 | GrGpuResource* prev = resourceIter.prev(); |
| 287 | if (resource->isPurgable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 288 | resource->cacheAccess().release(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 289 | } |
| 290 | resource = prev; |
| 291 | if (fCount <= fMaxCount && fBytes <= fMaxBytes) { |
| 292 | overBudget = false; |
| 293 | resource = NULL; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) { |
| 298 | // Despite the purge we're still over budget. Call our over budget callback. |
| 299 | (*fOverBudgetCB)(fOverBudgetData); |
| 300 | } |
| 301 | } while (overBudget && fNewlyPurgableResourceWhilePurging); |
| 302 | |
| 303 | fNewlyPurgableResourceWhilePurging = false; |
| 304 | fPurging = false; |
| 305 | } |
| 306 | |
| 307 | void GrResourceCache2::purgeAllUnlocked() { |
| 308 | SkASSERT(!fPurging); |
| 309 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 310 | |
| 311 | fPurging = true; |
| 312 | |
| 313 | AutoValidate av(this); // Put this after setting fPurging so we're allowed to be over budget. |
| 314 | |
| 315 | do { |
| 316 | fNewlyPurgableResourceWhilePurging = false; |
| 317 | ResourceList::Iter resourceIter; |
| 318 | GrGpuResource* resource = |
| 319 | resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart); |
| 320 | |
| 321 | while (resource) { |
| 322 | GrGpuResource* prev = resourceIter.prev(); |
| 323 | if (resource->isPurgable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 324 | resource->cacheAccess().release(); |
| 325 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 326 | resource = prev; |
| 327 | } |
| 328 | |
| 329 | if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) { |
| 330 | (*fOverBudgetCB)(fOverBudgetData); |
| 331 | } |
| 332 | } while (fNewlyPurgableResourceWhilePurging); |
| 333 | fPurging = false; |
| 334 | } |
| 335 | |
| 336 | #ifdef SK_DEBUG |
| 337 | void GrResourceCache2::validate() const { |
| 338 | size_t bytes = 0; |
| 339 | int count = 0; |
| 340 | int locked = 0; |
| 341 | int scratch = 0; |
| 342 | int couldBeScratch = 0; |
| 343 | int content = 0; |
| 344 | |
| 345 | ResourceList::Iter iter; |
| 346 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 347 | for ( ; resource; resource = iter.next()) { |
| 348 | bytes += resource->gpuMemorySize(); |
| 349 | ++count; |
| 350 | |
| 351 | if (!resource->isPurgable()) { |
| 352 | ++locked; |
| 353 | } |
| 354 | |
| 355 | if (resource->cacheAccess().isScratch()) { |
| 356 | SkASSERT(NULL == resource->cacheAccess().getContentKey()); |
| 357 | ++scratch; |
| 358 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
| 359 | } else if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 360 | SkASSERT(NULL != resource->cacheAccess().getContentKey()); |
| 361 | ++couldBeScratch; |
| 362 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
| 363 | } |
| 364 | |
| 365 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
| 366 | ++content; |
| 367 | SkASSERT(fContentHash.find(*contentKey) == resource); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | SkASSERT(bytes == fBytes); |
| 372 | SkASSERT(count == fCount); |
| 373 | #if GR_CACHE_STATS |
| 374 | SkASSERT(bytes <= fHighWaterBytes); |
| 375 | SkASSERT(count <= fHighWaterCount); |
| 376 | #endif |
| 377 | SkASSERT(content == fContentHash.count()); |
| 378 | SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
| 379 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 380 | // This assertion is not currently valid because we can be in recursive notifyIsPurgable() |
| 381 | // calls. This will be fixed when subresource registration is explicit. |
| 382 | // bool overBudget = bytes > fMaxBytes || count > fMaxCount; |
| 383 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 384 | } |
| 385 | #endif |
| 386 | |
| 387 | #if GR_CACHE_STATS |
| 388 | void GrResourceCache2::printStats() const { |
| 389 | this->validate(); |
| 390 | |
| 391 | int locked = 0; |
| 392 | int scratch = 0; |
| 393 | |
| 394 | ResourceList::Iter iter; |
| 395 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 396 | |
| 397 | for ( ; resource; resource = iter.next()) { |
| 398 | if (!resource->isPurgable()) { |
| 399 | ++locked; |
| 400 | } |
| 401 | if (resource->cacheAccess().isScratch()) { |
| 402 | ++scratch; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | float countUtilization = (100.f * fCount) / fMaxCount; |
| 407 | float byteUtilization = (100.f * fBytes) / fMaxBytes; |
| 408 | |
| 409 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
| 410 | SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), high %d\n", |
| 411 | fCount, locked, scratch, countUtilization, fHighWaterCount); |
| 412 | SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n", |
| 413 | fBytes, byteUtilization, fHighWaterBytes); |
| 414 | } |
| 415 | |
| 416 | #endif |