blob: c95049a491d802b8eaa20918dec492b18d9f85a3 [file] [log] [blame]
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001/*
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
12void GrDrawState::setFromPaint(const GrPaint& paint) {
bsalomon@google.com88becf42012-10-05 14:54:42 +000013 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.comaf84e742012-10-05 13:23:24 +000017 }
18 }
19
bsalomon@google.com88becf42012-10-05 14:54:42 +000020 this->setFirstCoverageStage(GrPaint::kFirstCoverageStage);
bsalomon@google.comaf84e742012-10-05 13:23:24 +000021
bsalomon@google.com88becf42012-10-05 14:54:42 +000022 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.comaf84e742012-10-05 13:23:24 +000026 }
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.comc7448ce2012-10-05 19:04:13 +000034 this->setColor(paint.getColor());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000035
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000036 this->setState(GrDrawState::kDither_StateBit, paint.isDither());
37 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000038
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000039 if (paint.isColorMatrixEnabled()) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +000040 this->enableState(GrDrawState::kColorMatrix_StateBit);
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000041 this->setColorMatrix(paint.getColorMatrix());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000042 } else {
43 this->disableState(GrDrawState::kColorMatrix_StateBit);
44 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000045 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff());
46 this->setColorFilter(paint.getColorFilterColor(), paint.getColorFilterMode());
47 this->setCoverage(paint.getCoverage());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000048}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +000049
50////////////////////////////////////////////////////////////////////////////////
51
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000052void 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)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +000057 fDrawState->sampler(s)->restoreCoordChange(fSavedCoordChanges[s]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000058 }
59 }
60 }
61 fDrawState = NULL;
62}
63
64void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
65 const GrMatrix& preconcatMatrix,
66 uint32_t explicitCoordStageMask) {
67 this->restore();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +000068
69 fDrawState = drawState;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000070 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);
bsalomon@google.com288d9542012-10-17 12:53:54 +000080 fDrawState->sampler(s)->saveCoordChange(&fSavedCoordChanges[s]);
81 drawState->sampler(s)->preConcatCoordChange(preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000082 }
83 }
84}
85
86////////////////////////////////////////////////////////////////////////////////
87
88void GrDrawState::AutoDeviceCoordDraw::restore() {
89 if (NULL != fDrawState) {
90 fDrawState->setViewMatrix(fViewMatrix);
91 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
92 if (fRestoreMask & (1 << s)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +000093 fDrawState->sampler(s)->restoreCoordChange(fSavedCoordChanges[s]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000094 }
95 }
96 }
97 fDrawState = NULL;
98}
99
100bool GrDrawState::AutoDeviceCoordDraw::set(GrDrawState* drawState,
101 uint32_t explicitCoordStageMask) {
102 GrAssert(NULL != drawState);
103
104 this->restore();
105
106 fDrawState = drawState;
107 if (NULL == fDrawState) {
108 return false;
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000109 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000110
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000111 fViewMatrix = drawState->getViewMatrix();
112 fRestoreMask = 0;
113 GrMatrix invVM;
114 bool inverted = false;
115
116 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
117 if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) {
118 if (!inverted && !fViewMatrix.invert(&invVM)) {
119 // sad trombone sound
120 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000121 return false;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000122 } else {
123 inverted = true;
124 }
125 fRestoreMask |= (1 << s);
126 GrSamplerState* sampler = drawState->sampler(s);
bsalomon@google.com288d9542012-10-17 12:53:54 +0000127 sampler->saveCoordChange(&fSavedCoordChanges[s]);
128 sampler->preConcatCoordChange(invVM);
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000129 }
130 }
131 drawState->viewMatrix()->reset();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000132 return true;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000133}