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 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 10 | #include "GrEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 11 | #include "GrGLEffect.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 12 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 14 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | struct GrGpuGL::ProgramCache::Entry { |
| 17 | SK_DECLARE_INST_COUNT_ROOT(Entry); |
| 18 | Entry() : fProgram(NULL), fLRUStamp(0) {} |
| 19 | |
| 20 | SkAutoTUnref<GrGLProgram> fProgram; |
| 21 | unsigned int fLRUStamp; |
| 22 | }; |
| 23 | |
| 24 | SK_DEFINE_INST_COUNT(GrGpuGL::ProgramCache::Entry); |
| 25 | |
| 26 | struct GrGpuGL::ProgramCache::ProgDescLess { |
| 27 | bool operator() (const GrGLProgramDesc& desc, const Entry* entry) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 28 | SkASSERT(NULL != entry->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 29 | return GrGLProgramDesc::Less(desc, entry->fProgram->getDesc()); |
| 30 | } |
| 31 | |
| 32 | bool operator() (const Entry* entry, const GrGLProgramDesc& desc) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 33 | SkASSERT(NULL != entry->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 34 | return GrGLProgramDesc::Less(entry->fProgram->getDesc(), desc); |
| 35 | } |
| 36 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 38 | GrGpuGL::ProgramCache::ProgramCache(GrGpuGL* gpu) |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 39 | : fCount(0) |
| 40 | , fCurrLRUStamp(0) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 41 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 42 | #ifdef PROGRAM_CACHE_STATS |
| 43 | , fTotalRequests(0) |
| 44 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 45 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 46 | #endif |
| 47 | { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 48 | for (int i = 0; i < 1 << kHashBits; ++i) { |
| 49 | fHashTable[i] = NULL; |
| 50 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | GrGpuGL::ProgramCache::~ProgramCache() { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 54 | for (int i = 0; i < fCount; ++i){ |
| 55 | SkDELETE(fEntries[i]); |
| 56 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 57 | // dump stats |
| 58 | #ifdef PROGRAM_CACHE_STATS |
| 59 | SkDebugf("--- Program Cache ---\n"); |
| 60 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 61 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 62 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 63 | 100.f * fCacheMisses / fTotalRequests : |
| 64 | 0.f); |
| 65 | int cacheHits = fTotalRequests - fCacheMisses; |
| 66 | SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f); |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 67 | SkDebugf("---------------------\n"); |
| 68 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 69 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 70 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 71 | void GrGpuGL::ProgramCache::abandon() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 72 | for (int i = 0; i < fCount; ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 73 | SkASSERT(NULL != fEntries[i]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 74 | fEntries[i]->fProgram->abandon(); |
| 75 | fEntries[i]->fProgram.reset(NULL); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 76 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 77 | fCount = 0; |
| 78 | } |
| 79 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 80 | int GrGpuGL::ProgramCache::search(const GrGLProgramDesc& desc) const { |
| 81 | ProgDescLess less; |
| 82 | return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); |
| 83 | } |
| 84 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 85 | GrGLProgram* GrGpuGL::ProgramCache::getProgram(const GrGLProgramDesc& desc, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 86 | const GrEffectStage* colorStages[], |
| 87 | const GrEffectStage* coverageStages[]) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 88 | #ifdef PROGRAM_CACHE_STATS |
| 89 | ++fTotalRequests; |
| 90 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 92 | Entry* entry = NULL; |
| 93 | |
| 94 | uint32_t hashIdx = desc.getChecksum(); |
| 95 | hashIdx ^= hashIdx >> 16; |
| 96 | if (kHashBits <= 8) { |
| 97 | hashIdx ^= hashIdx >> 8; |
| 98 | } |
| 99 | hashIdx &=((1 << kHashBits) - 1); |
| 100 | Entry* hashedEntry = fHashTable[hashIdx]; |
| 101 | if (NULL != hashedEntry && hashedEntry->fProgram->getDesc() == desc) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 102 | SkASSERT(NULL != hashedEntry->fProgram); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 103 | entry = hashedEntry; |
| 104 | } |
| 105 | |
| 106 | int entryIdx; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 107 | if (NULL == entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 108 | entryIdx = this->search(desc); |
| 109 | if (entryIdx >= 0) { |
| 110 | entry = fEntries[entryIdx]; |
| 111 | #ifdef PROGRAM_CACHE_STATS |
| 112 | ++fHashMisses; |
| 113 | #endif |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (NULL == entry) { |
| 118 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 119 | #ifdef PROGRAM_CACHE_STATS |
| 120 | ++fCacheMisses; |
| 121 | #endif |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 122 | GrGLProgram* program = GrGLProgram::Create(fGpu, desc, colorStages, coverageStages); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 123 | if (NULL == program) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 126 | int purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 127 | if (fCount < kMaxEntries) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 128 | entry = SkNEW(Entry); |
| 129 | purgeIdx = fCount++; |
| 130 | fEntries[purgeIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 131 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 132 | SkASSERT(fCount == kMaxEntries); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 133 | purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 134 | for (int i = 1; i < kMaxEntries; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 135 | if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { |
| 136 | purgeIdx = i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 137 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 138 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 139 | entry = fEntries[purgeIdx]; |
| 140 | int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); |
| 141 | if (fHashTable[purgedHashIdx] == entry) { |
| 142 | fHashTable[purgedHashIdx] = NULL; |
| 143 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 144 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 145 | SkASSERT(fEntries[purgeIdx] == entry); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 146 | entry->fProgram.reset(program); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 147 | // 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] | 148 | // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). |
| 149 | entryIdx = ~entryIdx; |
| 150 | if (entryIdx < purgeIdx) { |
| 151 | // Let E and P be the entries at index entryIdx and purgeIdx, respectively. |
| 152 | // If the entries array looks like this: |
| 153 | // aaaaEbbbbbPccccc |
| 154 | // we rearrange it to look like this: |
| 155 | // aaaaPEbbbbbccccc |
| 156 | size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); |
| 157 | memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); |
| 158 | fEntries[entryIdx] = entry; |
| 159 | } else if (purgeIdx < entryIdx) { |
| 160 | // If the entries array looks like this: |
| 161 | // aaaaPbbbbbEccccc |
| 162 | // we rearrange it to look like this: |
| 163 | // aaaabbbbbPEccccc |
| 164 | size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); |
| 165 | memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); |
| 166 | fEntries[entryIdx - 1] = entry; |
| 167 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 168 | #ifdef SK_DEBUG |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 169 | SkASSERT(NULL != fEntries[0]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 170 | for (int i = 0; i < fCount - 1; ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 171 | SkASSERT(NULL != fEntries[i + 1]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 172 | const GrGLProgramDesc& a = fEntries[i]->fProgram->getDesc(); |
| 173 | const GrGLProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 174 | SkASSERT(GrGLProgramDesc::Less(a, b)); |
| 175 | SkASSERT(!GrGLProgramDesc::Less(b, a)); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 178 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 180 | fHashTable[hashIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 181 | entry->fLRUStamp = fCurrLRUStamp; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 182 | |
| 183 | if (SK_MaxU32 == fCurrLRUStamp) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 184 | // wrap around! just trash our LRU, one time hit. |
| 185 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 186 | fEntries[i]->fLRUStamp = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | ++fCurrLRUStamp; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 190 | return entry->fProgram; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 191 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 192 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 193 | //////////////////////////////////////////////////////////////////////////////// |
| 194 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 195 | void GrGpuGL::abandonResources(){ |
| 196 | INHERITED::abandonResources(); |
| 197 | fProgramCache->abandon(); |
| 198 | fHWProgramID = 0; |
| 199 | } |
| 200 | |
| 201 | //////////////////////////////////////////////////////////////////////////////// |
| 202 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 203 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 204 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 205 | void GrGpuGL::flushPathStencilMatrix() { |
| 206 | const SkMatrix& viewMatrix = this->getDrawState().getViewMatrix(); |
| 207 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
| 208 | SkISize size; |
| 209 | size.set(rt->width(), rt->height()); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 210 | const SkMatrix& vm = this->getDrawState().getViewMatrix(); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 211 | |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 212 | if (fHWProjectionMatrixState.fRenderTargetOrigin != rt->origin() || |
| 213 | !fHWProjectionMatrixState.fViewMatrix.cheapEqualTo(viewMatrix) || |
| 214 | fHWProjectionMatrixState.fRenderTargetSize!= size) { |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 215 | |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 216 | fHWProjectionMatrixState.fViewMatrix = vm; |
| 217 | fHWProjectionMatrixState.fRenderTargetSize = size; |
| 218 | fHWProjectionMatrixState.fRenderTargetOrigin = rt->origin(); |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame^] | 219 | |
| 220 | GrGLfloat projectionMatrix[4 * 4]; |
| 221 | fHWProjectionMatrixState.getGLMatrix<4>(projectionMatrix); |
| 222 | GL_CALL(MatrixMode(GR_GL_PROJECTION)); |
| 223 | GL_CALL(LoadMatrixf(projectionMatrix)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 224 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 227 | bool GrGpuGL::flushGraphicsState(DrawType type, const GrDeviceCoordTexture* dstCopy) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 228 | const GrDrawState& drawState = this->getDrawState(); |
| 229 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 230 | // GrGpu::setupClipAndFlushState should have already checked this and bailed if not true. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 231 | SkASSERT(NULL != drawState.getRenderTarget()); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 232 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 233 | if (kStencilPath_DrawType == type) { |
| 234 | this->flushPathStencilMatrix(); |
| 235 | } else { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 236 | this->flushMiscFixedFunctionState(); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 237 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 238 | GrBlendCoeff srcCoeff; |
| 239 | GrBlendCoeff dstCoeff; |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 240 | GrDrawState::BlendOptFlags blendOpts = drawState.getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 241 | if (GrDrawState::kSkipDraw_BlendOptFlag & blendOpts) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 242 | return false; |
| 243 | } |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 244 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 245 | SkSTArray<8, const GrEffectStage*, true> colorStages; |
| 246 | SkSTArray<8, const GrEffectStage*, true> coverageStages; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 247 | GrGLProgramDesc desc; |
| 248 | GrGLProgramDesc::Build(this->getDrawState(), |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 249 | kDrawPoints_DrawType == type, |
| 250 | blendOpts, |
| 251 | srcCoeff, |
| 252 | dstCoeff, |
| 253 | this, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 254 | dstCopy, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 255 | &colorStages, |
| 256 | &coverageStages, |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 257 | &desc); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 258 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 259 | fCurrentProgram.reset(fProgramCache->getProgram(desc, |
| 260 | colorStages.begin(), |
| 261 | coverageStages.begin())); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 262 | if (NULL == fCurrentProgram.get()) { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 263 | SkDEBUGFAIL("Failed to create program!"); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 264 | return false; |
| 265 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 266 | fCurrentProgram.get()->ref(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 267 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 268 | GrGLuint programID = fCurrentProgram->programID(); |
| 269 | if (fHWProgramID != programID) { |
| 270 | GL_CALL(UseProgram(programID)); |
| 271 | fHWProgramID = programID; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 272 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 273 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 274 | fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 275 | this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 276 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 277 | fCurrentProgram->setData(blendOpts, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 278 | colorStages.begin(), |
| 279 | coverageStages.begin(), |
| 280 | dstCopy, |
| 281 | &fSharedGLProgramState); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 282 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 283 | this->flushStencil(type); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 284 | this->flushScissor(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 285 | this->flushAAState(type); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 286 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 287 | SkIRect* devRect = NULL; |
| 288 | SkIRect devClipBounds; |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 289 | if (drawState.isClipState()) { |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 290 | this->getClip()->getConservativeBounds(drawState.getRenderTarget(), &devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 291 | devRect = &devClipBounds; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 292 | } |
| 293 | // This must come after textures are flushed because a texture may need |
| 294 | // to be msaa-resolved (which will modify bound FBO state). |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 295 | this->flushRenderTarget(devRect); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 296 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 300 | void GrGpuGL::setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 301 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 302 | GrGLsizei stride = this->getDrawState().getVertexSize(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 303 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 304 | size_t vertexOffsetInBytes = stride * info.startVertex(); |
| 305 | |
| 306 | const GeometryPoolState& geoPoolState = this->getGeomPoolState(); |
| 307 | |
| 308 | GrGLVertexBuffer* vbuf; |
| 309 | switch (this->getGeomSrc().fVertexSrc) { |
| 310 | case kBuffer_GeometrySrcType: |
| 311 | vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; |
| 312 | break; |
| 313 | case kArray_GeometrySrcType: |
| 314 | case kReserved_GeometrySrcType: |
| 315 | this->finalizeReservedVertices(); |
| 316 | vertexOffsetInBytes += geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize; |
| 317 | vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer; |
| 318 | break; |
| 319 | default: |
| 320 | vbuf = NULL; // suppress warning |
| 321 | GrCrash("Unknown geometry src type!"); |
| 322 | } |
| 323 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 324 | SkASSERT(NULL != vbuf); |
| 325 | SkASSERT(!vbuf->isLocked()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 326 | vertexOffsetInBytes += vbuf->baseOffset(); |
| 327 | |
| 328 | GrGLIndexBuffer* ibuf = NULL; |
| 329 | if (info.isIndexed()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 330 | SkASSERT(NULL != indexOffsetInBytes); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 331 | |
| 332 | switch (this->getGeomSrc().fIndexSrc) { |
| 333 | case kBuffer_GeometrySrcType: |
| 334 | *indexOffsetInBytes = 0; |
| 335 | ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; |
| 336 | break; |
| 337 | case kArray_GeometrySrcType: |
| 338 | case kReserved_GeometrySrcType: |
| 339 | this->finalizeReservedIndices(); |
| 340 | *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort); |
| 341 | ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer; |
| 342 | break; |
| 343 | default: |
| 344 | ibuf = NULL; // suppress warning |
| 345 | GrCrash("Unknown geometry src type!"); |
| 346 | } |
| 347 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 348 | SkASSERT(NULL != ibuf); |
| 349 | SkASSERT(!ibuf->isLocked()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 350 | *indexOffsetInBytes += ibuf->baseOffset(); |
| 351 | } |
| 352 | GrGLAttribArrayState* attribState = |
| 353 | fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 354 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 355 | uint32_t usedAttribArraysMask = 0; |
| 356 | const GrVertexAttrib* vertexAttrib = this->getDrawState().getVertexAttribs(); |
| 357 | int vertexAttribCount = this->getDrawState().getVertexAttribCount(); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 358 | for (int vertexAttribIndex = 0; vertexAttribIndex < vertexAttribCount; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 359 | ++vertexAttribIndex, ++vertexAttrib) { |
robertphillips@google.com | af3a3b9 | 2013-02-28 23:08:28 +0000 | [diff] [blame] | 360 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 361 | usedAttribArraysMask |= (1 << vertexAttribIndex); |
| 362 | GrVertexAttribType attribType = vertexAttrib->fType; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 363 | attribState->set(this, |
| 364 | vertexAttribIndex, |
| 365 | vbuf, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 366 | GrGLAttribTypeToLayout(attribType).fCount, |
| 367 | GrGLAttribTypeToLayout(attribType).fType, |
| 368 | GrGLAttribTypeToLayout(attribType).fNormalized, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 369 | stride, |
| 370 | reinterpret_cast<GrGLvoid*>( |
| 371 | vertexOffsetInBytes + vertexAttrib->fOffset)); |
| 372 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 373 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 374 | attribState->disableUnusedAttribArrays(this, usedAttribArraysMask); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 375 | } |