blob: 2d5c7aa68f76c9d0d502375e1356fc65d24aec8a [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)) {
bsalomon@google.comadc65362013-01-28 14:26:09 +000016 fStages[s] = paint.getColorStage(i);
17 } else {
18 fStages[s].setEffect(NULL);
bsalomon@google.comaf84e742012-10-05 13:23:24 +000019 }
20 }
21
bsalomon@google.com88becf42012-10-05 14:54:42 +000022 this->setFirstCoverageStage(GrPaint::kFirstCoverageStage);
bsalomon@google.comaf84e742012-10-05 13:23:24 +000023
bsalomon@google.com88becf42012-10-05 14:54:42 +000024 for (int i = 0; i < GrPaint::kMaxCoverageStages; ++i) {
25 int s = i + GrPaint::kFirstCoverageStage;
26 if (paint.isCoverageStageEnabled(i)) {
bsalomon@google.comadc65362013-01-28 14:26:09 +000027 fStages[s] = paint.getCoverageStage(i);
28 } else {
29 fStages[s].setEffect(NULL);
bsalomon@google.comaf84e742012-10-05 13:23:24 +000030 }
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.comc7448ce2012-10-05 19:04:13 +000038 this->setColor(paint.getColor());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000039
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000040 this->setState(GrDrawState::kDither_StateBit, paint.isDither());
41 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000042
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000043 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff());
44 this->setColorFilter(paint.getColorFilterColor(), paint.getColorFilterMode());
45 this->setCoverage(paint.getCoverage());
bsalomon@google.comaf84e742012-10-05 13:23:24 +000046}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +000047
48////////////////////////////////////////////////////////////////////////////////
49
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000050void 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.comadc65362013-01-28 14:26:09 +000055 fDrawState->fStages[s].restoreCoordChange(fSavedCoordChanges[s]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000056 }
57 }
58 }
59 fDrawState = NULL;
60}
61
62void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000063 const SkMatrix& preconcatMatrix,
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000064 uint32_t explicitCoordStageMask) {
65 this->restore();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +000066
67 fDrawState = drawState;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000068 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.comadc65362013-01-28 14:26:09 +000078 fDrawState->fStages[s].saveCoordChange(&fSavedCoordChanges[s]);
79 drawState->fStages[s].preConcatCoordChange(preconcatMatrix);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000080 }
81 }
82}
83
84////////////////////////////////////////////////////////////////////////////////
85
86void 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.comadc65362013-01-28 14:26:09 +000091 fDrawState->fStages[s].restoreCoordChange(fSavedCoordChanges[s]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000092 }
93 }
94 }
95 fDrawState = NULL;
96}
97
98bool 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.comf467ce72012-10-09 02:01:37 +0000107 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000108
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000109 fViewMatrix = drawState->getViewMatrix();
110 fRestoreMask = 0;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000111 SkMatrix invVM;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000112 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.com2fdcdeb2012-10-08 17:15:55 +0000119 return false;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000120 } else {
121 inverted = true;
122 }
123 fRestoreMask |= (1 << s);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000124 GrEffectStage* stage = drawState->fStages + s;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000125 stage->saveCoordChange(&fSavedCoordChanges[s]);
126 stage->preConcatCoordChange(invVM);
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000127 }
128 }
129 drawState->viewMatrix()->reset();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000130 return true;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000131}