| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #ifndef GrDrawState_DEFINED | 8 #ifndef GrDrawState_DEFINED |
| 9 #define GrDrawState_DEFINED | 9 #define GrDrawState_DEFINED |
| 10 | 10 |
| 11 #include "GrBackendEffectFactory.h" | 11 #include "GrBackendEffectFactory.h" |
| 12 #include "GrColor.h" | 12 #include "GrColor.h" |
| 13 #include "GrEffectStage.h" | 13 #include "GrEffectStage.h" |
| 14 #include "GrPaint.h" | 14 #include "GrPaint.h" |
| 15 #include "GrRefCnt.h" | 15 #include "GrRefCnt.h" |
| 16 #include "GrRenderTarget.h" | 16 #include "GrRenderTarget.h" |
| 17 #include "GrStencil.h" | 17 #include "GrStencil.h" |
| 18 #include "GrTemplates.h" | 18 #include "GrTemplates.h" |
| 19 #include "GrTexture.h" | 19 #include "GrTexture.h" |
| 20 #include "GrTypesPriv.h" | 20 #include "GrTypesPriv.h" |
| 21 #include "effects/GrSimpleTextureEffect.h" | 21 #include "effects/GrSimpleTextureEffect.h" |
| 22 | 22 |
| 23 #include "SkMatrix.h" | 23 #include "SkMatrix.h" |
| 24 #include "SkXfermode.h" | 24 #include "SkXfermode.h" |
| 25 | 25 |
| 26 class GrDrawState : public GrRefCnt { | 26 class GrDrawState : public GrRefCnt { |
| 27 public: | 27 public: |
| 28 SK_DECLARE_INST_COUNT(GrDrawState) | 28 SK_DECLARE_INST_COUNT(GrDrawState) |
| 29 | 29 |
| 30 /** | 30 GrDrawState() { |
| 31 * Total number of effect stages. Each stage can host a GrEffect. A stage is
enabled if it has a | 31 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 32 * GrEffect. The effect produces an output color in the fragment shader. It'
s inputs are the | 32 this->reset(); |
| 33 * output from the previous enabled stage and a position. The position is ei
ther derived from | 33 } |
| 34 * the interpolated vertex positions or explicit per-vertex coords, dependin
g upon the | |
| 35 * GrAttribBindings used to draw. | |
| 36 * | |
| 37 * The stages are divided into two sets, color-computing and coverage-comput
ing. The final color | |
| 38 * stage produces the final pixel color. The coverage-computing stages funct
ion exactly as the | |
| 39 * color-computing but the output of the final coverage stage is treated as
a fractional pixel | |
| 40 * coverage rather than as input to the src/dst color blend step. | |
| 41 * | |
| 42 * The input color to the first enabled color-stage is either the constant c
olor or interpolated | |
| 43 * per-vertex colors. The input to the first coverage stage is either a cons
tant coverage | |
| 44 * (usually full-coverage) or interpolated per-vertex coverage. | |
| 45 * | |
| 46 * See the documentation of kCoverageDrawing_StateBit for information about
disabling the | |
| 47 * the color / coverage distinction. | |
| 48 * | |
| 49 * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied f
rom the client's | |
| 50 * GrPaint. Stage GrPaint::kTotalStages is earmarked for use by GrTextContex
t, GrPathRenderer- | |
| 51 * derived classes, and the rect/oval helper classes. GrPaint::kTotalStages+
1 is earmarked for | |
| 52 * clipping by GrClipMaskManager. TODO: replace fixed size array of stages w
ith variable size | |
| 53 * arrays of color and coverage stages. | |
| 54 */ | |
| 55 enum { | |
| 56 kNumStages = GrPaint::kTotalStages + 2, | |
| 57 }; | |
| 58 | 34 |
| 59 GrDrawState() { this->reset(); } | 35 GrDrawState(const SkMatrix& initialViewMatrix) { |
| 60 | 36 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 61 GrDrawState(const SkMatrix& initialViewMatrix) { this->reset(initialViewMatr
ix); } | 37 this->reset(initialViewMatrix); |
| 38 } |
| 62 | 39 |
| 63 /** | 40 /** |
| 64 * Copies another draw state. | 41 * Copies another draw state. |
| 65 **/ | 42 **/ |
| 66 GrDrawState(const GrDrawState& state) { | 43 GrDrawState(const GrDrawState& state) { |
| 44 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 67 *this = state; | 45 *this = state; |
| 68 } | 46 } |
| 69 | 47 |
| 70 /** | 48 /** |
| 71 * Copies another draw state with a preconcat to the view matrix. | 49 * Copies another draw state with a preconcat to the view matrix. |
| 72 **/ | 50 **/ |
| 73 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) { | 51 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) { |
| 52 GR_DEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 74 *this = state; | 53 *this = state; |
| 75 if (!preConcatMatrix.isIdentity()) { | 54 if (!preConcatMatrix.isIdentity()) { |
| 76 for (int i = 0; i < kNumStages; ++i) { | 55 for (int i = 0; i < fColorStages.count(); ++i) { |
| 77 if (this->isStageEnabled(i)) { | 56 fColorStages[i].localCoordChange(preConcatMatrix); |
| 78 fStages[i].localCoordChange(preConcatMatrix); | 57 } |
| 79 } | 58 for (int i = 0; i < fCoverageStages.count(); ++i) { |
| 59 fCoverageStages[i].localCoordChange(preConcatMatrix); |
| 80 } | 60 } |
| 81 } | 61 } |
| 82 } | 62 } |
| 83 | 63 |
| 84 virtual ~GrDrawState() { this->disableStages(); } | 64 virtual ~GrDrawState() { GrAssert(0 == fBlockEffectRemovalCnt); } |
| 85 | 65 |
| 86 /** | 66 /** |
| 87 * Resets to the default state. GrEffects will be removed from all stages. | 67 * Resets to the default state. GrEffects will be removed from all stages. |
| 88 */ | 68 */ |
| 89 void reset() { this->onReset(NULL); } | 69 void reset() { this->onReset(NULL); } |
| 90 | 70 |
| 91 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMa
trix); } | 71 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMa
trix); } |
| 92 | 72 |
| 93 /** | 73 /** |
| 94 * Initializes the GrDrawState based on a GrPaint, view matrix and render ta
rget. Note that | 74 * Initializes the GrDrawState based on a GrPaint, view matrix and render ta
rget. Note that |
| 95 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that ha
ve no GrPaint | 75 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that ha
ve no GrPaint |
| 96 * equivalents are set to default values. GrPaint has fewer stages than GrDr
awState. The extra | 76 * equivalents are set to default values. Clipping will be enabled. |
| 97 * GrDrawState stages are disabled. Clipping will be enabled. | |
| 98 */ | 77 */ |
| 99 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarge
t*); | 78 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarge
t*); |
| 100 | 79 |
| 101 /////////////////////////////////////////////////////////////////////////// | 80 /////////////////////////////////////////////////////////////////////////// |
| 102 /// @name Vertex Attributes | 81 /// @name Vertex Attributes |
| 103 //// | 82 //// |
| 104 | 83 |
| 105 enum { | 84 enum { |
| 106 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, | 85 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
| 107 }; | 86 }; |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 } | 331 } |
| 353 | 332 |
| 354 GrColor getCoverage() const { | 333 GrColor getCoverage() const { |
| 355 return fCommon.fCoverage; | 334 return fCommon.fCoverage; |
| 356 } | 335 } |
| 357 | 336 |
| 358 /// @} | 337 /// @} |
| 359 | 338 |
| 360 /////////////////////////////////////////////////////////////////////////// | 339 /////////////////////////////////////////////////////////////////////////// |
| 361 /// @name Effect Stages | 340 /// @name Effect Stages |
| 341 /// Each stage hosts a GrEffect. The effect produces an output color or cove
rage in the fragment |
| 342 /// shader. Its inputs are the output from the previous stage as well as som
e variables |
| 343 /// available to it in the fragment and vertex shader (e.g. the vertex posit
ion, the dst color, |
| 344 /// the fragment position, local coordinates). |
| 345 /// |
| 346 /// The stages are divided into two sets, color-computing and coverage-compu
ting. The final |
| 347 /// color stage produces the final pixel color. The coverage-computing stage
s function exactly |
| 348 /// as the color-computing but the output of the final coverage stage is tre
ated as a fractional |
| 349 /// pixel coverage rather than as input to the src/dst color blend step. |
| 350 /// |
| 351 /// The input color to the first color-stage is either the constant color or
interpolated |
| 352 /// per-vertex colors. The input to the first coverage stage is either a con
stant coverage |
| 353 /// (usually full-coverage) or interpolated per-vertex coverage. |
| 354 /// |
| 355 /// See the documentation of kCoverageDrawing_StateBit for information about
disabling the |
| 356 /// the color / coverage distinction. |
| 362 //// | 357 //// |
| 363 | 358 |
| 364 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) { | 359 const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1,
int attr1 = -1) { |
| 365 fStages[stageIdx].setEffect(effect); | 360 GrAssert(NULL != effect); |
| 361 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, att
r1)); |
| 366 return effect; | 362 return effect; |
| 367 } | 363 } |
| 368 | 364 |
| 369 const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect, | 365 const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 =
-1, int attr1 = -1) { |
| 370 int attr0, int attr1 = -1) { | 366 GrAssert(NULL != effect); |
| 371 fStages[stageIdx].setEffect(effect, attr0, attr1); | 367 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0,
attr1)); |
| 372 return effect; | 368 return effect; |
| 373 } | 369 } |
| 374 | 370 |
| 375 /** | 371 /** |
| 376 * Creates a GrSimpleTextureEffect that uses local coords as texture coordin
ates. | 372 * Creates a GrSimpleTextureEffect that uses local coords as texture coordin
ates. |
| 377 */ | 373 */ |
| 378 void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& m
atrix) { | 374 void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) { |
| 379 GrAssert(!this->getStage(stageIdx).getEffect()); | |
| 380 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); | 375 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); |
| 381 this->setEffect(stageIdx, effect)->unref(); | 376 this->addColorEffect(effect)->unref(); |
| 382 } | |
| 383 void createTextureEffect(int stageIdx, | |
| 384 GrTexture* texture, | |
| 385 const SkMatrix& matrix, | |
| 386 const GrTextureParams& params) { | |
| 387 GrAssert(!this->getStage(stageIdx).getEffect()); | |
| 388 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, par
ams); | |
| 389 this->setEffect(stageIdx, effect)->unref(); | |
| 390 } | 377 } |
| 391 | 378 |
| 392 bool stagesDisabled() { | 379 void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) { |
| 393 for (int i = 0; i < kNumStages; ++i) { | 380 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); |
| 394 if (NULL != fStages[i].getEffect()) { | 381 this->addCoverageEffect(effect)->unref(); |
| 395 return false; | |
| 396 } | |
| 397 } | |
| 398 return true; | |
| 399 } | 382 } |
| 400 | 383 |
| 401 void disableStage(int stageIdx) { | 384 void addColorTextureEffect(GrTexture* texture, |
| 402 this->setEffect(stageIdx, NULL); | 385 const SkMatrix& matrix, |
| 386 const GrTextureParams& params) { |
| 387 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, par
ams); |
| 388 this->addColorEffect(effect)->unref(); |
| 389 } |
| 390 |
| 391 void addCoverageTextureEffect(GrTexture* texture, |
| 392 const SkMatrix& matrix, |
| 393 const GrTextureParams& params) { |
| 394 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, par
ams); |
| 395 this->addCoverageEffect(effect)->unref(); |
| 403 } | 396 } |
| 404 | 397 |
| 405 /** | 398 /** |
| 406 * Release all the GrEffects referred to by this draw state. | 399 * When this object is destroyed it will remove any effects from the draw st
ate that were added |
| 400 * after its constructor. |
| 407 */ | 401 */ |
| 408 void disableStages() { | 402 class AutoRestoreEffects : public ::GrNoncopyable { |
| 409 for (int i = 0; i < kNumStages; ++i) { | 403 public: |
| 410 this->disableStage(i); | 404 AutoRestoreEffects() : fDrawState(NULL) {} |
| 411 } | |
| 412 } | |
| 413 | 405 |
| 414 class AutoStageDisable : public ::GrNoncopyable { | 406 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL) { this->set(ds);
} |
| 415 public: | 407 |
| 416 AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {} | 408 ~AutoRestoreEffects() { this->set(NULL); } |
| 417 ~AutoStageDisable() { | 409 |
| 410 void set(GrDrawState* ds) { |
| 418 if (NULL != fDrawState) { | 411 if (NULL != fDrawState) { |
| 419 fDrawState->disableStages(); | 412 int n = fDrawState->fColorStages.count() - fColorEffectCnt; |
| 413 GrAssert(n >= 0); |
| 414 fDrawState->fColorStages.pop_back_n(n); |
| 415 n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt; |
| 416 GrAssert(n >= 0); |
| 417 fDrawState->fCoverageStages.pop_back_n(n); |
| 418 GR_DEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) |
| 419 } |
| 420 fDrawState = ds; |
| 421 if (NULL != ds) { |
| 422 fColorEffectCnt = ds->fColorStages.count(); |
| 423 fCoverageEffectCnt = ds->fCoverageStages.count(); |
| 424 GR_DEBUGCODE(++ds->fBlockEffectRemovalCnt;) |
| 420 } | 425 } |
| 421 } | 426 } |
| 427 |
| 422 private: | 428 private: |
| 423 GrDrawState* fDrawState; | 429 GrDrawState* fDrawState; |
| 430 int fColorEffectCnt; |
| 431 int fCoverageEffectCnt; |
| 424 }; | 432 }; |
| 425 | 433 |
| 426 /** | 434 int numColorStages() const { return fColorStages.count(); } |
| 427 * Returns the current stage by index. | 435 int numCoverageStages() const { return fCoverageStages.count(); } |
| 428 */ | 436 int numTotalStages() const { return this->numColorStages() + this->numCovera
geStages(); } |
| 429 const GrEffectStage& getStage(int stageIdx) const { | |
| 430 GrAssert((unsigned)stageIdx < kNumStages); | |
| 431 return fStages[stageIdx]; | |
| 432 } | |
| 433 | 437 |
| 434 /** | 438 const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages
[stageIdx]; } |
| 435 * Called when the source coord system is changing. This ensures that effect
s will see the | 439 const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverage
Stages[stageIdx]; } |
| 436 * correct local coordinates. oldToNew gives the transformation from the old
coord system in | |
| 437 * which the geometry was specified to the new coordinate system from which
it will be rendered. | |
| 438 */ | |
| 439 void localCoordChange(const SkMatrix& oldToNew) { | |
| 440 for (int i = 0; i < kNumStages; ++i) { | |
| 441 if (this->isStageEnabled(i)) { | |
| 442 fStages[i].localCoordChange(oldToNew); | |
| 443 } | |
| 444 } | |
| 445 } | |
| 446 | 440 |
| 447 /** | 441 /** |
| 448 * Checks whether any of the effects will read the dst pixel color. | 442 * Checks whether any of the effects will read the dst pixel color. |
| 449 */ | 443 */ |
| 450 bool willEffectReadDstColor() const; | 444 bool willEffectReadDstColor() const; |
| 451 | 445 |
| 452 /// @} | 446 /// @} |
| 453 | 447 |
| 454 /////////////////////////////////////////////////////////////////////////// | 448 /////////////////////////////////////////////////////////////////////////// |
| 455 /// @name Coverage / Color Stages | |
| 456 //// | |
| 457 | |
| 458 /** | |
| 459 * A common pattern is to compute a color with the initial stages and then | |
| 460 * modulate that color by a coverage value in later stage(s) (AA, mask- | |
| 461 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be | |
| 462 * computed based on the pre-coverage-modulated color. The division of | |
| 463 * stages between color-computing and coverage-computing is specified by | |
| 464 * this method. Initially this is kNumStages (all stages | |
| 465 * are color-computing). | |
| 466 */ | |
| 467 void setFirstCoverageStage(int firstCoverageStage) { | |
| 468 GrAssert((unsigned)firstCoverageStage <= kNumStages); | |
| 469 fCommon.fFirstCoverageStage = firstCoverageStage; | |
| 470 } | |
| 471 | |
| 472 /** | |
| 473 * Gets the index of the first coverage-computing stage. | |
| 474 */ | |
| 475 int getFirstCoverageStage() const { | |
| 476 return fCommon.fFirstCoverageStage; | |
| 477 } | |
| 478 | |
| 479 ///@} | |
| 480 | |
| 481 /////////////////////////////////////////////////////////////////////////// | |
| 482 /// @name Blending | 449 /// @name Blending |
| 483 //// | 450 //// |
| 484 | 451 |
| 485 /** | 452 /** |
| 486 * Sets the blending function coefficients. | 453 * Sets the blending function coefficients. |
| 487 * | 454 * |
| 488 * The blend function will be: | 455 * The blend function will be: |
| 489 * D' = sat(S*srcCoef + D*dstCoef) | 456 * D' = sat(S*srcCoef + D*dstCoef) |
| 490 * | 457 * |
| 491 * where D is the existing destination color, S is the incoming source | 458 * where D is the existing destination color, S is the incoming source |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 */ | 634 */ |
| 668 void restore(); | 635 void restore(); |
| 669 | 636 |
| 670 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix); | 637 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix); |
| 671 | 638 |
| 672 /** Sets the draw state's matrix to identity. This can fail because the
current view matrix | 639 /** Sets the draw state's matrix to identity. This can fail because the
current view matrix |
| 673 is not invertible. */ | 640 is not invertible. */ |
| 674 bool setIdentity(GrDrawState* drawState); | 641 bool setIdentity(GrDrawState* drawState); |
| 675 | 642 |
| 676 private: | 643 private: |
| 677 GrDrawState* fDrawState; | 644 void doEffectCoordChanges(const SkMatrix& coordChangeMatrix); |
| 678 SkMatrix fViewMatrix; | 645 |
| 679 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNum
Stages]; | 646 GrDrawState* fDrawState; |
| 680 uint32_t fRestoreMask; | 647 SkMatrix fViewMatrix; |
| 648 int fNumColorStages; |
| 649 SkAutoSTArray<8, GrEffectStage::SavedCoordChange> fSavedCoordChanges; |
| 681 }; | 650 }; |
| 682 | 651 |
| 683 /// @} | 652 /// @} |
| 684 | 653 |
| 685 /////////////////////////////////////////////////////////////////////////// | 654 /////////////////////////////////////////////////////////////////////////// |
| 686 /// @name Render Target | 655 /// @name Render Target |
| 687 //// | 656 //// |
| 688 | 657 |
| 689 /** | 658 /** |
| 690 * Sets the render-target used at the next drawing call | 659 * Sets the render-target used at the next drawing call |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 * Gets whether the target is drawing clockwise, counterclockwise, | 867 * Gets whether the target is drawing clockwise, counterclockwise, |
| 899 * or both faces. | 868 * or both faces. |
| 900 * @return the current draw face(s). | 869 * @return the current draw face(s). |
| 901 */ | 870 */ |
| 902 DrawFace getDrawFace() const { return fCommon.fDrawFace; } | 871 DrawFace getDrawFace() const { return fCommon.fDrawFace; } |
| 903 | 872 |
| 904 /// @} | 873 /// @} |
| 905 | 874 |
| 906 /////////////////////////////////////////////////////////////////////////// | 875 /////////////////////////////////////////////////////////////////////////// |
| 907 | 876 |
| 908 bool isStageEnabled(int s) const { | |
| 909 GrAssert((unsigned)s < kNumStages); | |
| 910 return (NULL != fStages[s].getEffect()); | |
| 911 } | |
| 912 | |
| 913 bool operator ==(const GrDrawState& s) const { | 877 bool operator ==(const GrDrawState& s) const { |
| 914 if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon
) { | 878 if (fRenderTarget.get() != s.fRenderTarget.get() || |
| 879 fColorStages.count() != s.fColorStages.count() || |
| 880 fCoverageStages.count() != s.fCoverageStages.count() || |
| 881 fCommon != s.fCommon) { |
| 915 return false; | 882 return false; |
| 916 } | 883 } |
| 917 for (int i = 0; i < kNumStages; i++) { | 884 for (int i = 0; i < fColorStages.count(); i++) { |
| 918 bool enabled = this->isStageEnabled(i); | 885 if (fColorStages[i] != s.fColorStages[i]) { |
| 919 if (enabled != s.isStageEnabled(i)) { | |
| 920 return false; | |
| 921 } | |
| 922 if (enabled && this->fStages[i] != s.fStages[i]) { | |
| 923 return false; | 886 return false; |
| 924 } | 887 } |
| 925 } | 888 } |
| 889 for (int i = 0; i < fCoverageStages.count(); i++) { |
| 890 if (fCoverageStages[i] != s.fCoverageStages[i]) { |
| 891 return false; |
| 892 } |
| 893 } |
| 926 return true; | 894 return true; |
| 927 } | 895 } |
| 928 bool operator !=(const GrDrawState& s) const { return !(*this == s); } | 896 bool operator !=(const GrDrawState& s) const { return !(*this == s); } |
| 929 | 897 |
| 930 GrDrawState& operator= (const GrDrawState& s) { | 898 GrDrawState& operator= (const GrDrawState& s) { |
| 899 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
| 931 this->setRenderTarget(s.fRenderTarget.get()); | 900 this->setRenderTarget(s.fRenderTarget.get()); |
| 932 fCommon = s.fCommon; | 901 fCommon = s.fCommon; |
| 933 for (int i = 0; i < kNumStages; i++) { | 902 fColorStages = s.fColorStages; |
| 934 if (s.isStageEnabled(i)) { | 903 fCoverageStages = s.fCoverageStages; |
| 935 this->fStages[i] = s.fStages[i]; | |
| 936 } | |
| 937 } | |
| 938 return *this; | 904 return *this; |
| 939 } | 905 } |
| 940 | 906 |
| 941 private: | 907 private: |
| 942 | 908 |
| 943 void onReset(const SkMatrix* initialViewMatrix) { | 909 void onReset(const SkMatrix* initialViewMatrix) { |
| 944 | 910 GrAssert(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); |
| 945 this->disableStages(); | 911 fColorStages.reset(); |
| 912 fCoverageStages.reset(); |
| 946 | 913 |
| 947 fRenderTarget.reset(NULL); | 914 fRenderTarget.reset(NULL); |
| 948 | 915 |
| 949 this->setDefaultVertexAttribs(); | 916 this->setDefaultVertexAttribs(); |
| 950 | 917 |
| 951 fCommon.fColor = 0xffffffff; | 918 fCommon.fColor = 0xffffffff; |
| 952 if (NULL == initialViewMatrix) { | 919 if (NULL == initialViewMatrix) { |
| 953 fCommon.fViewMatrix.reset(); | 920 fCommon.fViewMatrix.reset(); |
| 954 } else { | 921 } else { |
| 955 fCommon.fViewMatrix = *initialViewMatrix; | 922 fCommon.fViewMatrix = *initialViewMatrix; |
| 956 } | 923 } |
| 957 fCommon.fSrcBlend = kOne_GrBlendCoeff; | 924 fCommon.fSrcBlend = kOne_GrBlendCoeff; |
| 958 fCommon.fDstBlend = kZero_GrBlendCoeff; | 925 fCommon.fDstBlend = kZero_GrBlendCoeff; |
| 959 fCommon.fBlendConstant = 0x0; | 926 fCommon.fBlendConstant = 0x0; |
| 960 fCommon.fFlagBits = 0x0; | 927 fCommon.fFlagBits = 0x0; |
| 961 fCommon.fStencilSettings.setDisabled(); | 928 fCommon.fStencilSettings.setDisabled(); |
| 962 fCommon.fFirstCoverageStage = kNumStages; | |
| 963 fCommon.fCoverage = 0xffffffff; | 929 fCommon.fCoverage = 0xffffffff; |
| 964 fCommon.fColorFilterMode = SkXfermode::kDst_Mode; | 930 fCommon.fColorFilterMode = SkXfermode::kDst_Mode; |
| 965 fCommon.fColorFilterColor = 0x0; | 931 fCommon.fColorFilterColor = 0x0; |
| 966 fCommon.fDrawFace = kBoth_DrawFace; | 932 fCommon.fDrawFace = kBoth_DrawFace; |
| 967 } | 933 } |
| 968 | 934 |
| 969 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState.
*/ | 935 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState.
*/ |
| 970 struct CommonState { | 936 struct CommonState { |
| 971 // These fields are roughly sorted by decreasing likelihood of being dif
ferent in op== | 937 // These fields are roughly sorted by decreasing likelihood of being dif
ferent in op== |
| 972 GrColor fColor; | 938 GrColor fColor; |
| 973 SkMatrix fViewMatrix; | 939 SkMatrix fViewMatrix; |
| 974 GrBlendCoeff fSrcBlend; | 940 GrBlendCoeff fSrcBlend; |
| 975 GrBlendCoeff fDstBlend; | 941 GrBlendCoeff fDstBlend; |
| 976 GrColor fBlendConstant; | 942 GrColor fBlendConstant; |
| 977 uint32_t fFlagBits; | 943 uint32_t fFlagBits; |
| 978 const GrVertexAttrib* fVAPtr; | 944 const GrVertexAttrib* fVAPtr; |
| 979 int fVACount; | 945 int fVACount; |
| 980 GrStencilSettings fStencilSettings; | 946 GrStencilSettings fStencilSettings; |
| 981 int fFirstCoverageStage; | |
| 982 GrColor fCoverage; | 947 GrColor fCoverage; |
| 983 SkXfermode::Mode fColorFilterMode; | 948 SkXfermode::Mode fColorFilterMode; |
| 984 GrColor fColorFilterColor; | 949 GrColor fColorFilterColor; |
| 985 DrawFace fDrawFace; | 950 DrawFace fDrawFace; |
| 986 | 951 |
| 987 // This is simply a different representation of info in fVertexAttribs a
nd thus does | 952 // This is simply a different representation of info in fVertexAttribs a
nd thus does |
| 988 // not need to be compared in op==. | 953 // not need to be compared in op==. |
| 989 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindin
gCnt]; | 954 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindin
gCnt]; |
| 990 | 955 |
| 991 bool operator== (const CommonState& other) const { | 956 bool operator== (const CommonState& other) const { |
| 992 bool result = fColor == other.fColor && | 957 bool result = fColor == other.fColor && |
| 993 fViewMatrix.cheapEqualTo(other.fViewMatrix) && | 958 fViewMatrix.cheapEqualTo(other.fViewMatrix) && |
| 994 fSrcBlend == other.fSrcBlend && | 959 fSrcBlend == other.fSrcBlend && |
| 995 fDstBlend == other.fDstBlend && | 960 fDstBlend == other.fDstBlend && |
| 996 fBlendConstant == other.fBlendConstant && | 961 fBlendConstant == other.fBlendConstant && |
| 997 fFlagBits == other.fFlagBits && | 962 fFlagBits == other.fFlagBits && |
| 998 fVACount == other.fVACount && | 963 fVACount == other.fVACount && |
| 999 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVert
exAttrib)) && | 964 !memcmp(fVAPtr, other.fVAPtr, fVACount * sizeof(GrVert
exAttrib)) && |
| 1000 fStencilSettings == other.fStencilSettings && | 965 fStencilSettings == other.fStencilSettings && |
| 1001 fFirstCoverageStage == other.fFirstCoverageStage && | |
| 1002 fCoverage == other.fCoverage && | 966 fCoverage == other.fCoverage && |
| 1003 fColorFilterMode == other.fColorFilterMode && | 967 fColorFilterMode == other.fColorFilterMode && |
| 1004 fColorFilterColor == other.fColorFilterColor && | 968 fColorFilterColor == other.fColorFilterColor && |
| 1005 fDrawFace == other.fDrawFace; | 969 fDrawFace == other.fDrawFace; |
| 1006 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices, | 970 GrAssert(!result || 0 == memcmp(fFixedFunctionVertexAttribIndices, |
| 1007 other.fFixedFunctionVertexAttribIndi
ces, | 971 other.fFixedFunctionVertexAttribIndi
ces, |
| 1008 sizeof(fFixedFunctionVertexAttribInd
ices))); | 972 sizeof(fFixedFunctionVertexAttribInd
ices))); |
| 1009 return result; | 973 return result; |
| 1010 } | 974 } |
| 1011 bool operator!= (const CommonState& other) const { return !(*this == oth
er); } | 975 bool operator!= (const CommonState& other) const { return !(*this == oth
er); } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1035 ~DeferredState() { SkSafeUnref(fRenderTarget); } | 999 ~DeferredState() { SkSafeUnref(fRenderTarget); } |
| 1036 | 1000 |
| 1037 void saveFrom(const GrDrawState& drawState) { | 1001 void saveFrom(const GrDrawState& drawState) { |
| 1038 fCommon = drawState.fCommon; | 1002 fCommon = drawState.fCommon; |
| 1039 // TODO: Here we will copy the GrRenderTarget pointer without taking
a ref. | 1003 // TODO: Here we will copy the GrRenderTarget pointer without taking
a ref. |
| 1040 fRenderTarget = drawState.fRenderTarget.get(); | 1004 fRenderTarget = drawState.fRenderTarget.get(); |
| 1041 SkSafeRef(fRenderTarget); | 1005 SkSafeRef(fRenderTarget); |
| 1042 // Here we ref the effects directly rather than the effect-refs. TOD
O: When the effect- | 1006 // Here we ref the effects directly rather than the effect-refs. TOD
O: When the effect- |
| 1043 // ref gets fully unref'ed it will cause the underlying effect to un
ref its resources | 1007 // ref gets fully unref'ed it will cause the underlying effect to un
ref its resources |
| 1044 // and recycle them to the cache (if no one else is holding a ref to
the resources). | 1008 // and recycle them to the cache (if no one else is holding a ref to
the resources). |
| 1045 for (int i = 0; i < kNumStages; ++i) { | 1009 fStages.reset(drawState.fColorStages.count() + drawState.fCoverageSt
ages.count()); |
| 1046 fStages[i].saveFrom(drawState.fStages[i]); | 1010 fColorStageCnt = drawState.fColorStages.count(); |
| 1011 for (int i = 0; i < fColorStageCnt; ++i) { |
| 1012 fStages[i].saveFrom(drawState.fColorStages[i]); |
| 1013 } |
| 1014 for (int i = 0; i < drawState.fCoverageStages.count(); ++i) { |
| 1015 fStages[i + fColorStageCnt].saveFrom(drawState.fCoverageStages[i
]); |
| 1047 } | 1016 } |
| 1048 GR_DEBUGCODE(fInitialized = true;) | 1017 GR_DEBUGCODE(fInitialized = true;) |
| 1049 } | 1018 } |
| 1050 | 1019 |
| 1051 void restoreTo(GrDrawState* drawState) { | 1020 void restoreTo(GrDrawState* drawState) { |
| 1052 GrAssert(fInitialized); | 1021 GrAssert(fInitialized); |
| 1053 drawState->fCommon = fCommon; | 1022 drawState->fCommon = fCommon; |
| 1054 drawState->setRenderTarget(fRenderTarget); | 1023 drawState->setRenderTarget(fRenderTarget); |
| 1055 for (int i = 0; i < kNumStages; ++i) { | 1024 // reinflate color/cov stage arrays. |
| 1056 fStages[i].restoreTo(&drawState->fStages[i]); | 1025 drawState->fColorStages.reset(fColorStageCnt); |
| 1026 for (int i = 0; i < fColorStageCnt; ++i) { |
| 1027 fStages[i].restoreTo(&drawState->fColorStages[i]); |
| 1028 } |
| 1029 int coverageStageCnt = fStages.count() - fColorStageCnt; |
| 1030 drawState->fCoverageStages.reset(coverageStageCnt); |
| 1031 for (int i = 0; i < coverageStageCnt; ++i) { |
| 1032 fStages[fColorStageCnt + i].restoreTo(&drawState->fCoverageStage
s[i]); |
| 1057 } | 1033 } |
| 1058 } | 1034 } |
| 1059 | 1035 |
| 1060 bool isEqual(const GrDrawState& state) const { | 1036 bool isEqual(const GrDrawState& state) const { |
| 1061 if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.f
Common) { | 1037 int numCoverageStages = fStages.count() - fColorStageCnt; |
| 1038 if (fRenderTarget != state.fRenderTarget.get() || |
| 1039 fColorStageCnt != state.fColorStages.count() || |
| 1040 numCoverageStages != state.fCoverageStages.count() || |
| 1041 fCommon != state.fCommon) { |
| 1062 return false; | 1042 return false; |
| 1063 } | 1043 } |
| 1064 for (int i = 0; i < kNumStages; ++i) { | 1044 bool explicitLocalCoords = state.hasLocalCoordAttribute(); |
| 1065 if (!fStages[i].isEqual(state.fStages[i], state.hasLocalCoordAtt
ribute())) { | 1045 for (int i = 0; i < fColorStageCnt; ++i) { |
| 1046 if (!fStages[i].isEqual(state.fColorStages[i], explicitLocalCoor
ds)) { |
| 1066 return false; | 1047 return false; |
| 1067 } | 1048 } |
| 1068 } | 1049 } |
| 1050 for (int i = 0; i < numCoverageStages; ++i) { |
| 1051 int s = fColorStageCnt + i; |
| 1052 if (!fStages[s].isEqual(state.fCoverageStages[i], explicitLocalC
oords)) { |
| 1053 return false; |
| 1054 } |
| 1055 } |
| 1069 return true; | 1056 return true; |
| 1070 } | 1057 } |
| 1071 | 1058 |
| 1072 private: | 1059 private: |
| 1060 typedef SkAutoSTArray<8, GrEffectStage::DeferredStage> DeferredStageArra
y; |
| 1061 |
| 1073 GrRenderTarget* fRenderTarget; | 1062 GrRenderTarget* fRenderTarget; |
| 1074 CommonState fCommon; | 1063 CommonState fCommon; |
| 1075 GrEffectStage::DeferredStage fStages[kNumStages]; | 1064 int fColorStageCnt; |
| 1065 DeferredStageArray fStages; |
| 1076 | 1066 |
| 1077 GR_DEBUGCODE(bool fInitialized;) | 1067 GR_DEBUGCODE(bool fInitialized;) |
| 1078 }; | 1068 }; |
| 1079 | 1069 |
| 1080 private: | 1070 private: |
| 1081 | 1071 |
| 1082 SkAutoTUnref<GrRenderTarget> fRenderTarget; | 1072 SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 1083 CommonState fCommon; | 1073 CommonState fCommon; |
| 1084 GrEffectStage fStages[kNumStages]; | 1074 |
| 1075 typedef SkSTArray<4, GrEffectStage> EffectStageArray; |
| 1076 EffectStageArray fColorStages; |
| 1077 EffectStageArray fCoverageStages; |
| 1078 |
| 1079 // Some of the auto restore objects assume that no effects are removed durin
g their lifetime. |
| 1080 // This is used to assert that this condition holds. |
| 1081 GR_DEBUGCODE(int fBlockEffectRemovalCnt;) |
| 1085 | 1082 |
| 1086 /** | 1083 /** |
| 1087 * Sets vertex attributes for next draw. | 1084 * Sets vertex attributes for next draw. |
| 1088 * | 1085 * |
| 1089 * @param attribs the array of vertex attributes to set. | 1086 * @param attribs the array of vertex attributes to set. |
| 1090 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. | 1087 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. |
| 1091 */ | 1088 */ |
| 1092 void setVertexAttribs(const GrVertexAttrib attribs[], int count); | 1089 void setVertexAttribs(const GrVertexAttrib attribs[], int count); |
| 1093 | 1090 |
| 1094 typedef GrRefCnt INHERITED; | 1091 typedef GrRefCnt INHERITED; |
| 1095 }; | 1092 }; |
| 1096 | 1093 |
| 1097 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); | 1094 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
| 1098 | 1095 |
| 1099 #endif | 1096 #endif |
| OLD | NEW |