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 "GrGpuResource.h" | 11 #include "GrGpuResourceCacheAccess.h" |
12 | |
13 #include "SkChecksum.h" | 12 #include "SkChecksum.h" |
14 #include "SkGr.h" | 13 #include "SkGr.h" |
15 #include "SkMessageBus.h" | 14 #include "SkMessageBus.h" |
16 | 15 |
17 DECLARE_SKMESSAGEBUS_MESSAGE(GrContentKeyInvalidatedMessage); | 16 DECLARE_SKMESSAGEBUS_MESSAGE(GrContentKeyInvalidatedMessage); |
18 | 17 |
19 ////////////////////////////////////////////////////////////////////////////// | 18 ////////////////////////////////////////////////////////////////////////////// |
20 | 19 |
21 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { | 20 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
22 static int32_t gType = INHERITED::kInvalidDomain + 1; | 21 static int32_t gType = INHERITED::kInvalidDomain + 1; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 SkASSERT(!fPurging); | 93 SkASSERT(!fPurging); |
95 fResources.addToHead(resource); | 94 fResources.addToHead(resource); |
96 | 95 |
97 size_t size = resource->gpuMemorySize(); | 96 size_t size = resource->gpuMemorySize(); |
98 ++fCount; | 97 ++fCount; |
99 fBytes += size; | 98 fBytes += size; |
100 #if GR_CACHE_STATS | 99 #if GR_CACHE_STATS |
101 fHighWaterCount = SkTMax(fCount, fHighWaterCount); | 100 fHighWaterCount = SkTMax(fCount, fHighWaterCount); |
102 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); | 101 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
103 #endif | 102 #endif |
104 if (resource->cacheAccess().isBudgeted()) { | 103 if (resource->resourcePriv().isBudgeted()) { |
105 ++fBudgetedCount; | 104 ++fBudgetedCount; |
106 fBudgetedBytes += size; | 105 fBudgetedBytes += size; |
107 #if GR_CACHE_STATS | 106 #if GR_CACHE_STATS |
108 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount
); | 107 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount
); |
109 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes
); | 108 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes
); |
110 #endif | 109 #endif |
111 } | 110 } |
112 if (resource->cacheAccess().getScratchKey().isValid()) { | 111 if (resource->resourcePriv().getScratchKey().isValid()) { |
113 SkASSERT(!resource->cacheAccess().isWrapped()); | 112 SkASSERT(!resource->cacheAccess().isWrapped()); |
114 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); | 113 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
115 } | 114 } |
116 | 115 |
117 this->purgeAsNeeded(); | 116 this->purgeAsNeeded(); |
118 } | 117 } |
119 | 118 |
120 void GrResourceCache::removeResource(GrGpuResource* resource) { | 119 void GrResourceCache::removeResource(GrGpuResource* resource) { |
121 SkASSERT(this->isInCache(resource)); | 120 SkASSERT(this->isInCache(resource)); |
122 | 121 |
123 size_t size = resource->gpuMemorySize(); | 122 size_t size = resource->gpuMemorySize(); |
124 --fCount; | 123 --fCount; |
125 fBytes -= size; | 124 fBytes -= size; |
126 if (resource->cacheAccess().isBudgeted()) { | 125 if (resource->resourcePriv().isBudgeted()) { |
127 --fBudgetedCount; | 126 --fBudgetedCount; |
128 fBudgetedBytes -= size; | 127 fBudgetedBytes -= size; |
129 } | 128 } |
130 | 129 |
131 fResources.remove(resource); | 130 fResources.remove(resource); |
132 if (resource->cacheAccess().getScratchKey().isValid()) { | 131 if (resource->resourcePriv().getScratchKey().isValid()) { |
133 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); | 132 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
134 } | 133 } |
135 if (resource->getContentKey().isValid()) { | 134 if (resource->getContentKey().isValid()) { |
136 fContentHash.remove(resource->getContentKey()); | 135 fContentHash.remove(resource->getContentKey()); |
137 } | 136 } |
138 this->validate(); | 137 this->validate(); |
139 } | 138 } |
140 | 139 |
141 void GrResourceCache::abandonAll() { | 140 void GrResourceCache::abandonAll() { |
142 AutoValidate av(this); | 141 AutoValidate av(this); |
143 | 142 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); | 209 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
211 if (resource) { | 210 if (resource) { |
212 resource->ref(); | 211 resource->ref(); |
213 this->makeResourceMRU(resource); | 212 this->makeResourceMRU(resource); |
214 this->validate(); | 213 this->validate(); |
215 } | 214 } |
216 return resource; | 215 return resource; |
217 } | 216 } |
218 | 217 |
219 void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { | 218 void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
220 SkASSERT(resource->cacheAccess().getScratchKey().isValid()); | 219 SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
221 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); | 220 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
222 } | 221 } |
223 | 222 |
224 void GrResourceCache::willRemoveContentKey(const GrGpuResource* resource) { | 223 void GrResourceCache::willRemoveContentKey(const GrGpuResource* resource) { |
225 // Someone has a ref to this resource in order to invalidate it. When the re
f count reaches | 224 // 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. | 225 // zero we will get a notifyPurgable() and figure out what to do with it. |
227 SkASSERT(resource->getContentKey().isValid()); | 226 SkASSERT(resource->getContentKey().isValid()); |
228 fContentHash.remove(resource->getContentKey()); | 227 fContentHash.remove(resource->getContentKey()); |
229 } | 228 } |
230 | 229 |
231 bool GrResourceCache::didSetContentKey(GrGpuResource* resource) { | 230 bool GrResourceCache::didSetContentKey(GrGpuResource* resource) { |
(...skipping 29 matching lines...) Expand all Loading... |
261 // that additional resources became purgeable. | 260 // that additional resources became purgeable. |
262 if (fPurging) { | 261 if (fPurging) { |
263 fNewlyPurgeableResourceWhilePurging = true; | 262 fNewlyPurgeableResourceWhilePurging = true; |
264 return; | 263 return; |
265 } | 264 } |
266 | 265 |
267 bool release = false; | 266 bool release = false; |
268 | 267 |
269 if (resource->cacheAccess().isWrapped()) { | 268 if (resource->cacheAccess().isWrapped()) { |
270 release = true; | 269 release = true; |
271 } else if (!resource->cacheAccess().isBudgeted()) { | 270 } else if (!resource->resourcePriv().isBudgeted()) { |
272 // Check whether this resource could still be used as a scratch resource
. | 271 // Check whether this resource could still be used as a scratch resource
. |
273 if (resource->cacheAccess().getScratchKey().isValid()) { | 272 if (resource->resourcePriv().getScratchKey().isValid()) { |
274 // We won't purge an existing resource to make room for this one. | 273 // We won't purge an existing resource to make room for this one. |
275 bool underBudget = fBudgetedCount < fMaxCount && | 274 bool underBudget = fBudgetedCount < fMaxCount && |
276 fBudgetedBytes + resource->gpuMemorySize() <= fMa
xBytes; | 275 fBudgetedBytes + resource->gpuMemorySize() <= fMa
xBytes; |
277 if (underBudget) { | 276 if (underBudget) { |
278 resource->cacheAccess().makeBudgeted(); | 277 resource->resourcePriv().makeBudgeted(); |
279 } else { | 278 } else { |
280 release = true; | 279 release = true; |
281 } | 280 } |
282 } else { | 281 } else { |
283 release = true; | 282 release = true; |
284 } | 283 } |
285 } else { | 284 } else { |
286 // Purge the resource if we're over budget | 285 // Purge the resource if we're over budget |
287 bool overBudget = fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxByt
es; | 286 bool overBudget = fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxByt
es; |
288 | 287 |
289 // Also purge if the resource has neither a valid scratch key nor a cont
ent key. | 288 // Also purge if the resource has neither a valid scratch key nor a cont
ent key. |
290 bool noKey = !resource->cacheAccess().getScratchKey().isValid() && | 289 bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
291 !resource->getContentKey().isValid(); | 290 !resource->getContentKey().isValid(); |
292 if (overBudget || noKey) { | 291 if (overBudget || noKey) { |
293 release = true; | 292 release = true; |
294 } | 293 } |
295 } | 294 } |
296 | 295 |
297 if (release) { | 296 if (release) { |
298 SkDEBUGCODE(int beforeCount = fCount;) | 297 SkDEBUGCODE(int beforeCount = fCount;) |
299 resource->cacheAccess().release(); | 298 resource->cacheAccess().release(); |
300 // We should at least free this resource, perhaps dependent resources as
well. | 299 // We should at least free this resource, perhaps dependent resources as
well. |
301 SkASSERT(fCount < beforeCount); | 300 SkASSERT(fCount < beforeCount); |
302 } | 301 } |
303 this->validate(); | 302 this->validate(); |
304 } | 303 } |
305 | 304 |
306 void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size
_t oldSize) { | 305 void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size
_t oldSize) { |
307 // SkASSERT(!fPurging); GrPathRange increases size during flush. :( | 306 // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
308 SkASSERT(resource); | 307 SkASSERT(resource); |
309 SkASSERT(this->isInCache(resource)); | 308 SkASSERT(this->isInCache(resource)); |
310 | 309 |
311 ptrdiff_t delta = resource->gpuMemorySize() - oldSize; | 310 ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
312 | 311 |
313 fBytes += delta; | 312 fBytes += delta; |
314 #if GR_CACHE_STATS | 313 #if GR_CACHE_STATS |
315 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); | 314 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
316 #endif | 315 #endif |
317 if (resource->cacheAccess().isBudgeted()) { | 316 if (resource->resourcePriv().isBudgeted()) { |
318 fBudgetedBytes += delta; | 317 fBudgetedBytes += delta; |
319 #if GR_CACHE_STATS | 318 #if GR_CACHE_STATS |
320 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes
); | 319 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes
); |
321 #endif | 320 #endif |
322 } | 321 } |
323 | 322 |
324 this->purgeAsNeeded(); | 323 this->purgeAsNeeded(); |
325 this->validate(); | 324 this->validate(); |
326 } | 325 } |
327 | 326 |
328 void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { | 327 void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
329 SkASSERT(!fPurging); | 328 SkASSERT(!fPurging); |
330 SkASSERT(resource); | 329 SkASSERT(resource); |
331 SkASSERT(this->isInCache(resource)); | 330 SkASSERT(this->isInCache(resource)); |
332 | 331 |
333 size_t size = resource->gpuMemorySize(); | 332 size_t size = resource->gpuMemorySize(); |
334 | 333 |
335 if (resource->cacheAccess().isBudgeted()) { | 334 if (resource->resourcePriv().isBudgeted()) { |
336 ++fBudgetedCount; | 335 ++fBudgetedCount; |
337 fBudgetedBytes += size; | 336 fBudgetedBytes += size; |
338 #if GR_CACHE_STATS | 337 #if GR_CACHE_STATS |
339 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes
); | 338 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes
); |
340 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount
); | 339 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount
); |
341 #endif | 340 #endif |
342 this->purgeAsNeeded(); | 341 this->purgeAsNeeded(); |
343 } else { | 342 } else { |
344 --fBudgetedCount; | 343 --fBudgetedCount; |
345 fBudgetedBytes -= size; | 344 fBudgetedBytes -= size; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 } while (fNewlyPurgeableResourceWhilePurging); | 410 } while (fNewlyPurgeableResourceWhilePurging); |
412 fPurging = false; | 411 fPurging = false; |
413 this->validate(); | 412 this->validate(); |
414 } | 413 } |
415 | 414 |
416 void GrResourceCache::processInvalidContentKeys( | 415 void GrResourceCache::processInvalidContentKeys( |
417 const SkTArray<GrContentKeyInvalidatedMessage>& msgs) { | 416 const SkTArray<GrContentKeyInvalidatedMessage>& msgs) { |
418 for (int i = 0; i < msgs.count(); ++i) { | 417 for (int i = 0; i < msgs.count(); ++i) { |
419 GrGpuResource* resource = this->findAndRefContentResource(msgs[i].key())
; | 418 GrGpuResource* resource = this->findAndRefContentResource(msgs[i].key())
; |
420 if (resource) { | 419 if (resource) { |
421 resource->cacheAccess().removeContentKey(); | 420 resource->resourcePriv().removeContentKey(); |
422 resource->unref(); // will call notifyPurgeable, if it is indeed now
purgeable. | 421 resource->unref(); // will call notifyPurgeable, if it is indeed now
purgeable. |
423 } | 422 } |
424 } | 423 } |
425 } | 424 } |
426 | 425 |
427 #ifdef SK_DEBUG | 426 #ifdef SK_DEBUG |
428 void GrResourceCache::validate() const { | 427 void GrResourceCache::validate() const { |
429 // Reduce the frequency of validations for large resource counts. | 428 // Reduce the frequency of validations for large resource counts. |
430 static SkRandom gRandom; | 429 static SkRandom gRandom; |
431 int mask = (SkNextPow2(fCount + 1) >> 5) - 1; | 430 int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
(...skipping 16 matching lines...) Expand all Loading... |
448 bytes += resource->gpuMemorySize(); | 447 bytes += resource->gpuMemorySize(); |
449 ++count; | 448 ++count; |
450 | 449 |
451 if (!resource->isPurgeable()) { | 450 if (!resource->isPurgeable()) { |
452 ++locked; | 451 ++locked; |
453 } | 452 } |
454 | 453 |
455 if (resource->cacheAccess().isScratch()) { | 454 if (resource->cacheAccess().isScratch()) { |
456 SkASSERT(!resource->getContentKey().isValid()); | 455 SkASSERT(!resource->getContentKey().isValid()); |
457 ++scratch; | 456 ++scratch; |
458 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK
ey())); | 457 SkASSERT(fScratchMap.countForKey(resource->resourcePriv().getScratch
Key())); |
459 SkASSERT(!resource->cacheAccess().isWrapped()); | 458 SkASSERT(!resource->cacheAccess().isWrapped()); |
460 } else if (resource->cacheAccess().getScratchKey().isValid()) { | 459 } else if (resource->resourcePriv().getScratchKey().isValid()) { |
461 SkASSERT(!resource->cacheAccess().isBudgeted() || | 460 SkASSERT(!resource->resourcePriv().isBudgeted() || |
462 resource->getContentKey().isValid()); | 461 resource->getContentKey().isValid()); |
463 ++couldBeScratch; | 462 ++couldBeScratch; |
464 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK
ey())); | 463 SkASSERT(fScratchMap.countForKey(resource->resourcePriv().getScratch
Key())); |
465 SkASSERT(!resource->cacheAccess().isWrapped()); | 464 SkASSERT(!resource->cacheAccess().isWrapped()); |
466 } | 465 } |
467 const GrContentKey& contentKey = resource->getContentKey(); | 466 const GrContentKey& contentKey = resource->getContentKey(); |
468 if (contentKey.isValid()) { | 467 if (contentKey.isValid()) { |
469 ++content; | 468 ++content; |
470 SkASSERT(fContentHash.find(contentKey) == resource); | 469 SkASSERT(fContentHash.find(contentKey) == resource); |
471 SkASSERT(!resource->cacheAccess().isWrapped()); | 470 SkASSERT(!resource->cacheAccess().isWrapped()); |
472 SkASSERT(resource->cacheAccess().isBudgeted()); | 471 SkASSERT(resource->resourcePriv().isBudgeted()); |
473 } | 472 } |
474 | 473 |
475 if (resource->cacheAccess().isBudgeted()) { | 474 if (resource->resourcePriv().isBudgeted()) { |
476 ++budgetedCount; | 475 ++budgetedCount; |
477 budgetedBytes += resource->gpuMemorySize(); | 476 budgetedBytes += resource->gpuMemorySize(); |
478 } | 477 } |
479 } | 478 } |
480 | 479 |
481 SkASSERT(fBudgetedCount <= fCount); | 480 SkASSERT(fBudgetedCount <= fCount); |
482 SkASSERT(fBudgetedBytes <= fBudgetedBytes); | 481 SkASSERT(fBudgetedBytes <= fBudgetedBytes); |
483 SkASSERT(bytes == fBytes); | 482 SkASSERT(bytes == fBytes); |
484 SkASSERT(count == fCount); | 483 SkASSERT(count == fCount); |
485 SkASSERT(budgetedBytes == fBudgetedBytes); | 484 SkASSERT(budgetedBytes == fBudgetedBytes); |
486 SkASSERT(budgetedCount == fBudgetedCount); | 485 SkASSERT(budgetedCount == fBudgetedCount); |
487 #if GR_CACHE_STATS | 486 #if GR_CACHE_STATS |
488 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); | 487 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
489 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); | 488 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
490 SkASSERT(bytes <= fHighWaterBytes); | 489 SkASSERT(bytes <= fHighWaterBytes); |
491 SkASSERT(count <= fHighWaterCount); | 490 SkASSERT(count <= fHighWaterCount); |
492 SkASSERT(budgetedBytes <= fBudgetedHighWaterBytes); | 491 SkASSERT(budgetedBytes <= fBudgetedHighWaterBytes); |
493 SkASSERT(budgetedCount <= fBudgetedHighWaterCount); | 492 SkASSERT(budgetedCount <= fBudgetedHighWaterCount); |
494 #endif | 493 #endif |
495 SkASSERT(content == fContentHash.count()); | 494 SkASSERT(content == fContentHash.count()); |
496 SkASSERT(scratch + couldBeScratch == fScratchMap.count()); | 495 SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
497 | 496 |
498 // This assertion is not currently valid because we can be in recursive noti
fyIsPurgeable() | 497 // This assertion is not currently valid because we can be in recursive noti
fyIsPurgeable() |
499 // calls. This will be fixed when subresource registration is explicit. | 498 // calls. This will be fixed when subresource registration is explicit. |
500 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; | 499 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
501 // SkASSERT(!overBudget || locked == count || fPurging); | 500 // SkASSERT(!overBudget || locked == count || fPurging); |
502 } | 501 } |
503 #endif | 502 #endif |
OLD | NEW |