blob: d74b54aebab7604d118b465ecf0330a105dced7c [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"
wangyix6af0c932015-07-22 10:21:17 -070012#include "GrGLFragmentProcessor.h"
egdaniel8a4c1032014-09-16 07:18:54 -070013#include "GrGLPathRendering.h"
egdaniel170f90b2014-09-16 12:54:40 -070014#include "SkRTConf.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015#include "SkTSearch.h"
junov@google.comf93e7172011-03-31 21:26:24 +000016
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000017#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000018SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false,
19 "Display program cache usage.");
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000020#endif
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000021
kkinnunen7510b222014-07-30 00:04:16 -070022typedef GrGLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000023
bsalomon861e1032014-12-16 07:33:49 -080024struct GrGLGpu::ProgramCache::Entry {
mtklein2766c002015-06-26 11:45:03 -070025
halcanary96fcdcc2015-08-27 07:41:13 -070026 Entry() : fProgram(nullptr), fLRUStamp(0) {}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000027
28 SkAutoTUnref<GrGLProgram> fProgram;
29 unsigned int fLRUStamp;
30};
31
bsalomon861e1032014-12-16 07:33:49 -080032struct GrGLGpu::ProgramCache::ProgDescLess {
joshualitt79f8fae2014-10-28 17:59:26 -070033 bool operator() (const GrProgramDesc& desc, const Entry* entry) {
bsalomon49f085d2014-09-05 13:34:00 -070034 SkASSERT(entry->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -070035 return GrProgramDesc::Less(desc, entry->fProgram->getDesc());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000036 }
37
joshualitt79f8fae2014-10-28 17:59:26 -070038 bool operator() (const Entry* entry, const GrProgramDesc& desc) {
bsalomon49f085d2014-09-05 13:34:00 -070039 SkASSERT(entry->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -070040 return GrProgramDesc::Less(entry->fProgram->getDesc(), desc);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000041 }
42};
junov@google.comf93e7172011-03-31 21:26:24 +000043
bsalomon861e1032014-12-16 07:33:49 -080044GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu)
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000045 : fCount(0)
46 , fCurrLRUStamp(0)
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000047 , fGpu(gpu)
jvanverth@google.com94878772013-03-12 16:00:54 +000048#ifdef PROGRAM_CACHE_STATS
49 , fTotalRequests(0)
50 , fCacheMisses(0)
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000051 , fHashMisses(0)
jvanverth@google.com94878772013-03-12 16:00:54 +000052#endif
53{
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000054 for (int i = 0; i < 1 << kHashBits; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070055 fHashTable[i] = nullptr;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000056 }
jvanverth@google.com94878772013-03-12 16:00:54 +000057}
58
bsalomon861e1032014-12-16 07:33:49 -080059GrGLGpu::ProgramCache::~ProgramCache() {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000060 for (int i = 0; i < fCount; ++i){
halcanary385fe4d2015-08-26 13:07:48 -070061 delete fEntries[i];
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000062 }
jvanverth@google.com94878772013-03-12 16:00:54 +000063 // dump stats
64#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000065 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.com94878772013-03-12 16:00:54 +000076#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000077}
junov@google.comf93e7172011-03-31 21:26:24 +000078
bsalomon861e1032014-12-16 07:33:49 -080079void GrGLGpu::ProgramCache::abandon() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000080 for (int i = 0; i < fCount; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -070081 SkASSERT(fEntries[i]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000082 fEntries[i]->fProgram->abandon();
halcanary385fe4d2015-08-26 13:07:48 -070083 delete fEntries[i];
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000084 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000085 fCount = 0;
86}
87
bsalomon861e1032014-12-16 07:33:49 -080088int GrGLGpu::ProgramCache::search(const GrProgramDesc& desc) const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000089 ProgDescLess less;
90 return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less);
91}
92
bsalomon6df86402015-06-01 10:41:49 -070093GrGLProgram* GrGLGpu::ProgramCache::refProgram(const DrawArgs& args) {
jvanverth@google.com94878772013-03-12 16:00:54 +000094#ifdef PROGRAM_CACHE_STATS
95 ++fTotalRequests;
96#endif
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000097
halcanary96fcdcc2015-08-27 07:41:13 -070098 Entry* entry = nullptr;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000099
joshualitt873ad0e2015-01-20 09:08:51 -0800100 uint32_t hashIdx = args.fDesc->getChecksum();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000101 hashIdx ^= hashIdx >> 16;
102 if (kHashBits <= 8) {
103 hashIdx ^= hashIdx >> 8;
104 }
105 hashIdx &=((1 << kHashBits) - 1);
106 Entry* hashedEntry = fHashTable[hashIdx];
joshualitt873ad0e2015-01-20 09:08:51 -0800107 if (hashedEntry && hashedEntry->fProgram->getDesc() == *args.fDesc) {
bsalomon49f085d2014-09-05 13:34:00 -0700108 SkASSERT(hashedEntry->fProgram);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000109 entry = hashedEntry;
110 }
111
112 int entryIdx;
halcanary96fcdcc2015-08-27 07:41:13 -0700113 if (nullptr == entry) {
joshualitt873ad0e2015-01-20 09:08:51 -0800114 entryIdx = this->search(*args.fDesc);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000115 if (entryIdx >= 0) {
116 entry = fEntries[entryIdx];
117#ifdef PROGRAM_CACHE_STATS
118 ++fHashMisses;
119#endif
120 }
121 }
122
halcanary96fcdcc2015-08-27 07:41:13 -0700123 if (nullptr == entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000124 // We have a cache miss
jvanverth@google.com94878772013-03-12 16:00:54 +0000125#ifdef PROGRAM_CACHE_STATS
126 ++fCacheMisses;
127#endif
joshualitt873ad0e2015-01-20 09:08:51 -0800128 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(args, fGpu);
halcanary96fcdcc2015-08-27 07:41:13 -0700129 if (nullptr == program) {
130 return nullptr;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000131 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000132 int purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000133 if (fCount < kMaxEntries) {
halcanary385fe4d2015-08-26 13:07:48 -0700134 entry = new Entry;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000135 purgeIdx = fCount++;
136 fEntries[purgeIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000137 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000138 SkASSERT(fCount == kMaxEntries);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000139 purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000140 for (int i = 1; i < kMaxEntries; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000141 if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) {
142 purgeIdx = i;
junov@google.comf93e7172011-03-31 21:26:24 +0000143 }
junov@google.comf93e7172011-03-31 21:26:24 +0000144 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000145 entry = fEntries[purgeIdx];
146 int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1);
147 if (fHashTable[purgedHashIdx] == entry) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 fHashTable[purgedHashIdx] = nullptr;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000149 }
junov@google.comf93e7172011-03-31 21:26:24 +0000150 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000151 SkASSERT(fEntries[purgeIdx] == entry);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000152 entry->fProgram.reset(program);
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000153 // We need to shift fEntries around so that the entry currently at purgeIdx is placed
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000154 // 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.org515dcd32013-08-28 14:17:03 +0000174#ifdef SK_DEBUG
bsalomon49f085d2014-09-05 13:34:00 -0700175 SkASSERT(fEntries[0]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000176 for (int i = 0; i < fCount - 1; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700177 SkASSERT(fEntries[i + 1]->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -0700178 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.com2db3ded2013-05-22 14:34:04 +0000182 }
183#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000184 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000185
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000186 fHashTable[hashIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000187 entry->fLRUStamp = fCurrLRUStamp;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000188
189 if (SK_MaxU32 == fCurrLRUStamp) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000190 // wrap around! just trash our LRU, one time hit.
191 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000192 fEntries[i]->fLRUStamp = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000193 }
194 }
195 ++fCurrLRUStamp;
bsalomon6df86402015-06-01 10:41:49 -0700196 return SkRef(entry->fProgram.get());
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000197}