bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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. |
| 6 | */ |
| 7 | |
| 8 | #include "GrDrawState.h" |
| 9 | |
| 10 | #include "GrPaint.h" |
| 11 | |
| 12 | void GrDrawState::setFromPaint(const GrPaint& paint) { |
bsalomon@google.com | 88becf4 | 2012-10-05 14:54:42 +0000 | [diff] [blame] | 13 | for (int i = 0; i < GrPaint::kMaxColorStages; ++i) { |
| 14 | int s = i + GrPaint::kFirstColorStage; |
| 15 | if (paint.isColorStageEnabled(i)) { |
| 16 | *this->sampler(s) = paint.getColorSampler(i); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 17 | } |
| 18 | } |
| 19 | |
bsalomon@google.com | 88becf4 | 2012-10-05 14:54:42 +0000 | [diff] [blame] | 20 | this->setFirstCoverageStage(GrPaint::kFirstCoverageStage); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 88becf4 | 2012-10-05 14:54:42 +0000 | [diff] [blame] | 22 | for (int i = 0; i < GrPaint::kMaxCoverageStages; ++i) { |
| 23 | int s = i + GrPaint::kFirstCoverageStage; |
| 24 | if (paint.isCoverageStageEnabled(i)) { |
| 25 | *this->sampler(s) = paint.getCoverageSampler(i); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | // disable all stages not accessible via the paint |
| 30 | for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) { |
| 31 | this->disableStage(s); |
| 32 | } |
| 33 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 34 | this->setColor(paint.getColor()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 35 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 36 | this->setState(GrDrawState::kDither_StateBit, paint.isDither()); |
| 37 | this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 39 | if (paint.isColorMatrixEnabled()) { |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 40 | this->enableState(GrDrawState::kColorMatrix_StateBit); |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 41 | this->setColorMatrix(paint.getColorMatrix()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 42 | } else { |
| 43 | this->disableState(GrDrawState::kColorMatrix_StateBit); |
| 44 | } |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 45 | this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff()); |
| 46 | this->setColorFilter(paint.getColorFilterColor(), paint.getColorFilterMode()); |
| 47 | this->setCoverage(paint.getCoverage()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 48 | } |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 49 | |
| 50 | //////////////////////////////////////////////////////////////////////////////// |
| 51 | |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame^] | 52 | void GrDrawState::AutoViewMatrixRestore::restore() { |
| 53 | if (NULL != fDrawState) { |
| 54 | fDrawState->setViewMatrix(fViewMatrix); |
| 55 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 56 | if (fRestoreMask & (1 << s)) { |
| 57 | *fDrawState->sampler(s)->matrix() = fSamplerMatrices[s]; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | fDrawState = NULL; |
| 62 | } |
| 63 | |
| 64 | void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, |
| 65 | const GrMatrix& preconcatMatrix, |
| 66 | uint32_t explicitCoordStageMask) { |
| 67 | this->restore(); |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 68 | |
| 69 | fDrawState = drawState; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame^] | 70 | if (NULL == drawState) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | fRestoreMask = 0; |
| 75 | fViewMatrix = drawState->getViewMatrix(); |
| 76 | drawState->preConcatViewMatrix(preconcatMatrix); |
| 77 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 78 | if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) { |
| 79 | fRestoreMask |= (1 << s); |
| 80 | drawState->sampler(s)->preConcatMatrix(preconcatMatrix); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | //////////////////////////////////////////////////////////////////////////////// |
| 86 | |
| 87 | void GrDrawState::AutoDeviceCoordDraw::restore() { |
| 88 | if (NULL != fDrawState) { |
| 89 | fDrawState->setViewMatrix(fViewMatrix); |
| 90 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 91 | if (fRestoreMask & (1 << s)) { |
| 92 | *fDrawState->sampler(s)->matrix() = fSamplerMatrices[s]; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | fDrawState = NULL; |
| 97 | } |
| 98 | |
| 99 | bool GrDrawState::AutoDeviceCoordDraw::set(GrDrawState* drawState, |
| 100 | uint32_t explicitCoordStageMask) { |
| 101 | GrAssert(NULL != drawState); |
| 102 | |
| 103 | this->restore(); |
| 104 | |
| 105 | fDrawState = drawState; |
| 106 | if (NULL == fDrawState) { |
| 107 | return false; |
| 108 | } |
| 109 | |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 110 | fViewMatrix = drawState->getViewMatrix(); |
| 111 | fRestoreMask = 0; |
| 112 | GrMatrix invVM; |
| 113 | bool inverted = false; |
| 114 | |
| 115 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 116 | if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) { |
| 117 | if (!inverted && !fViewMatrix.invert(&invVM)) { |
| 118 | // sad trombone sound |
| 119 | fDrawState = NULL; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame^] | 120 | return false; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 121 | } else { |
| 122 | inverted = true; |
| 123 | } |
| 124 | fRestoreMask |= (1 << s); |
| 125 | GrSamplerState* sampler = drawState->sampler(s); |
| 126 | fSamplerMatrices[s] = sampler->getMatrix(); |
| 127 | sampler->preConcatMatrix(invVM); |
| 128 | } |
| 129 | } |
| 130 | drawState->viewMatrix()->reset(); |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame^] | 131 | return true; |
bsalomon@google.com | 5b3e890 | 2012-10-05 20:13:28 +0000 | [diff] [blame] | 132 | } |