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