GrGLGpuProgramCache.cpp revision 64c4728c70001ed074fecf5c4e083781987b12e9
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc. 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 6f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com */ 7f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 839edf7664f50b6c890b933b5bbed67a8735b349bjvanverth#include "GrGLGpu.h" 9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 1047bb38283072dc87dc93220cd2f370ca109972ffjoshualitt#include "builders/GrGLProgramBuilder.h" 11b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#include "GrProcessor.h" 128a4c1030ff4b8336b5ac5b0712691e2f65383440egdaniel#include "GrGLPathRendering.h" 1364c4728c70001ed074fecf5c4e083781987b12e9egdaniel#include "glsl/GrGLSLFragmentProcessor.h" 14018fb62d12d1febf121fe265da5b6117b86a6541egdaniel#include "glsl/GrGLSLProgramDataManager.h" 15170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel#include "SkRTConf.h" 162db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#include "SkTSearch.h" 17f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 18a2f4b15d4e861b8b29e20ec37743fd3fd4b05b03jvanverth@google.com#ifdef PROGRAM_CACHE_STATS 195c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.comSK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false, 205c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com "Display program cache usage."); 21a2f4b15d4e861b8b29e20ec37743fd3fd4b05b03jvanverth@google.com#endif 225c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com 23018fb62d12d1febf121fe265da5b6117b86a6541egdanieltypedef GrGLSLProgramDataManager::UniformHandle UniformHandle; 24dbbc4e2da93cef5c0cfb0b3c92ff6c2c80f6e67absalomon@google.com 25861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonstruct GrGLGpu::ProgramCache::Entry { 262766c00fc0b6a07d46e5f74cdad45da2ef625237mtklein 2796fcdcc219d2a0d3579719b84b28bede76efba64halcanary Entry() : fProgram(nullptr), fLRUStamp(0) {} 282db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 292db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com SkAutoTUnref<GrGLProgram> fProgram; 302db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com unsigned int fLRUStamp; 312db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com}; 322db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 33861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonstruct GrGLGpu::ProgramCache::ProgDescLess { 3479f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt bool operator() (const GrProgramDesc& desc, const Entry* entry) { 3549f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(entry->fProgram.get()); 3679f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt return GrProgramDesc::Less(desc, entry->fProgram->getDesc()); 372db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 382db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 3979f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt bool operator() (const Entry* entry, const GrProgramDesc& desc) { 4049f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(entry->fProgram.get()); 4179f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt return GrProgramDesc::Less(entry->fProgram->getDesc(), desc); 422db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 432db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com}; 44f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 45861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonGrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) 46c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com : fCount(0) 47c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com , fCurrLRUStamp(0) 489188a15f846ae79892c332aed2a72ee38116bdc6commit-bot@chromium.org , fGpu(gpu) 49948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#ifdef PROGRAM_CACHE_STATS 50948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com , fTotalRequests(0) 51948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com , fCacheMisses(0) 522db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com , fHashMisses(0) 53948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#endif 54948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com{ 552db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com for (int i = 0; i < 1 << kHashBits; ++i) { 5696fcdcc219d2a0d3579719b84b28bede76efba64halcanary fHashTable[i] = nullptr; 572db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 58948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com} 59948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com 60861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonGrGLGpu::ProgramCache::~ProgramCache() { 612db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com for (int i = 0; i < fCount; ++i){ 62385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary delete fEntries[i]; 632db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 64948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com // dump stats 65948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#ifdef PROGRAM_CACHE_STATS 665c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com if (c_DisplayCache) { 675c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com SkDebugf("--- Program Cache ---\n"); 685c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com SkDebugf("Total requests: %d\n", fTotalRequests); 695c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com SkDebugf("Cache misses: %d\n", fCacheMisses); 705c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? 715c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com 100.f * fCacheMisses / fTotalRequests : 725c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com 0.f); 735c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com int cacheHits = fTotalRequests - fCacheMisses; 745c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f); 755c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com SkDebugf("---------------------\n"); 765c9b6faefff735110a59932793e81cf5b9dec51djvanverth@google.com } 77948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#endif 78c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com} 79f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 80861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonvoid GrGLGpu::ProgramCache::abandon() { 81c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com for (int i = 0; i < fCount; ++i) { 8249f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(fEntries[i]->fProgram.get()); 832db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[i]->fProgram->abandon(); 84385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary delete fEntries[i]; 85f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 86c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com fCount = 0; 87c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com} 88f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 89861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonint GrGLGpu::ProgramCache::search(const GrProgramDesc& desc) const { 902db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com ProgDescLess less; 912db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); 922db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com} 932db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 946df86409ca586c3cb34f616f03501bd96181f9e4bsalomonGrGLProgram* GrGLGpu::ProgramCache::refProgram(const DrawArgs& args) { 95948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#ifdef PROGRAM_CACHE_STATS 96948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com ++fTotalRequests; 97948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#endif 989ba4fa6f0fb8ef496d81ccac36e780aa806fea83bsalomon@google.com 9996fcdcc219d2a0d3579719b84b28bede76efba64halcanary Entry* entry = nullptr; 1002db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 101873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt uint32_t hashIdx = args.fDesc->getChecksum(); 1022db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com hashIdx ^= hashIdx >> 16; 1032db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (kHashBits <= 8) { 1042db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com hashIdx ^= hashIdx >> 8; 1052db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1062db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com hashIdx &=((1 << kHashBits) - 1); 1072db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com Entry* hashedEntry = fHashTable[hashIdx]; 108873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt if (hashedEntry && hashedEntry->fProgram->getDesc() == *args.fDesc) { 10949f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(hashedEntry->fProgram); 1102db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry = hashedEntry; 1112db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1122db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 1132db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com int entryIdx; 11496fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (nullptr == entry) { 115873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt entryIdx = this->search(*args.fDesc); 1162db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (entryIdx >= 0) { 1172db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry = fEntries[entryIdx]; 1182db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#ifdef PROGRAM_CACHE_STATS 1192db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com ++fHashMisses; 1202db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#endif 1212db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1222db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1232db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 12496fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (nullptr == entry) { 1252db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // We have a cache miss 126948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#ifdef PROGRAM_CACHE_STATS 127948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com ++fCacheMisses; 128948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#endif 129873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt GrGLProgram* program = GrGLProgramBuilder::CreateProgram(args, fGpu); 13096fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (nullptr == program) { 13196fcdcc219d2a0d3579719b84b28bede76efba64halcanary return nullptr; 132c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com } 1332db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com int purgeIdx = 0; 134c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com if (fCount < kMaxEntries) { 135385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary entry = new Entry; 1362db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com purgeIdx = fCount++; 1372db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[purgeIdx] = entry; 138c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com } else { 139f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(fCount == kMaxEntries); 1402db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com purgeIdx = 0; 141c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com for (int i = 1; i < kMaxEntries; ++i) { 1422db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { 1432db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com purgeIdx = i; 144f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 145f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 1462db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry = fEntries[purgeIdx]; 1472db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); 1482db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (fHashTable[purgedHashIdx] == entry) { 14996fcdcc219d2a0d3579719b84b28bede76efba64halcanary fHashTable[purgedHashIdx] = nullptr; 1502db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 151f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 152f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(fEntries[purgeIdx] == entry); 1532db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry->fProgram.reset(program); 1542d816ad36e806e5b1cf3c447e547829bbbe74fd1skia.committer@gmail.com // We need to shift fEntries around so that the entry currently at purgeIdx is placed 1552db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). 1562db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entryIdx = ~entryIdx; 1572db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (entryIdx < purgeIdx) { 1582db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // Let E and P be the entries at index entryIdx and purgeIdx, respectively. 1592db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // If the entries array looks like this: 1602db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaaEbbbbbPccccc 1612db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // we rearrange it to look like this: 1622db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaaPEbbbbbccccc 1632db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); 1642db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); 1652db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[entryIdx] = entry; 1662db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } else if (purgeIdx < entryIdx) { 1672db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // If the entries array looks like this: 1682db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaaPbbbbbEccccc 1692db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // we rearrange it to look like this: 1702db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaabbbbbPEccccc 1712db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); 1722db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); 1732db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[entryIdx - 1] = entry; 1742db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 175515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG 17649f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(fEntries[0]->fProgram.get()); 1772db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com for (int i = 0; i < fCount - 1; ++i) { 17849f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(fEntries[i + 1]->fProgram.get()); 17979f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt const GrProgramDesc& a = fEntries[i]->fProgram->getDesc(); 18079f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); 18179f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt SkASSERT(GrProgramDesc::Less(a, b)); 18279f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt SkASSERT(!GrProgramDesc::Less(b, a)); 1832db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1842db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#endif 185c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com } 186f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 1872db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fHashTable[hashIdx] = entry; 188c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com entry->fLRUStamp = fCurrLRUStamp; 1892db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 1902db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (SK_MaxU32 == fCurrLRUStamp) { 191c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com // wrap around! just trash our LRU, one time hit. 192c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com for (int i = 0; i < fCount; ++i) { 1932db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[i]->fLRUStamp = 0; 194f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 195f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 196c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com ++fCurrLRUStamp; 1976df86409ca586c3cb34f616f03501bd96181f9e4bsalomon return SkRef(entry->fProgram.get()); 198c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com} 199