Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/gpu/GrContext.cpp

Issue 608883003: GrResourceCache2 manages scratch texture. (Closed) Base URL: https://skia.googlesource.com/skia.git@surfimpl
Patch Set: wrap line and delete unneeded friends Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 fResourceCache->purgeAsNeeded(1, texture->gpuMemorySize()); 432 fResourceCache->purgeAsNeeded(1, texture->gpuMemorySize());
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
robertphillips 2014/09/29 15:25:07 createNewScratchTexture ?
bsalomon 2014/09/29 19:58:13 sure
462 static GrTexture* create_scratch_texture(GrGpu* gpu, 443 bool GrContext::addNewScratchTexture(const GrTextureDesc& desc) {
463 GrResourceCache* resourceCache, 444 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
robertphillips 2014/09/29 15:25:07 NULL == texture ?
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) {
478 454
455 // kNoStencil has no meaning if kRT isn't set.
479 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || 456 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
480 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit)); 457 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit));
481 458
482 // Renderable A8 targets are not universally supported (e.g., not on ANGLE) 459 // Make sure caller has checked for renderability if kRT is set.
483 SkASSERT(this->isConfigRenderable(kAlpha_8_GrPixelConfig, inDesc.fSampleCnt > 0) || 460 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
484 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || 461 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
485 (inDesc.fConfig != kAlpha_8_GrPixelConfig));
486 462
487 if (!fGpu->caps()->reuseScratchTextures() && 463 SkTCopyOnFirstWrite<GrTextureDesc> desc(inDesc);
488 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit)) { 464
489 // If we're never recycling this texture we can always make it the right size 465 if (!fGpu->caps()->reuseScratchTextures() && !(desc->fFlags & kRenderTarget_ GrTextureFlagBit)) {
490 return create_scratch_texture(fGpu, fResourceCache, inDesc); 466 // There is a regression here in that the texture won't be freed when it 's ref and io counts
467 // reach zero. TODO: Make GrResourceCache2 free scratch resources immedi ately after it is
468 // the sole owner and reuseScratchTextures is false.
469 if (!this->addNewScratchTexture(*desc)) {
robertphillips 2014/09/29 15:25:07 return false; -> return NULL; ?
bsalomon 2014/09/29 19:58:13 Done.
470 return false;
471 }
472 } else {
473 GrTextureFlags origFlags = desc->fFlags;
474 if (kApprox_ScratchTexMatch == match) {
475 // bin by pow2 with a reasonable min
476 static const int MIN_SIZE = 16;
477 GrTextureDesc* wdesc = desc.writable();
478 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
479 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
480 }
481
482 do {
483 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
484 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource (key);
485 if (resource) {
486 fResourceCache->makeResourceMRU(resource);
487 return static_cast<GrTexture*>(resource);
488 }
489
490 if (kExact_ScratchTexMatch == match) {
491 break;
492 }
493 // We had a cache miss and we are in approx mode, relax the fit of t he flags.
494
495 // We no longer try to reuse textures that were previously used as r ender targets in
496 // situations where no RT is needed; doing otherwise can confuse the video driver and
497 // cause significant performance problems in some cases.
498 if (desc->fFlags & kNoStencil_GrTextureFlagBit) {
499 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrTextureFl agBit;
500 } else {
501 break;
502 }
503
504 } while (true);
505
506 desc.writable()->fFlags = origFlags;
507
508 if (!this->addNewScratchTexture(*desc)) {
robertphillips 2014/09/29 15:25:07 return false -> return NULL ?
bsalomon 2014/09/29 19:58:13 Done.
509 return false;
510 }
491 } 511 }
492 512
robertphillips 2014/09/29 15:25:07 pull both "if (!this->addNewScratchTexture ... " b
bsalomon 2014/09/29 19:58:13 Done.
493 GrTextureDesc desc = inDesc; 513 // If we got here then we didn't find a cached texture, but we should have a dded one.
494 514 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
495 if (kApprox_ScratchTexMatch == match) { 515 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key);
496 // bin by pow2 with a reasonable min 516 SkASSERT(resource);
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 }
501
502 GrGpuResource* resource = NULL;
503 int origWidth = desc.fWidth;
504 int origHeight = desc.fHeight;
505
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); 517 return static_cast<GrTexture*>(resource);
538 } 518 }
539 519
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) { 520 bool GrContext::OverbudgetCB(void* data) {
616 SkASSERT(data); 521 SkASSERT(data);
617 522
618 GrContext* context = reinterpret_cast<GrContext*>(data); 523 GrContext* context = reinterpret_cast<GrContext*>(data);
619 524
620 // Flush the InOrderDrawBuffer to possibly free up some textures 525 // Flush the InOrderDrawBuffer to possibly free up some textures
621 context->fFlushToReduceCacheSize = true; 526 context->fFlushToReduceCacheSize = true;
622 527
623 return true; 528 return true;
624 } 529 }
(...skipping 1334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1959 fResourceCache->printStats(); 1864 fResourceCache->printStats();
1960 } 1865 }
1961 #endif 1866 #endif
1962 1867
1963 #if GR_GPU_STATS 1868 #if GR_GPU_STATS
1964 const GrContext::GPUStats* GrContext::gpuStats() const { 1869 const GrContext::GPUStats* GrContext::gpuStats() const {
1965 return fGpu->gpuStats(); 1870 return fGpu->gpuStats();
1966 } 1871 }
1967 #endif 1872 #endif
1968 1873
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698