blob: df8cf60c7b3cf0d04298f61fe9c5e80c1bf1fe69 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comf93e7172011-03-31 21:26:24 +00006 */
7
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
joshualitt47bb3822014-10-07 16:43:25 -070010#include "builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
egdaniel8a4c1032014-09-16 07:18:54 -070012#include "GrGLPathRendering.h"
egdaniel64c47282015-11-13 06:54:19 -080013#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel170f90b2014-09-16 12:54:40 -070015#include "SkRTConf.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016#include "SkTSearch.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000018#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000019SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false,
20 "Display program cache usage.");
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000021#endif
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000022
egdaniel018fb622015-10-28 07:26:40 -070023typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000024
bsalomon861e1032014-12-16 07:33:49 -080025struct GrGLGpu::ProgramCache::Entry {
mtklein2766c002015-06-26 11:45:03 -070026
halcanary96fcdcc2015-08-27 07:41:13 -070027 Entry() : fProgram(nullptr), fLRUStamp(0) {}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000028
29 SkAutoTUnref<GrGLProgram> fProgram;
30 unsigned int fLRUStamp;
31};
32
bsalomon861e1032014-12-16 07:33:49 -080033struct GrGLGpu::ProgramCache::ProgDescLess {
joshualitt79f8fae2014-10-28 17:59:26 -070034 bool operator() (const GrProgramDesc& desc, const Entry* entry) {
bsalomon49f085d2014-09-05 13:34:00 -070035 SkASSERT(entry->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -070036 return GrProgramDesc::Less(desc, entry->fProgram->getDesc());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000037 }
38
joshualitt79f8fae2014-10-28 17:59:26 -070039 bool operator() (const Entry* entry, const GrProgramDesc& desc) {
bsalomon49f085d2014-09-05 13:34:00 -070040 SkASSERT(entry->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -070041 return GrProgramDesc::Less(entry->fProgram->getDesc(), desc);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000042 }
43};
junov@google.comf93e7172011-03-31 21:26:24 +000044
bsalomon861e1032014-12-16 07:33:49 -080045GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu)
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000046 : fCount(0)
47 , fCurrLRUStamp(0)
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000048 , fGpu(gpu)
jvanverth@google.com94878772013-03-12 16:00:54 +000049#ifdef PROGRAM_CACHE_STATS
50 , fTotalRequests(0)
51 , fCacheMisses(0)
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000052 , fHashMisses(0)
jvanverth@google.com94878772013-03-12 16:00:54 +000053#endif
54{
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000055 for (int i = 0; i < 1 << kHashBits; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070056 fHashTable[i] = nullptr;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000057 }
jvanverth@google.com94878772013-03-12 16:00:54 +000058}
59
bsalomon861e1032014-12-16 07:33:49 -080060GrGLGpu::ProgramCache::~ProgramCache() {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000061 for (int i = 0; i < fCount; ++i){
halcanary385fe4d2015-08-26 13:07:48 -070062 delete fEntries[i];
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000063 }
jvanverth@google.com94878772013-03-12 16:00:54 +000064 // dump stats
65#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000066 if (c_DisplayCache) {
67 SkDebugf("--- Program Cache ---\n");
68 SkDebugf("Total requests: %d\n", fTotalRequests);
69 SkDebugf("Cache misses: %d\n", fCacheMisses);
70 SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ?
71 100.f * fCacheMisses / fTotalRequests :
72 0.f);
73 int cacheHits = fTotalRequests - fCacheMisses;
74 SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f);
75 SkDebugf("---------------------\n");
76 }
jvanverth@google.com94878772013-03-12 16:00:54 +000077#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000078}
junov@google.comf93e7172011-03-31 21:26:24 +000079
joshualitt8fd844f2015-12-02 13:36:47 -080080void GrGLGpu::ProgramCache::reset() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000081 for (int i = 0; i < fCount; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -070082 SkASSERT(fEntries[i]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000083 fEntries[i]->fProgram->abandon();
halcanary385fe4d2015-08-26 13:07:48 -070084 delete fEntries[i];
joshualitt8fd844f2015-12-02 13:36:47 -080085 fEntries[i] = nullptr;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000086 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000087 fCount = 0;
joshualitt8fd844f2015-12-02 13:36:47 -080088
89 // zero out hash table
90 for (int i = 0; i < 1 << kHashBits; i++) {
91 fHashTable[i] = nullptr;
92 }
93
94 fCurrLRUStamp = 0;
95#ifdef PROGRAM_CACHE_STATS
96 fTotalRequests = 0;
97 fCacheMisses = 0;
98 fHashMisses = 0;
99#endif
100}
101
102void GrGLGpu::ProgramCache::abandon() {
103 this->reset();
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000104}
105
bsalomon861e1032014-12-16 07:33:49 -0800106int GrGLGpu::ProgramCache::search(const GrProgramDesc& desc) const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000107 ProgDescLess less;
108 return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less);
109}
110
bsalomon6df86402015-06-01 10:41:49 -0700111GrGLProgram* GrGLGpu::ProgramCache::refProgram(const DrawArgs& args) {
jvanverth@google.com94878772013-03-12 16:00:54 +0000112#ifdef PROGRAM_CACHE_STATS
113 ++fTotalRequests;
114#endif
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000115
halcanary96fcdcc2015-08-27 07:41:13 -0700116 Entry* entry = nullptr;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000117
joshualitt873ad0e2015-01-20 09:08:51 -0800118 uint32_t hashIdx = args.fDesc->getChecksum();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000119 hashIdx ^= hashIdx >> 16;
120 if (kHashBits <= 8) {
121 hashIdx ^= hashIdx >> 8;
122 }
123 hashIdx &=((1 << kHashBits) - 1);
124 Entry* hashedEntry = fHashTable[hashIdx];
joshualitt873ad0e2015-01-20 09:08:51 -0800125 if (hashedEntry && hashedEntry->fProgram->getDesc() == *args.fDesc) {
bsalomon49f085d2014-09-05 13:34:00 -0700126 SkASSERT(hashedEntry->fProgram);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000127 entry = hashedEntry;
128 }
129
130 int entryIdx;
halcanary96fcdcc2015-08-27 07:41:13 -0700131 if (nullptr == entry) {
joshualitt873ad0e2015-01-20 09:08:51 -0800132 entryIdx = this->search(*args.fDesc);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000133 if (entryIdx >= 0) {
134 entry = fEntries[entryIdx];
135#ifdef PROGRAM_CACHE_STATS
136 ++fHashMisses;
137#endif
138 }
139 }
140
halcanary96fcdcc2015-08-27 07:41:13 -0700141 if (nullptr == entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000142 // We have a cache miss
jvanverth@google.com94878772013-03-12 16:00:54 +0000143#ifdef PROGRAM_CACHE_STATS
144 ++fCacheMisses;
145#endif
joshualitt873ad0e2015-01-20 09:08:51 -0800146 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(args, fGpu);
halcanary96fcdcc2015-08-27 07:41:13 -0700147 if (nullptr == program) {
148 return nullptr;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000149 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000150 int purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000151 if (fCount < kMaxEntries) {
halcanary385fe4d2015-08-26 13:07:48 -0700152 entry = new Entry;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000153 purgeIdx = fCount++;
154 fEntries[purgeIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000155 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000156 SkASSERT(fCount == kMaxEntries);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000157 purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000158 for (int i = 1; i < kMaxEntries; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000159 if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) {
160 purgeIdx = i;
junov@google.comf93e7172011-03-31 21:26:24 +0000161 }
junov@google.comf93e7172011-03-31 21:26:24 +0000162 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000163 entry = fEntries[purgeIdx];
164 int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1);
165 if (fHashTable[purgedHashIdx] == entry) {
halcanary96fcdcc2015-08-27 07:41:13 -0700166 fHashTable[purgedHashIdx] = nullptr;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000167 }
junov@google.comf93e7172011-03-31 21:26:24 +0000168 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000169 SkASSERT(fEntries[purgeIdx] == entry);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000170 entry->fProgram.reset(program);
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000171 // We need to shift fEntries around so that the entry currently at purgeIdx is placed
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000172 // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor).
173 entryIdx = ~entryIdx;
174 if (entryIdx < purgeIdx) {
175 // Let E and P be the entries at index entryIdx and purgeIdx, respectively.
176 // If the entries array looks like this:
177 // aaaaEbbbbbPccccc
178 // we rearrange it to look like this:
179 // aaaaPEbbbbbccccc
180 size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*);
181 memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize);
182 fEntries[entryIdx] = entry;
183 } else if (purgeIdx < entryIdx) {
184 // If the entries array looks like this:
185 // aaaaPbbbbbEccccc
186 // we rearrange it to look like this:
187 // aaaabbbbbPEccccc
188 size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*);
189 memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize);
190 fEntries[entryIdx - 1] = entry;
191 }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000192#ifdef SK_DEBUG
bsalomon49f085d2014-09-05 13:34:00 -0700193 SkASSERT(fEntries[0]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000194 for (int i = 0; i < fCount - 1; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700195 SkASSERT(fEntries[i + 1]->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -0700196 const GrProgramDesc& a = fEntries[i]->fProgram->getDesc();
197 const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc();
198 SkASSERT(GrProgramDesc::Less(a, b));
199 SkASSERT(!GrProgramDesc::Less(b, a));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000200 }
201#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000202 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000203
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000204 fHashTable[hashIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000205 entry->fLRUStamp = fCurrLRUStamp;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000206
207 if (SK_MaxU32 == fCurrLRUStamp) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000208 // wrap around! just trash our LRU, one time hit.
209 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000210 fEntries[i]->fLRUStamp = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000211 }
212 }
213 ++fCurrLRUStamp;
bsalomon6df86402015-06-01 10:41:49 -0700214 return SkRef(entry->fProgram.get());
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000215}