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