blob: b9b708e4bf828250ce628042a592a5bc2ef43276 [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
52GrDrawState::AutoDeviceCoordDraw::AutoDeviceCoordDraw(GrDrawState* drawState,
53 uint32_t explicitCoordStageMask) {
54 GrAssert(NULL != drawState);
55
56 fDrawState = drawState;
57 fViewMatrix = drawState->getViewMatrix();
58 fRestoreMask = 0;
59 GrMatrix invVM;
60 bool inverted = false;
61
62 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
63 if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) {
64 if (!inverted && !fViewMatrix.invert(&invVM)) {
65 // sad trombone sound
66 fDrawState = NULL;
67 return;
68 } else {
69 inverted = true;
70 }
71 fRestoreMask |= (1 << s);
72 GrSamplerState* sampler = drawState->sampler(s);
73 fSamplerMatrices[s] = sampler->getMatrix();
74 sampler->preConcatMatrix(invVM);
75 }
76 }
77 drawState->viewMatrix()->reset();
78}
79
80GrDrawState::AutoDeviceCoordDraw::~AutoDeviceCoordDraw() {
81 if (NULL != fDrawState) {
82 fDrawState->setViewMatrix(fViewMatrix);
83 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
84 if (fRestoreMask & (1 << s)) {
85 *fDrawState->sampler(s)->matrix() = fSamplerMatrices[s];
86 }
87 }
88 }
89}