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" | |
12 #include "GrBlend.h" | 11 #include "GrBlend.h" |
13 #include "GrColor.h" | 12 #include "GrColor.h" |
14 #include "GrEffectStage.h" | 13 #include "GrEffectStage.h" |
15 #include "GrPaint.h" | |
16 #include "GrRenderTarget.h" | |
17 #include "GrStencil.h" | 14 #include "GrStencil.h" |
18 #include "GrTemplates.h" | |
19 #include "GrTexture.h" | |
20 #include "GrTypesPriv.h" | |
21 #include "effects/GrSimpleTextureEffect.h" | 15 #include "effects/GrSimpleTextureEffect.h" |
22 | 16 |
23 #include "SkMatrix.h" | 17 #include "SkMatrix.h" |
24 #include "SkTypes.h" | 18 |
25 #include "SkXfermode.h" | 19 class GrDrawTargetCaps; |
| 20 class GrPaint; |
| 21 class GrRenderTarget; |
| 22 class GrTexture; |
26 | 23 |
27 class GrDrawState : public SkRefCnt { | 24 class GrDrawState : public SkRefCnt { |
28 public: | 25 public: |
29 SK_DECLARE_INST_COUNT(GrDrawState) | 26 SK_DECLARE_INST_COUNT(GrDrawState) |
30 | 27 |
31 GrDrawState() { | 28 GrDrawState() { |
32 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) | 29 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
33 this->reset(); | 30 this->reset(); |
34 } | 31 } |
35 | 32 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 /** | 151 /** |
155 * Determines whether src alpha is guaranteed to be one for all src pixels | 152 * Determines whether src alpha is guaranteed to be one for all src pixels |
156 */ | 153 */ |
157 bool srcAlphaWillBeOne() const; | 154 bool srcAlphaWillBeOne() const; |
158 | 155 |
159 /** | 156 /** |
160 * Determines whether the output coverage is guaranteed to be one for all pi
xels hit by a draw. | 157 * Determines whether the output coverage is guaranteed to be one for all pi
xels hit by a draw. |
161 */ | 158 */ |
162 bool hasSolidCoverage() const; | 159 bool hasSolidCoverage() const; |
163 | 160 |
| 161 /** |
| 162 * Depending on features available in the underlying 3D API and the color bl
end mode requested |
| 163 * it may or may not be possible to correctly blend with fractional pixel co
verage generated by |
| 164 * the fragment shader. |
| 165 * |
| 166 * This function considers the current draw state and the draw target's capa
bilities to |
| 167 * determine whether coverage can be handled correctly. This function assume
s that the caller |
| 168 * intends to specify fractional pixel coverage (via setCoverage(), through
a coverage vertex |
| 169 * attribute, or a coverage effect) but may not have specified it yet. |
| 170 */ |
| 171 bool couldApplyCoverage(const GrDrawTargetCaps& caps) const; |
| 172 |
164 /// @} | 173 /// @} |
165 | 174 |
166 /////////////////////////////////////////////////////////////////////////// | 175 /////////////////////////////////////////////////////////////////////////// |
167 /// @name Color | 176 /// @name Color |
168 //// | 177 //// |
169 | 178 |
170 /** | 179 /** |
171 * Sets color for next draw to a premultiplied-alpha color. | 180 * Sets color for next draw to a premultiplied-alpha color. |
172 * | 181 * |
173 * @param color the color to set. | 182 * @param color the color to set. |
174 */ | 183 */ |
175 void setColor(GrColor color) { | 184 void setColor(GrColor color) { |
176 fColor = color; | 185 fColor = color; |
177 this->invalidateBlendOptFlags(); | 186 this->invalidateBlendOptFlags(); |
178 } | 187 } |
179 | 188 |
180 GrColor getColor() const { return fColor; } | 189 GrColor getColor() const { return fColor; } |
181 | 190 |
182 /** | 191 /** |
183 * Sets the color to be used for the next draw to be | 192 * Sets the color to be used for the next draw to be |
184 * (r,g,b,a) = (alpha, alpha, alpha, alpha). | 193 * (r,g,b,a) = (alpha, alpha, alpha, alpha). |
185 * | 194 * |
186 * @param alpha The alpha value to set as the color. | 195 * @param alpha The alpha value to set as the color. |
187 */ | 196 */ |
188 void setAlpha(uint8_t a) { | 197 void setAlpha(uint8_t a) { this->setColor((a << 24) | (a << 16) | (a << 8) |
a); } |
189 this->setColor((a << 24) | (a << 16) | (a << 8) | a); | |
190 } | |
191 | 198 |
192 /// @} | 199 /// @} |
193 | 200 |
194 /////////////////////////////////////////////////////////////////////////// | 201 /////////////////////////////////////////////////////////////////////////// |
195 /// @name Coverage | 202 /// @name Coverage |
196 //// | 203 //// |
197 | 204 |
198 /** | 205 /** |
199 * Sets a constant fractional coverage to be applied to the draw. The | 206 * Sets a constant fractional coverage to be applied to the draw. The |
200 * initial value (after construction or reset()) is 0xff. The constant | 207 * initial value (after construction or reset()) is 0xff. The constant |
201 * coverage is ignored when per-vertex coverage is provided. | 208 * coverage is ignored when per-vertex coverage is provided. |
202 */ | 209 */ |
203 void setCoverage(uint8_t coverage) { | 210 void setCoverage(uint8_t coverage) { |
204 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage); | 211 fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
205 this->invalidateBlendOptFlags(); | 212 this->invalidateBlendOptFlags(); |
206 } | 213 } |
207 | 214 |
208 uint8_t getCoverage() const { | 215 uint8_t getCoverage() const { return GrColorUnpackR(fCoverage); } |
209 return GrColorUnpackR(fCoverage); | |
210 } | |
211 | 216 |
212 GrColor getCoverageColor() const { | 217 GrColor getCoverageColor() const { return fCoverage; } |
213 return fCoverage; | |
214 } | |
215 | 218 |
216 /// @} | 219 /// @} |
217 | 220 |
218 /** | 221 /** |
219 * This struct is here so that the GrDrawState can have multiple instances o
f state information. | 222 * This struct is here so that the GrDrawState can have multiple instances o
f state information. |
220 * The use of this will come in a future revision when we want to keep track
of the original | 223 * The use of this will come in a future revision when we want to keep track
of the original |
221 * draw state as well as an optimized version of it. | 224 * draw state as well as an optimized version of it. |
222 */ | 225 */ |
223 struct State { | 226 struct State { |
224 State() { | 227 State() { |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 557 |
555 /////////////////////////////////////////////////////////////////////////// | 558 /////////////////////////////////////////////////////////////////////////// |
556 /// @name Render Target | 559 /// @name Render Target |
557 //// | 560 //// |
558 | 561 |
559 /** | 562 /** |
560 * Sets the render-target used at the next drawing call | 563 * Sets the render-target used at the next drawing call |
561 * | 564 * |
562 * @param target The render target to set. | 565 * @param target The render target to set. |
563 */ | 566 */ |
564 void setRenderTarget(GrRenderTarget* target) { | 567 void setRenderTarget(GrRenderTarget* target) { fRenderTarget.reset(SkSafeRef
(target)); } |
565 fRenderTarget.reset(SkSafeRef(target)); | |
566 } | |
567 | 568 |
568 /** | 569 /** |
569 * Retrieves the currently set render-target. | 570 * Retrieves the currently set render-target. |
570 * | 571 * |
571 * @return The currently set render target. | 572 * @return The currently set render target. |
572 */ | 573 */ |
573 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get();
} | 574 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get();
} |
574 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } | 575 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } |
575 | 576 |
576 class AutoRenderTargetRestore : public ::SkNoncopyable { | 577 class AutoRenderTargetRestore : public ::SkNoncopyable { |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 * @param enable if true enable stateBits, otherwise disable | 716 * @param enable if true enable stateBits, otherwise disable |
716 */ | 717 */ |
717 void setState(uint32_t stateBits, bool enable) { | 718 void setState(uint32_t stateBits, bool enable) { |
718 if (enable) { | 719 if (enable) { |
719 this->enableState(stateBits); | 720 this->enableState(stateBits); |
720 } else { | 721 } else { |
721 this->disableState(stateBits); | 722 this->disableState(stateBits); |
722 } | 723 } |
723 } | 724 } |
724 | 725 |
725 bool isDitherState() const { | 726 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & f
FlagBits); } |
726 return 0 != (fFlagBits & kDither_StateBit); | |
727 } | |
728 | 727 |
729 bool isHWAntialiasState() const { | 728 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); } |
730 return 0 != (fFlagBits & kHWAntialias_StateBit); | 729 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_Stat
eBit); } |
731 } | 730 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } |
732 | 731 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_
StateBit); } |
733 bool isClipState() const { | 732 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_S
tateBit); } |
734 return 0 != (fFlagBits & kClip_StateBit); | |
735 } | |
736 | |
737 bool isColorWriteDisabled() const { | |
738 return 0 != (fFlagBits & kNoColorWrites_StateBit); | |
739 } | |
740 | |
741 bool isCoverageDrawing() const { | |
742 return 0 != (fFlagBits & kCoverageDrawing_StateBit); | |
743 } | |
744 | |
745 bool isStateFlagEnabled(uint32_t stateBit) const { | |
746 return 0 != (stateBit & fFlagBits); | |
747 } | |
748 | 733 |
749 /// @} | 734 /// @} |
750 | 735 |
751 /////////////////////////////////////////////////////////////////////////// | 736 /////////////////////////////////////////////////////////////////////////// |
752 /// @name Face Culling | 737 /// @name Face Culling |
753 //// | 738 //// |
754 | 739 |
755 enum DrawFace { | 740 enum DrawFace { |
756 kInvalid_DrawFace = -1, | 741 kInvalid_DrawFace = -1, |
757 | 742 |
(...skipping 14 matching lines...) Expand all Loading... |
772 /** | 757 /** |
773 * Gets whether the target is drawing clockwise, counterclockwise, | 758 * Gets whether the target is drawing clockwise, counterclockwise, |
774 * or both faces. | 759 * or both faces. |
775 * @return the current draw face(s). | 760 * @return the current draw face(s). |
776 */ | 761 */ |
777 DrawFace getDrawFace() const { return fDrawFace; } | 762 DrawFace getDrawFace() const { return fDrawFace; } |
778 | 763 |
779 /// @} | 764 /// @} |
780 | 765 |
781 /////////////////////////////////////////////////////////////////////////// | 766 /////////////////////////////////////////////////////////////////////////// |
| 767 /// @name Hints |
| 768 /// Hints that when provided can enable optimizations. |
| 769 //// |
| 770 |
| 771 enum Hints { kVertexColorsAreOpaque_Hint = 0x1, }; |
| 772 |
| 773 void setHint(Hints hint, bool value) { fHints = value ? (fHints | hint) : (f
Hints & ~hint); } |
| 774 |
| 775 /// @} |
| 776 |
| 777 /////////////////////////////////////////////////////////////////////////// |
782 | 778 |
783 /** Return type for CombineIfPossible. */ | 779 /** Return type for CombineIfPossible. */ |
784 enum CombinedState { | 780 enum CombinedState { |
785 /** The GrDrawStates cannot be combined. */ | 781 /** The GrDrawStates cannot be combined. */ |
786 kIncompatible_CombinedState, | 782 kIncompatible_CombinedState, |
787 /** Either draw state can be used in place of the other. */ | 783 /** Either draw state can be used in place of the other. */ |
788 kAOrB_CombinedState, | 784 kAOrB_CombinedState, |
789 /** Use the first draw state. */ | 785 /** Use the first draw state. */ |
790 kA_CombinedState, | 786 kA_CombinedState, |
791 /** Use the second draw state. */ | 787 /** Use the second draw state. */ |
792 kB_CombinedState, | 788 kB_CombinedState, |
793 }; | 789 }; |
794 | 790 |
795 /** This function determines whether the GrDrawStates used for two draws can
be combined into | 791 /** This function determines whether the GrDrawStates used for two draws can
be combined into |
796 a single GrDrawState. This is used to avoid storing redundant GrDrawStat
es and to determine | 792 a single GrDrawState. This is used to avoid storing redundant GrDrawStat
es and to determine |
797 if draws can be batched. The return value indicates whether combining is
possible and, if | 793 if draws can be batched. The return value indicates whether combining is
possible and, if |
798 so, which of the two inputs should be used. */ | 794 so, which of the two inputs should be used. */ |
799 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawSta
te& b); | 795 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawSta
te& b, |
| 796 const GrDrawTargetCaps& caps); |
800 | 797 |
801 GrDrawState& operator= (const GrDrawState& that); | 798 GrDrawState& operator= (const GrDrawState& that); |
802 | 799 |
803 private: | 800 private: |
804 void onReset(const SkMatrix* initialViewMatrix); | 801 void onReset(const SkMatrix* initialViewMatrix); |
805 | 802 |
806 BlendOptFlags calcBlendOpts(bool forceCoverage = false, | 803 BlendOptFlags calcBlendOpts(bool forceCoverage = false, |
807 GrBlendCoeff* srcCoeff = NULL, | 804 GrBlendCoeff* srcCoeff = NULL, |
808 GrBlendCoeff* dstCoeff = NULL) const; | 805 GrBlendCoeff* dstCoeff = NULL) const; |
809 | 806 |
810 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== | 807 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== |
811 SkAutoTUnref<GrRenderTarget> fRenderTarget; | 808 SkAutoTUnref<GrRenderTarget> fRenderTarget; |
812 GrColor fColor; | 809 GrColor fColor; |
813 SkMatrix fViewMatrix; | 810 SkMatrix fViewMatrix; |
814 GrColor fBlendConstant; | 811 GrColor fBlendConstant; |
815 uint32_t fFlagBits; | 812 uint32_t fFlagBits; |
816 const GrVertexAttrib* fVAPtr; | 813 const GrVertexAttrib* fVAPtr; |
817 int fVACount; | 814 int fVACount; |
818 GrStencilSettings fStencilSettings; | 815 GrStencilSettings fStencilSettings; |
819 GrColor fCoverage; | 816 GrColor fCoverage; |
820 DrawFace fDrawFace; | 817 DrawFace fDrawFace; |
821 | 818 |
822 State fState; | 819 State fState; |
| 820 |
| 821 uint32_t fHints; |
823 | 822 |
824 mutable GrBlendCoeff fOptSrcBlend; | 823 mutable GrBlendCoeff fOptSrcBlend; |
825 mutable GrBlendCoeff fOptDstBlend; | 824 mutable GrBlendCoeff fOptDstBlend; |
826 mutable BlendOptFlags fBlendOptFlags; | 825 mutable BlendOptFlags fBlendOptFlags; |
827 | 826 |
828 // This is simply a different representation of info in fVertexAttribs and t
hus does | 827 // This is simply a different representation of info in fVertexAttribs and t
hus does |
829 // not need to be compared in op==. | 828 // not need to be compared in op==. |
830 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt
]; | 829 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt
]; |
831 | 830 |
832 // Some of the auto restore objects assume that no effects are removed durin
g their lifetime. | 831 // Some of the auto restore objects assume that no effects are removed durin
g their lifetime. |
833 // This is used to assert that this condition holds. | 832 // This is used to assert that this condition holds. |
834 SkDEBUGCODE(int fBlockEffectRemovalCnt;) | 833 SkDEBUGCODE(int fBlockEffectRemovalCnt;) |
835 | 834 |
836 /** | 835 /** |
837 * Sets vertex attributes for next draw. | 836 * Sets vertex attributes for next draw. |
838 * | 837 * |
839 * @param attribs the array of vertex attributes to set. | 838 * @param attribs the array of vertex attributes to set. |
840 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. | 839 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. |
841 */ | 840 */ |
842 void setVertexAttribs(const GrVertexAttrib attribs[], int count); | 841 void setVertexAttribs(const GrVertexAttrib attribs[], int count); |
843 | 842 |
844 typedef SkRefCnt INHERITED; | 843 typedef SkRefCnt INHERITED; |
845 }; | 844 }; |
846 | 845 |
847 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); | 846 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
848 | 847 |
849 #endif | 848 #endif |
OLD | NEW |