blob: 0f0a27a928bee7035bb09b8de1f533801d39e316 [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 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff());
40 this->setColorFilter(paint.getColorFilterColor(), paint.getColorFilterMode());
41 this->setCoverage(paint.getCoverage());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000042}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +000043
44////////////////////////////////////////////////////////////////////////////////
45
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000046void GrDrawState::AutoViewMatrixRestore::restore() {
47 if (NULL != fDrawState) {
48 fDrawState->setViewMatrix(fViewMatrix);
49 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
50 if (fRestoreMask & (1 << s)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +000051 fDrawState->sampler(s)->restoreCoordChange(fSavedCoordChanges[s]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000052 }
53 }
54 }
55 fDrawState = NULL;
56}
57
58void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
59 const GrMatrix& preconcatMatrix,
60 uint32_t explicitCoordStageMask) {
61 this->restore();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +000062
63 fDrawState = drawState;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000064 if (NULL == drawState) {
65 return;
66 }
67
68 fRestoreMask = 0;
69 fViewMatrix = drawState->getViewMatrix();
70 drawState->preConcatViewMatrix(preconcatMatrix);
71 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
72 if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) {
73 fRestoreMask |= (1 << s);
bsalomon@google.com288d9542012-10-17 12:53:54 +000074 fDrawState->sampler(s)->saveCoordChange(&fSavedCoordChanges[s]);
75 drawState->sampler(s)->preConcatCoordChange(preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000076 }
77 }
78}
79
80////////////////////////////////////////////////////////////////////////////////
81
82void GrDrawState::AutoDeviceCoordDraw::restore() {
83 if (NULL != fDrawState) {
84 fDrawState->setViewMatrix(fViewMatrix);
85 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
86 if (fRestoreMask & (1 << s)) {
bsalomon@google.com288d9542012-10-17 12:53:54 +000087 fDrawState->sampler(s)->restoreCoordChange(fSavedCoordChanges[s]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000088 }
89 }
90 }
91 fDrawState = NULL;
92}
93
94bool GrDrawState::AutoDeviceCoordDraw::set(GrDrawState* drawState,
95 uint32_t explicitCoordStageMask) {
96 GrAssert(NULL != drawState);
97
98 this->restore();
99
100 fDrawState = drawState;
101 if (NULL == fDrawState) {
102 return false;
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000103 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000104
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000105 fViewMatrix = drawState->getViewMatrix();
106 fRestoreMask = 0;
107 GrMatrix invVM;
108 bool inverted = false;
109
110 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
111 if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) {
112 if (!inverted && !fViewMatrix.invert(&invVM)) {
113 // sad trombone sound
114 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000115 return false;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000116 } else {
117 inverted = true;
118 }
119 fRestoreMask |= (1 << s);
120 GrSamplerState* sampler = drawState->sampler(s);
bsalomon@google.com288d9542012-10-17 12:53:54 +0000121 sampler->saveCoordChange(&fSavedCoordChanges[s]);
122 sampler->preConcatCoordChange(invVM);
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000123 }
124 }
125 drawState->viewMatrix()->reset();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000126 return true;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000127}