junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 8 | #include "GrGLGpu.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | #include "builders/GrGLProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrProcessor.h" |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 12 | #include "GrGLFragmentProcessor.h" |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame] | 13 | #include "GrGLPathRendering.h" |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 14 | #include "SkRTConf.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 15 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 16 | |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 17 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 18 | SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false, |
| 19 | "Display program cache usage."); |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 20 | #endif |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 21 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 22 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 23 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 24 | struct GrGLGpu::ProgramCache::Entry { |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 25 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 26 | Entry() : fProgram(nullptr), fLRUStamp(0) {} |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 27 | |
| 28 | SkAutoTUnref<GrGLProgram> fProgram; |
| 29 | unsigned int fLRUStamp; |
| 30 | }; |
| 31 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 32 | struct GrGLGpu::ProgramCache::ProgDescLess { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 33 | bool operator() (const GrProgramDesc& desc, const Entry* entry) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 34 | SkASSERT(entry->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 35 | return GrProgramDesc::Less(desc, entry->fProgram->getDesc()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 36 | } |
| 37 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 38 | bool operator() (const Entry* entry, const GrProgramDesc& desc) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 39 | SkASSERT(entry->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 40 | return GrProgramDesc::Less(entry->fProgram->getDesc(), desc); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 41 | } |
| 42 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 43 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 44 | GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 45 | : fCount(0) |
| 46 | , fCurrLRUStamp(0) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 47 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 48 | #ifdef PROGRAM_CACHE_STATS |
| 49 | , fTotalRequests(0) |
| 50 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 51 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 52 | #endif |
| 53 | { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 54 | for (int i = 0; i < 1 << kHashBits; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 55 | fHashTable[i] = nullptr; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 56 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 57 | } |
| 58 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 59 | GrGLGpu::ProgramCache::~ProgramCache() { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 60 | for (int i = 0; i < fCount; ++i){ |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 61 | delete fEntries[i]; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 62 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 63 | // dump stats |
| 64 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 65 | if (c_DisplayCache) { |
| 66 | SkDebugf("--- Program Cache ---\n"); |
| 67 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 68 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 69 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 70 | 100.f * fCacheMisses / fTotalRequests : |
| 71 | 0.f); |
| 72 | int cacheHits = fTotalRequests - fCacheMisses; |
| 73 | SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f); |
| 74 | SkDebugf("---------------------\n"); |
| 75 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 76 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 77 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 78 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 79 | void GrGLGpu::ProgramCache::abandon() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 80 | for (int i = 0; i < fCount; ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 81 | SkASSERT(fEntries[i]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 82 | fEntries[i]->fProgram->abandon(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 83 | delete fEntries[i]; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 84 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 85 | fCount = 0; |
| 86 | } |
| 87 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 88 | int GrGLGpu::ProgramCache::search(const GrProgramDesc& desc) const { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 89 | ProgDescLess less; |
| 90 | return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); |
| 91 | } |
| 92 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 93 | GrGLProgram* GrGLGpu::ProgramCache::refProgram(const DrawArgs& args) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 94 | #ifdef PROGRAM_CACHE_STATS |
| 95 | ++fTotalRequests; |
| 96 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 97 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 98 | Entry* entry = nullptr; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 99 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 100 | uint32_t hashIdx = args.fDesc->getChecksum(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 101 | hashIdx ^= hashIdx >> 16; |
| 102 | if (kHashBits <= 8) { |
| 103 | hashIdx ^= hashIdx >> 8; |
| 104 | } |
| 105 | hashIdx &=((1 << kHashBits) - 1); |
| 106 | Entry* hashedEntry = fHashTable[hashIdx]; |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 107 | if (hashedEntry && hashedEntry->fProgram->getDesc() == *args.fDesc) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 108 | SkASSERT(hashedEntry->fProgram); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 109 | entry = hashedEntry; |
| 110 | } |
| 111 | |
| 112 | int entryIdx; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 113 | if (nullptr == entry) { |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 114 | entryIdx = this->search(*args.fDesc); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 115 | if (entryIdx >= 0) { |
| 116 | entry = fEntries[entryIdx]; |
| 117 | #ifdef PROGRAM_CACHE_STATS |
| 118 | ++fHashMisses; |
| 119 | #endif |
| 120 | } |
| 121 | } |
| 122 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 123 | if (nullptr == entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 124 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 125 | #ifdef PROGRAM_CACHE_STATS |
| 126 | ++fCacheMisses; |
| 127 | #endif |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 128 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(args, fGpu); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 129 | if (nullptr == program) { |
| 130 | return nullptr; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 131 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 132 | int purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 133 | if (fCount < kMaxEntries) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 134 | entry = new Entry; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 135 | purgeIdx = fCount++; |
| 136 | fEntries[purgeIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 137 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 138 | SkASSERT(fCount == kMaxEntries); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 139 | purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 140 | for (int i = 1; i < kMaxEntries; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 141 | if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { |
| 142 | purgeIdx = i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 143 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 144 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 145 | entry = fEntries[purgeIdx]; |
| 146 | int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); |
| 147 | if (fHashTable[purgedHashIdx] == entry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 148 | fHashTable[purgedHashIdx] = nullptr; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 149 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 150 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 151 | SkASSERT(fEntries[purgeIdx] == entry); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 152 | entry->fProgram.reset(program); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 153 | // We need to shift fEntries around so that the entry currently at purgeIdx is placed |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 154 | // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). |
| 155 | entryIdx = ~entryIdx; |
| 156 | if (entryIdx < purgeIdx) { |
| 157 | // Let E and P be the entries at index entryIdx and purgeIdx, respectively. |
| 158 | // If the entries array looks like this: |
| 159 | // aaaaEbbbbbPccccc |
| 160 | // we rearrange it to look like this: |
| 161 | // aaaaPEbbbbbccccc |
| 162 | size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); |
| 163 | memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); |
| 164 | fEntries[entryIdx] = entry; |
| 165 | } else if (purgeIdx < entryIdx) { |
| 166 | // If the entries array looks like this: |
| 167 | // aaaaPbbbbbEccccc |
| 168 | // we rearrange it to look like this: |
| 169 | // aaaabbbbbPEccccc |
| 170 | size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); |
| 171 | memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); |
| 172 | fEntries[entryIdx - 1] = entry; |
| 173 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 174 | #ifdef SK_DEBUG |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 175 | SkASSERT(fEntries[0]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 176 | for (int i = 0; i < fCount - 1; ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 177 | SkASSERT(fEntries[i + 1]->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 178 | const GrProgramDesc& a = fEntries[i]->fProgram->getDesc(); |
| 179 | const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); |
| 180 | SkASSERT(GrProgramDesc::Less(a, b)); |
| 181 | SkASSERT(!GrProgramDesc::Less(b, a)); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 182 | } |
| 183 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 184 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 185 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 186 | fHashTable[hashIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 187 | entry->fLRUStamp = fCurrLRUStamp; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 188 | |
| 189 | if (SK_MaxU32 == fCurrLRUStamp) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 190 | // wrap around! just trash our LRU, one time hit. |
| 191 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 192 | fEntries[i]->fLRUStamp = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | ++fCurrLRUStamp; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 196 | return SkRef(entry->fProgram.get()); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 197 | } |