OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 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 "GrContext.h" | 10 #include "GrContext.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMI
T * 1024 * 1024; | 63 static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMI
T * 1024 * 1024; |
64 | 64 |
65 static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; | 65 static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; |
66 static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; | 66 static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; |
67 | 67 |
68 static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11; | 68 static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11; |
69 static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4; | 69 static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4; |
70 | 70 |
71 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) | 71 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
72 | 72 |
73 GrTexture* GrAutoScratchTexture::detach() { | |
74 if (NULL == fTexture) { | |
75 return NULL; | |
76 } | |
77 GrTexture* texture = fTexture; | |
78 fTexture = NULL; | |
79 | |
80 // This GrAutoScratchTexture has a ref from lockAndRefScratchTexture, which
we give up now. | |
81 // The cache also has a ref which we are lending to the caller of detach().
When the caller | |
82 // lets go of the ref and the ref count goes to 0 internal_dispose will see
this flag is | |
83 // set and re-ref the texture, thereby restoring the cache's ref. | |
84 SkASSERT(!texture->unique()); | |
85 texture->texturePriv().setFlag((GrTextureFlags) GrTexture::kReturnToCache_Fl
agBit); | |
86 texture->unref(); | |
87 SkASSERT(texture->getCacheEntry()); | |
88 | |
89 return texture; | |
90 } | |
91 | |
92 // Glorified typedef to avoid including GrDrawState.h in GrContext.h | 73 // Glorified typedef to avoid including GrDrawState.h in GrContext.h |
93 class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {}; | 74 class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {}; |
94 | 75 |
95 class GrContext::AutoCheckFlush { | 76 class GrContext::AutoCheckFlush { |
96 public: | 77 public: |
97 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context);
} | 78 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context);
} |
98 | 79 |
99 ~AutoCheckFlush() { | 80 ~AutoCheckFlush() { |
100 if (fContext->fFlushToReduceCacheSize) { | 81 if (fContext->fFlushToReduceCacheSize) { |
101 fContext->flush(); | 82 fContext->flush(); |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 fResourceCache->addResource(resourceKey, texture); | 433 fResourceCache->addResource(resourceKey, texture); |
453 | 434 |
454 if (cacheKey) { | 435 if (cacheKey) { |
455 *cacheKey = resourceKey; | 436 *cacheKey = resourceKey; |
456 } | 437 } |
457 } | 438 } |
458 | 439 |
459 return texture; | 440 return texture; |
460 } | 441 } |
461 | 442 |
462 static GrTexture* create_scratch_texture(GrGpu* gpu, | 443 bool GrContext::createNewScratchTexture(const GrTextureDesc& desc) { |
463 GrResourceCache* resourceCache, | 444 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0)); |
464 const GrTextureDesc& desc) { | 445 if (!texture) { |
465 GrTexture* texture = gpu->createTexture(desc, NULL, 0); | 446 return false; |
466 if (texture) { | |
467 GrResourceKey key = GrTexturePriv::ComputeScratchKey(texture->desc()); | |
468 // Adding a resource could put us overbudget. Try to free up the | |
469 // necessary space before adding it. | |
470 resourceCache->purgeAsNeeded(1, texture->gpuMemorySize()); | |
471 // Make the resource exclusive so future 'find' calls don't return it | |
472 resourceCache->addResource(key, texture, GrResourceCache::kHide_Ownershi
pFlag); | |
473 } | 447 } |
474 return texture; | 448 fResourceCache->addResource(texture->getScratchKey(), texture); |
| 449 texture->fIsScratch = GrIORef::kYes_IsScratch; |
| 450 return true; |
475 } | 451 } |
476 | 452 |
477 GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, Scra
tchTexMatch match) { | 453 GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, Scra
tchTexMatch match, |
| 454 uint32_t internalFlags) { |
478 | 455 |
| 456 // kNoStencil has no meaning if kRT isn't set. |
479 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || | 457 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || |
480 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit)); | 458 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
481 | 459 |
482 // Renderable A8 targets are not universally supported (e.g., not on ANGLE) | 460 // Make sure caller has checked for renderability if kRT is set. |
483 SkASSERT(this->isConfigRenderable(kAlpha_8_GrPixelConfig, inDesc.fSampleCnt
> 0) || | 461 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || |
484 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || | 462 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0)); |
485 (inDesc.fConfig != kAlpha_8_GrPixelConfig)); | |
486 | 463 |
487 if (!fGpu->caps()->reuseScratchTextures() && | 464 SkTCopyOnFirstWrite<GrTextureDesc> desc(inDesc); |
488 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit)) { | 465 |
489 // If we're never recycling this texture we can always make it the right
size | 466 // There is a regression here in that when reuseScratchTextures is false, th
e texture won't be |
490 return create_scratch_texture(fGpu, fResourceCache, inDesc); | 467 // freed when its ref and io counts reach zero. TODO: Make GrResourceCache2
free scratch |
| 468 // resources immediately after it is the sole owner and reuseScratchTextures
is false. |
| 469 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_Gr
TextureFlagBit)) { |
| 470 GrTextureFlags origFlags = desc->fFlags; |
| 471 if (kApprox_ScratchTexMatch == match) { |
| 472 // bin by pow2 with a reasonable min |
| 473 static const int MIN_SIZE = 16; |
| 474 GrTextureDesc* wdesc = desc.writable(); |
| 475 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth)); |
| 476 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight)); |
| 477 } |
| 478 |
| 479 do { |
| 480 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc); |
| 481 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource
(key, internalFlags); |
| 482 if (resource) { |
| 483 fResourceCache->makeResourceMRU(resource); |
| 484 return static_cast<GrTexture*>(resource); |
| 485 } |
| 486 |
| 487 if (kExact_ScratchTexMatch == match) { |
| 488 break; |
| 489 } |
| 490 // We had a cache miss and we are in approx mode, relax the fit of t
he flags. |
| 491 |
| 492 // We no longer try to reuse textures that were previously used as r
ender targets in |
| 493 // situations where no RT is needed; doing otherwise can confuse the
video driver and |
| 494 // cause significant performance problems in some cases. |
| 495 if (desc->fFlags & kNoStencil_GrTextureFlagBit) { |
| 496 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrTextureFl
agBit; |
| 497 } else { |
| 498 break; |
| 499 } |
| 500 |
| 501 } while (true); |
| 502 |
| 503 desc.writable()->fFlags = origFlags; |
491 } | 504 } |
492 | 505 |
493 GrTextureDesc desc = inDesc; | 506 if (!this->createNewScratchTexture(*desc)) { |
494 | 507 return NULL; |
495 if (kApprox_ScratchTexMatch == match) { | |
496 // bin by pow2 with a reasonable min | |
497 static const int MIN_SIZE = 16; | |
498 desc.fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc.fWidth)); | |
499 desc.fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc.fHeight)); | |
500 } | 508 } |
501 | 509 |
502 GrGpuResource* resource = NULL; | 510 // If we got here then we didn't find a cached texture, but we just added on
e. |
503 int origWidth = desc.fWidth; | 511 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc); |
504 int origHeight = desc.fHeight; | 512 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, in
ternalFlags); |
505 | 513 SkASSERT(resource); |
506 do { | |
507 GrResourceKey key = GrTexturePriv::ComputeScratchKey(desc); | |
508 // Ensure we have exclusive access to the texture so future 'find' calls
don't return it | |
509 resource = fResourceCache->find(key, GrResourceCache::kHide_OwnershipFla
g); | |
510 if (resource) { | |
511 resource->ref(); | |
512 break; | |
513 } | |
514 if (kExact_ScratchTexMatch == match) { | |
515 break; | |
516 } | |
517 // We had a cache miss and we are in approx mode, relax the fit of the f
lags. | |
518 | |
519 // We no longer try to reuse textures that were previously used as rende
r targets in | |
520 // situations where no RT is needed; doing otherwise can confuse the vid
eo driver and | |
521 // cause significant performance problems in some cases. | |
522 if (desc.fFlags & kNoStencil_GrTextureFlagBit) { | |
523 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit; | |
524 } else { | |
525 break; | |
526 } | |
527 | |
528 } while (true); | |
529 | |
530 if (NULL == resource) { | |
531 desc.fFlags = inDesc.fFlags; | |
532 desc.fWidth = origWidth; | |
533 desc.fHeight = origHeight; | |
534 resource = create_scratch_texture(fGpu, fResourceCache, desc); | |
535 } | |
536 | |
537 return static_cast<GrTexture*>(resource); | 514 return static_cast<GrTexture*>(resource); |
538 } | 515 } |
539 | 516 |
540 void GrContext::addExistingTextureToCache(GrTexture* texture) { | |
541 | |
542 if (NULL == texture) { | |
543 return; | |
544 } | |
545 | |
546 // This texture should already have a cache entry since it was once | |
547 // attached | |
548 SkASSERT(texture->getCacheEntry()); | |
549 | |
550 // Conceptually, the cache entry is going to assume responsibility | |
551 // for the creation ref. Assert refcnt == 1. | |
552 // Except that this also gets called when the texture is prematurely | |
553 // abandoned. In that case the ref count may be > 1. | |
554 // SkASSERT(texture->unique()); | |
555 | |
556 if (fGpu->caps()->reuseScratchTextures() || texture->asRenderTarget()) { | |
557 // Since this texture came from an AutoScratchTexture it should | |
558 // still be in the exclusive pile. Recycle it. | |
559 fResourceCache->makeNonExclusive(texture->getCacheEntry()); | |
560 this->purgeCache(); | |
561 } else { | |
562 // When we aren't reusing textures we know this scratch texture | |
563 // will never be reused and would be just wasting time in the cache | |
564 fResourceCache->makeNonExclusive(texture->getCacheEntry()); | |
565 fResourceCache->deleteResource(texture->getCacheEntry()); | |
566 } | |
567 } | |
568 | |
569 void GrContext::unlockScratchTexture(GrTexture* texture) { | |
570 if (texture->wasDestroyed()) { | |
571 if (texture->getCacheEntry()->key().isScratch()) { | |
572 // This texture was detached from the cache but the cache still had
a ref to it but | |
573 // not a pointer to it. This will unref the texture and delete its r
esource cache | |
574 // entry. | |
575 delete texture->getCacheEntry(); | |
576 } | |
577 return; | |
578 } | |
579 | |
580 ASSERT_OWNED_RESOURCE(texture); | |
581 SkASSERT(texture->getCacheEntry()); | |
582 | |
583 // If this is a scratch texture we detached it from the cache | |
584 // while it was locked (to avoid two callers simultaneously getting | |
585 // the same texture). | |
586 if (texture->getCacheEntry()->key().isScratch()) { | |
587 if (fGpu->caps()->reuseScratchTextures() || texture->asRenderTarget()) { | |
588 fResourceCache->makeNonExclusive(texture->getCacheEntry()); | |
589 this->purgeCache(); | |
590 } else if (texture->unique()) { | |
591 // Only the cache now knows about this texture. Since we're never | |
592 // reusing scratch textures (in this code path) it would just be | |
593 // wasting time sitting in the cache. | |
594 fResourceCache->makeNonExclusive(texture->getCacheEntry()); | |
595 fResourceCache->deleteResource(texture->getCacheEntry()); | |
596 } else { | |
597 // In this case (there is still a non-cache ref) but we don't really | |
598 // want to readd it to the cache (since it will never be reused). | |
599 // Instead, give up the cache's ref and leave the decision up to | |
600 // addExistingTextureToCache once its ref count reaches 0. For | |
601 // this to work we need to leave it in the exclusive list. | |
602 texture->texturePriv().setFlag((GrTextureFlags) GrTexture::kReturnTo
Cache_FlagBit); | |
603 // Give up the cache's ref to the texture | |
604 texture->unref(); | |
605 } | |
606 } | |
607 } | |
608 | |
609 void GrContext::purgeCache() { | |
610 if (fResourceCache) { | |
611 fResourceCache->purgeAsNeeded(); | |
612 } | |
613 } | |
614 | |
615 bool GrContext::OverbudgetCB(void* data) { | 517 bool GrContext::OverbudgetCB(void* data) { |
616 SkASSERT(data); | 518 SkASSERT(data); |
617 | 519 |
618 GrContext* context = reinterpret_cast<GrContext*>(data); | 520 GrContext* context = reinterpret_cast<GrContext*>(data); |
619 | 521 |
620 // Flush the InOrderDrawBuffer to possibly free up some textures | 522 // Flush the InOrderDrawBuffer to possibly free up some textures |
621 context->fFlushToReduceCacheSize = true; | 523 context->fFlushToReduceCacheSize = true; |
622 | 524 |
623 return true; | 525 return true; |
624 } | 526 } |
(...skipping 1346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1971 fResourceCache->printStats(); | 1873 fResourceCache->printStats(); |
1972 } | 1874 } |
1973 #endif | 1875 #endif |
1974 | 1876 |
1975 #if GR_GPU_STATS | 1877 #if GR_GPU_STATS |
1976 const GrContext::GPUStats* GrContext::gpuStats() const { | 1878 const GrContext::GPUStats* GrContext::gpuStats() const { |
1977 return fGpu->gpuStats(); | 1879 return fGpu->gpuStats(); |
1978 } | 1880 } |
1979 #endif | 1881 #endif |
1980 | 1882 |
OLD | NEW |