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 | resource->ref(); |
| 97 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 98 | ++fCount; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 99 | SkDEBUGCODE(fHighWaterCount = SkTMax(fCount, fHighWaterCount)); |
| 100 | fBytes += resource->gpuMemorySize(); |
| 101 | SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes)); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 102 | if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 103 | // TODO(bsalomon): Make this assertion possible. |
| 104 | // SkASSERT(!resource->isWrapped()); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 105 | fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 106 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 107 | |
| 108 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 112 | AutoValidate av(this); |
| 113 | |
| 114 | --fCount; |
| 115 | fBytes -= resource->gpuMemorySize(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 116 | SkASSERT(this->isInCache(resource)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 117 | fResources.remove(resource); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 118 | if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 119 | fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 120 | } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 121 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 122 | fContentHash.remove(*contentKey); |
| 123 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void GrResourceCache2::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 127 | AutoValidate av(this); |
| 128 | |
| 129 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 130 | while (GrGpuResource* head = fResources.head()) { |
| 131 | SkASSERT(!head->wasDestroyed()); |
| 132 | head->abandon(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 133 | head->unref(); |
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()); |
| 148 | head->release(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 149 | head->unref(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 150 | // release should have already removed this from the list. |
| 151 | SkASSERT(head != fResources.head()); |
| 152 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 153 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 154 | SkASSERT(!fCount); |
| 155 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 156 | |
| 157 | class GrResourceCache2::AvailableForScratchUse { |
| 158 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 159 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 160 | |
| 161 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 162 | if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 163 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 164 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 165 | |
| 166 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 167 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 168 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 169 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 170 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 174 | uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 175 | AutoValidate av(this); |
| 176 | |
| 177 | SkASSERT(!fPurging); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 178 | SkASSERT(scratchKey.isScratch()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 179 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 180 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 181 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 182 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 183 | if (resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 184 | this->makeResourceMRU(resource); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 185 | return SkRef(resource); |
| 186 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 187 | return NULL; |
| 188 | } |
| 189 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 190 | // but there is still space in our budget for the resource. |
| 191 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 192 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 193 | if (resource) { |
| 194 | resource->ref(); |
| 195 | this->makeResourceMRU(resource); |
| 196 | } |
| 197 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 198 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 199 | |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 200 | bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 201 | SkASSERT(!fPurging); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 202 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 203 | SkASSERT(this->isInCache(resource)); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 204 | SkASSERT(resource->cacheAccess().getContentKey()); |
| 205 | SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 206 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 207 | GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 208 | if (NULL != res) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | fContentHash.add(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 213 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 214 | return true; |
| 215 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame^] | 216 | |
| 217 | void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) { |
| 218 | AutoValidate av(this); |
| 219 | |
| 220 | SkASSERT(!fPurging); |
| 221 | SkASSERT(resource); |
| 222 | SkASSERT(this->isInCache(resource)); |
| 223 | fResources.remove(resource); |
| 224 | fResources.addToHead(resource); |
| 225 | } |
| 226 | |
| 227 | void GrResourceCache2::notifyPurgable(const GrGpuResource* resource) { |
| 228 | SkASSERT(resource); |
| 229 | SkASSERT(this->isInCache(resource)); |
| 230 | SkASSERT(resource->isPurgable()); |
| 231 | |
| 232 | // We can't purge if in the middle of purging because purge is iterating. Instead record |
| 233 | // that additional resources became purgable. |
| 234 | if (fPurging) { |
| 235 | fNewlyPurgableResourceWhilePurging = true; |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | // Purge the resource if we're over budget |
| 240 | bool overBudget = fCount > fMaxCount || fBytes > fMaxBytes; |
| 241 | |
| 242 | // We should not be over budget here unless all resources are unpuragble. |
| 243 | #ifdef SK_DEBUG |
| 244 | if (overBudget) { |
| 245 | ResourceList::Iter iter; |
| 246 | GrGpuResource* r = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 247 | for ( ; r; r = iter.next()) { |
| 248 | SkASSERT(r == resource || !r->isPurgable()); |
| 249 | } |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | // Also purge if the resource has neither a valid scratch key nor a content key. |
| 254 | bool noKey = !resource->cacheAccess().isScratch() && |
| 255 | (NULL == resource->cacheAccess().getContentKey()); |
| 256 | |
| 257 | if (overBudget || noKey) { |
| 258 | SkDEBUGCODE(int beforeCount = fCount;) |
| 259 | resource->unref(); |
| 260 | // We should at least have freed resource. It may have in turn freed other resources. |
| 261 | SkASSERT(fCount < beforeCount); |
| 262 | } |
| 263 | |
| 264 | this->validate(); |
| 265 | } |
| 266 | |
| 267 | void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 268 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 269 | SkASSERT(resource); |
| 270 | SkASSERT(this->isInCache(resource)); |
| 271 | |
| 272 | fBytes += resource->gpuMemorySize() - oldSize; |
| 273 | SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes)); |
| 274 | |
| 275 | this->purgeAsNeeded(); |
| 276 | this->validate(); |
| 277 | } |
| 278 | |
| 279 | void GrResourceCache2::internalPurgeAsNeeded() { |
| 280 | SkASSERT(!fPurging); |
| 281 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 282 | SkASSERT(fCount > fMaxCount || fBytes > fMaxBytes); |
| 283 | |
| 284 | fPurging = true; |
| 285 | |
| 286 | AutoValidate av(this); // Put this after setting fPurging so we're allowed to be over budget. |
| 287 | |
| 288 | bool overBudget = true; |
| 289 | do { |
| 290 | fNewlyPurgableResourceWhilePurging = false; |
| 291 | ResourceList::Iter resourceIter; |
| 292 | GrGpuResource* resource = resourceIter.init(fResources, |
| 293 | ResourceList::Iter::kTail_IterStart); |
| 294 | |
| 295 | while (resource) { |
| 296 | GrGpuResource* prev = resourceIter.prev(); |
| 297 | if (resource->isPurgable()) { |
| 298 | resource->unref(); |
| 299 | } |
| 300 | resource = prev; |
| 301 | if (fCount <= fMaxCount && fBytes <= fMaxBytes) { |
| 302 | overBudget = false; |
| 303 | resource = NULL; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) { |
| 308 | // Despite the purge we're still over budget. Call our over budget callback. |
| 309 | (*fOverBudgetCB)(fOverBudgetData); |
| 310 | } |
| 311 | } while (overBudget && fNewlyPurgableResourceWhilePurging); |
| 312 | |
| 313 | fNewlyPurgableResourceWhilePurging = false; |
| 314 | fPurging = false; |
| 315 | } |
| 316 | |
| 317 | void GrResourceCache2::purgeAllUnlocked() { |
| 318 | SkASSERT(!fPurging); |
| 319 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 320 | |
| 321 | fPurging = true; |
| 322 | |
| 323 | AutoValidate av(this); // Put this after setting fPurging so we're allowed to be over budget. |
| 324 | |
| 325 | do { |
| 326 | fNewlyPurgableResourceWhilePurging = false; |
| 327 | ResourceList::Iter resourceIter; |
| 328 | GrGpuResource* resource = |
| 329 | resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart); |
| 330 | |
| 331 | while (resource) { |
| 332 | GrGpuResource* prev = resourceIter.prev(); |
| 333 | if (resource->isPurgable()) { |
| 334 | resource->unref(); |
| 335 | } |
| 336 | resource = prev; |
| 337 | } |
| 338 | |
| 339 | if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) { |
| 340 | (*fOverBudgetCB)(fOverBudgetData); |
| 341 | } |
| 342 | } while (fNewlyPurgableResourceWhilePurging); |
| 343 | fPurging = false; |
| 344 | } |
| 345 | |
| 346 | #ifdef SK_DEBUG |
| 347 | void GrResourceCache2::validate() const { |
| 348 | size_t bytes = 0; |
| 349 | int count = 0; |
| 350 | int locked = 0; |
| 351 | int scratch = 0; |
| 352 | int couldBeScratch = 0; |
| 353 | int content = 0; |
| 354 | |
| 355 | ResourceList::Iter iter; |
| 356 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 357 | for ( ; resource; resource = iter.next()) { |
| 358 | bytes += resource->gpuMemorySize(); |
| 359 | ++count; |
| 360 | |
| 361 | if (!resource->isPurgable()) { |
| 362 | ++locked; |
| 363 | } |
| 364 | |
| 365 | if (resource->cacheAccess().isScratch()) { |
| 366 | SkASSERT(NULL == resource->cacheAccess().getContentKey()); |
| 367 | ++scratch; |
| 368 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
| 369 | } else if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 370 | SkASSERT(NULL != resource->cacheAccess().getContentKey()); |
| 371 | ++couldBeScratch; |
| 372 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
| 373 | } |
| 374 | |
| 375 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
| 376 | ++content; |
| 377 | SkASSERT(fContentHash.find(*contentKey) == resource); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | SkASSERT(bytes == fBytes); |
| 382 | SkASSERT(count == fCount); |
| 383 | #if GR_CACHE_STATS |
| 384 | SkASSERT(bytes <= fHighWaterBytes); |
| 385 | SkASSERT(count <= fHighWaterCount); |
| 386 | #endif |
| 387 | SkASSERT(content == fContentHash.count()); |
| 388 | SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
| 389 | |
| 390 | bool overBudget = bytes > fMaxBytes || count > fMaxCount; |
| 391 | SkASSERT(!overBudget || locked == count || fPurging); |
| 392 | } |
| 393 | #endif |
| 394 | |
| 395 | #if GR_CACHE_STATS |
| 396 | void GrResourceCache2::printStats() const { |
| 397 | this->validate(); |
| 398 | |
| 399 | int locked = 0; |
| 400 | int scratch = 0; |
| 401 | |
| 402 | ResourceList::Iter iter; |
| 403 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 404 | |
| 405 | for ( ; resource; resource = iter.next()) { |
| 406 | if (!resource->isPurgable()) { |
| 407 | ++locked; |
| 408 | } |
| 409 | if (resource->cacheAccess().isScratch()) { |
| 410 | ++scratch; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | float countUtilization = (100.f * fCount) / fMaxCount; |
| 415 | float byteUtilization = (100.f * fBytes) / fMaxBytes; |
| 416 | |
| 417 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
| 418 | SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), high %d\n", |
| 419 | fCount, locked, scratch, countUtilization, fHighWaterCount); |
| 420 | SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n", |
| 421 | fBytes, byteUtilization, fHighWaterBytes); |
| 422 | } |
| 423 | |
| 424 | #endif |