blob: da81a76a736c1652c2446b187c1f5e481ad1ad95 [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)) {
57 *fDrawState->sampler(s)->matrix() = fSamplerMatrices[s];
58 }
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);
80 drawState->sampler(s)->preConcatMatrix(preconcatMatrix);
81 }
82 }
83}
84
85////////////////////////////////////////////////////////////////////////////////
86
87void 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
99bool 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;
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000108 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000109
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000110 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.com2fdcdeb2012-10-08 17:15:55 +0000120 return false;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000121 } 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.com2fdcdeb2012-10-08 17:15:55 +0000131 return true;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000132}