OLD | NEW |
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 #include "GrPaint.h" | 9 #include "GrPaint.h" |
10 | 10 |
| 11 //////////////////////////////////////////////////////////////////////////////s |
| 12 |
| 13 GrDrawState::CombinedState GrDrawState::CombineIfPossible( |
| 14 const GrDrawState& a, const GrDrawState& b) { |
| 15 |
| 16 bool usingVertexColors = a.hasColorVertexAttribute(); |
| 17 if (!usingVertexColors && a.fColor != b.fColor) { |
| 18 return kIncompatible_CombinedState; |
| 19 } |
| 20 |
| 21 if (a.fRenderTarget.get() != b.fRenderTarget.get() || |
| 22 a.fColorStages.count() != b.fColorStages.count() || |
| 23 a.fCoverageStages.count() != b.fCoverageStages.count() || |
| 24 !a.fViewMatrix.cheapEqualTo(b.fViewMatrix) || |
| 25 a.fSrcBlend != b.fSrcBlend || |
| 26 a.fDstBlend != b.fDstBlend || |
| 27 a.fBlendConstant != b.fBlendConstant || |
| 28 a.fFlagBits != b.fFlagBits || |
| 29 a.fVACount != b.fVACount || |
| 30 memcmp(a.fVAPtr, b.fVAPtr, a.fVACount * sizeof(GrVertexAttrib)) || |
| 31 a.fStencilSettings != b.fStencilSettings || |
| 32 a.fDrawFace != b.fDrawFace) { |
| 33 return kIncompatible_CombinedState; |
| 34 } |
| 35 |
| 36 bool usingVertexCoverage = a.hasCoverageVertexAttribute(); |
| 37 if (!usingVertexCoverage && a.fCoverage != b.fCoverage) { |
| 38 return kIncompatible_CombinedState; |
| 39 } |
| 40 |
| 41 bool explicitLocalCoords = a.hasLocalCoordAttribute(); |
| 42 for (int i = 0; i < a.fColorStages.count(); i++) { |
| 43 if (!GrEffectStage::AreCompatible(a.fColorStages[i], b.fColorStages[i], |
| 44 explicitLocalCoords)) { |
| 45 return kIncompatible_CombinedState; |
| 46 } |
| 47 } |
| 48 for (int i = 0; i < a.fCoverageStages.count(); i++) { |
| 49 if (!GrEffectStage::AreCompatible(a.fCoverageStages[i], b.fCoverageStage
s[i], |
| 50 explicitLocalCoords)) { |
| 51 return kIncompatible_CombinedState; |
| 52 } |
| 53 } |
| 54 SkASSERT(0 == memcmp(a.fFixedFunctionVertexAttribIndices, |
| 55 b.fFixedFunctionVertexAttribIndices, |
| 56 sizeof(a.fFixedFunctionVertexAttribIndices))); |
| 57 return kAOrB_CombinedState; |
| 58 } |
| 59 |
| 60 |
| 61 //////////////////////////////////////////////////////////////////////////////s |
| 62 |
| 63 GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatr
ix) { |
| 64 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 65 *this = state; |
| 66 if (!preConcatMatrix.isIdentity()) { |
| 67 for (int i = 0; i < fColorStages.count(); ++i) { |
| 68 fColorStages[i].localCoordChange(preConcatMatrix); |
| 69 } |
| 70 for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 71 fCoverageStages[i].localCoordChange(preConcatMatrix); |
| 72 } |
| 73 this->invalidateBlendOptFlags(); |
| 74 } |
| 75 } |
| 76 |
| 77 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
| 78 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
| 79 this->setRenderTarget(that.fRenderTarget.get()); |
| 80 fColor = that.fColor; |
| 81 fViewMatrix = that.fViewMatrix; |
| 82 fSrcBlend = that.fSrcBlend; |
| 83 fDstBlend = that.fDstBlend; |
| 84 fBlendConstant = that.fBlendConstant; |
| 85 fFlagBits = that.fFlagBits; |
| 86 fVACount = that.fVACount; |
| 87 fVAPtr = that.fVAPtr; |
| 88 fStencilSettings = that.fStencilSettings; |
| 89 fCoverage = that.fCoverage; |
| 90 fDrawFace = that.fDrawFace; |
| 91 fColorStages = that.fColorStages; |
| 92 fCoverageStages = that.fCoverageStages; |
| 93 fOptSrcBlend = that.fOptSrcBlend; |
| 94 fOptDstBlend = that.fOptDstBlend; |
| 95 fBlendOptFlags = that.fBlendOptFlags; |
| 96 |
| 97 memcpy(fFixedFunctionVertexAttribIndices, |
| 98 that.fFixedFunctionVertexAttribIndices, |
| 99 sizeof(fFixedFunctionVertexAttribIndices)); |
| 100 return *this; |
| 101 } |
| 102 |
| 103 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
| 104 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
| 105 fColorStages.reset(); |
| 106 fCoverageStages.reset(); |
| 107 |
| 108 fRenderTarget.reset(NULL); |
| 109 |
| 110 this->setDefaultVertexAttribs(); |
| 111 |
| 112 fColor = 0xffffffff; |
| 113 if (NULL == initialViewMatrix) { |
| 114 fViewMatrix.reset(); |
| 115 } else { |
| 116 fViewMatrix = *initialViewMatrix; |
| 117 } |
| 118 fSrcBlend = kOne_GrBlendCoeff; |
| 119 fDstBlend = kZero_GrBlendCoeff; |
| 120 fBlendConstant = 0x0; |
| 121 fFlagBits = 0x0; |
| 122 fStencilSettings.setDisabled(); |
| 123 fCoverage = 0xffffffff; |
| 124 fDrawFace = kBoth_DrawFace; |
| 125 |
| 126 this->invalidateBlendOptFlags(); |
| 127 } |
| 128 |
11 bool GrDrawState::setIdentityViewMatrix() { | 129 bool GrDrawState::setIdentityViewMatrix() { |
12 if (fColorStages.count() || fCoverageStages.count()) { | 130 if (fColorStages.count() || fCoverageStages.count()) { |
13 SkMatrix invVM; | 131 SkMatrix invVM; |
14 if (!fViewMatrix.invert(&invVM)) { | 132 if (!fViewMatrix.invert(&invVM)) { |
15 // sad trombone sound | 133 // sad trombone sound |
16 return false; | 134 return false; |
17 } | 135 } |
18 for (int s = 0; s < fColorStages.count(); ++s) { | 136 for (int s = 0; s < fColorStages.count(); ++s) { |
19 fColorStages[s].localCoordChange(invVM); | 137 fColorStages[s].localCoordChange(invVM); |
20 } | 138 } |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 522 } |
405 | 523 |
406 bool GrDrawState::canIgnoreColorAttribute() const { | 524 bool GrDrawState::canIgnoreColorAttribute() const { |
407 if (fBlendOptFlags & kInvalid_BlendOptFlag) { | 525 if (fBlendOptFlags & kInvalid_BlendOptFlag) { |
408 this->getBlendOpts(); | 526 this->getBlendOpts(); |
409 } | 527 } |
410 return SkToBool(fBlendOptFlags & (GrDrawState::kEmitTransBlack_BlendOptFlag
| | 528 return SkToBool(fBlendOptFlags & (GrDrawState::kEmitTransBlack_BlendOptFlag
| |
411 GrDrawState::kEmitCoverage_BlendOptFlag)); | 529 GrDrawState::kEmitCoverage_BlendOptFlag)); |
412 } | 530 } |
413 | 531 |
| 532 ////////////////////////////////////////////////////////////////////////////// |
| 533 |
| 534 GrDrawState::AutoVertexAttribRestore::AutoVertexAttribRestore( |
| 535 GrDrawState* drawState) { |
| 536 SkASSERT(NULL != drawState); |
| 537 fDrawState = drawState; |
| 538 fVAPtr = drawState->fVAPtr; |
| 539 fVACount = drawState->fVACount; |
| 540 fDrawState->setDefaultVertexAttribs(); |
| 541 } |
| 542 |
| 543 //////////////////////////////////////////////////////////////////////////////s |
| 544 |
| 545 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { |
| 546 if (NULL != fDrawState) { |
| 547 int m = fDrawState->fColorStages.count() - fColorEffectCnt; |
| 548 SkASSERT(m >= 0); |
| 549 fDrawState->fColorStages.pop_back_n(m); |
| 550 |
| 551 int n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt; |
| 552 SkASSERT(n >= 0); |
| 553 fDrawState->fCoverageStages.pop_back_n(n); |
| 554 if (m + n > 0) { |
| 555 fDrawState->invalidateBlendOptFlags(); |
| 556 } |
| 557 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
| 558 } |
| 559 fDrawState = ds; |
| 560 if (NULL != ds) { |
| 561 fColorEffectCnt = ds->fColorStages.count(); |
| 562 fCoverageEffectCnt = ds->fCoverageStages.count(); |
| 563 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 564 } |
| 565 } |
414 | 566 |
415 //////////////////////////////////////////////////////////////////////////////// | 567 //////////////////////////////////////////////////////////////////////////////// |
416 | 568 |
417 void GrDrawState::AutoViewMatrixRestore::restore() { | 569 void GrDrawState::AutoViewMatrixRestore::restore() { |
418 if (NULL != fDrawState) { | 570 if (NULL != fDrawState) { |
419 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) | 571 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
420 fDrawState->fViewMatrix = fViewMatrix; | 572 fDrawState->fViewMatrix = fViewMatrix; |
421 SkASSERT(fDrawState->numColorStages() >= fNumColorStages); | 573 SkASSERT(fDrawState->numColorStages() >= fNumColorStages); |
422 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; | 574 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; |
423 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); | 575 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 fDrawState->fColorStages[s].saveCoordChange(&fSavedCoordChanges[i]); | 643 fDrawState->fColorStages[s].saveCoordChange(&fSavedCoordChanges[i]); |
492 fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix); | 644 fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix); |
493 } | 645 } |
494 | 646 |
495 int numCoverageStages = fDrawState->numCoverageStages(); | 647 int numCoverageStages = fDrawState->numCoverageStages(); |
496 for (int s = 0; s < numCoverageStages; ++s, ++i) { | 648 for (int s = 0; s < numCoverageStages; ++s, ++i) { |
497 fDrawState->fCoverageStages[s].saveCoordChange(&fSavedCoordChanges[i]); | 649 fDrawState->fCoverageStages[s].saveCoordChange(&fSavedCoordChanges[i]); |
498 fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix); | 650 fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix); |
499 } | 651 } |
500 } | 652 } |
OLD | NEW |