blob: 465c10c99ac61a25947978e46ed122362937e9b3 [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;
89 if (fColorProcInfoValid) {
90 fColorProcInfo = that.fColorProcInfo;
91 }
92 if (fCoverageProcInfoValid) {
93 fCoverageProcInfo = that.fCoverageProcInfo;
94 }
bsalomon8f727332014-08-05 07:50:06 -070095 return *this;
96}
97
98void GrDrawState::onReset(const SkMatrix* initialViewMatrix) {
joshualitt8c0f6152014-12-10 14:12:22 -080099 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomonae59b772014-11-19 08:23:49 -0800100 fRenderTarget.reset(NULL);
bsalomon2a9ca782014-09-05 14:27:43 -0700101
joshualitt8c0f6152014-12-10 14:12:22 -0800102 fGeometryProcessor.reset(NULL);
egdanielc016fb82014-12-03 11:41:54 -0800103 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode));
egdaniel8cbf3d52014-08-21 06:27:22 -0700104 fColorStages.reset();
105 fCoverageStages.reset();
bsalomon8f727332014-08-05 07:50:06 -0700106
bsalomon8f727332014-08-05 07:50:06 -0700107 if (NULL == initialViewMatrix) {
108 fViewMatrix.reset();
109 } else {
110 fViewMatrix = *initialViewMatrix;
111 }
bsalomon8f727332014-08-05 07:50:06 -0700112 fFlagBits = 0x0;
113 fStencilSettings.setDisabled();
bsalomon8f727332014-08-05 07:50:06 -0700114 fDrawFace = kBoth_DrawFace;
115
bsalomon62c447d2014-08-08 08:08:50 -0700116 fHints = 0;
egdanielb6cbc382014-11-13 11:00:34 -0800117
118 fColorProcInfoValid = false;
119 fCoverageProcInfoValid = false;
joshualitt9b338222014-12-10 12:28:08 -0800120
121 fColorCache = GrColor_ILLEGAL;
122 fCoverageCache = GrColor_ILLEGAL;
bsalomon8f727332014-08-05 07:50:06 -0700123}
124
bsalomon@google.com137f1342013-05-29 21:27:53 +0000125bool GrDrawState::setIdentityViewMatrix() {
joshualitt4dd99882014-11-11 08:51:30 -0800126 if (this->numFragmentStages()) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000127 SkMatrix invVM;
bsalomon2ed5ef82014-07-07 08:44:05 -0700128 if (!fViewMatrix.invert(&invVM)) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000129 // sad trombone sound
130 return false;
131 }
egdaniel776bdbd2014-08-06 11:07:02 -0700132 for (int s = 0; s < this->numColorStages(); ++s) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700133 fColorStages[s].localCoordChange(invVM);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000134 }
egdaniel776bdbd2014-08-06 11:07:02 -0700135 for (int s = 0; s < this->numCoverageStages(); ++s) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700136 fCoverageStages[s].localCoordChange(invVM);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000137 }
138 }
bsalomon2ed5ef82014-07-07 08:44:05 -0700139 fViewMatrix.reset();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000140 return true;
141}
142
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000143void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRenderTarget* rt) {
joshualitt8c0f6152014-12-10 14:12:22 -0800144 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000145
joshualitt8c0f6152014-12-10 14:12:22 -0800146 fGeometryProcessor.reset(NULL);
egdaniel8cbf3d52014-08-21 06:27:22 -0700147 fColorStages.reset();
148 fCoverageStages.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000149
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000150 for (int i = 0; i < paint.numColorStages(); ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700151 fColorStages.push_back(paint.getColorStage(i));
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000152 }
153
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000154 for (int i = 0; i < paint.numCoverageStages(); ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700155 fCoverageStages.push_back(paint.getCoverageStage(i));
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000156 }
157
egdaniel378092f2014-12-03 10:40:13 -0800158 fXPFactory.reset(SkRef(paint.getXPFactory()));
159
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000160 this->setRenderTarget(rt);
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000161
bsalomon2ed5ef82014-07-07 08:44:05 -0700162 fViewMatrix = vm;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000163
164 // These have no equivalent in GrPaint, set them to defaults
bsalomon2ed5ef82014-07-07 08:44:05 -0700165 fDrawFace = kBoth_DrawFace;
166 fStencilSettings.setDisabled();
bsalomon04ddf892014-11-19 12:36:22 -0800167 fFlagBits = 0;
bsalomon62c447d2014-08-08 08:08:50 -0700168 fHints = 0;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000169
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000170 // Enable the clip bit
171 this->enableState(GrDrawState::kClip_StateBit);
172
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000173 this->setState(GrDrawState::kDither_StateBit, paint.isDither());
174 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias());
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000175
egdanielb6cbc382014-11-13 11:00:34 -0800176 fColorProcInfoValid = false;
177 fCoverageProcInfoValid = false;
joshualitt2e3b3e32014-12-09 13:31:14 -0800178
179 fColorCache = GrColor_ILLEGAL;
180 fCoverageCache = GrColor_ILLEGAL;
bsalomon@google.comaf84e742012-10-05 13:23:24 +0000181}
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000182
183////////////////////////////////////////////////////////////////////////////////
184
joshualitt2e3b3e32014-12-09 13:31:14 -0800185bool GrDrawState::canUseFracCoveragePrimProc(GrColor color, const GrDrawTargetCaps& caps) const {
bsalomon62c447d2014-08-08 08:08:50 -0700186 if (caps.dualSourceBlendingSupport()) {
187 return true;
188 }
egdaniel95131432014-12-09 11:15:43 -0800189
joshualitt2e3b3e32014-12-09 13:31:14 -0800190 this->calcColorInvariantOutput(color);
191
192 // The coverage isn't actually white, its unknown, but this will produce the same effect
193 // TODO we want to cache the result of this call, but we can probably clean up the interface
194 // so we don't have to pass in a seemingly known coverage
195 this->calcCoverageInvariantOutput(GrColor_WHITE);
egdaniel95131432014-12-09 11:15:43 -0800196 return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo,
197 this->isCoverageDrawing(), this->isColorWriteDisabled());
bsalomon62c447d2014-08-08 08:08:50 -0700198}
199
joshualitt8c0f6152014-12-10 14:12:22 -0800200bool GrDrawState::hasSolidCoverage(GrColor coverage) const {
egdaniel89af44a2014-09-26 06:15:04 -0700201 // If we're drawing coverage directly then coverage is effectively treated as color.
202 if (this->isCoverageDrawing()) {
203 return true;
204 }
205
joshualitt4dd99882014-11-11 08:51:30 -0800206 if (this->numCoverageStages() > 0) {
207 return false;
208 }
209
joshualitt8c0f6152014-12-10 14:12:22 -0800210 this->calcCoverageInvariantOutput(coverage);
egdanielb6cbc382014-11-13 11:00:34 -0800211 return fCoverageProcInfo.isSolidWhite();
egdaniel89af44a2014-09-26 06:15:04 -0700212}
213
egdaniel21aed572014-08-26 12:24:06 -0700214//////////////////////////////////////////////////////////////////////////////s
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000215
joshualitt8c0f6152014-12-10 14:12:22 -0800216bool GrDrawState::willEffectReadDstColor(GrColor color, GrColor coverage) const {
217 this->calcColorInvariantOutput(color);
218 this->calcCoverageInvariantOutput(coverage);
egdaniel95131432014-12-09 11:15:43 -0800219 // TODO: Remove need to create the XP here.
220 // Also once all custom blends are turned into XPs we can remove the need
221 // to check other stages since only xp's will be able to read dst
222 SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(fColorProcInfo,
223 fCoverageProcInfo));
224 if (xferProcessor && xferProcessor->willReadDstColor()) {
225 return true;
226 }
227
egdaniel89af44a2014-09-26 06:15:04 -0700228 if (!this->isColorWriteDisabled()) {
egdanielb6cbc382014-11-13 11:00:34 -0800229 if (fColorProcInfo.readsDst()) {
egdaniel89af44a2014-09-26 06:15:04 -0700230 return true;
231 }
232 }
egdanielb6cbc382014-11-13 11:00:34 -0800233 return fCoverageProcInfo.readsDst();
egdaniel89af44a2014-09-26 06:15:04 -0700234}
235
egdaniel21aed572014-08-26 12:24:06 -0700236void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) {
bsalomon49f085d2014-09-05 13:34:00 -0700237 if (fDrawState) {
joshualitt8c0f6152014-12-10 14:12:22 -0800238 // See the big comment on the class definition about GPs.
239 if (SK_InvalidUniqueID == fOriginalGPID) {
240 fDrawState->fGeometryProcessor.reset(NULL);
241 } else {
242 SkASSERT(fDrawState->getGeometryProcessor()->getUniqueID() ==
243 fOriginalGPID);
244 fOriginalGPID = SK_InvalidUniqueID;
245 }
246
egdaniel21aed572014-08-26 12:24:06 -0700247 int m = fDrawState->numColorStages() - fColorEffectCnt;
248 SkASSERT(m >= 0);
249 fDrawState->fColorStages.pop_back_n(m);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000250
egdaniel21aed572014-08-26 12:24:06 -0700251 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt;
252 SkASSERT(n >= 0);
253 fDrawState->fCoverageStages.pop_back_n(n);
egdanielb6cbc382014-11-13 11:00:34 -0800254 if (m + n > 0) {
255 fDrawState->fColorProcInfoValid = false;
256 fDrawState->fCoverageProcInfoValid = false;
257 }
egdaniel21aed572014-08-26 12:24:06 -0700258 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000259 }
egdaniel21aed572014-08-26 12:24:06 -0700260 fDrawState = ds;
261 if (NULL != ds) {
joshualitt8c0f6152014-12-10 14:12:22 -0800262 SkASSERT(SK_InvalidUniqueID == fOriginalGPID);
263 if (NULL != ds->getGeometryProcessor()) {
264 fOriginalGPID = ds->getGeometryProcessor()->getUniqueID();
265 }
egdaniel21aed572014-08-26 12:24:06 -0700266 fColorEffectCnt = ds->numColorStages();
267 fCoverageEffectCnt = ds->numCoverageStages();
268 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;)
269 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000270}
271
jvanverth@google.comcc782382013-01-28 20:39:48 +0000272////////////////////////////////////////////////////////////////////////////////
273
egdaniel89af44a2014-09-26 06:15:04 -0700274// Some blend modes allow folding a fractional coverage value into the color's alpha channel, while
275// others will blend incorrectly.
276bool GrDrawState::canTweakAlphaForCoverage() const {
egdaniel95131432014-12-09 11:15:43 -0800277 return fXPFactory->canTweakAlphaForCoverage(this->isCoverageDrawing());
egdaniel89af44a2014-09-26 06:15:04 -0700278}
279
280////////////////////////////////////////////////////////////////////////////////
281
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000282void GrDrawState::AutoViewMatrixRestore::restore() {
bsalomon49f085d2014-09-05 13:34:00 -0700283 if (fDrawState) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000284 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;)
bsalomon2ed5ef82014-07-07 08:44:05 -0700285 fDrawState->fViewMatrix = fViewMatrix;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000286 SkASSERT(fDrawState->numColorStages() >= fNumColorStages);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000287 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000288 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000289
290 int i = 0;
291 for (int s = 0; s < fNumColorStages; ++s, ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700292 fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000293 }
294 for (int s = 0; s < numCoverageStages; ++s, ++i) {
egdaniel8cbf3d52014-08-21 06:27:22 -0700295 fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges[i]);
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000296 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000297 fDrawState = NULL;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000298 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000299}
300
301void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000302 const SkMatrix& preconcatMatrix) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000303 this->restore();
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000304
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000305 SkASSERT(NULL == fDrawState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000306 if (NULL == drawState || preconcatMatrix.isIdentity()) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000307 return;
308 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000309 fDrawState = drawState;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000310
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000311 fViewMatrix = drawState->getViewMatrix();
bsalomon2ed5ef82014-07-07 08:44:05 -0700312 drawState->fViewMatrix.preConcat(preconcatMatrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000313
314 this->doEffectCoordChanges(preconcatMatrix);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000315 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000316}
317
bsalomon@google.com137f1342013-05-29 21:27:53 +0000318bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000319 this->restore();
320
bsalomon@google.com137f1342013-05-29 21:27:53 +0000321 if (NULL == drawState) {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000322 return false;
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000323 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000324
bsalomon@google.com137f1342013-05-29 21:27:53 +0000325 if (drawState->getViewMatrix().isIdentity()) {
326 return true;
327 }
328
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000329 fViewMatrix = drawState->getViewMatrix();
joshualitt4dd99882014-11-11 08:51:30 -0800330 if (0 == drawState->numFragmentStages()) {
bsalomon2ed5ef82014-07-07 08:44:05 -0700331 drawState->fViewMatrix.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000332 fDrawState = drawState;
333 fNumColorStages = 0;
334 fSavedCoordChanges.reset(0);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000335 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000336 return true;
337 } else {
338 SkMatrix inv;
339 if (!fViewMatrix.invert(&inv)) {
340 return false;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000341 }
bsalomon2ed5ef82014-07-07 08:44:05 -0700342 drawState->fViewMatrix.reset();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000343 fDrawState = drawState;
344 this->doEffectCoordChanges(inv);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000345 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;)
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000346 return true;
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000347 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000348}
349
350void GrDrawState::AutoViewMatrixRestore::doEffectCoordChanges(const SkMatrix& coordChangeMatrix) {
joshualitt4dd99882014-11-11 08:51:30 -0800351 fSavedCoordChanges.reset(fDrawState->numFragmentStages());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000352 int i = 0;
353
354 fNumColorStages = fDrawState->numColorStages();
355 for (int s = 0; s < fNumColorStages; ++s, ++i) {
egdaniel776bdbd2014-08-06 11:07:02 -0700356 fDrawState->getColorStage(s).saveCoordChange(&fSavedCoordChanges[i]);
egdaniel8cbf3d52014-08-21 06:27:22 -0700357 fDrawState->fColorStages[s].localCoordChange(coordChangeMatrix);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000358 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000359
360 int numCoverageStages = fDrawState->numCoverageStages();
361 for (int s = 0; s < numCoverageStages; ++s, ++i) {
egdaniel776bdbd2014-08-06 11:07:02 -0700362 fDrawState->getCoverageStage(s).saveCoordChange(&fSavedCoordChanges[i]);
egdaniel8cbf3d52014-08-21 06:27:22 -0700363 fDrawState->fCoverageStages[s].localCoordChange(coordChangeMatrix);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000364 }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000365}
egdaniel21aed572014-08-26 12:24:06 -0700366
egdaniel170f90b2014-09-16 12:54:40 -0700367////////////////////////////////////////////////////////////////////////////////
368
egdaniel170f90b2014-09-16 12:54:40 -0700369GrDrawState::~GrDrawState() {
egdaniel170f90b2014-09-16 12:54:40 -0700370 SkASSERT(0 == fBlockEffectRemovalCnt);
371}
372
egdaniel89af44a2014-09-26 06:15:04 -0700373////////////////////////////////////////////////////////////////////////////////
374
joshualitt8c0f6152014-12-10 14:12:22 -0800375bool GrDrawState::srcAlphaWillBeOne(GrColor color, GrColor coverage) const {
376 this->calcColorInvariantOutput(color);
377 if (this->isCoverageDrawing()) {
378 this->calcCoverageInvariantOutput(coverage);
379 return (fColorProcInfo.isOpaque() && fCoverageProcInfo.isOpaque());
380 }
381 return fColorProcInfo.isOpaque();
382}
383
384bool GrDrawState::willBlendWithDst(GrColor color, GrColor coverage) const {
385 this->calcColorInvariantOutput(color);
386 this->calcCoverageInvariantOutput(coverage);
egdaniel95131432014-12-09 11:15:43 -0800387 return fXPFactory->willBlendWithDst(fColorProcInfo, fCoverageProcInfo,
388 this->isCoverageDrawing(), this->isColorWriteDisabled());
egdanielcd8b6302014-11-11 14:46:05 -0800389}
390
joshualitt2e3b3e32014-12-09 13:31:14 -0800391void GrDrawState::calcColorInvariantOutput(GrColor color) const {
392 if (!fColorProcInfoValid || color != fColorCache) {
joshualitt8c0f6152014-12-10 14:12:22 -0800393 GrColorComponentFlags flags;
394 if (this->hasColorVertexAttribute()) {
395 if (fHints & kVertexColorsAreOpaque_Hint) {
396 flags = kA_GrColorComponentFlag;
397 color = 0xFF << GrColor_SHIFT_A;
398 } else {
399 flags = static_cast<GrColorComponentFlags>(0);
400 color = 0;
401 }
402 } else {
403 flags = kRGBA_GrColorComponentFlags;
404 }
405 fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(),
406 color, flags, false);
egdanielb6cbc382014-11-13 11:00:34 -0800407 fColorProcInfoValid = true;
joshualitt2e3b3e32014-12-09 13:31:14 -0800408 fColorCache = color;
egdanielb6cbc382014-11-13 11:00:34 -0800409 }
410}
411
joshualitt2e3b3e32014-12-09 13:31:14 -0800412void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const {
413 if (!fCoverageProcInfoValid || coverage != fCoverageCache) {
joshualitt8c0f6152014-12-10 14:12:22 -0800414 GrColorComponentFlags flags;
415 // Check if per-vertex or constant color may have partial alpha
416 if (this->hasCoverageVertexAttribute()) {
417 flags = static_cast<GrColorComponentFlags>(0);
418 coverage = 0;
419 } else {
420 flags = kRGBA_GrColorComponentFlags;
421 }
422 fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), this->numCoverageStages(),
423 coverage, flags, true, fGeometryProcessor.get());
egdanielb6cbc382014-11-13 11:00:34 -0800424 fCoverageProcInfoValid = true;
joshualitt2e3b3e32014-12-09 13:31:14 -0800425 fCoverageCache = coverage;
egdanielb6cbc382014-11-13 11:00:34 -0800426 }
427}