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 808fd844f38ed46eaac78bb187623ffcc40b592707joshualittvoid GrGLGpu::ProgramCache::reset() { 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]; 858fd844f38ed46eaac78bb187623ffcc40b592707joshualitt fEntries[i] = nullptr; 86f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 87c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com fCount = 0; 888fd844f38ed46eaac78bb187623ffcc40b592707joshualitt 898fd844f38ed46eaac78bb187623ffcc40b592707joshualitt // zero out hash table 908fd844f38ed46eaac78bb187623ffcc40b592707joshualitt for (int i = 0; i < 1 << kHashBits; i++) { 918fd844f38ed46eaac78bb187623ffcc40b592707joshualitt fHashTable[i] = nullptr; 928fd844f38ed46eaac78bb187623ffcc40b592707joshualitt } 938fd844f38ed46eaac78bb187623ffcc40b592707joshualitt 948fd844f38ed46eaac78bb187623ffcc40b592707joshualitt fCurrLRUStamp = 0; 958fd844f38ed46eaac78bb187623ffcc40b592707joshualitt#ifdef PROGRAM_CACHE_STATS 968fd844f38ed46eaac78bb187623ffcc40b592707joshualitt fTotalRequests = 0; 978fd844f38ed46eaac78bb187623ffcc40b592707joshualitt fCacheMisses = 0; 988fd844f38ed46eaac78bb187623ffcc40b592707joshualitt fHashMisses = 0; 998fd844f38ed46eaac78bb187623ffcc40b592707joshualitt#endif 1008fd844f38ed46eaac78bb187623ffcc40b592707joshualitt} 1018fd844f38ed46eaac78bb187623ffcc40b592707joshualitt 1028fd844f38ed46eaac78bb187623ffcc40b592707joshualittvoid GrGLGpu::ProgramCache::abandon() { 1038fd844f38ed46eaac78bb187623ffcc40b592707joshualitt this->reset(); 104c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com} 105f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 106861e1037017bbb7ac52ec5ebecab3a636a82a3e8bsalomonint GrGLGpu::ProgramCache::search(const GrProgramDesc& desc) const { 1072db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com ProgDescLess less; 1082db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); 1092db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com} 1102db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 1116df86409ca586c3cb34f616f03501bd96181f9e4bsalomonGrGLProgram* GrGLGpu::ProgramCache::refProgram(const DrawArgs& args) { 112948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#ifdef PROGRAM_CACHE_STATS 113948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com ++fTotalRequests; 114948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#endif 1159ba4fa6f0fb8ef496d81ccac36e780aa806fea83bsalomon@google.com 11696fcdcc219d2a0d3579719b84b28bede76efba64halcanary Entry* entry = nullptr; 1172db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 118873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt uint32_t hashIdx = args.fDesc->getChecksum(); 1192db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com hashIdx ^= hashIdx >> 16; 1202db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (kHashBits <= 8) { 1212db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com hashIdx ^= hashIdx >> 8; 1222db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1232db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com hashIdx &=((1 << kHashBits) - 1); 1242db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com Entry* hashedEntry = fHashTable[hashIdx]; 125873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt if (hashedEntry && hashedEntry->fProgram->getDesc() == *args.fDesc) { 12649f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(hashedEntry->fProgram); 1272db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry = hashedEntry; 1282db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1292db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 1302db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com int entryIdx; 13196fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (nullptr == entry) { 132873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt entryIdx = this->search(*args.fDesc); 1332db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (entryIdx >= 0) { 1342db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry = fEntries[entryIdx]; 1352db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#ifdef PROGRAM_CACHE_STATS 1362db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com ++fHashMisses; 1372db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#endif 1382db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1392db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 1402db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 14196fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (nullptr == entry) { 1422db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // We have a cache miss 143948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#ifdef PROGRAM_CACHE_STATS 144948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com ++fCacheMisses; 145948787737b77555318fa2433e7ff941516fe950ejvanverth@google.com#endif 146873ad0e0b4d67bdc7bad025018f597450e7004c6joshualitt GrGLProgram* program = GrGLProgramBuilder::CreateProgram(args, fGpu); 14796fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (nullptr == program) { 14896fcdcc219d2a0d3579719b84b28bede76efba64halcanary return nullptr; 149c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com } 1502db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com int purgeIdx = 0; 151c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com if (fCount < kMaxEntries) { 152385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary entry = new Entry; 1532db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com purgeIdx = fCount++; 1542db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[purgeIdx] = entry; 155c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com } else { 156f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(fCount == kMaxEntries); 1572db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com purgeIdx = 0; 158c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com for (int i = 1; i < kMaxEntries; ++i) { 1592db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { 1602db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com purgeIdx = i; 161f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 162f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 1632db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry = fEntries[purgeIdx]; 1642db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); 1652db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (fHashTable[purgedHashIdx] == entry) { 16696fcdcc219d2a0d3579719b84b28bede76efba64halcanary fHashTable[purgedHashIdx] = nullptr; 1672db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 168f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 169f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org SkASSERT(fEntries[purgeIdx] == entry); 1702db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entry->fProgram.reset(program); 1712d816ad36e806e5b1cf3c447e547829bbbe74fd1skia.committer@gmail.com // We need to shift fEntries around so that the entry currently at purgeIdx is placed 1722db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). 1732db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com entryIdx = ~entryIdx; 1742db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (entryIdx < purgeIdx) { 1752db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // Let E and P be the entries at index entryIdx and purgeIdx, respectively. 1762db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // If the entries array looks like this: 1772db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaaEbbbbbPccccc 1782db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // we rearrange it to look like this: 1792db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaaPEbbbbbccccc 1802db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); 1812db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); 1822db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[entryIdx] = entry; 1832db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } else if (purgeIdx < entryIdx) { 1842db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // If the entries array looks like this: 1852db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaaPbbbbbEccccc 1862db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // we rearrange it to look like this: 1872db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com // aaaabbbbbPEccccc 1882db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); 1892db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); 1902db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[entryIdx - 1] = entry; 1912db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 192515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG 19349f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(fEntries[0]->fProgram.get()); 1942db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com for (int i = 0; i < fCount - 1; ++i) { 19549f085dddff10473b6ebf832a974288300224e60bsalomon SkASSERT(fEntries[i + 1]->fProgram.get()); 19679f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt const GrProgramDesc& a = fEntries[i]->fProgram->getDesc(); 19779f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); 19879f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt SkASSERT(GrProgramDesc::Less(a, b)); 19979f8faeea2692d2948c0f634e956d1e7fc8333e0joshualitt SkASSERT(!GrProgramDesc::Less(b, a)); 2002db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com } 2012db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#endif 202c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com } 203f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com 2042db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fHashTable[hashIdx] = entry; 205c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com entry->fLRUStamp = fCurrLRUStamp; 2062db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com 2072db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com if (SK_MaxU32 == fCurrLRUStamp) { 208c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com // wrap around! just trash our LRU, one time hit. 209c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com for (int i = 0; i < fCount; ++i) { 2102db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com fEntries[i]->fLRUStamp = 0; 211f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 212f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.com } 213c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com ++fCurrLRUStamp; 2146df86409ca586c3cb34f616f03501bd96181f9e4bsalomon return SkRef(entry->fProgram.get()); 215c1d2a58ec8510b226e080f5415a05723a686aab3bsalomon@google.com} 216