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 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 8 | #include "GrGpuGL.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" |
| 12 | #include "GrGLProcessor.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 "GrOptDrawState.h" |
| 15 | #include "SkRTConf.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 18 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 19 | SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false, |
| 20 | "Display program cache usage."); |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 21 | #endif |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 22 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 23 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 25 | struct GrGpuGL::ProgramCache::Entry { |
| 26 | SK_DECLARE_INST_COUNT_ROOT(Entry); |
| 27 | Entry() : fProgram(NULL), fLRUStamp(0) {} |
| 28 | |
| 29 | SkAutoTUnref<GrGLProgram> fProgram; |
| 30 | unsigned int fLRUStamp; |
| 31 | }; |
| 32 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 33 | struct GrGpuGL::ProgramCache::ProgDescLess { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 34 | bool operator() (const GrProgramDesc& desc, const Entry* entry) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 35 | SkASSERT(entry->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 36 | return GrProgramDesc::Less(desc, entry->fProgram->getDesc()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 37 | } |
| 38 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 39 | bool operator() (const Entry* entry, const GrProgramDesc& desc) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 40 | SkASSERT(entry->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 41 | return GrProgramDesc::Less(entry->fProgram->getDesc(), desc); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 42 | } |
| 43 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 45 | GrGpuGL::ProgramCache::ProgramCache(GrGpuGL* gpu) |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 46 | : fCount(0) |
| 47 | , fCurrLRUStamp(0) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 48 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 49 | #ifdef PROGRAM_CACHE_STATS |
| 50 | , fTotalRequests(0) |
| 51 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 52 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 53 | #endif |
| 54 | { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 55 | for (int i = 0; i < 1 << kHashBits; ++i) { |
| 56 | fHashTable[i] = NULL; |
| 57 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | GrGpuGL::ProgramCache::~ProgramCache() { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 61 | for (int i = 0; i < fCount; ++i){ |
| 62 | SkDELETE(fEntries[i]); |
| 63 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 64 | // dump stats |
| 65 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 66 | 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.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 77 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 78 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 79 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 80 | void GrGpuGL::ProgramCache::abandon() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 81 | for (int i = 0; i < fCount; ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 82 | SkASSERT(fEntries[i]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 83 | fEntries[i]->fProgram->abandon(); |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 84 | SkDELETE(fEntries[i]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 85 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 86 | fCount = 0; |
| 87 | } |
| 88 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 89 | int GrGpuGL::ProgramCache::search(const GrProgramDesc& desc) const { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 90 | ProgDescLess less; |
| 91 | return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); |
| 92 | } |
| 93 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 94 | GrGLProgram* GrGpuGL::ProgramCache::getProgram(const GrOptDrawState& optState, DrawType type) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 95 | #ifdef PROGRAM_CACHE_STATS |
| 96 | ++fTotalRequests; |
| 97 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 98 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 99 | Entry* entry = NULL; |
| 100 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 101 | uint32_t hashIdx = optState.programDesc().getChecksum(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 102 | hashIdx ^= hashIdx >> 16; |
| 103 | if (kHashBits <= 8) { |
| 104 | hashIdx ^= hashIdx >> 8; |
| 105 | } |
| 106 | hashIdx &=((1 << kHashBits) - 1); |
| 107 | Entry* hashedEntry = fHashTable[hashIdx]; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 108 | if (hashedEntry && hashedEntry->fProgram->getDesc() == optState.programDesc()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 109 | SkASSERT(hashedEntry->fProgram); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 110 | entry = hashedEntry; |
| 111 | } |
| 112 | |
| 113 | int entryIdx; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 114 | if (NULL == entry) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 115 | entryIdx = this->search(optState.programDesc()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 116 | if (entryIdx >= 0) { |
| 117 | entry = fEntries[entryIdx]; |
| 118 | #ifdef PROGRAM_CACHE_STATS |
| 119 | ++fHashMisses; |
| 120 | #endif |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (NULL == entry) { |
| 125 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 126 | #ifdef PROGRAM_CACHE_STATS |
| 127 | ++fCacheMisses; |
| 128 | #endif |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 129 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(optState, type, fGpu); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 130 | if (NULL == program) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 133 | int purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 134 | if (fCount < kMaxEntries) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 135 | entry = SkNEW(Entry); |
| 136 | purgeIdx = fCount++; |
| 137 | fEntries[purgeIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 138 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 139 | SkASSERT(fCount == kMaxEntries); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 140 | purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 141 | for (int i = 1; i < kMaxEntries; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 142 | if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { |
| 143 | purgeIdx = i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 144 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 145 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 146 | entry = fEntries[purgeIdx]; |
| 147 | int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); |
| 148 | if (fHashTable[purgedHashIdx] == entry) { |
| 149 | fHashTable[purgedHashIdx] = NULL; |
| 150 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 151 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 152 | SkASSERT(fEntries[purgeIdx] == entry); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 153 | entry->fProgram.reset(program); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 154 | // 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] | 155 | // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). |
| 156 | entryIdx = ~entryIdx; |
| 157 | if (entryIdx < purgeIdx) { |
| 158 | // Let E and P be the entries at index entryIdx and purgeIdx, respectively. |
| 159 | // If the entries array looks like this: |
| 160 | // aaaaEbbbbbPccccc |
| 161 | // we rearrange it to look like this: |
| 162 | // aaaaPEbbbbbccccc |
| 163 | size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); |
| 164 | memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); |
| 165 | fEntries[entryIdx] = entry; |
| 166 | } else if (purgeIdx < entryIdx) { |
| 167 | // If the entries array looks like this: |
| 168 | // aaaaPbbbbbEccccc |
| 169 | // we rearrange it to look like this: |
| 170 | // aaaabbbbbPEccccc |
| 171 | size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); |
| 172 | memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); |
| 173 | fEntries[entryIdx - 1] = entry; |
| 174 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 175 | #ifdef SK_DEBUG |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 176 | SkASSERT(fEntries[0]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 177 | for (int i = 0; i < fCount - 1; ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 178 | SkASSERT(fEntries[i + 1]->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 179 | const GrProgramDesc& a = fEntries[i]->fProgram->getDesc(); |
| 180 | const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); |
| 181 | SkASSERT(GrProgramDesc::Less(a, b)); |
| 182 | SkASSERT(!GrProgramDesc::Less(b, a)); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 183 | } |
| 184 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 185 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 186 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 187 | fHashTable[hashIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 188 | entry->fLRUStamp = fCurrLRUStamp; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 189 | |
| 190 | if (SK_MaxU32 == fCurrLRUStamp) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 191 | // wrap around! just trash our LRU, one time hit. |
| 192 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 193 | fEntries[i]->fLRUStamp = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | ++fCurrLRUStamp; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 197 | return entry->fProgram; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 198 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 199 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 202 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 203 | |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 204 | bool GrGpuGL::flushGraphicsState(DrawType type, |
| 205 | const ScissorState& scissorState, |
| 206 | const GrDeviceCoordTexture* dstCopy) { |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 207 | SkAutoTUnref<GrOptDrawState> optState(GrOptDrawState::Create(this->getDrawState(), |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 208 | this, |
| 209 | dstCopy, |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 210 | type)); |
| 211 | |
| 212 | if (!optState) { |
| 213 | return false; |
| 214 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 215 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 216 | // GrGpu::setupClipAndFlushState should have already checked this and bailed if not true. |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 217 | SkASSERT(optState->getRenderTarget()); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 218 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 219 | if (kStencilPath_DrawType == type) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 220 | const GrRenderTarget* rt = optState->getRenderTarget(); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 221 | SkISize size; |
| 222 | size.set(rt->width(), rt->height()); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 223 | this->glPathRendering()->setProjectionMatrix(optState->getViewMatrix(), size, rt->origin()); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 224 | } else { |
egdaniel | d632ea4 | 2014-09-24 10:30:12 -0700 | [diff] [blame] | 225 | this->flushMiscFixedFunctionState(*optState.get()); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 226 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 227 | GrBlendCoeff srcCoeff = optState->getSrcBlendCoeff(); |
| 228 | GrBlendCoeff dstCoeff = optState->getDstBlendCoeff(); |
| 229 | |
| 230 | // In these blend coeff's we end up drawing nothing so we can skip draw all together |
| 231 | if (kZero_GrBlendCoeff == srcCoeff && kOne_GrBlendCoeff == dstCoeff && |
| 232 | !optState->getStencil().doesWrite()) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 233 | return false; |
| 234 | } |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 235 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 236 | fCurrentProgram.reset(fProgramCache->getProgram(*optState.get(), type)); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 237 | if (NULL == fCurrentProgram.get()) { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 238 | SkDEBUGFAIL("Failed to create program!"); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 239 | return false; |
| 240 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 241 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 242 | fCurrentProgram.get()->ref(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 243 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 244 | GrGLuint programID = fCurrentProgram->programID(); |
| 245 | if (fHWProgramID != programID) { |
| 246 | GL_CALL(UseProgram(programID)); |
| 247 | fHWProgramID = programID; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 248 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 249 | |
egdaniel | d632ea4 | 2014-09-24 10:30:12 -0700 | [diff] [blame] | 250 | this->flushBlend(*optState.get(), kDrawLines_DrawType == type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 251 | |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 252 | fCurrentProgram->setData(*optState.get(), type, dstCopy); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 253 | } |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 254 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 255 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(optState->getRenderTarget()); |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 256 | this->flushStencil(optState->getStencil(), type); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 257 | this->flushScissor(scissorState, glRT->getViewport(), glRT->origin()); |
egdaniel | d632ea4 | 2014-09-24 10:30:12 -0700 | [diff] [blame] | 258 | this->flushAAState(*optState.get(), type); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 259 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 260 | SkIRect* devRect = NULL; |
| 261 | SkIRect devClipBounds; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 262 | if (optState->isClipState()) { |
| 263 | this->getClip()->getConservativeBounds(optState->getRenderTarget(), &devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 264 | devRect = &devClipBounds; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 265 | } |
| 266 | // This must come after textures are flushed because a texture may need |
| 267 | // to be msaa-resolved (which will modify bound FBO state). |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 268 | this->flushRenderTarget(glRT, devRect); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 269 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 273 | void GrGpuGL::setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes) { |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 274 | SkAutoTUnref<GrOptDrawState> optState( |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 275 | GrOptDrawState::Create(this->getDrawState(), this, info.getDstCopy(), |
egdaniel | b109ac2 | 2014-10-07 06:45:44 -0700 | [diff] [blame] | 276 | PrimTypeToDrawType(info.primitiveType()))); |
| 277 | |
| 278 | // If the optState would is NULL it should have been caught in flushGraphicsState before getting |
| 279 | // here. |
| 280 | SkASSERT(optState); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 281 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 282 | GrGLsizei stride = static_cast<GrGLsizei>(optState->getVertexStride()); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 283 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 284 | size_t vertexOffsetInBytes = stride * info.startVertex(); |
| 285 | |
| 286 | const GeometryPoolState& geoPoolState = this->getGeomPoolState(); |
| 287 | |
| 288 | GrGLVertexBuffer* vbuf; |
| 289 | switch (this->getGeomSrc().fVertexSrc) { |
| 290 | case kBuffer_GeometrySrcType: |
| 291 | vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; |
| 292 | break; |
| 293 | case kArray_GeometrySrcType: |
| 294 | case kReserved_GeometrySrcType: |
| 295 | this->finalizeReservedVertices(); |
| 296 | vertexOffsetInBytes += geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize; |
| 297 | vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer; |
| 298 | break; |
| 299 | default: |
| 300 | vbuf = NULL; // suppress warning |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 301 | SkFAIL("Unknown geometry src type!"); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 302 | } |
| 303 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 304 | SkASSERT(vbuf); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 305 | SkASSERT(!vbuf->isMapped()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 306 | vertexOffsetInBytes += vbuf->baseOffset(); |
| 307 | |
| 308 | GrGLIndexBuffer* ibuf = NULL; |
| 309 | if (info.isIndexed()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 310 | SkASSERT(indexOffsetInBytes); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 311 | |
| 312 | switch (this->getGeomSrc().fIndexSrc) { |
| 313 | case kBuffer_GeometrySrcType: |
| 314 | *indexOffsetInBytes = 0; |
| 315 | ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; |
| 316 | break; |
| 317 | case kArray_GeometrySrcType: |
| 318 | case kReserved_GeometrySrcType: |
| 319 | this->finalizeReservedIndices(); |
| 320 | *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort); |
| 321 | ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer; |
| 322 | break; |
| 323 | default: |
| 324 | ibuf = NULL; // suppress warning |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 325 | SkFAIL("Unknown geometry src type!"); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 326 | } |
| 327 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 328 | SkASSERT(ibuf); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 329 | SkASSERT(!ibuf->isMapped()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 330 | *indexOffsetInBytes += ibuf->baseOffset(); |
| 331 | } |
| 332 | GrGLAttribArrayState* attribState = |
| 333 | fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 334 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 335 | if (fCurrentProgram->hasVertexShader()) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 336 | int vertexAttribCount = optState->getVertexAttribCount(); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 337 | uint32_t usedAttribArraysMask = 0; |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 338 | const GrVertexAttrib* vertexAttrib = optState->getVertexAttribs(); |
egdaniel | 02cafcc | 2014-07-21 11:37:28 -0700 | [diff] [blame] | 339 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 340 | for (int vertexAttribIndex = 0; vertexAttribIndex < vertexAttribCount; |
| 341 | ++vertexAttribIndex, ++vertexAttrib) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 342 | usedAttribArraysMask |= (1 << vertexAttribIndex); |
| 343 | GrVertexAttribType attribType = vertexAttrib->fType; |
| 344 | attribState->set(this, |
| 345 | vertexAttribIndex, |
| 346 | vbuf, |
| 347 | GrGLAttribTypeToLayout(attribType).fCount, |
| 348 | GrGLAttribTypeToLayout(attribType).fType, |
| 349 | GrGLAttribTypeToLayout(attribType).fNormalized, |
| 350 | stride, |
| 351 | reinterpret_cast<GrGLvoid*>( |
commit-bot@chromium.org | 6ebfbf9 | 2014-02-24 12:05:02 +0000 | [diff] [blame] | 352 | vertexOffsetInBytes + vertexAttrib->fOffset)); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 353 | } |
commit-bot@chromium.org | 6ebfbf9 | 2014-02-24 12:05:02 +0000 | [diff] [blame] | 354 | attribState->disableUnusedArrays(this, usedAttribArraysMask); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 355 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 356 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 357 | |
| 358 | void GrGpuGL::buildProgramDesc(const GrOptDrawState& optState, |
| 359 | const GrProgramDesc::DescInfo& descInfo, |
| 360 | GrGpu::DrawType drawType, |
| 361 | const GrDeviceCoordTexture* dstCopy, |
| 362 | GrProgramDesc* desc) { |
| 363 | if (!GrGLProgramDescBuilder::Build(optState, descInfo, drawType, this, dstCopy, desc)) { |
| 364 | SkDEBUGFAIL("Failed to generate GL program descriptor"); |
| 365 | } |
| 366 | } |