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 "GrOptDrawState.h" | 10 #include "GrOptDrawState.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 102 } |
103 } | 103 } |
104 return aIsOpaque ? kB_CombinedState : kA_CombinedState; | 104 return aIsOpaque ? kB_CombinedState : kA_CombinedState; |
105 } | 105 } |
106 } | 106 } |
107 return kAOrB_CombinedState; | 107 return kAOrB_CombinedState; |
108 } | 108 } |
109 | 109 |
110 //////////////////////////////////////////////////////////////////////////////s | 110 //////////////////////////////////////////////////////////////////////////////s |
111 | 111 |
112 GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatr
ix) | 112 GrDrawState::GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatr
ix) { |
113 : fCachedOptState(NULL) { | |
114 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) | 113 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
115 *this = state; | 114 *this = state; |
116 if (!preConcatMatrix.isIdentity()) { | 115 if (!preConcatMatrix.isIdentity()) { |
117 for (int i = 0; i < this->numColorStages(); ++i) { | 116 for (int i = 0; i < this->numColorStages(); ++i) { |
118 fColorStages[i].localCoordChange(preConcatMatrix); | 117 fColorStages[i].localCoordChange(preConcatMatrix); |
119 } | 118 } |
120 for (int i = 0; i < this->numCoverageStages(); ++i) { | 119 for (int i = 0; i < this->numCoverageStages(); ++i) { |
121 fCoverageStages[i].localCoordChange(preConcatMatrix); | 120 fCoverageStages[i].localCoordChange(preConcatMatrix); |
122 } | 121 } |
123 this->invalidateOptState(); | |
124 } | 122 } |
125 } | 123 } |
126 | 124 |
127 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { | 125 GrDrawState& GrDrawState::operator=(const GrDrawState& that) { |
128 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | 126 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
129 SkASSERT(!that.fRenderTarget.ownsPendingIO()); | 127 SkASSERT(!that.fRenderTarget.ownsPendingIO()); |
130 SkASSERT(!this->fRenderTarget.ownsPendingIO()); | 128 SkASSERT(!this->fRenderTarget.ownsPendingIO()); |
131 this->setRenderTarget(that.getRenderTarget()); | 129 this->setRenderTarget(that.getRenderTarget()); |
132 fColor = that.fColor; | 130 fColor = that.fColor; |
133 fViewMatrix = that.fViewMatrix; | 131 fViewMatrix = that.fViewMatrix; |
(...skipping 10 matching lines...) Expand all Loading... |
144 if (that.hasGeometryProcessor()) { | 142 if (that.hasGeometryProcessor()) { |
145 fGeometryProcessor.initAndRef(that.fGeometryProcessor); | 143 fGeometryProcessor.initAndRef(that.fGeometryProcessor); |
146 } else { | 144 } else { |
147 fGeometryProcessor.reset(NULL); | 145 fGeometryProcessor.reset(NULL); |
148 } | 146 } |
149 fColorStages = that.fColorStages; | 147 fColorStages = that.fColorStages; |
150 fCoverageStages = that.fCoverageStages; | 148 fCoverageStages = that.fCoverageStages; |
151 | 149 |
152 fHints = that.fHints; | 150 fHints = that.fHints; |
153 | 151 |
154 SkRefCnt_SafeAssign(fCachedOptState, that.fCachedOptState); | |
155 | |
156 memcpy(fFixedFunctionVertexAttribIndices, | 152 memcpy(fFixedFunctionVertexAttribIndices, |
157 that.fFixedFunctionVertexAttribIndices, | 153 that.fFixedFunctionVertexAttribIndices, |
158 sizeof(fFixedFunctionVertexAttribIndices)); | 154 sizeof(fFixedFunctionVertexAttribIndices)); |
159 return *this; | 155 return *this; |
160 } | 156 } |
161 | 157 |
162 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { | 158 void GrDrawState::onReset(const SkMatrix* initialViewMatrix) { |
163 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | 159 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
164 SkASSERT(!fRenderTarget.ownsPendingIO()); | 160 SkASSERT(!fRenderTarget.ownsPendingIO()); |
165 | 161 |
(...skipping 13 matching lines...) Expand all Loading... |
179 } | 175 } |
180 fSrcBlend = kOne_GrBlendCoeff; | 176 fSrcBlend = kOne_GrBlendCoeff; |
181 fDstBlend = kZero_GrBlendCoeff; | 177 fDstBlend = kZero_GrBlendCoeff; |
182 fBlendConstant = 0x0; | 178 fBlendConstant = 0x0; |
183 fFlagBits = 0x0; | 179 fFlagBits = 0x0; |
184 fStencilSettings.setDisabled(); | 180 fStencilSettings.setDisabled(); |
185 fCoverage = 0xff; | 181 fCoverage = 0xff; |
186 fDrawFace = kBoth_DrawFace; | 182 fDrawFace = kBoth_DrawFace; |
187 | 183 |
188 fHints = 0; | 184 fHints = 0; |
189 | |
190 this->invalidateOptState(); | |
191 } | 185 } |
192 | 186 |
193 bool GrDrawState::setIdentityViewMatrix() { | 187 bool GrDrawState::setIdentityViewMatrix() { |
194 if (this->numTotalStages()) { | 188 if (this->numTotalStages()) { |
195 SkMatrix invVM; | 189 SkMatrix invVM; |
196 if (!fViewMatrix.invert(&invVM)) { | 190 if (!fViewMatrix.invert(&invVM)) { |
197 // sad trombone sound | 191 // sad trombone sound |
198 return false; | 192 return false; |
199 } | 193 } |
200 for (int s = 0; s < this->numColorStages(); ++s) { | 194 for (int s = 0; s < this->numColorStages(); ++s) { |
201 fColorStages[s].localCoordChange(invVM); | 195 fColorStages[s].localCoordChange(invVM); |
202 } | 196 } |
203 for (int s = 0; s < this->numCoverageStages(); ++s) { | 197 for (int s = 0; s < this->numCoverageStages(); ++s) { |
204 fCoverageStages[s].localCoordChange(invVM); | 198 fCoverageStages[s].localCoordChange(invVM); |
205 } | 199 } |
206 } | 200 } |
207 this->invalidateOptState(); | |
208 fViewMatrix.reset(); | 201 fViewMatrix.reset(); |
209 return true; | 202 return true; |
210 } | 203 } |
211 | 204 |
212 void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende
rTarget* rt) { | 205 void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende
rTarget* rt) { |
213 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | 206 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
214 | 207 |
215 fGeometryProcessor.reset(NULL); | 208 fGeometryProcessor.reset(NULL); |
216 fColorStages.reset(); | 209 fColorStages.reset(); |
217 fCoverageStages.reset(); | 210 fCoverageStages.reset(); |
(...skipping 19 matching lines...) Expand all Loading... |
237 | 230 |
238 // Enable the clip bit | 231 // Enable the clip bit |
239 this->enableState(GrDrawState::kClip_StateBit); | 232 this->enableState(GrDrawState::kClip_StateBit); |
240 | 233 |
241 this->setColor(paint.getColor()); | 234 this->setColor(paint.getColor()); |
242 this->setState(GrDrawState::kDither_StateBit, paint.isDither()); | 235 this->setState(GrDrawState::kDither_StateBit, paint.isDither()); |
243 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); | 236 this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias()); |
244 | 237 |
245 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff()); | 238 this->setBlendFunc(paint.getSrcBlendCoeff(), paint.getDstBlendCoeff()); |
246 this->setCoverage(paint.getCoverage()); | 239 this->setCoverage(paint.getCoverage()); |
247 this->invalidateOptState(); | |
248 } | 240 } |
249 | 241 |
250 //////////////////////////////////////////////////////////////////////////////// | 242 //////////////////////////////////////////////////////////////////////////////// |
251 | 243 |
252 bool GrDrawState::validateVertexAttribs() const { | 244 bool GrDrawState::validateVertexAttribs() const { |
253 // check consistency of effects and attributes | 245 // check consistency of effects and attributes |
254 GrSLType slTypes[kMaxVertexAttribCnt]; | 246 GrSLType slTypes[kMaxVertexAttribCnt]; |
255 for (int i = 0; i < kMaxVertexAttribCnt; ++i) { | 247 for (int i = 0; i < kMaxVertexAttribCnt; ++i) { |
256 slTypes[i] = static_cast<GrSLType>(-1); | 248 slTypes[i] = static_cast<GrSLType>(-1); |
257 } | 249 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 fFixedFunctionVertexAttribIndices[attribs[i].fBinding] = i; | 327 fFixedFunctionVertexAttribIndices[attribs[i].fBinding] = i; |
336 } | 328 } |
337 #ifdef SK_DEBUG | 329 #ifdef SK_DEBUG |
338 size_t dwordCount = GrVertexAttribTypeSize(attribs[i].fType) >> 2; | 330 size_t dwordCount = GrVertexAttribTypeSize(attribs[i].fType) >> 2; |
339 uint32_t mask = (1 << dwordCount)-1; | 331 uint32_t mask = (1 << dwordCount)-1; |
340 size_t offsetShift = attribs[i].fOffset >> 2; | 332 size_t offsetShift = attribs[i].fOffset >> 2; |
341 SkASSERT(!(overlapCheck & (mask << offsetShift))); | 333 SkASSERT(!(overlapCheck & (mask << offsetShift))); |
342 overlapCheck |= (mask << offsetShift); | 334 overlapCheck |= (mask << offsetShift); |
343 #endif | 335 #endif |
344 } | 336 } |
345 this->invalidateOptState(); | |
346 // Positions must be specified. | 337 // Positions must be specified. |
347 SkASSERT(-1 != fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBin
ding]); | 338 SkASSERT(-1 != fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBin
ding]); |
348 } | 339 } |
349 | 340 |
350 //////////////////////////////////////////////////////////////////////////////// | 341 //////////////////////////////////////////////////////////////////////////////// |
351 | 342 |
352 void GrDrawState::setDefaultVertexAttribs() { | 343 void GrDrawState::setDefaultVertexAttribs() { |
353 static const GrVertexAttrib kPositionAttrib = | 344 static const GrVertexAttrib kPositionAttrib = |
354 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}; | 345 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}; |
355 | 346 |
356 fVAPtr = &kPositionAttrib; | 347 fVAPtr = &kPositionAttrib; |
357 fVACount = 1; | 348 fVACount = 1; |
358 fVAStride = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); | 349 fVAStride = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); |
359 | 350 |
360 // set all the fixed function indices to -1 except position. | 351 // set all the fixed function indices to -1 except position. |
361 memset(fFixedFunctionVertexAttribIndices, | 352 memset(fFixedFunctionVertexAttribIndices, |
362 0xff, | 353 0xff, |
363 sizeof(fFixedFunctionVertexAttribIndices)); | 354 sizeof(fFixedFunctionVertexAttribIndices)); |
364 fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding] = 0; | 355 fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding] = 0; |
365 this->invalidateOptState(); | |
366 } | 356 } |
367 | 357 |
368 //////////////////////////////////////////////////////////////////////////////// | 358 //////////////////////////////////////////////////////////////////////////////// |
369 | 359 |
370 bool GrDrawState::couldApplyCoverage(const GrDrawTargetCaps& caps) const { | 360 bool GrDrawState::couldApplyCoverage(const GrDrawTargetCaps& caps) const { |
371 if (caps.dualSourceBlendingSupport()) { | 361 if (caps.dualSourceBlendingSupport()) { |
372 return true; | 362 return true; |
373 } | 363 } |
374 // we can correctly apply coverage if a) we have dual source blending | 364 // we can correctly apply coverage if a) we have dual source blending |
375 // or b) one of our blend optimizations applies | 365 // or b) one of our blend optimizations applies |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 fOriginalGPID = SK_InvalidUniqueID; | 440 fOriginalGPID = SK_InvalidUniqueID; |
451 } | 441 } |
452 | 442 |
453 int m = fDrawState->numColorStages() - fColorEffectCnt; | 443 int m = fDrawState->numColorStages() - fColorEffectCnt; |
454 SkASSERT(m >= 0); | 444 SkASSERT(m >= 0); |
455 fDrawState->fColorStages.pop_back_n(m); | 445 fDrawState->fColorStages.pop_back_n(m); |
456 | 446 |
457 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; | 447 int n = fDrawState->numCoverageStages() - fCoverageEffectCnt; |
458 SkASSERT(n >= 0); | 448 SkASSERT(n >= 0); |
459 fDrawState->fCoverageStages.pop_back_n(n); | 449 fDrawState->fCoverageStages.pop_back_n(n); |
460 if (m + n > 0) { | |
461 fDrawState->invalidateOptState(); | |
462 } | |
463 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) | 450 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
464 } | 451 } |
465 fDrawState = ds; | 452 fDrawState = ds; |
466 if (NULL != ds) { | 453 if (NULL != ds) { |
467 SkASSERT(SK_InvalidUniqueID == fOriginalGPID); | 454 SkASSERT(SK_InvalidUniqueID == fOriginalGPID); |
468 if (NULL != ds->getGeometryProcessor()) { | 455 if (NULL != ds->getGeometryProcessor()) { |
469 fOriginalGPID = ds->getGeometryProcessor()->getUniqueID(); | 456 fOriginalGPID = ds->getGeometryProcessor()->getUniqueID(); |
470 } | 457 } |
471 fColorEffectCnt = ds->numColorStages(); | 458 fColorEffectCnt = ds->numColorStages(); |
472 fCoverageEffectCnt = ds->numCoverageStages(); | 459 fCoverageEffectCnt = ds->numCoverageStages(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; | 493 int numCoverageStages = fSavedCoordChanges.count() - fNumColorStages; |
507 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); | 494 SkASSERT(fDrawState->numCoverageStages() >= numCoverageStages); |
508 | 495 |
509 int i = 0; | 496 int i = 0; |
510 for (int s = 0; s < fNumColorStages; ++s, ++i) { | 497 for (int s = 0; s < fNumColorStages; ++s, ++i) { |
511 fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]
); | 498 fDrawState->fColorStages[s].restoreCoordChange(fSavedCoordChanges[i]
); |
512 } | 499 } |
513 for (int s = 0; s < numCoverageStages; ++s, ++i) { | 500 for (int s = 0; s < numCoverageStages; ++s, ++i) { |
514 fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges
[i]); | 501 fDrawState->fCoverageStages[s].restoreCoordChange(fSavedCoordChanges
[i]); |
515 } | 502 } |
516 fDrawState->invalidateOptState(); | |
517 fDrawState = NULL; | 503 fDrawState = NULL; |
518 } | 504 } |
519 } | 505 } |
520 | 506 |
521 void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, | 507 void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState, |
522 const SkMatrix& preconcatMatrix) { | 508 const SkMatrix& preconcatMatrix) { |
523 this->restore(); | 509 this->restore(); |
524 | 510 |
525 SkASSERT(NULL == fDrawState); | 511 SkASSERT(NULL == fDrawState); |
526 if (NULL == drawState || preconcatMatrix.isIdentity()) { | 512 if (NULL == drawState || preconcatMatrix.isIdentity()) { |
527 return; | 513 return; |
528 } | 514 } |
529 fDrawState = drawState; | 515 fDrawState = drawState; |
530 | 516 |
531 fViewMatrix = drawState->getViewMatrix(); | 517 fViewMatrix = drawState->getViewMatrix(); |
532 drawState->fViewMatrix.preConcat(preconcatMatrix); | 518 drawState->fViewMatrix.preConcat(preconcatMatrix); |
533 | 519 |
534 this->doEffectCoordChanges(preconcatMatrix); | 520 this->doEffectCoordChanges(preconcatMatrix); |
535 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) | 521 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
536 drawState->invalidateOptState(); | |
537 } | 522 } |
538 | 523 |
539 bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) { | 524 bool GrDrawState::AutoViewMatrixRestore::setIdentity(GrDrawState* drawState) { |
540 this->restore(); | 525 this->restore(); |
541 | 526 |
542 if (NULL == drawState) { | 527 if (NULL == drawState) { |
543 return false; | 528 return false; |
544 } | 529 } |
545 | 530 |
546 if (drawState->getViewMatrix().isIdentity()) { | 531 if (drawState->getViewMatrix().isIdentity()) { |
547 return true; | 532 return true; |
548 } | 533 } |
549 | 534 |
550 drawState->invalidateOptState(); | |
551 fViewMatrix = drawState->getViewMatrix(); | 535 fViewMatrix = drawState->getViewMatrix(); |
552 if (0 == drawState->numTotalStages()) { | 536 if (0 == drawState->numTotalStages()) { |
553 drawState->fViewMatrix.reset(); | 537 drawState->fViewMatrix.reset(); |
554 fDrawState = drawState; | 538 fDrawState = drawState; |
555 fNumColorStages = 0; | 539 fNumColorStages = 0; |
556 fSavedCoordChanges.reset(0); | 540 fSavedCoordChanges.reset(0); |
557 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) | 541 SkDEBUGCODE(++fDrawState->fBlockEffectRemovalCnt;) |
558 return true; | 542 return true; |
559 } else { | 543 } else { |
560 SkMatrix inv; | 544 SkMatrix inv; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 if (fGeometryProcessor) { | 581 if (fGeometryProcessor) { |
598 fGeometryProcessor.convertToPendingExec(); | 582 fGeometryProcessor.convertToPendingExec(); |
599 } | 583 } |
600 for (int i = 0; i < fCoverageStages.count(); ++i) { | 584 for (int i = 0; i < fCoverageStages.count(); ++i) { |
601 fCoverageStages[i].convertToPendingExec(); | 585 fCoverageStages[i].convertToPendingExec(); |
602 } | 586 } |
603 } | 587 } |
604 | 588 |
605 //////////////////////////////////////////////////////////////////////////////// | 589 //////////////////////////////////////////////////////////////////////////////// |
606 | 590 |
607 void GrDrawState::invalidateOptState() const { | |
608 SkSafeSetNull(fCachedOptState); | |
609 } | |
610 | |
611 //////////////////////////////////////////////////////////////////////////////// | |
612 | |
613 GrDrawState::~GrDrawState() { | 591 GrDrawState::~GrDrawState() { |
614 SkSafeUnref(fCachedOptState); | |
615 SkASSERT(0 == fBlockEffectRemovalCnt); | 592 SkASSERT(0 == fBlockEffectRemovalCnt); |
616 } | 593 } |
617 | 594 |
618 //////////////////////////////////////////////////////////////////////////////// | 595 //////////////////////////////////////////////////////////////////////////////// |
619 | 596 |
620 GrDrawState::BlendOptFlags GrDrawState::getBlendOpts(bool forceCoverage, | 597 GrDrawState::BlendOptFlags GrDrawState::getBlendOpts(bool forceCoverage, |
621 GrBlendCoeff* srcCoeff, | 598 GrBlendCoeff* srcCoeff, |
622 GrBlendCoeff* dstCoeff) con
st { | 599 GrBlendCoeff* dstCoeff) con
st { |
623 GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; | 600 GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; |
624 if (NULL == srcCoeff) { | 601 if (NULL == srcCoeff) { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 } | 735 } |
759 | 736 |
760 // Since the shader will multiply coverage and color, the only way the f
inal A==1 is if | 737 // Since the shader will multiply coverage and color, the only way the f
inal A==1 is if |
761 // coverage and color both have A==1. | 738 // coverage and color both have A==1. |
762 return (inoutColor.isOpaque() && inoutCoverage.isOpaque()); | 739 return (inoutColor.isOpaque() && inoutCoverage.isOpaque()); |
763 } | 740 } |
764 | 741 |
765 return inoutColor.isOpaque(); | 742 return inoutColor.isOpaque(); |
766 } | 743 } |
767 | 744 |
OLD | NEW |