| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrDrawState.h" | 8 #include "GrDrawState.h" |
| 9 | 9 |
| 10 #include "GrBlend.h" | 10 #include "GrBlend.h" |
| 11 #include "GrOptDrawState.h" | 11 #include "GrOptDrawState.h" |
| 12 #include "GrPaint.h" | 12 #include "GrPaint.h" |
| 13 #include "GrProcOptInfo.h" | 13 #include "GrProcOptInfo.h" |
| 14 #include "GrXferProcessor.h" | 14 #include "GrXferProcessor.h" |
| 15 #include "effects/GrPorterDuffXferProcessor.h" | 15 #include "effects/GrPorterDuffXferProcessor.h" |
| 16 | 16 |
| 17 bool GrDrawState::isEqual(const GrDrawState& that, bool explicitLocalCoords) con
st { | 17 bool GrDrawState::isEqual(const GrDrawState& that) const { |
| 18 if (this->getRenderTarget() != that.getRenderTarget() || | 18 if (this->getRenderTarget() != that.getRenderTarget() || |
| 19 this->fColorStages.count() != that.fColorStages.count() || | 19 this->fColorStages.count() != that.fColorStages.count() || |
| 20 this->fCoverageStages.count() != that.fCoverageStages.count() || | 20 this->fCoverageStages.count() != that.fCoverageStages.count() || |
| 21 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || | 21 !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
| 22 this->fFlagBits != that.fFlagBits || | 22 this->fFlagBits != that.fFlagBits || |
| 23 this->fStencilSettings != that.fStencilSettings || | 23 this->fStencilSettings != that.fStencilSettings || |
| 24 this->fDrawFace != that.fDrawFace) { | 24 this->fDrawFace != that.fDrawFace) { |
| 25 return false; | 25 return false; |
| 26 } | 26 } |
| 27 | 27 |
| 28 bool explicitLocalCoords = this->hasLocalCoordAttribute(); |
| 29 if (this->hasGeometryProcessor()) { |
| 30 if (!that.hasGeometryProcessor()) { |
| 31 return false; |
| 32 } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProce
ssor())) { |
| 33 return false; |
| 34 } |
| 35 } else if (that.hasGeometryProcessor()) { |
| 36 return false; |
| 37 } |
| 38 |
| 28 if (!this->getXPFactory()->isEqual(*that.getXPFactory())) { | 39 if (!this->getXPFactory()->isEqual(*that.getXPFactory())) { |
| 29 return false; | 40 return false; |
| 30 } | 41 } |
| 31 | 42 |
| 32 for (int i = 0; i < this->numColorStages(); i++) { | 43 for (int i = 0; i < this->numColorStages(); i++) { |
| 33 if (!GrFragmentStage::AreCompatible(this->getColorStage(i), that.getColo
rStage(i), | 44 if (!GrFragmentStage::AreCompatible(this->getColorStage(i), that.getColo
rStage(i), |
| 34 explicitLocalCoords)) { | 45 explicitLocalCoords)) { |
| 35 return false; | 46 return false; |
| 36 } | 47 } |
| 37 } | 48 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 59 } | 70 } |
| 60 } | 71 } |
| 61 } | 72 } |
| 62 | 73 |
| 63 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { | 74 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
| 64 fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); | 75 fRenderTarget.reset(SkSafeRef(that.fRenderTarget.get())); |
| 65 fViewMatrix = that.fViewMatrix; | 76 fViewMatrix = that.fViewMatrix; |
| 66 fFlagBits = that.fFlagBits; | 77 fFlagBits = that.fFlagBits; |
| 67 fStencilSettings = that.fStencilSettings; | 78 fStencilSettings = that.fStencilSettings; |
| 68 fDrawFace = that.fDrawFace; | 79 fDrawFace = that.fDrawFace; |
| 80 fGeometryProcessor.reset(SkSafeRef(that.fGeometryProcessor.get())); |
| 69 fXPFactory.reset(SkRef(that.getXPFactory())); | 81 fXPFactory.reset(SkRef(that.getXPFactory())); |
| 70 fColorStages = that.fColorStages; | 82 fColorStages = that.fColorStages; |
| 71 fCoverageStages = that.fCoverageStages; | 83 fCoverageStages = that.fCoverageStages; |
| 72 | 84 |
| 73 fHints = that.fHints; | 85 fHints = that.fHints; |
| 74 | 86 |
| 75 fColorProcInfoValid = that.fColorProcInfoValid; | 87 fColorProcInfoValid = that.fColorProcInfoValid; |
| 76 fCoverageProcInfoValid = that.fCoverageProcInfoValid; | 88 fCoverageProcInfoValid = that.fCoverageProcInfoValid; |
| 77 if (fColorProcInfoValid) { | 89 if (fColorProcInfoValid) { |
| 78 fColorProcInfo = that.fColorProcInfo; | 90 fColorProcInfo = that.fColorProcInfo; |
| 79 } | 91 } |
| 80 if (fCoverageProcInfoValid) { | 92 if (fCoverageProcInfoValid) { |
| 81 fCoverageProcInfo = that.fCoverageProcInfo; | 93 fCoverageProcInfo = that.fCoverageProcInfo; |
| 82 } | 94 } |
| 83 return *this; | 95 return *this; |
| 84 } | 96 } |
| 85 | 97 |
| 86 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { | 98 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
| 87 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); | 99 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
| 88 fRenderTarget.reset(NULL); | 100 fRenderTarget.reset(NULL); |
| 89 | 101 |
| 102 fGeometryProcessor.reset(NULL); |
| 90 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); | 103 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); |
| 91 fColorStages.reset(); | 104 fColorStages.reset(); |
| 92 fCoverageStages.reset(); | 105 fCoverageStages.reset(); |
| 93 | 106 |
| 94 if (NULL == initialViewMatrix) { | 107 if (NULL == initialViewMatrix) { |
| 95 fViewMatrix.reset(); | 108 fViewMatrix.reset(); |
| 96 } else { | 109 } else { |
| 97 fViewMatrix = *initialViewMatrix; | 110 fViewMatrix = *initialViewMatrix; |
| 98 } | 111 } |
| 99 fFlagBits = 0x0; | 112 fFlagBits = 0x0; |
| 100 fStencilSettings.setDisabled(); | 113 fStencilSettings.setDisabled(); |
| 101 fDrawFace = kBoth_DrawFace; | 114 fDrawFace = kBoth_DrawFace; |
| 102 | 115 |
| 103 fHints = 0; | 116 fHints = 0; |
| 104 | 117 |
| 105 fColorProcInfoValid = false; | 118 fColorProcInfoValid = false; |
| 106 fCoverageProcInfoValid = false; | 119 fCoverageProcInfoValid = false; |
| 107 | 120 |
| 108 fColorCache = GrColor_ILLEGAL; | 121 fColorCache = GrColor_ILLEGAL; |
| 109 fCoverageCache = GrColor_ILLEGAL; | 122 fCoverageCache = GrColor_ILLEGAL; |
| 110 | |
| 111 fColorPrimProc = NULL; | |
| 112 fCoveragePrimProc = NULL; | |
| 113 | |
| 114 } | 123 } |
| 115 | 124 |
| 116 bool GrDrawState::setIdentityViewMatrix() { | 125 bool GrDrawState::setIdentityViewMatrix() { |
| 117 if (this->numFragmentStages()) { | 126 if (this->numFragmentStages()) { |
| 118 SkMatrix invVM; | 127 SkMatrix invVM; |
| 119 if (!fViewMatrix.invert(&invVM)) { | 128 if (!fViewMatrix.invert(&invVM)) { |
| 120 // sad trombone sound | 129 // sad trombone sound |
| 121 return false; | 130 return false; |
| 122 } | 131 } |
| 123 for (int s = 0; s < this->numColorStages(); ++s) { | 132 for (int s = 0; s < this->numColorStages(); ++s) { |
| 124 fColorStages[s].localCoordChange(invVM); | 133 fColorStages[s].localCoordChange(invVM); |
| 125 } | 134 } |
| 126 for (int s = 0; s < this->numCoverageStages(); ++s) { | 135 for (int s = 0; s < this->numCoverageStages(); ++s) { |
| 127 fCoverageStages[s].localCoordChange(invVM); | 136 fCoverageStages[s].localCoordChange(invVM); |
| 128 } | 137 } |
| 129 } | 138 } |
| 130 fViewMatrix.reset(); | 139 fViewMatrix.reset(); |
| 131 return true; | 140 return true; |
| 132 } | 141 } |
| 133 | 142 |
| 134 void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende
rTarget* rt) { | 143 void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende
rTarget* rt) { |
| 135 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages()); | 144 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
| 136 | 145 |
| 146 fGeometryProcessor.reset(NULL); |
| 137 fColorStages.reset(); | 147 fColorStages.reset(); |
| 138 fCoverageStages.reset(); | 148 fCoverageStages.reset(); |
| 139 | 149 |
| 140 for (int i = 0; i < paint.numColorStages(); ++i) { | 150 for (int i = 0; i < paint.numColorStages(); ++i) { |
| 141 fColorStages.push_back(paint.getColorStage(i)); | 151 fColorStages.push_back(paint.getColorStage(i)); |
| 142 } | 152 } |
| 143 | 153 |
| 144 for (int i = 0; i < paint.numCoverageStages(); ++i) { | 154 for (int i = 0; i < paint.numCoverageStages(); ++i) { |
| 145 fCoverageStages.push_back(paint.getCoverageStage(i)); | 155 fCoverageStages.push_back(paint.getCoverageStage(i)); |
| 146 } | 156 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 this->calcColorInvariantOutput(color); | 190 this->calcColorInvariantOutput(color); |
| 181 | 191 |
| 182 // The coverage isn't actually white, its unknown, but this will produce the
same effect | 192 // The coverage isn't actually white, its unknown, but this will produce the
same effect |
| 183 // TODO we want to cache the result of this call, but we can probably clean
up the interface | 193 // TODO we want to cache the result of this call, but we can probably clean
up the interface |
| 184 // so we don't have to pass in a seemingly known coverage | 194 // so we don't have to pass in a seemingly known coverage |
| 185 this->calcCoverageInvariantOutput(GrColor_WHITE); | 195 this->calcCoverageInvariantOutput(GrColor_WHITE); |
| 186 return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo, | 196 return fXPFactory->canApplyCoverage(fColorProcInfo, fCoverageProcInfo, |
| 187 this->isCoverageDrawing(), this->isColor
WriteDisabled()); | 197 this->isCoverageDrawing(), this->isColor
WriteDisabled()); |
| 188 } | 198 } |
| 189 | 199 |
| 190 bool GrDrawState::hasSolidCoverage(const GrPrimitiveProcessor* pp) const { | 200 bool GrDrawState::hasSolidCoverage(GrColor coverage) const { |
| 191 // If we're drawing coverage directly then coverage is effectively treated a
s color. | 201 // If we're drawing coverage directly then coverage is effectively treated a
s color. |
| 192 if (this->isCoverageDrawing()) { | 202 if (this->isCoverageDrawing()) { |
| 193 return true; | 203 return true; |
| 194 } | 204 } |
| 195 | 205 |
| 196 if (this->numCoverageStages() > 0) { | 206 if (this->numCoverageStages() > 0) { |
| 197 return false; | 207 return false; |
| 198 } | 208 } |
| 199 | 209 |
| 200 this->calcCoverageInvariantOutput(pp); | 210 this->calcCoverageInvariantOutput(coverage); |
| 201 return fCoverageProcInfo.isSolidWhite(); | 211 return fCoverageProcInfo.isSolidWhite(); |
| 202 } | 212 } |
| 203 | 213 |
| 204 //////////////////////////////////////////////////////////////////////////////s | 214 //////////////////////////////////////////////////////////////////////////////s |
| 205 | 215 |
| 206 bool GrDrawState::willEffectReadDstColor(const GrPrimitiveProcessor* pp) const { | 216 bool GrDrawState::willEffectReadDstColor(GrColor color, GrColor coverage) const
{ |
| 207 this->calcColorInvariantOutput(pp); | 217 this->calcColorInvariantOutput(color); |
| 208 this->calcCoverageInvariantOutput(pp); | 218 this->calcCoverageInvariantOutput(coverage); |
| 209 // TODO: Remove need to create the XP here. | 219 // TODO: Remove need to create the XP here. |
| 210 // Also once all custom blends are turned into XPs we can remove the n
eed | 220 // Also once all custom blends are turned into XPs we can remove the n
eed |
| 211 // to check other stages since only xp's will be able to read dst | 221 // to check other stages since only xp's will be able to read dst |
| 212 SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(
fColorProcInfo, | 222 SkAutoTUnref<GrXferProcessor> xferProcessor(fXPFactory->createXferProcessor(
fColorProcInfo, |
| 213
fCoverageProcInfo)); | 223
fCoverageProcInfo)); |
| 214 if (xferProcessor && xferProcessor->willReadDstColor()) { | 224 if (xferProcessor && xferProcessor->willReadDstColor()) { |
| 215 return true; | 225 return true; |
| 216 } | 226 } |
| 217 | 227 |
| 218 if (!this->isColorWriteDisabled()) { | 228 if (!this->isColorWriteDisabled()) { |
| 219 if (fColorProcInfo.readsDst()) { | 229 if (fColorProcInfo.readsDst()) { |
| 220 return true; | 230 return true; |
| 221 } | 231 } |
| 222 } | 232 } |
| 223 return fCoverageProcInfo.readsDst(); | 233 return fCoverageProcInfo.readsDst(); |
| 224 } | 234 } |
| 225 | 235 |
| 226 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { | 236 void GrDrawState::AutoRestoreEffects::set(GrDrawState* ds) { |
| 227 if (fDrawState) { | 237 if (fDrawState) { |
| 238 // 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 |
| 228 int m = fDrawState->numColorStages() - fColorEffectCnt; | 247 int m = fDrawState->numColorStages() - fColorEffectCnt; |
| 229 SkASSERT(m >= 0); | 248 SkASSERT(m >= 0); |
| 230 fDrawState->fColorStages.pop_back_n(m); | 249 fDrawState->fColorStages.pop_back_n(m); |
| 231 | 250 |
| 232 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; | 251 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
| 233 SkASSERT(n >= 0); | 252 SkASSERT(n >= 0); |
| 234 fDrawState->fCoverageStages.pop_back_n(n); | 253 fDrawState->fCoverageStages.pop_back_n(n); |
| 235 if (m + n > 0) { | 254 if (m + n > 0) { |
| 236 fDrawState->fColorProcInfoValid = false; | 255 fDrawState->fColorProcInfoValid = false; |
| 237 fDrawState->fCoverageProcInfoValid = false; | 256 fDrawState->fCoverageProcInfoValid = false; |
| 238 } | 257 } |
| 239 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) | 258 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
| 240 } | 259 } |
| 241 fDrawState = ds; | 260 fDrawState = ds; |
| 242 if (NULL != ds) { | 261 if (NULL != ds) { |
| 262 SkASSERT(SK_InvalidUniqueID == fOriginalGPID); |
| 263 if (NULL != ds->getGeometryProcessor()) { |
| 264 fOriginalGPID = ds->getGeometryProcessor()->getUniqueID(); |
| 265 } |
| 243 fColorEffectCnt = ds->numColorStages(); | 266 fColorEffectCnt = ds->numColorStages(); |
| 244 fCoverageEffectCnt = ds->numCoverageStages(); | 267 fCoverageEffectCnt = ds->numCoverageStages(); |
| 245 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) | 268 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 246 } | 269 } |
| 247 } | 270 } |
| 248 | 271 |
| 249 //////////////////////////////////////////////////////////////////////////////// | 272 //////////////////////////////////////////////////////////////////////////////// |
| 250 | 273 |
| 251 // Some blend modes allow folding a fractional coverage value into the color's a
lpha channel, while | 274 // Some blend modes allow folding a fractional coverage value into the color's a
lpha channel, while |
| 252 // others will blend incorrectly. | 275 // others will blend incorrectly. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 } | 365 } |
| 343 | 366 |
| 344 //////////////////////////////////////////////////////////////////////////////// | 367 //////////////////////////////////////////////////////////////////////////////// |
| 345 | 368 |
| 346 GrDrawState::~GrDrawState() { | 369 GrDrawState::~GrDrawState() { |
| 347 SkASSERT(0 == fBlockEffectRemovalCnt); | 370 SkASSERT(0 == fBlockEffectRemovalCnt); |
| 348 } | 371 } |
| 349 | 372 |
| 350 //////////////////////////////////////////////////////////////////////////////// | 373 //////////////////////////////////////////////////////////////////////////////// |
| 351 | 374 |
| 352 bool GrDrawState::willBlendWithDst(const GrPrimitiveProcessor* pp) const { | 375 bool GrDrawState::srcAlphaWillBeOne(GrColor color, GrColor coverage) const { |
| 353 this->calcColorInvariantOutput(pp); | 376 this->calcColorInvariantOutput(color); |
| 354 this->calcCoverageInvariantOutput(pp); | 377 if (this->isCoverageDrawing()) { |
| 378 this->calcCoverageInvariantOutput(coverage); |
| 379 return (fColorProcInfo.isOpaque() && fCoverageProcInfo.isOpaque()); |
| 380 } |
| 381 return fColorProcInfo.isOpaque(); |
| 382 } |
| 383 |
| 384 bool GrDrawState::willBlendWithDst(GrColor color, GrColor coverage) const { |
| 385 this->calcColorInvariantOutput(color); |
| 386 this->calcCoverageInvariantOutput(coverage); |
| 355 return fXPFactory->willBlendWithDst(fColorProcInfo, fCoverageProcInfo, | 387 return fXPFactory->willBlendWithDst(fColorProcInfo, fCoverageProcInfo, |
| 356 this->isCoverageDrawing(), this->isColor
WriteDisabled()); | 388 this->isCoverageDrawing(), this->isColor
WriteDisabled()); |
| 357 } | 389 } |
| 358 | 390 |
| 359 void GrDrawState::calcColorInvariantOutput(const GrPrimitiveProcessor* pp) const
{ | |
| 360 if (!fColorProcInfoValid || fColorPrimProc != pp) { | |
| 361 fColorProcInfo.calcColorWithPrimProc(pp, fColorStages.begin(), this->num
ColorStages()); | |
| 362 fColorProcInfoValid = true; | |
| 363 fColorPrimProc = pp; | |
| 364 } | |
| 365 } | |
| 366 | |
| 367 void GrDrawState::calcCoverageInvariantOutput(const GrPrimitiveProcessor* pp) co
nst { | |
| 368 if (!fCoverageProcInfoValid || fCoveragePrimProc != pp) { | |
| 369 fCoverageProcInfo.calcCoverageWithPrimProc(pp, fCoverageStages.begin(), | |
| 370 this->numCoverageStages()); | |
| 371 fCoverageProcInfoValid = true; | |
| 372 fCoveragePrimProc = pp; | |
| 373 } | |
| 374 } | |
| 375 | |
| 376 void GrDrawState::calcColorInvariantOutput(GrColor color) const { | 391 void GrDrawState::calcColorInvariantOutput(GrColor color) const { |
| 377 if (!fColorProcInfoValid || color != fColorCache) { | 392 if (!fColorProcInfoValid || color != fColorCache) { |
| 378 GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; | 393 GrColorComponentFlags flags; |
| 379 fColorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColo
rStages(), color, | 394 if (this->hasColorVertexAttribute()) { |
| 380 flags, false); | 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->numColo
rStages(), |
| 406 color, flags, false); |
| 381 fColorProcInfoValid = true; | 407 fColorProcInfoValid = true; |
| 382 fColorCache = color; | 408 fColorCache = color; |
| 383 } | 409 } |
| 384 } | 410 } |
| 385 | 411 |
| 386 void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const { | 412 void GrDrawState::calcCoverageInvariantOutput(GrColor coverage) const { |
| 387 if (!fCoverageProcInfoValid || coverage != fCoverageCache) { | 413 if (!fCoverageProcInfoValid || coverage != fCoverageCache) { |
| 388 GrColorComponentFlags flags = kRGBA_GrColorComponentFlags; | 414 GrColorComponentFlags flags; |
| 389 fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), | 415 // Check if per-vertex or constant color may have partial alpha |
| 390 this->numCoverageStages(), c
overage, flags, | 416 if (this->hasCoverageVertexAttribute()) { |
| 391 true); | 417 flags = static_cast<GrColorComponentFlags>(0); |
| 418 coverage = 0; |
| 419 } else { |
| 420 flags = kRGBA_GrColorComponentFlags; |
| 421 } |
| 422 fCoverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), this->n
umCoverageStages(), |
| 423 coverage, flags, true, fGeometry
Processor.get()); |
| 392 fCoverageProcInfoValid = true; | 424 fCoverageProcInfoValid = true; |
| 393 fCoverageCache = coverage; | 425 fCoverageCache = coverage; |
| 394 } | 426 } |
| 395 } | 427 } |
| OLD | NEW |