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 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied f
rom the client's | 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- | 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 | 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 | 52 * clipping by GrClipMaskManager. TODO: replace fixed size array of stages w
ith variable size |
53 * arrays of color and coverage stages. | 53 * arrays of color and coverage stages. |
54 */ | 54 */ |
55 enum { | 55 enum { |
56 kNumStages = GrPaint::kTotalStages + 2, | 56 kNumStages = GrPaint::kTotalStages + 2, |
57 }; | 57 }; |
58 | 58 |
59 GrDrawState() { | 59 GrDrawState() { this->reset(); } |
60 this->reset(); | |
61 } | |
62 | 60 |
| 61 GrDrawState(const SkMatrix& initialViewMatrix) { this->reset(initialViewMatr
ix); } |
| 62 |
| 63 /** |
| 64 * Copies another draw state. |
| 65 **/ |
63 GrDrawState(const GrDrawState& state) { | 66 GrDrawState(const GrDrawState& state) { |
64 *this = state; | 67 *this = state; |
65 } | 68 } |
66 | 69 |
67 virtual ~GrDrawState() { | 70 /** |
68 this->disableStages(); | 71 * Copies another draw state with a preconcat to the view matrix. |
| 72 **/ |
| 73 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) { |
| 74 *this = state; |
| 75 if (!preConcatMatrix.isIdentity()) { |
| 76 for (int i = 0; i < kNumStages; ++i) { |
| 77 if (this->isStageEnabled(i)) { |
| 78 fStages[i].localCoordChange(preConcatMatrix); |
| 79 } |
| 80 } |
| 81 } |
69 } | 82 } |
70 | 83 |
| 84 virtual ~GrDrawState() { this->disableStages(); } |
| 85 |
71 /** | 86 /** |
72 * Resets to the default state. | 87 * Resets to the default state. GrEffects will be removed from all stages. |
73 * GrEffects will be removed from all stages. | |
74 */ | 88 */ |
75 void reset() { | 89 void reset() { this->onReset(NULL); } |
76 | 90 |
77 this->disableStages(); | 91 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMa
trix); } |
78 | |
79 fRenderTarget.reset(NULL); | |
80 | |
81 this->setDefaultVertexAttribs(); | |
82 | |
83 fCommon.fColor = 0xffffffff; | |
84 fCommon.fViewMatrix.reset(); | |
85 fCommon.fSrcBlend = kOne_GrBlendCoeff; | |
86 fCommon.fDstBlend = kZero_GrBlendCoeff; | |
87 fCommon.fBlendConstant = 0x0; | |
88 fCommon.fFlagBits = 0x0; | |
89 fCommon.fStencilSettings.setDisabled(); | |
90 fCommon.fFirstCoverageStage = kNumStages; | |
91 fCommon.fCoverage = 0xffffffff; | |
92 fCommon.fColorFilterMode = SkXfermode::kDst_Mode; | |
93 fCommon.fColorFilterColor = 0x0; | |
94 fCommon.fDrawFace = kBoth_DrawFace; | |
95 } | |
96 | 92 |
97 /** | 93 /** |
98 * Initializes the GrDrawState based on a GrPaint, view matrix and render ta
rget. Note that | 94 * Initializes the GrDrawState based on a GrPaint, view matrix and render ta
rget. Note that |
99 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that ha
ve no GrPaint | 95 * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that ha
ve no GrPaint |
100 * equivalents are set to default values. GrPaint has fewer stages than GrDr
awState. The extra | 96 * equivalents are set to default values. GrPaint has fewer stages than GrDr
awState. The extra |
101 * GrDrawState stages are disabled. | 97 * GrDrawState stages are disabled. |
102 */ | 98 */ |
103 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarge
t*); | 99 void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarge
t*); |
104 | 100 |
105 /////////////////////////////////////////////////////////////////////////// | 101 /////////////////////////////////////////////////////////////////////////// |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 GrBlendCoeff* srcCoeff = NULL, | 605 GrBlendCoeff* srcCoeff = NULL, |
610 GrBlendCoeff* dstCoeff = NULL) const; | 606 GrBlendCoeff* dstCoeff = NULL) const; |
611 | 607 |
612 /// @} | 608 /// @} |
613 | 609 |
614 /////////////////////////////////////////////////////////////////////////// | 610 /////////////////////////////////////////////////////////////////////////// |
615 /// @name View Matrix | 611 /// @name View Matrix |
616 //// | 612 //// |
617 | 613 |
618 /** | 614 /** |
619 * Sets the matrix applied to vertex positions. | 615 * Sets the view matrix to identity and updates any installed effects to com
pensate for the |
620 * | 616 * coord system change. |
621 * In the post-view-matrix space the rectangle [0,w]x[0,h] | |
622 * fully covers the render target. (w and h are the width and height of the | |
623 * the render-target.) | |
624 */ | 617 */ |
625 void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; } | 618 bool setIdentityViewMatrix(); |
626 | |
627 /** | |
628 * Gets a writable pointer to the view matrix. | |
629 */ | |
630 SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; } | |
631 | |
632 /** | |
633 * Multiplies the current view matrix by a matrix | |
634 * | |
635 * After this call V' = V*m where V is the old view matrix, | |
636 * m is the parameter to this function, and V' is the new view matrix. | |
637 * (We consider positions to be column vectors so position vector p is | |
638 * transformed by matrix X as p' = X*p.) | |
639 * | |
640 * @param m the matrix used to modify the view matrix. | |
641 */ | |
642 void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(
m); } | |
643 | |
644 /** | |
645 * Multiplies the current view matrix by a matrix | |
646 * | |
647 * After this call V' = m*V where V is the old view matrix, | |
648 * m is the parameter to this function, and V' is the new view matrix. | |
649 * (We consider positions to be column vectors so position vector p is | |
650 * transformed by matrix X as p' = X*p.) | |
651 * | |
652 * @param m the matrix used to modify the view matrix. | |
653 */ | |
654 void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConca
t(m); } | |
655 | 619 |
656 /** | 620 /** |
657 * Retrieves the current view matrix | 621 * Retrieves the current view matrix |
658 * @return the current view matrix. | 622 * @return the current view matrix. |
659 */ | 623 */ |
660 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; } | 624 const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; } |
661 | 625 |
662 /** | 626 /** |
663 * Retrieves the inverse of the current view matrix. | 627 * Retrieves the inverse of the current view matrix. |
664 * | 628 * |
(...skipping 13 matching lines...) Expand all Loading... |
678 } | 642 } |
679 return true; | 643 return true; |
680 } | 644 } |
681 return false; | 645 return false; |
682 } | 646 } |
683 | 647 |
684 //////////////////////////////////////////////////////////////////////////// | 648 //////////////////////////////////////////////////////////////////////////// |
685 | 649 |
686 /** | 650 /** |
687 * Preconcats the current view matrix and restores the previous view matrix
in the destructor. | 651 * Preconcats the current view matrix and restores the previous view matrix
in the destructor. |
688 * Effect matrices are automatically adjusted to compensate. | 652 * Effect matrices are automatically adjusted to compensate and adjusted bac
k in the destructor. |
689 */ | 653 */ |
690 class AutoViewMatrixRestore : public ::GrNoncopyable { | 654 class AutoViewMatrixRestore : public ::GrNoncopyable { |
691 public: | 655 public: |
692 AutoViewMatrixRestore() : fDrawState(NULL) {} | 656 AutoViewMatrixRestore() : fDrawState(NULL) {} |
693 | 657 |
694 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix)
{ | 658 AutoViewMatrixRestore(GrDrawState* ds, const SkMatrix& preconcatMatrix)
{ |
695 fDrawState = NULL; | 659 fDrawState = NULL; |
696 this->set(ds, preconcatMatrix); | 660 this->set(ds, preconcatMatrix); |
697 } | 661 } |
698 | 662 |
699 ~AutoViewMatrixRestore() { this->restore(); } | 663 ~AutoViewMatrixRestore() { this->restore(); } |
700 | 664 |
701 /** | 665 /** |
702 * Can be called prior to destructor to restore the original matrix. | 666 * Can be called prior to destructor to restore the original matrix. |
703 */ | 667 */ |
704 void restore(); | 668 void restore(); |
705 | 669 |
706 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix); | 670 void set(GrDrawState* drawState, const SkMatrix& preconcatMatrix); |
707 | 671 |
708 bool isSet() const { return NULL != fDrawState; } | 672 /** Sets the draw state's matrix to identity. This can fail because the
current view matrix |
| 673 is not invertible. */ |
| 674 bool setIdentity(GrDrawState* drawState); |
709 | 675 |
710 private: | 676 private: |
711 GrDrawState* fDrawState; | 677 GrDrawState* fDrawState; |
712 SkMatrix fViewMatrix; | |
713 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNum
Stages]; | |
714 uint32_t fRestoreMask; | |
715 }; | |
716 | |
717 //////////////////////////////////////////////////////////////////////////// | |
718 | |
719 /** | |
720 * This sets the view matrix to identity and adjusts stage matrices to compe
nsate. The | |
721 * destructor undoes the changes, restoring the view matrix that was set bef
ore the | |
722 * constructor. It is similar to passing the inverse of the current view mat
rix to | |
723 * AutoViewMatrixRestore, but lazily computes the inverse only if necessary. | |
724 */ | |
725 class AutoDeviceCoordDraw : ::GrNoncopyable { | |
726 public: | |
727 AutoDeviceCoordDraw() : fDrawState(NULL) {} | |
728 /** | |
729 * If a stage's texture matrix is applied to explicit per-vertex coords,
rather than to | |
730 * positions, then we don't want to modify its matrix. The explicitCoord
StageMask is used | |
731 * to specify such stages. | |
732 */ | |
733 AutoDeviceCoordDraw(GrDrawState* drawState) { | |
734 fDrawState = NULL; | |
735 this->set(drawState); | |
736 } | |
737 | |
738 ~AutoDeviceCoordDraw() { this->restore(); } | |
739 | |
740 bool set(GrDrawState* drawState); | |
741 | |
742 /** | |
743 * Returns true if this object was successfully initialized on to a GrDr
awState. It may | |
744 * return false because a non-default constructor or set() were never ca
lled or because | |
745 * the view matrix was not invertible. | |
746 */ | |
747 bool succeeded() const { return NULL != fDrawState; } | |
748 | |
749 /** | |
750 * Returns the matrix that was set previously set on the drawState. This
is only valid | |
751 * if succeeded returns true. | |
752 */ | |
753 const SkMatrix& getOriginalMatrix() const { | |
754 GrAssert(this->succeeded()); | |
755 return fViewMatrix; | |
756 } | |
757 | |
758 /** | |
759 * Can be called prior to destructor to restore the original matrix. | |
760 */ | |
761 void restore(); | |
762 | |
763 private: | |
764 GrDrawState* fDrawState; | |
765 SkMatrix fViewMatrix; | 678 SkMatrix fViewMatrix; |
766 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNum
Stages]; | 679 GrEffectStage::SavedCoordChange fSavedCoordChanges[GrDrawState::kNum
Stages]; |
767 uint32_t fRestoreMask; | 680 uint32_t fRestoreMask; |
768 }; | 681 }; |
769 | 682 |
770 /// @} | 683 /// @} |
771 | 684 |
772 /////////////////////////////////////////////////////////////////////////// | 685 /////////////////////////////////////////////////////////////////////////// |
773 /// @name Render Target | 686 /// @name Render Target |
774 //// | 687 //// |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1020 for (int i = 0; i < kNumStages; i++) { | 933 for (int i = 0; i < kNumStages; i++) { |
1021 if (s.isStageEnabled(i)) { | 934 if (s.isStageEnabled(i)) { |
1022 this->fStages[i] = s.fStages[i]; | 935 this->fStages[i] = s.fStages[i]; |
1023 } | 936 } |
1024 } | 937 } |
1025 return *this; | 938 return *this; |
1026 } | 939 } |
1027 | 940 |
1028 private: | 941 private: |
1029 | 942 |
| 943 void onReset(const SkMatrix* initialViewMatrix) { |
| 944 |
| 945 this->disableStages(); |
| 946 |
| 947 fRenderTarget.reset(NULL); |
| 948 |
| 949 this->setDefaultVertexAttribs(); |
| 950 |
| 951 fCommon.fColor = 0xffffffff; |
| 952 if (NULL == initialViewMatrix) { |
| 953 fCommon.fViewMatrix.reset(); |
| 954 } else { |
| 955 fCommon.fViewMatrix = *initialViewMatrix; |
| 956 } |
| 957 fCommon.fSrcBlend = kOne_GrBlendCoeff; |
| 958 fCommon.fDstBlend = kZero_GrBlendCoeff; |
| 959 fCommon.fBlendConstant = 0x0; |
| 960 fCommon.fFlagBits = 0x0; |
| 961 fCommon.fStencilSettings.setDisabled(); |
| 962 fCommon.fFirstCoverageStage = kNumStages; |
| 963 fCommon.fCoverage = 0xffffffff; |
| 964 fCommon.fColorFilterMode = SkXfermode::kDst_Mode; |
| 965 fCommon.fColorFilterColor = 0x0; |
| 966 fCommon.fDrawFace = kBoth_DrawFace; |
| 967 } |
| 968 |
1030 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState.
*/ | 969 /** Fields that are identical in GrDrawState and GrDrawState::DeferredState.
*/ |
1031 struct CommonState { | 970 struct CommonState { |
1032 // These fields are roughly sorted by decreasing likelihood of being dif
ferent in op== | 971 // These fields are roughly sorted by decreasing likelihood of being dif
ferent in op== |
1033 GrColor fColor; | 972 GrColor fColor; |
1034 SkMatrix fViewMatrix; | 973 SkMatrix fViewMatrix; |
1035 GrBlendCoeff fSrcBlend; | 974 GrBlendCoeff fSrcBlend; |
1036 GrBlendCoeff fDstBlend; | 975 GrBlendCoeff fDstBlend; |
1037 GrColor fBlendConstant; | 976 GrColor fBlendConstant; |
1038 uint32_t fFlagBits; | 977 uint32_t fFlagBits; |
1039 const GrVertexAttrib* fVAPtr; | 978 const GrVertexAttrib* fVAPtr; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. | 1090 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. |
1152 */ | 1091 */ |
1153 void setVertexAttribs(const GrVertexAttrib attribs[], int count); | 1092 void setVertexAttribs(const GrVertexAttrib attribs[], int count); |
1154 | 1093 |
1155 typedef GrRefCnt INHERITED; | 1094 typedef GrRefCnt INHERITED; |
1156 }; | 1095 }; |
1157 | 1096 |
1158 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); | 1097 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
1159 | 1098 |
1160 #endif | 1099 #endif |
OLD | NEW |