OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "GrResourceCache.h" | 10 #include "GrResourceCache.h" |
11 #include "GrGpuResourceCacheAccess.h" | 11 #include "GrGpuResourceCacheAccess.h" |
12 #include "SkChecksum.h" | 12 #include "SkChecksum.h" |
13 #include "SkGr.h" | 13 #include "SkGr.h" |
14 #include "SkMessageBus.h" | 14 #include "SkMessageBus.h" |
15 | 15 |
16 DECLARE_SKMESSAGEBUS_MESSAGE(GrContentKeyInvalidatedMessage); | 16 DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); |
17 | 17 |
18 ////////////////////////////////////////////////////////////////////////////// | 18 ////////////////////////////////////////////////////////////////////////////// |
19 | 19 |
20 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { | 20 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
21 static int32_t gType = INHERITED::kInvalidDomain + 1; | 21 static int32_t gType = INHERITED::kInvalidDomain + 1; |
22 | 22 |
23 int32_t type = sk_atomic_inc(&gType); | 23 int32_t type = sk_atomic_inc(&gType); |
24 if (type > SK_MaxU16) { | 24 if (type > SK_MaxU16) { |
25 SkFAIL("Too many Resource Types"); | 25 SkFAIL("Too many Resource Types"); |
26 } | 26 } |
27 | 27 |
28 return static_cast<ResourceType>(type); | 28 return static_cast<ResourceType>(type); |
29 } | 29 } |
30 | 30 |
31 GrContentKey::Domain GrContentKey::GenerateDomain() { | 31 GrUniqueKey::Domain GrUniqueKey::GenerateDomain() { |
32 static int32_t gDomain = INHERITED::kInvalidDomain + 1; | 32 static int32_t gDomain = INHERITED::kInvalidDomain + 1; |
33 | 33 |
34 int32_t domain = sk_atomic_inc(&gDomain); | 34 int32_t domain = sk_atomic_inc(&gDomain); |
35 if (domain > SK_MaxU16) { | 35 if (domain > SK_MaxU16) { |
36 SkFAIL("Too many Content Key Domains"); | 36 SkFAIL("Too many GrUniqueKey Domains"); |
37 } | 37 } |
38 | 38 |
39 return static_cast<Domain>(domain); | 39 return static_cast<Domain>(domain); |
40 } | 40 } |
41 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { | 41 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { |
42 return SkChecksum::Compute(data, size); | 42 return SkChecksum::Compute(data, size); |
43 } | 43 } |
44 | 44 |
45 ////////////////////////////////////////////////////////////////////////////// | 45 ////////////////////////////////////////////////////////////////////////////// |
46 | 46 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 SkDEBUGCODE(--fCount;) | 131 SkDEBUGCODE(--fCount;) |
132 fBytes -= size; | 132 fBytes -= size; |
133 if (resource->resourcePriv().isBudgeted()) { | 133 if (resource->resourcePriv().isBudgeted()) { |
134 --fBudgetedCount; | 134 --fBudgetedCount; |
135 fBudgetedBytes -= size; | 135 fBudgetedBytes -= size; |
136 } | 136 } |
137 | 137 |
138 if (resource->resourcePriv().getScratchKey().isValid()) { | 138 if (resource->resourcePriv().getScratchKey().isValid()) { |
139 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); | 139 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
140 } | 140 } |
141 if (resource->getContentKey().isValid()) { | 141 if (resource->getUniqueKey().isValid()) { |
142 fContentHash.remove(resource->getContentKey()); | 142 fUniqueHash.remove(resource->getUniqueKey()); |
143 } | 143 } |
144 this->validate(); | 144 this->validate(); |
145 } | 145 } |
146 | 146 |
147 void GrResourceCache::abandonAll() { | 147 void GrResourceCache::abandonAll() { |
148 AutoValidate av(this); | 148 AutoValidate av(this); |
149 | 149 |
150 while (fNonpurgeableResources.count()) { | 150 while (fNonpurgeableResources.count()) { |
151 GrGpuResource* back = *(fNonpurgeableResources.end() - 1); | 151 GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
152 SkASSERT(!back->wasDestroyed()); | 152 SkASSERT(!back->wasDestroyed()); |
153 back->cacheAccess().abandon(); | 153 back->cacheAccess().abandon(); |
154 } | 154 } |
155 | 155 |
156 while (fPurgeableQueue.count()) { | 156 while (fPurgeableQueue.count()) { |
157 GrGpuResource* top = fPurgeableQueue.peek(); | 157 GrGpuResource* top = fPurgeableQueue.peek(); |
158 SkASSERT(!top->wasDestroyed()); | 158 SkASSERT(!top->wasDestroyed()); |
159 top->cacheAccess().abandon(); | 159 top->cacheAccess().abandon(); |
160 } | 160 } |
161 | 161 |
162 SkASSERT(!fScratchMap.count()); | 162 SkASSERT(!fScratchMap.count()); |
163 SkASSERT(!fContentHash.count()); | 163 SkASSERT(!fUniqueHash.count()); |
164 SkASSERT(!fCount); | 164 SkASSERT(!fCount); |
165 SkASSERT(!this->getResourceCount()); | 165 SkASSERT(!this->getResourceCount()); |
166 SkASSERT(!fBytes); | 166 SkASSERT(!fBytes); |
167 SkASSERT(!fBudgetedCount); | 167 SkASSERT(!fBudgetedCount); |
168 SkASSERT(!fBudgetedBytes); | 168 SkASSERT(!fBudgetedBytes); |
169 } | 169 } |
170 | 170 |
171 void GrResourceCache::releaseAll() { | 171 void GrResourceCache::releaseAll() { |
172 AutoValidate av(this); | 172 AutoValidate av(this); |
173 | 173 |
174 while(fNonpurgeableResources.count()) { | 174 while(fNonpurgeableResources.count()) { |
175 GrGpuResource* back = *(fNonpurgeableResources.end() - 1); | 175 GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
176 SkASSERT(!back->wasDestroyed()); | 176 SkASSERT(!back->wasDestroyed()); |
177 back->cacheAccess().release(); | 177 back->cacheAccess().release(); |
178 } | 178 } |
179 | 179 |
180 while (fPurgeableQueue.count()) { | 180 while (fPurgeableQueue.count()) { |
181 GrGpuResource* top = fPurgeableQueue.peek(); | 181 GrGpuResource* top = fPurgeableQueue.peek(); |
182 SkASSERT(!top->wasDestroyed()); | 182 SkASSERT(!top->wasDestroyed()); |
183 top->cacheAccess().release(); | 183 top->cacheAccess().release(); |
184 } | 184 } |
185 | 185 |
186 SkASSERT(!fScratchMap.count()); | 186 SkASSERT(!fScratchMap.count()); |
187 SkASSERT(!fContentHash.count()); | 187 SkASSERT(!fUniqueHash.count()); |
188 SkASSERT(!fCount); | 188 SkASSERT(!fCount); |
189 SkASSERT(!this->getResourceCount()); | 189 SkASSERT(!this->getResourceCount()); |
190 SkASSERT(!fBytes); | 190 SkASSERT(!fBytes); |
191 SkASSERT(!fBudgetedCount); | 191 SkASSERT(!fBudgetedCount); |
192 SkASSERT(!fBudgetedBytes); | 192 SkASSERT(!fBudgetedBytes); |
193 } | 193 } |
194 | 194 |
195 class GrResourceCache::AvailableForScratchUse { | 195 class GrResourceCache::AvailableForScratchUse { |
196 public: | 196 public: |
197 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin
gIO) { } | 197 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin
gIO) { } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 this->validate(); | 230 this->validate(); |
231 } | 231 } |
232 return resource; | 232 return resource; |
233 } | 233 } |
234 | 234 |
235 void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { | 235 void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
236 SkASSERT(resource->resourcePriv().getScratchKey().isValid()); | 236 SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
237 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); | 237 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
238 } | 238 } |
239 | 239 |
240 void GrResourceCache::willRemoveContentKey(const GrGpuResource* resource) { | 240 void GrResourceCache::willRemoveUniqueKey(const GrGpuResource* resource) { |
241 // Someone has a ref to this resource in order to invalidate it. When the re
f count reaches | 241 // Someone has a ref to this resource in order to invalidate it. When the re
f count reaches |
242 // zero we will get a notifyPurgable() and figure out what to do with it. | 242 // zero we will get a notifyPurgable() and figure out what to do with it. |
243 SkASSERT(resource->getContentKey().isValid()); | 243 SkASSERT(resource->getUniqueKey().isValid()); |
244 fContentHash.remove(resource->getContentKey()); | 244 fUniqueHash.remove(resource->getUniqueKey()); |
245 } | 245 } |
246 | 246 |
247 bool GrResourceCache::didSetContentKey(GrGpuResource* resource) { | 247 bool GrResourceCache::didSetUniqueKey(GrGpuResource* resource) { |
248 SkASSERT(resource); | 248 SkASSERT(resource); |
249 SkASSERT(this->isInCache(resource)); | 249 SkASSERT(this->isInCache(resource)); |
250 SkASSERT(resource->getContentKey().isValid()); | 250 SkASSERT(resource->getUniqueKey().isValid()); |
251 | 251 |
252 GrGpuResource* res = fContentHash.find(resource->getContentKey()); | 252 GrGpuResource* res = fUniqueHash.find(resource->getUniqueKey()); |
253 if (NULL != res) { | 253 if (NULL != res) { |
254 return false; | 254 return false; |
255 } | 255 } |
256 | 256 |
257 fContentHash.add(resource); | 257 fUniqueHash.add(resource); |
258 this->validate(); | 258 this->validate(); |
259 return true; | 259 return true; |
260 } | 260 } |
261 | 261 |
262 void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { | 262 void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
263 SkASSERT(resource); | 263 SkASSERT(resource); |
264 SkASSERT(this->isInCache(resource)); | 264 SkASSERT(this->isInCache(resource)); |
265 if (resource->isPurgeable()) { | 265 if (resource->isPurgeable()) { |
266 // It's about to become unpurgeable. | 266 // It's about to become unpurgeable. |
267 fPurgeableQueue.remove(resource); | 267 fPurgeableQueue.remove(resource); |
(...skipping 18 matching lines...) Expand all Loading... |
286 resource->resourcePriv().getScratchKey().isValid()) { | 286 resource->resourcePriv().getScratchKey().isValid()) { |
287 // We won't purge an existing resource to make room for this one. | 287 // We won't purge an existing resource to make room for this one. |
288 if (fBudgetedCount < fMaxCount && | 288 if (fBudgetedCount < fMaxCount && |
289 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { | 289 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
290 resource->resourcePriv().makeBudgeted(); | 290 resource->resourcePriv().makeBudgeted(); |
291 return; | 291 return; |
292 } | 292 } |
293 } | 293 } |
294 } else { | 294 } else { |
295 // Purge the resource immediately if we're over budget | 295 // Purge the resource immediately if we're over budget |
296 // Also purge if the resource has neither a valid scratch key nor a cont
ent key. | 296 // Also purge if the resource has neither a valid scratch key nor a uniq
ue key. |
297 bool noKey = !resource->resourcePriv().getScratchKey().isValid() && | 297 bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
298 !resource->getContentKey().isValid(); | 298 !resource->getUniqueKey().isValid(); |
299 if (!this->overBudget() && !noKey) { | 299 if (!this->overBudget() && !noKey) { |
300 return; | 300 return; |
301 } | 301 } |
302 } | 302 } |
303 | 303 |
304 SkDEBUGCODE(int beforeCount = this->getResourceCount();) | 304 SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
305 resource->cacheAccess().release(); | 305 resource->cacheAccess().release(); |
306 // We should at least free this resource, perhaps dependent resources as wel
l. | 306 // We should at least free this resource, perhaps dependent resources as wel
l. |
307 SkASSERT(this->getResourceCount() < beforeCount); | 307 SkASSERT(this->getResourceCount() < beforeCount); |
308 this->validate(); | 308 this->validate(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 // Moreover, this is rarely called. | 381 // Moreover, this is rarely called. |
382 while (fPurgeableQueue.count()) { | 382 while (fPurgeableQueue.count()) { |
383 GrGpuResource* resource = fPurgeableQueue.peek(); | 383 GrGpuResource* resource = fPurgeableQueue.peek(); |
384 SkASSERT(resource->isPurgeable()); | 384 SkASSERT(resource->isPurgeable()); |
385 resource->cacheAccess().release(); | 385 resource->cacheAccess().release(); |
386 } | 386 } |
387 | 387 |
388 this->validate(); | 388 this->validate(); |
389 } | 389 } |
390 | 390 |
391 void GrResourceCache::processInvalidContentKeys( | 391 void GrResourceCache::processInvalidUniqueKeys( |
392 const SkTArray<GrContentKeyInvalidatedMessage>& msgs) { | 392 const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
393 for (int i = 0; i < msgs.count(); ++i) { | 393 for (int i = 0; i < msgs.count(); ++i) { |
394 GrGpuResource* resource = this->findAndRefContentResource(msgs[i].key())
; | 394 GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
395 if (resource) { | 395 if (resource) { |
396 resource->resourcePriv().removeContentKey(); | 396 resource->resourcePriv().removeUniqueKey(); |
397 resource->unref(); // will call notifyPurgeable, if it is indeed now
purgeable. | 397 resource->unref(); // will call notifyPurgeable, if it is indeed now
purgeable. |
398 } | 398 } |
399 } | 399 } |
400 } | 400 } |
401 | 401 |
402 void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { | 402 void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
403 int index = fNonpurgeableResources.count(); | 403 int index = fNonpurgeableResources.count(); |
404 *fNonpurgeableResources.append() = resource; | 404 *fNonpurgeableResources.append() = resource; |
405 *resource->cacheAccess().accessCacheIndex() = index; | 405 *resource->cacheAccess().accessCacheIndex() = index; |
406 } | 406 } |
(...skipping 21 matching lines...) Expand all Loading... |
428 | 428 |
429 struct Stats { | 429 struct Stats { |
430 size_t fBytes; | 430 size_t fBytes; |
431 int fBudgetedCount; | 431 int fBudgetedCount; |
432 size_t fBudgetedBytes; | 432 size_t fBudgetedBytes; |
433 int fLocked; | 433 int fLocked; |
434 int fScratch; | 434 int fScratch; |
435 int fCouldBeScratch; | 435 int fCouldBeScratch; |
436 int fContent; | 436 int fContent; |
437 const ScratchMap* fScratchMap; | 437 const ScratchMap* fScratchMap; |
438 const ContentHash* fContentHash; | 438 const UniqueHash* fUniqueHash; |
439 | 439 |
440 Stats(const GrResourceCache* cache) { | 440 Stats(const GrResourceCache* cache) { |
441 memset(this, 0, sizeof(*this)); | 441 memset(this, 0, sizeof(*this)); |
442 fScratchMap = &cache->fScratchMap; | 442 fScratchMap = &cache->fScratchMap; |
443 fContentHash = &cache->fContentHash; | 443 fUniqueHash = &cache->fUniqueHash; |
444 } | 444 } |
445 | 445 |
446 void update(GrGpuResource* resource) { | 446 void update(GrGpuResource* resource) { |
447 fBytes += resource->gpuMemorySize(); | 447 fBytes += resource->gpuMemorySize(); |
448 | 448 |
449 if (!resource->isPurgeable()) { | 449 if (!resource->isPurgeable()) { |
450 ++fLocked; | 450 ++fLocked; |
451 } | 451 } |
452 | 452 |
453 if (resource->cacheAccess().isScratch()) { | 453 if (resource->cacheAccess().isScratch()) { |
454 SkASSERT(!resource->getContentKey().isValid()); | 454 SkASSERT(!resource->getUniqueKey().isValid()); |
455 ++fScratch; | 455 ++fScratch; |
456 SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getSc
ratchKey())); | 456 SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getSc
ratchKey())); |
457 SkASSERT(!resource->cacheAccess().isWrapped()); | 457 SkASSERT(!resource->cacheAccess().isWrapped()); |
458 } else if (resource->resourcePriv().getScratchKey().isValid()) { | 458 } else if (resource->resourcePriv().getScratchKey().isValid()) { |
459 SkASSERT(!resource->resourcePriv().isBudgeted() || | 459 SkASSERT(!resource->resourcePriv().isBudgeted() || |
460 resource->getContentKey().isValid()); | 460 resource->getUniqueKey().isValid()); |
461 ++fCouldBeScratch; | 461 ++fCouldBeScratch; |
462 SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getSc
ratchKey())); | 462 SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getSc
ratchKey())); |
463 SkASSERT(!resource->cacheAccess().isWrapped()); | 463 SkASSERT(!resource->cacheAccess().isWrapped()); |
464 } | 464 } |
465 const GrContentKey& contentKey = resource->getContentKey(); | 465 const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
466 if (contentKey.isValid()) { | 466 if (uniqueKey.isValid()) { |
467 ++fContent; | 467 ++fContent; |
468 SkASSERT(fContentHash->find(contentKey) == resource); | 468 SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
469 SkASSERT(!resource->cacheAccess().isWrapped()); | 469 SkASSERT(!resource->cacheAccess().isWrapped()); |
470 SkASSERT(resource->resourcePriv().isBudgeted()); | 470 SkASSERT(resource->resourcePriv().isBudgeted()); |
471 } | 471 } |
472 | 472 |
473 if (resource->resourcePriv().isBudgeted()) { | 473 if (resource->resourcePriv().isBudgeted()) { |
474 ++fBudgetedCount; | 474 ++fBudgetedCount; |
475 fBudgetedBytes += resource->gpuMemorySize(); | 475 fBudgetedBytes += resource->gpuMemorySize(); |
476 } | 476 } |
477 } | 477 } |
478 }; | 478 }; |
(...skipping 20 matching lines...) Expand all Loading... |
499 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); | 499 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
500 SkASSERT(stats.fBudgetedCount == fBudgetedCount); | 500 SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
501 #if GR_CACHE_STATS | 501 #if GR_CACHE_STATS |
502 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); | 502 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
503 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); | 503 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
504 SkASSERT(fBytes <= fHighWaterBytes); | 504 SkASSERT(fBytes <= fHighWaterBytes); |
505 SkASSERT(fCount <= fHighWaterCount); | 505 SkASSERT(fCount <= fHighWaterCount); |
506 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); | 506 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
507 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); | 507 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
508 #endif | 508 #endif |
509 SkASSERT(stats.fContent == fContentHash.count()); | 509 SkASSERT(stats.fContent == fUniqueHash.count()); |
510 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); | 510 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
511 | 511 |
512 // This assertion is not currently valid because we can be in recursive noti
fyIsPurgeable() | 512 // This assertion is not currently valid because we can be in recursive noti
fyIsPurgeable() |
513 // calls. This will be fixed when subresource registration is explicit. | 513 // calls. This will be fixed when subresource registration is explicit. |
514 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; | 514 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
515 // SkASSERT(!overBudget || locked == count || fPurging); | 515 // SkASSERT(!overBudget || locked == count || fPurging); |
516 } | 516 } |
517 | 517 |
518 bool GrResourceCache::isInCache(const GrGpuResource* resource) const { | 518 bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
519 int index = *resource->cacheAccess().accessCacheIndex(); | 519 int index = *resource->cacheAccess().accessCacheIndex(); |
520 if (index < 0) { | 520 if (index < 0) { |
521 return false; | 521 return false; |
522 } | 522 } |
523 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource
) { | 523 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource
) { |
524 return true; | 524 return true; |
525 } | 525 } |
526 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index]
== resource) { | 526 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index]
== resource) { |
527 return true; | 527 return true; |
528 } | 528 } |
529 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the ca
che."); | 529 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the ca
che."); |
530 return false; | 530 return false; |
531 } | 531 } |
532 | 532 |
533 #endif | 533 #endif |
OLD | NEW |