blob: faf8cd4453f169868da287e24c61621ad0389631 [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"
egdaniel3658f382014-09-15 07:01:59 -07009
egdanielb1cff032014-11-13 06:19:25 -080010#include "GrBlend.h"
egdaniel3658f382014-09-15 07:01:59 -070011#include "GrOptDrawState.h"
12#include "GrPaint.h"
egdanielb6cbc382014-11-13 11:00:34 -080013#include "GrProcOptInfo.h"
egdaniel378092f2014-12-03 10:40:13 -080014#include "GrXferProcessor.h"
15#include "effects/GrPorterDuffXferProcessor.h"
egdaniel3658f382014-09-15 07:01:59 -070016
joshualitt8c0f6152014-12-10 14:12:22 -080017bool GrDrawState::isEqual(const GrDrawState& that) const {
egdaniel89af44a2014-09-26 06:15:04 -070018 if (this->getRenderTarget() != that.getRenderTarget() ||
19 this->fColorStages.count() != that.fColorStages.count() ||
20 this->fCoverageStages.count() != that.fCoverageStages.count() ||
21 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) ||
egdaniel89af44a2014-09-26 06:15:04 -070022 this->fFlagBits != that.fFlagBits ||
egdaniel89af44a2014-09-26 06:15:04 -070023 this->fStencilSettings != that.fStencilSettings ||
24 this->fDrawFace != that.fDrawFace) {
25 return false;
26 }
27
joshualitt8c0f6152014-12-10 14:12:22 -080028 bool explicitLocalCoords = this->hasLocalCoordAttribute();
29 if (this->hasGeometryProcessor()) {
30 if (!that.hasGeometryProcessor()) {
31 return false;
32 } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProcessor())) {
33 return false;
34 }
35 } else if (that.hasGeometryProcessor()) {
36 return false;
37 }
38
egdaniel915187b2014-12-05 12:58:28 -080039 if (!this->getXPFactory()->isEqual(*that.getXPFactory())) {
40 return false;
41 }
42
egdaniel89af44a2014-09-26 06:15:04 -070043 for (int i = 0; i < this->numColorStages(); i++) {
joshualitta5305a12014-10-10 17:47:00 -070044 if (!GrFragmentStage::AreCompatible(this->getColorStage(i), that.getColorStage(i),
egdaniel89af44a2014-09-26 06:15:04 -070045 explicitLocalCoords)) {
46 return false;
47 }
48 }
49 for (int i = 0; i < this->numCoverageStages(); i++) {
joshualitta5305a12014-10-10 17:47:00 -070050 if (!GrFragmentStage::AreCompatible(this->getCoverageStage(i), that.getCoverageStage(i),
egdaniel89af44a2014-09-26 06:15:04 -070051 explicitLocalCoords)) {
52 return false;
53 }
54 }
55
egdaniel89af44a2014-09-26 06:15:04 -070056 return true;
57}
58
bsalomon8f727332014-08-05 07:50:06 -070059//////////////////////////////////////////////////////////////////////////////s
60
egdaniel69bb90c2014-11-11 07:32:45 -080061GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) {
bsalomon8f727332014-08-05 07:50:06 -070062 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
63 *this = state;
64 if (!preConcatMatrix.isIdentity()) {
egdaniel776bdbd2014-08-06 11:07:02 -070065 for (int i = 0; i < this->numColorStages(); ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -070066 fColorStages[i].localCoordChange(preConcatMatrix);
bsalomon8f727332014-08-05 07:50:06 -070067 }
egdaniel776bdbd2014-08-06 11:07:02 -070068 for (int i = 0; i < this->numCoverageStages(); ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -070069 fCoverageStages[i].localCoordChange(preConcatMatrix);
bsalomon8f727332014-08-05 07:50:06 -070070 }
bsalomon8f727332014-08-05 07:50:06 -070071 }
72}
73
74GrDrawState& GrDrawState::operator=(const GrDrawState& that) {
bsalomonae59b772014-11-19 08:23:49 -080075 fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get()));
bsalomon8f727332014-08-05 07:50:06 -070076 fViewMatrix = that.fViewMatrix;
bsalomon8f727332014-08-05 07:50:06 -070077 fFlagBits = that.fFlagBits;
bsalomon8f727332014-08-05 07:50:06 -070078 fStencilSettings = that.fStencilSettings;
bsalomon8f727332014-08-05 07:50:06 -070079 fDrawFace = that.fDrawFace;
joshualitt8c0f6152014-12-10 14:12:22 -080080 fGeometryProcessor.reset(SkSafeRef(that.fGeometryProcessor.get()));
egdaniel378092f2014-12-03 10:40:13 -080081 fXPFactory.reset(SkRef(that.getXPFactory()));
egdaniel8cbf3d52014-08-21 06:27:22 -070082 fColorStages = that.fColorStages;
83 fCoverageStages = that.fCoverageStages;
bsalomon8f727332014-08-05 07:50:06 -070084
bsalomon62c447d2014-08-08 08:08:50 -070085 fHints = that.fHints;
egdaniel776bdbd2014-08-06 11:07:02 -070086
egdanielb6cbc382014-11-13 11:00:34 -080087 fColorProcInfoValid = that.fColorProcInfoValid;
88 fCoverageProcInfoValid = that.fCoverageProcInfoValid;
joshualittf364b612014-12-11 06:52:01 -080089 fColorCache = that.fColorCache;
90 fCoverageCache = that.fCoverageCache;
egdanielb6cbc382014-11-13 11:00:34 -080091 if (fColorProcInfoValid) {
92 fColorProcInfo = that.fColorProcInfo;
93 }
94 if (fCoverageProcInfoValid) {
95 fCoverageProcInfo = that.fCoverageProcInfo;
96 }
bsalomon8f727332014-08-05 07:50:06 -070097 return *this;
98}
99
100void GrDrawState::onReset(const SkMatrix* initialViewMatrix) {
joshualitt8c0f6152014-12-10 14:12:22 -0800101 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomonae59b772014-11-19 08:23:49 -0800102 fRenderTarget.reset(NULL);
bsalomon2a9ca782014-09-05 14:27:43 -0700103
joshualitt8c0f6152014-12-10 14:12:22 -0800104 fGeometryProcessor.reset(NULL);
egdanielc016fb82014-12-03 11:41:54 -0800105 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode));
egdaniel8cbf3d52014-08-21 06:27:22 -0700106 fColorStages.reset();
107 fCoverageStages.reset();
bsalomon8f727332014-08-05 07:50:06 -0700108
bsalomon8f727332014-08-05 07:50:06 -0700109 if (NULL == initialViewMatrix) {
110 fViewMatrix.reset();
111 } else {
112 fViewMatrix = *initialViewMatrix;
113 }
bsalomon8f727332014-08-05 07:50:06 -0700114 fFlagBits = 0x0;
115 fStencilSettings.setDisabled();
bsalomon8f727332014-08-05 07:50:06 -0700116 fDrawFace = kBoth_DrawFace;
117
bsalomon62c447d2014-08-08 08:08:50 -0700118 fHints = 0;
egdanielb6cbc382014-11-13 11:00:34 -0800119
120 fColorProcInfoValid = false;
121 fCoverageProcInfoValid = false;
joshualitt9b338222014-12-10 12:28:08 -0800122
123 fColorCache = GrColor_ILLEGAL;
124 fCoverageCache = GrColor_ILLEGAL;
bsalomon8f727332014-08-05 07:50:06 -0700125}
126
bsalomon@google.com137f1342013-05-29 21:27:53 +0000127bool GrDrawState::setIdentityViewMatrix() {
joshualitt4dd99882014-11-11 08:51:30 -0800128 if (this->numFragmentStages()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000129 SkMatrix invVM;
bsalomon2ed5ef82014-07-07 08:44:05 -0700130 if (!fViewMatrix.invert(&invVM)) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000131 // sad trombone sound
132 return false;
133 }
egdaniel776bdbd2014-08-06 11:07:02 -0700134 for (int s = 0; s < this->numColorStages(); ++s) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700135 fColorStages[s].localCoordChange(invVM);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000136 }
egdaniel776bdbd2014-08-06 11:07:02 -0700137 for (int s = 0; s < this->numCoverageStages(); ++s) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700138 fCoverageStages[s].localCoordChange(invVM);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000139 }
140 }
bsalomon2ed5ef82014-07-07 08:44:05 -0700141 fViewMatrix.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000142 return true;
143}
144
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000145void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRenderTarget* rt) {
joshualitt8c0f6152014-12-10 14:12:22 -0800146 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000147
joshualitt8c0f6152014-12-10 14:12:22 -0800148 fGeometryProcessor.reset(NULL);
egdaniel8cbf3d52014-08-21 06:27:22 -0700149 fColorStages.reset();
150 fCoverageStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000151
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000152 for (int i = 0; i < paint.numColorStages(); ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700153 fColorStages.push_back(paint.getColorStage(i));
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000154 }
155
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000156 for (int i = 0; i < paint.numCoverageStages(); ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700157 fCoverageStages.push_back(paint.getCoverageStage(i));
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000158 }
159
egdaniel378092f2014-12-03 10:40:13 -0800160 fXPFactory.reset(SkRef(paint.getXPFactory()));
161
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000162 this->setRenderTarget(rt);
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000163
bsalomon2ed5ef82014-07-07 08:44:05 -0700164 fViewMatrix = vm;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000165
166 // These have no equivalent in GrPaint, set them to defaults
bsalomon2ed5ef82014-07-07 08:44:05 -0700167 fDrawFace = kBoth_DrawFace;
168 fStencilSettings.setDisabled();
bsalomon04ddf892014-11-19 12:36:22 -0800169 fFlagBits = 0;
bsalomon62c447d2014-08-08 08:08:50 -0700170 fHints = 0;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000171
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000172 // Enable the clip bit
173 this->enableState(GrDrawState::kClip_StateBit);
174
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000175 this->setState(GrDrawState::kDither_StateBit, paint.isDither());
176 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias());
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000177
egdanielb6cbc382014-11-13 11:00:34 -0800178 fColorProcInfoValid = false;
179 fCoverageProcInfoValid = false;
joshualitt2e3b3e32014-12-09 13:31:14 -0800180
181 fColorCache = GrColor_ILLEGAL;
182 fCoverageCache = GrColor_ILLEGAL;
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000183}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000184
185////////////////////////////////////////////////////////////////////////////////
186
joshualitt2e3b3e32014-12-09 13:31:14 -0800187bool GrDrawState::canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const {
bsalomon62c447d2014-08-08 08:08:50 -0700188 if (caps.dualSourceBlendingSupport()) {
189 return true;
190 }
egdaniel95131432014-12-09 11:15:43 -0800191
joshualitt2e3b3e32014-12-09 13:31:14 -0800192 this->calcColorInvariantOutput(color);
193
194 // The coverage isn't actually white, its unknown, but this will produce the same effect
195 // TODO we want to cache the result of this call, but we can probably clean up the interface
196 // so we don't have to pass in a seemingly known coverage
197 this->calcCoverageInvariantOutput(GrColor_WHITE);
egdaniel95131432014-12-09 11:15:43 -0800198 return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo,
199 this->isCoverageDrawing(), this->isColorWriteDisabled());
bsalomon62c447d2014-08-08 08:08:50 -0700200}
201
joshualitt8c0f6152014-12-10 14:12:22 -0800202bool GrDrawState::hasSolidCoverage(GrColor coverage) const {
egdaniel89af44a2014-09-26 06:15:04 -0700203 // If we're drawing coverage directly then coverage is effectively treated as color.
204 if (this->isCoverageDrawing()) {
205 return true;
206 }
207
joshualitt4dd99882014-11-11 08:51:30 -0800208 if (this->numCoverageStages() > 0) {
209 return false;
210 }
211
joshualitt8c0f6152014-12-10 14:12:22 -0800212 this->calcCoverageInvariantOutput(coverage);
egdanielb6cbc382014-11-13 11:00:34 -0800213 return fCoverageProcInfo.isSolidWhite();
egdaniel89af44a2014-09-26 06:15:04 -0700214}
215
egdaniel21aed572014-08-26 12:24:06 -0700216//////////////////////////////////////////////////////////////////////////////s
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000217
joshualitt8c0f6152014-12-10 14:12:22 -0800218bool GrDrawState::willEffectReadDstColor(GrColor color, GrColor coverage) const {
219 this->calcColorInvariantOutput(color);
220 this->calcCoverageInvariantOutput(coverage);
egdaniel95131432014-12-09 11:15:43 -0800221 // TODO: Remove need to create the XP here.
222 // Also once all custom blends are turned into XPs we can remove the need
223 // to check other stages since only xp's will be able to read dst
224 SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(fColorProcInfo,
225 fCoverageProcInfo));
226 if (xferProcessor && xferProcessor->willReadDstColor()) {
227 return true;
228 }
229
egdaniel89af44a2014-09-26 06:15:04 -0700230 if (!this->isColorWriteDisabled()) {
egdanielb6cbc382014-11-13 11:00:34 -0800231 if (fColorProcInfo.readsDst()) {
egdaniel89af44a2014-09-26 06:15:04 -0700232 return true;
233 }
234 }
egdanielb6cbc382014-11-13 11:00:34 -0800235 return fCoverageProcInfo.readsDst();
egdaniel89af44a2014-09-26 06:15:04 -0700236}
237
egdaniel21aed572014-08-26 12:24:06 -0700238void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) {
bsalomon49f085d2014-09-05 13:34:00 -0700239 if (fDrawState) {
joshualitt8c0f6152014-12-10 14:12:22 -0800240 // See the big comment on the class definition about GPs.
241 if (SK_InvalidUniqueID == fOriginalGPID) {
242 fDrawState->fGeometryProcessor.reset(NULL);
243 } else {
244 SkASSERT(fDrawState->getGeometryProcessor()->getUniqueID() ==
245 fOriginalGPID);
246 fOriginalGPID = SK_InvalidUniqueID;
247 }
248
egdaniel21aed572014-08-26 12:24:06 -0700249 int m = fDrawState->numColorStages() - fColorEffectCnt;
250 SkASSERT(m >= 0);
251 fDrawState->fColorStages.pop_back_n(m);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000252
egdaniel21aed572014-08-26 12:24:06 -0700253 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt;
254 SkASSERT(n >= 0);
255 fDrawState->fCoverageStages.pop_back_n(n);
egdanielb6cbc382014-11-13 11:00:34 -0800256 if (m + n > 0) {
257 fDrawState->fColorProcInfoValid = false;
258 fDrawState->fCoverageProcInfoValid = false;
259 }
egdaniel21aed572014-08-26 12:24:06 -0700260 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000261 }
egdaniel21aed572014-08-26 12:24:06 -0700262 fDrawState = ds;
263 if (NULL != ds) {
joshualitt8c0f6152014-12-10 14:12:22 -0800264 SkASSERT(SK_InvalidUniqueID == fOriginalGPID);
265 if (NULL != ds->getGeometryProcessor()) {
266 fOriginalGPID = ds->getGeometryProcessor()->getUniqueID();
267 }
egdaniel21aed572014-08-26 12:24:06 -0700268 fColorEffectCnt = ds->numColorStages();
269 fCoverageEffectCnt = ds->numCoverageStages();
270 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;)
271 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000272}
273
jvanverth@google.comcc782382013-01-28 20:39:48 +0000274////////////////////////////////////////////////////////////////////////////////
275
egdaniel89af44a2014-09-26 06:15:04 -0700276// Some blend modes allow folding a fractional coverage value into the color's alpha channel, while
277// others will blend incorrectly.
278bool GrDrawState::canTweakAlphaForCoverage() const {
egdaniel95131432014-12-09 11:15:43 -0800279 return fXPFactory->canTweakAlphaForCoverage(this->isCoverageDrawing());
egdaniel89af44a2014-09-26 06:15:04 -0700280}
281
282////////////////////////////////////////////////////////////////////////////////
283
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000284void GrDrawState::AutoViewMatrixRestore::restore() {
bsalomon49f085d2014-09-05 13:34:00 -0700285 if (fDrawState) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000286 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
bsalomon2ed5ef82014-07-07 08:44:05 -0700287 fDrawState->fViewMatrix = fViewMatrix;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000288 SkASSERT(fDrawState->numColorStages() >= fNumColorStages);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000289 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000290 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000291
292 int i = 0;
293 for (int s = 0; s < fNumColorStages; ++s, ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700294 fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000295 }
296 for (int s = 0; s < numCoverageStages; ++s, ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700297 fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges[i]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000298 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000299 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000300 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000301}
302
303void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000304 const SkMatrix& preconcatMatrix) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000305 this->restore();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000306
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000307 SkASSERT(NULL == fDrawState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000308 if (NULL == drawState || preconcatMatrix.isIdentity()) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000309 return;
310 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000311 fDrawState = drawState;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000312
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000313 fViewMatrix = drawState->getViewMatrix();
bsalomon2ed5ef82014-07-07 08:44:05 -0700314 drawState->fViewMatrix.preConcat(preconcatMatrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000315
316 this->doEffectCoordChanges(preconcatMatrix);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000317 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000318}
319
bsalomon@google.com137f1342013-05-29 21:27:53 +0000320bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000321 this->restore();
322
bsalomon@google.com137f1342013-05-29 21:27:53 +0000323 if (NULL == drawState) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000324 return false;
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000325 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000326
bsalomon@google.com137f1342013-05-29 21:27:53 +0000327 if (drawState->getViewMatrix().isIdentity()) {
328 return true;
329 }
330
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000331 fViewMatrix = drawState->getViewMatrix();
joshualitt4dd99882014-11-11 08:51:30 -0800332 if (0 == drawState->numFragmentStages()) {
bsalomon2ed5ef82014-07-07 08:44:05 -0700333 drawState->fViewMatrix.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000334 fDrawState = drawState;
335 fNumColorStages = 0;
336 fSavedCoordChanges.reset(0);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000337 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000338 return true;
339 } else {
340 SkMatrix inv;
341 if (!fViewMatrix.invert(&inv)) {
342 return false;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000343 }
bsalomon2ed5ef82014-07-07 08:44:05 -0700344 drawState->fViewMatrix.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000345 fDrawState = drawState;
346 this->doEffectCoordChanges(inv);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000347 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000348 return true;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000349 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000350}
351
352void GrDrawState::AutoViewMatrixRestore::doEffectCoordChanges(const SkMatrix& coordChangeMatrix) {
joshualitt4dd99882014-11-11 08:51:30 -0800353 fSavedCoordChanges.reset(fDrawState->numFragmentStages());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000354 int i = 0;
355
356 fNumColorStages = fDrawState->numColorStages();
357 for (int s = 0; s < fNumColorStages; ++s, ++i) {
egdaniel776bdbd2014-08-06 11:07:02 -0700358 fDrawState->getColorStage(s).saveCoordChange(&fSavedCoordChanges[i]);
egdaniel8cbf3d52014-08-21 06:27:22 -0700359 fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000360 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000361
362 int numCoverageStages = fDrawState->numCoverageStages();
363 for (int s = 0; s < numCoverageStages; ++s, ++i) {
egdaniel776bdbd2014-08-06 11:07:02 -0700364 fDrawState->getCoverageStage(s).saveCoordChange(&fSavedCoordChanges[i]);
egdaniel8cbf3d52014-08-21 06:27:22 -0700365 fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000366 }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000367}
egdaniel21aed572014-08-26 12:24:06 -0700368
egdaniel170f90b2014-09-16 12:54:40 -0700369////////////////////////////////////////////////////////////////////////////////
370
egdaniel170f90b2014-09-16 12:54:40 -0700371GrDrawState::~GrDrawState() {
egdaniel170f90b2014-09-16 12:54:40 -0700372 SkASSERT(0 == fBlockEffectRemovalCnt);
373}
374
egdaniel89af44a2014-09-26 06:15:04 -0700375////////////////////////////////////////////////////////////////////////////////
376
joshualitt8c0f6152014-12-10 14:12:22 -0800377bool GrDrawState::srcAlphaWillBeOne(GrColor color, GrColor coverage) const {
378 this->calcColorInvariantOutput(color);
379 if (this->isCoverageDrawing()) {
380 this->calcCoverageInvariantOutput(coverage);
381 return (fColorProcInfo.isOpaque() && fCoverageProcInfo.isOpaque());
382 }
383 return fColorProcInfo.isOpaque();
384}
385
386bool GrDrawState::willBlendWithDst(GrColor color, GrColor coverage) const {
387 this->calcColorInvariantOutput(color);
388 this->calcCoverageInvariantOutput(coverage);
egdaniel95131432014-12-09 11:15:43 -0800389 return fXPFactory->willBlendWithDst(fColorProcInfo, fCoverageProcInfo,
390 this->isCoverageDrawing(), this->isColorWriteDisabled());
egdanielcd8b6302014-11-11 14:46:05 -0800391}
392
joshualitt2e3b3e32014-12-09 13:31:14 -0800393void GrDrawState::calcColorInvariantOutput(GrColor color) const {
394 if (!fColorProcInfoValid || color != fColorCache) {
joshualitt8c0f6152014-12-10 14:12:22 -0800395 GrColorComponentFlags flags;
396 if (this->hasColorVertexAttribute()) {
397 if (fHints & kVertexColorsAreOpaque_Hint) {
398 flags = kA_GrColorComponentFlag;
399 color = 0xFF << GrColor_SHIFT_A;
400 } else {
401 flags = static_cast<GrColorComponentFlags>(0);
402 color = 0;
403 }
404 } else {
405 flags = kRGBA_GrColorComponentFlags;
406 }
407 fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(),
408 color, flags, false);
egdanielb6cbc382014-11-13 11:00:34 -0800409 fColorProcInfoValid = true;
joshualitt2e3b3e32014-12-09 13:31:14 -0800410 fColorCache = color;
egdanielb6cbc382014-11-13 11:00:34 -0800411 }
412}
413
joshualitt2e3b3e32014-12-09 13:31:14 -0800414void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const {
415 if (!fCoverageProcInfoValid || coverage != fCoverageCache) {
joshualitt8c0f6152014-12-10 14:12:22 -0800416 GrColorComponentFlags flags;
417 // Check if per-vertex or constant color may have partial alpha
418 if (this->hasCoverageVertexAttribute()) {
419 flags = static_cast<GrColorComponentFlags>(0);
420 coverage = 0;
421 } else {
422 flags = kRGBA_GrColorComponentFlags;
423 }
424 fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), this->numCoverageStages(),
425 coverage, flags, true, fGeometryProcessor.get());
egdanielb6cbc382014-11-13 11:00:34 -0800426 fCoverageProcInfoValid = true;
joshualitt2e3b3e32014-12-09 13:31:14 -0800427 fCoverageCache = coverage;
egdanielb6cbc382014-11-13 11:00:34 -0800428 }
429}