Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/gpu/GrDrawState.cpp

Issue 508663002: Create an optimized draw state but not hooked in yet to gpu pipeline (Closed) Base URL: https://skia.googlesource.com/skia.git@drawBase
Patch Set: Review changes Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrDrawState.h" 8 #include "GrDrawState.h"
9
10 #include "GrDrawTargetCaps.h"
11 #include "GrOptDrawState.h"
9 #include "GrPaint.h" 12 #include "GrPaint.h"
10 #include "GrDrawTargetCaps.h"
11 13
12 //////////////////////////////////////////////////////////////////////////////s 14 //////////////////////////////////////////////////////////////////////////////s
13 15
16 GrOptDrawState* GrDrawState::createOptState() const {
17 if (NULL == fCachedOptState) {
18 fCachedOptState = SkNEW_ARGS(GrOptDrawState, (*this));
19 } else {
20 SkASSERT(GrOptDrawState(*this) == *fCachedOptState);
21 }
22 fCachedOptState->ref();
23 return fCachedOptState;
24 }
25
26 //////////////////////////////////////////////////////////////////////////////s
27
14 GrDrawState::CombinedState GrDrawState::CombineIfPossible( 28 GrDrawState::CombinedState GrDrawState::CombineIfPossible(
15 const GrDrawState& a, const GrDrawState& b, const GrDrawTargetCaps& caps) { 29 const GrDrawState& a, const GrDrawState& b, const GrDrawTargetCaps& caps) {
16 30
17 if (!a.isEqual(b)) { 31 if (!a.isEqual(b)) {
18 return kIncompatible_CombinedState; 32 return kIncompatible_CombinedState;
19 } 33 }
20 34
21 // If the general draw states are equal (from check above) we know hasColorV ertexAttribute() 35 // If the general draw states are equal (from check above) we know hasColorV ertexAttribute()
22 // is equivalent for both a and b 36 // is equivalent for both a and b
23 if (a.hasColorVertexAttribute()) { 37 if (a.hasColorVertexAttribute()) {
(...skipping 19 matching lines...) Expand all
43 } 57 }
44 } 58 }
45 return aIsOpaque ? kB_CombinedState : kA_CombinedState; 59 return aIsOpaque ? kB_CombinedState : kA_CombinedState;
46 } 60 }
47 } 61 }
48 return kAOrB_CombinedState; 62 return kAOrB_CombinedState;
49 } 63 }
50 64
51 //////////////////////////////////////////////////////////////////////////////s 65 //////////////////////////////////////////////////////////////////////////////s
52 66
53 GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatr ix) { 67 GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatr ix)
68 : fCachedOptState(NULL) {
54 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) 69 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
55 *this = state; 70 *this = state;
56 if (!preConcatMatrix.isIdentity()) { 71 if (!preConcatMatrix.isIdentity()) {
57 for (int i = 0; i < this->numColorStages(); ++i) { 72 for (int i = 0; i < this->numColorStages(); ++i) {
58 fColorStages[i].localCoordChange(preConcatMatrix); 73 fColorStages[i].localCoordChange(preConcatMatrix);
59 } 74 }
60 for (int i = 0; i < this->numCoverageStages(); ++i) { 75 for (int i = 0; i < this->numCoverageStages(); ++i) {
61 fCoverageStages[i].localCoordChange(preConcatMatrix); 76 fCoverageStages[i].localCoordChange(preConcatMatrix);
62 } 77 }
63 this->invalidateBlendOptFlags(); 78 this->invalidateOptState();
64 } 79 }
65 } 80 }
66 81
67 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { 82 GrDrawState& GrDrawState::operator=(const GrDrawState& that) {
68 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); 83 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
69 this->setRenderTarget(that.fRenderTarget.get()); 84 this->setRenderTarget(that.fRenderTarget.get());
70 fColor = that.fColor; 85 fColor = that.fColor;
71 fViewMatrix = that.fViewMatrix; 86 fViewMatrix = that.fViewMatrix;
72 fSrcBlend = that.fSrcBlend; 87 fSrcBlend = that.fSrcBlend;
73 fDstBlend = that.fDstBlend; 88 fDstBlend = that.fDstBlend;
74 fBlendConstant = that.fBlendConstant; 89 fBlendConstant = that.fBlendConstant;
75 fFlagBits = that.fFlagBits; 90 fFlagBits = that.fFlagBits;
76 fVACount = that.fVACount; 91 fVACount = that.fVACount;
77 fVAPtr = that.fVAPtr; 92 fVAPtr = that.fVAPtr;
78 fVertexSize = that.fVertexSize; 93 fVertexSize = that.fVertexSize;
79 fStencilSettings = that.fStencilSettings; 94 fStencilSettings = that.fStencilSettings;
80 fCoverage = that.fCoverage; 95 fCoverage = that.fCoverage;
81 fDrawFace = that.fDrawFace; 96 fDrawFace = that.fDrawFace;
82 fColorStages = that.fColorStages; 97 fColorStages = that.fColorStages;
83 fCoverageStages = that.fCoverageStages; 98 fCoverageStages = that.fCoverageStages;
84 fOptSrcBlend = that.fOptSrcBlend; 99 fOptSrcBlend = that.fOptSrcBlend;
85 fOptDstBlend = that.fOptDstBlend; 100 fOptDstBlend = that.fOptDstBlend;
86 fBlendOptFlags = that.fBlendOptFlags; 101 fBlendOptFlags = that.fBlendOptFlags;
87 102
88 fHints = that.fHints; 103 fHints = that.fHints;
89 104
105 SkRefCnt_SafeAssign(fCachedOptState, that.fCachedOptState);
106
90 memcpy(fFixedFunctionVertexAttribIndices, 107 memcpy(fFixedFunctionVertexAttribIndices,
91 that.fFixedFunctionVertexAttribIndices, 108 that.fFixedFunctionVertexAttribIndices,
92 sizeof(fFixedFunctionVertexAttribIndices)); 109 sizeof(fFixedFunctionVertexAttribIndices));
93 return *this; 110 return *this;
94 } 111 }
95 112
96 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { 113 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) {
97 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); 114 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
98 fColorStages.reset(); 115 fColorStages.reset();
99 fCoverageStages.reset(); 116 fCoverageStages.reset();
(...skipping 11 matching lines...) Expand all
111 fSrcBlend = kOne_GrBlendCoeff; 128 fSrcBlend = kOne_GrBlendCoeff;
112 fDstBlend = kZero_GrBlendCoeff; 129 fDstBlend = kZero_GrBlendCoeff;
113 fBlendConstant = 0x0; 130 fBlendConstant = 0x0;
114 fFlagBits = 0x0; 131 fFlagBits = 0x0;
115 fStencilSettings.setDisabled(); 132 fStencilSettings.setDisabled();
116 fCoverage = 0xff; 133 fCoverage = 0xff;
117 fDrawFace = kBoth_DrawFace; 134 fDrawFace = kBoth_DrawFace;
118 135
119 fHints = 0; 136 fHints = 0;
120 137
121 this->invalidateBlendOptFlags(); 138 this->invalidateOptState();
122 } 139 }
123 140
124 bool GrDrawState::setIdentityViewMatrix() { 141 bool GrDrawState::setIdentityViewMatrix() {
125 if (this->numTotalStages()) { 142 if (this->numTotalStages()) {
126 SkMatrix invVM; 143 SkMatrix invVM;
127 if (!fViewMatrix.invert(&invVM)) { 144 if (!fViewMatrix.invert(&invVM)) {
128 // sad trombone sound 145 // sad trombone sound
129 return false; 146 return false;
130 } 147 }
131 for (int s = 0; s < this->numColorStages(); ++s) { 148 for (int s = 0; s < this->numColorStages(); ++s) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 183
167 // Enable the clip bit 184 // Enable the clip bit
168 this->enableState(GrDrawState::kClip_StateBit); 185 this->enableState(GrDrawState::kClip_StateBit);
169 186
170 this->setColor(paint.getColor()); 187 this->setColor(paint.getColor());
171 this->setState(GrDrawState::kDither_StateBit, paint.isDither()); 188 this->setState(GrDrawState::kDither_StateBit, paint.isDither());
172 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); 189 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias());
173 190
174 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff()); 191 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff());
175 this->setCoverage(paint.getCoverage()); 192 this->setCoverage(paint.getCoverage());
176 this->invalidateBlendOptFlags(); 193 this->invalidateOptState();
177 } 194 }
178 195
179 //////////////////////////////////////////////////////////////////////////////// 196 ////////////////////////////////////////////////////////////////////////////////
180 197
181 static size_t vertex_size(const GrVertexAttrib* attribs, int count) { 198 static size_t vertex_size(const GrVertexAttrib* attribs, int count) {
182 // this works as long as we're 4 byte-aligned 199 // this works as long as we're 4 byte-aligned
183 #ifdef SK_DEBUG 200 #ifdef SK_DEBUG
184 uint32_t overlapCheck = 0; 201 uint32_t overlapCheck = 0;
185 #endif 202 #endif
186 SkASSERT(count <= GrRODrawState::kMaxVertexAttribCnt); 203 SkASSERT(count <= GrRODrawState::kMaxVertexAttribCnt);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 fFixedFunctionVertexAttribIndices[attribs[i].fBinding] = i; 241 fFixedFunctionVertexAttribIndices[attribs[i].fBinding] = i;
225 } 242 }
226 #ifdef SK_DEBUG 243 #ifdef SK_DEBUG
227 size_t dwordCount = GrVertexAttribTypeSize(attribs[i].fType) >> 2; 244 size_t dwordCount = GrVertexAttribTypeSize(attribs[i].fType) >> 2;
228 uint32_t mask = (1 << dwordCount)-1; 245 uint32_t mask = (1 << dwordCount)-1;
229 size_t offsetShift = attribs[i].fOffset >> 2; 246 size_t offsetShift = attribs[i].fOffset >> 2;
230 SkASSERT(!(overlapCheck & (mask << offsetShift))); 247 SkASSERT(!(overlapCheck & (mask << offsetShift)));
231 overlapCheck |= (mask << offsetShift); 248 overlapCheck |= (mask << offsetShift);
232 #endif 249 #endif
233 } 250 }
234 this->invalidateBlendOptFlags(); 251 this->invalidateOptState();
235 // Positions must be specified. 252 // Positions must be specified.
236 SkASSERT(-1 != fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBin ding]); 253 SkASSERT(-1 != fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBin ding]);
237 } 254 }
238 255
239 //////////////////////////////////////////////////////////////////////////////// 256 ////////////////////////////////////////////////////////////////////////////////
240 257
241 void GrDrawState::setDefaultVertexAttribs() { 258 void GrDrawState::setDefaultVertexAttribs() {
242 static const GrVertexAttrib kPositionAttrib = 259 static const GrVertexAttrib kPositionAttrib =
243 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}; 260 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding};
244 261
245 fVAPtr = &kPositionAttrib; 262 fVAPtr = &kPositionAttrib;
246 fVACount = 1; 263 fVACount = 1;
247 fVertexSize = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); 264 fVertexSize = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType);
248 265
249 // set all the fixed function indices to -1 except position. 266 // set all the fixed function indices to -1 except position.
250 memset(fFixedFunctionVertexAttribIndices, 267 memset(fFixedFunctionVertexAttribIndices,
251 0xff, 268 0xff,
252 sizeof(fFixedFunctionVertexAttribIndices)); 269 sizeof(fFixedFunctionVertexAttribIndices));
253 fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding] = 0; 270 fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding] = 0;
254 this->invalidateBlendOptFlags(); 271 this->invalidateOptState();
255 } 272 }
256 273
257 //////////////////////////////////////////////////////////////////////////////// 274 ////////////////////////////////////////////////////////////////////////////////
258 275
259 bool GrDrawState::couldApplyCoverage(const GrDrawTargetCaps& caps) const { 276 bool GrDrawState::couldApplyCoverage(const GrDrawTargetCaps& caps) const {
260 if (caps.dualSourceBlendingSupport()) { 277 if (caps.dualSourceBlendingSupport()) {
261 return true; 278 return true;
262 } 279 }
263 // we can correctly apply coverage if a) we have dual source blending 280 // we can correctly apply coverage if a) we have dual source blending
264 // or b) one of our blend optimizations applies 281 // or b) one of our blend optimizations applies
265 // or c) the src, dst blend coeffs are 1,0 and we will read Dst Color 282 // or c) the src, dst blend coeffs are 1,0 and we will read Dst Color
266 GrBlendCoeff srcCoeff; 283 GrBlendCoeff srcCoeff;
267 GrBlendCoeff dstCoeff; 284 GrBlendCoeff dstCoeff;
268 GrRODrawState::BlendOptFlags flag = this->getBlendOpts(true, &srcCoeff, &dst Coeff); 285 GrRODrawState::BlendOptFlags flag = this->getBlendOpts(true, &srcCoeff, &dst Coeff);
269 return GrRODrawState::kNone_BlendOpt != flag || 286 return GrRODrawState::kNone_BlendOpt != flag ||
270 (this->willEffectReadDstColor() && 287 (this->willEffectReadDstColor() &&
271 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff); 288 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff);
272 } 289 }
273 290
274 ////////////////////////////////////////////////////////////////////////////// 291 //////////////////////////////////////////////////////////////////////////////
275 292
276 GrDrawState::AutoVertexAttribRestore::AutoVertexAttribRestore( 293 GrDrawState::AutoVertexAttribRestore::AutoVertexAttribRestore(GrDrawState* drawS tate) {
277 GrDrawState* drawState) {
278 SkASSERT(NULL != drawState); 294 SkASSERT(NULL != drawState);
279 fDrawState = drawState; 295 fDrawState = drawState;
280 fVAPtr = drawState->fVAPtr; 296 fVAPtr = drawState->fVAPtr;
281 fVACount = drawState->fVACount; 297 fVACount = drawState->fVACount;
282 fDrawState->setDefaultVertexAttribs(); 298 fDrawState->setDefaultVertexAttribs();
283 } 299 }
284 300
285 //////////////////////////////////////////////////////////////////////////////s 301 //////////////////////////////////////////////////////////////////////////////s
286 302
287 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { 303 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) {
288 if (NULL != fDrawState) { 304 if (NULL != fDrawState) {
289 int m = fDrawState->numColorStages() - fColorEffectCnt; 305 int m = fDrawState->numColorStages() - fColorEffectCnt;
290 SkASSERT(m >= 0); 306 SkASSERT(m >= 0);
291 fDrawState->fColorStages.pop_back_n(m); 307 fDrawState->fColorStages.pop_back_n(m);
292 308
293 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; 309 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt;
294 SkASSERT(n >= 0); 310 SkASSERT(n >= 0);
295 fDrawState->fCoverageStages.pop_back_n(n); 311 fDrawState->fCoverageStages.pop_back_n(n);
296 if (m + n > 0) { 312 if (m + n > 0) {
297 fDrawState->invalidateBlendOptFlags(); 313 fDrawState->invalidateOptState();
298 } 314 }
299 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) 315 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
300 } 316 }
301 fDrawState = ds; 317 fDrawState = ds;
302 if (NULL != ds) { 318 if (NULL != ds) {
303 fColorEffectCnt = ds->numColorStages(); 319 fColorEffectCnt = ds->numColorStages();
304 fCoverageEffectCnt = ds->numCoverageStages(); 320 fCoverageEffectCnt = ds->numCoverageStages();
305 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) 321 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;)
306 } 322 }
307 } 323 }
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; 444 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages;
429 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); 445 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages);
430 446
431 int i = 0; 447 int i = 0;
432 for (int s = 0; s < fNumColorStages; ++s, ++i) { 448 for (int s = 0; s < fNumColorStages; ++s, ++i) {
433 fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i] ); 449 fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i] );
434 } 450 }
435 for (int s = 0; s < numCoverageStages; ++s, ++i) { 451 for (int s = 0; s < numCoverageStages; ++s, ++i) {
436 fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges [i]); 452 fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges [i]);
437 } 453 }
454 fDrawState->invalidateOptState();
438 fDrawState = NULL; 455 fDrawState = NULL;
439 } 456 }
440 } 457 }
441 458
442 void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, 459 void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
443 const SkMatrix& preconcatMatrix) { 460 const SkMatrix& preconcatMatrix) {
444 this->restore(); 461 this->restore();
445 462
446 SkASSERT(NULL == fDrawState); 463 SkASSERT(NULL == fDrawState);
447 if (NULL == drawState || preconcatMatrix.isIdentity()) { 464 if (NULL == drawState || preconcatMatrix.isIdentity()) {
448 return; 465 return;
449 } 466 }
450 fDrawState = drawState; 467 fDrawState = drawState;
451 468
452 fViewMatrix = drawState->getViewMatrix(); 469 fViewMatrix = drawState->getViewMatrix();
453 drawState->fViewMatrix.preConcat(preconcatMatrix); 470 drawState->fViewMatrix.preConcat(preconcatMatrix);
454 471
455 this->doEffectCoordChanges(preconcatMatrix); 472 this->doEffectCoordChanges(preconcatMatrix);
456 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) 473 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
474 drawState->invalidateOptState();
457 } 475 }
458 476
459 bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) { 477 bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) {
460 this->restore(); 478 this->restore();
461 479
462 if (NULL == drawState) { 480 if (NULL == drawState) {
463 return false; 481 return false;
464 } 482 }
465 483
466 if (drawState->getViewMatrix().isIdentity()) { 484 if (drawState->getViewMatrix().isIdentity()) {
467 return true; 485 return true;
468 } 486 }
469 487
488 drawState->invalidateOptState();
470 fViewMatrix = drawState->getViewMatrix(); 489 fViewMatrix = drawState->getViewMatrix();
471 if (0 == drawState->numTotalStages()) { 490 if (0 == drawState->numTotalStages()) {
472 drawState->fViewMatrix.reset(); 491 drawState->fViewMatrix.reset();
473 fDrawState = drawState; 492 fDrawState = drawState;
474 fNumColorStages = 0; 493 fNumColorStages = 0;
475 fSavedCoordChanges.reset(0); 494 fSavedCoordChanges.reset(0);
476 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) 495 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
477 return true; 496 return true;
478 } else { 497 } else {
479 SkMatrix inv; 498 SkMatrix inv;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 //////////////////////////////////////////////////////////////////////////////// 580 ////////////////////////////////////////////////////////////////////////////////
562 581
563 bool GrDrawState::canIgnoreColorAttribute() const { 582 bool GrDrawState::canIgnoreColorAttribute() const {
564 if (fBlendOptFlags & kInvalid_BlendOptFlag) { 583 if (fBlendOptFlags & kInvalid_BlendOptFlag) {
565 this->getBlendOpts(); 584 this->getBlendOpts();
566 } 585 }
567 return SkToBool(fBlendOptFlags & (GrRODrawState::kEmitTransBlack_BlendOptFla g | 586 return SkToBool(fBlendOptFlags & (GrRODrawState::kEmitTransBlack_BlendOptFla g |
568 GrRODrawState::kEmitCoverage_BlendOptFlag) ); 587 GrRODrawState::kEmitCoverage_BlendOptFlag) );
569 } 588 }
570 589
OLDNEW
« no previous file with comments | « src/gpu/GrDrawState.h ('k') | src/gpu/GrOptDrawState.h » ('j') | src/gpu/GrOptDrawState.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698