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 "GrResourceCache2.h" | 10 #include "GrResourceCache2.h" |
11 #include "GrGpuResource.h" | 11 #include "GrGpuResource.h" |
12 | 12 |
13 #include "SkChecksum.h" | 13 #include "SkChecksum.h" |
14 #include "SkGr.h" | 14 #include "SkGr.h" |
15 #include "SkMessageBus.h" | 15 #include "SkMessageBus.h" |
16 | 16 |
17 DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); | 17 DECLARE_SKMESSAGEBUS_MESSAGE(GrContentKeyInvalidatedMessage); |
18 | 18 |
19 ////////////////////////////////////////////////////////////////////////////// | 19 ////////////////////////////////////////////////////////////////////////////// |
20 | 20 |
21 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { | 21 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
22 static int32_t gType = INHERITED::kInvalidDomain + 1; | 22 static int32_t gType = INHERITED::kInvalidDomain + 1; |
23 | 23 |
24 int32_t type = sk_atomic_inc(&gType); | 24 int32_t type = sk_atomic_inc(&gType); |
25 if (type > SK_MaxU16) { | 25 if (type > SK_MaxU16) { |
26 SkFAIL("Too many Resource Types"); | 26 SkFAIL("Too many Resource Types"); |
27 } | 27 } |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 this->validate(); | 214 this->validate(); |
215 } | 215 } |
216 return resource; | 216 return resource; |
217 } | 217 } |
218 | 218 |
219 void GrResourceCache2::willRemoveScratchKey(const GrGpuResource* resource) { | 219 void GrResourceCache2::willRemoveScratchKey(const GrGpuResource* resource) { |
220 SkASSERT(resource->cacheAccess().getScratchKey().isValid()); | 220 SkASSERT(resource->cacheAccess().getScratchKey().isValid()); |
221 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); | 221 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
222 } | 222 } |
223 | 223 |
| 224 void GrResourceCache2::willRemoveContentKey(const GrGpuResource* resource) { |
| 225 // Someone has a ref to this resource in order to invalidate it. When the re
f count reaches |
| 226 // zero we will get a notifyPurgable() and figure out what to do with it. |
| 227 SkASSERT(resource->getContentKey().isValid()); |
| 228 fContentHash.remove(resource->getContentKey()); |
| 229 } |
| 230 |
224 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { | 231 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { |
225 SkASSERT(!fPurging); | 232 SkASSERT(!fPurging); |
226 SkASSERT(resource); | 233 SkASSERT(resource); |
227 SkASSERT(this->isInCache(resource)); | 234 SkASSERT(this->isInCache(resource)); |
228 SkASSERT(resource->getContentKey().isValid()); | 235 SkASSERT(resource->getContentKey().isValid()); |
229 | 236 |
230 GrGpuResource* res = fContentHash.find(resource->getContentKey()); | 237 GrGpuResource* res = fContentHash.find(resource->getContentKey()); |
231 if (NULL != res) { | 238 if (NULL != res) { |
232 return false; | 239 return false; |
233 } | 240 } |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } | 406 } |
400 | 407 |
401 if (!fNewlyPurgeableResourceWhilePurging && fCount && fOverBudgetCB) { | 408 if (!fNewlyPurgeableResourceWhilePurging && fCount && fOverBudgetCB) { |
402 (*fOverBudgetCB)(fOverBudgetData); | 409 (*fOverBudgetCB)(fOverBudgetData); |
403 } | 410 } |
404 } while (fNewlyPurgeableResourceWhilePurging); | 411 } while (fNewlyPurgeableResourceWhilePurging); |
405 fPurging = false; | 412 fPurging = false; |
406 this->validate(); | 413 this->validate(); |
407 } | 414 } |
408 | 415 |
| 416 void GrResourceCache2::processInvalidContentKeys( |
| 417 const SkTArray<GrContentKeyInvalidatedMessage>& msgs) { |
| 418 for (int i = 0; i < msgs.count(); ++i) { |
| 419 GrGpuResource* resource = this->findAndRefContentResource(msgs[i].key())
; |
| 420 if (resource) { |
| 421 resource->cacheAccess().removeContentKey(); |
| 422 resource->unref(); // will call notifyPurgeable, if it is indeed now
purgeable. |
| 423 } |
| 424 } |
| 425 } |
| 426 |
409 #ifdef SK_DEBUG | 427 #ifdef SK_DEBUG |
410 void GrResourceCache2::validate() const { | 428 void GrResourceCache2::validate() const { |
411 // Reduce the frequency of validations for large resource counts. | 429 // Reduce the frequency of validations for large resource counts. |
412 static SkRandom gRandom; | 430 static SkRandom gRandom; |
413 int mask = (SkNextPow2(fCount + 1) >> 5) - 1; | 431 int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
414 if (~mask && (gRandom.nextU() & mask)) { | 432 if (~mask && (gRandom.nextU() & mask)) { |
415 return; | 433 return; |
416 } | 434 } |
417 | 435 |
418 size_t bytes = 0; | 436 size_t bytes = 0; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 #endif | 494 #endif |
477 SkASSERT(content == fContentHash.count()); | 495 SkASSERT(content == fContentHash.count()); |
478 SkASSERT(scratch + couldBeScratch == fScratchMap.count()); | 496 SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
479 | 497 |
480 // This assertion is not currently valid because we can be in recursive noti
fyIsPurgeable() | 498 // This assertion is not currently valid because we can be in recursive noti
fyIsPurgeable() |
481 // calls. This will be fixed when subresource registration is explicit. | 499 // calls. This will be fixed when subresource registration is explicit. |
482 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; | 500 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
483 // SkASSERT(!overBudget || locked == count || fPurging); | 501 // SkASSERT(!overBudget || locked == count || fPurging); |
484 } | 502 } |
485 #endif | 503 #endif |
OLD | NEW |