| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 * Copies another draw state. | 42 * Copies another draw state. |
| 43 **/ | 43 **/ |
| 44 GrDrawState(const GrDrawState& state) : INHERITED() { | 44 GrDrawState(const GrDrawState& state) : INHERITED() { |
| 45 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) | 45 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) |
| 46 *this = state; | 46 *this = state; |
| 47 } | 47 } |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * Copies another draw state with a preconcat to the view matrix. | 50 * Copies another draw state with a preconcat to the view matrix. |
| 51 **/ | 51 **/ |
| 52 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix) { | 52 GrDrawState(const GrDrawState& state, const SkMatrix& preConcatMatrix); |
| 53 SkDEBUGCODE(fBlockEffectRemovalCnt = 0;) | |
| 54 *this = state; | |
| 55 if (!preConcatMatrix.isIdentity()) { | |
| 56 for (int i = 0; i < fColorStages.count(); ++i) { | |
| 57 fColorStages[i].localCoordChange(preConcatMatrix); | |
| 58 } | |
| 59 for (int i = 0; i < fCoverageStages.count(); ++i) { | |
| 60 fCoverageStages[i].localCoordChange(preConcatMatrix); | |
| 61 } | |
| 62 this->invalidateBlendOptFlags(); | |
| 63 } | |
| 64 } | |
| 65 | 53 |
| 66 virtual ~GrDrawState() { SkASSERT(0 == fBlockEffectRemovalCnt); } | 54 virtual ~GrDrawState() { SkASSERT(0 == fBlockEffectRemovalCnt); } |
| 67 | 55 |
| 68 /** | 56 /** |
| 69 * Resets to the default state. GrEffects will be removed from all stages. | 57 * Resets to the default state. GrEffects will be removed from all stages. |
| 70 */ | 58 */ |
| 71 void reset() { this->onReset(NULL); } | 59 void reset() { this->onReset(NULL); } |
| 72 | 60 |
| 73 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMa
trix); } | 61 void reset(const SkMatrix& initialViewMatrix) { this->onReset(&initialViewMa
trix); } |
| 74 | 62 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribB
inding]; | 132 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribB
inding]; |
| 145 } | 133 } |
| 146 | 134 |
| 147 bool validateVertexAttribs() const; | 135 bool validateVertexAttribs() const; |
| 148 | 136 |
| 149 /** | 137 /** |
| 150 * Helper to save/restore vertex attribs | 138 * Helper to save/restore vertex attribs |
| 151 */ | 139 */ |
| 152 class AutoVertexAttribRestore { | 140 class AutoVertexAttribRestore { |
| 153 public: | 141 public: |
| 154 AutoVertexAttribRestore(GrDrawState* drawState) { | 142 AutoVertexAttribRestore(GrDrawState* drawState); |
| 155 SkASSERT(NULL != drawState); | |
| 156 fDrawState = drawState; | |
| 157 fVAPtr = drawState->fVAPtr; | |
| 158 fVACount = drawState->fVACount; | |
| 159 fDrawState->setDefaultVertexAttribs(); | |
| 160 } | |
| 161 | 143 |
| 162 ~AutoVertexAttribRestore(){ | 144 ~AutoVertexAttribRestore() { fDrawState->setVertexAttribs(fVAPtr, fVACo
unt); } |
| 163 fDrawState->setVertexAttribs(fVAPtr, fVACount); | |
| 164 } | |
| 165 | 145 |
| 166 private: | 146 private: |
| 167 GrDrawState* fDrawState; | 147 GrDrawState* fDrawState; |
| 168 const GrVertexAttrib* fVAPtr; | 148 const GrVertexAttrib* fVAPtr; |
| 169 int fVACount; | 149 int fVACount; |
| 170 }; | 150 }; |
| 171 | 151 |
| 172 /** | |
| 173 * Accessing positions, local coords, or colors, of a vertex within an array
is a hassle | |
| 174 * involving casts and simple math. These helpers exist to keep GrDrawTarget
clients' code a bit | |
| 175 * nicer looking. | |
| 176 */ | |
| 177 | |
| 178 /** | |
| 179 * Gets a pointer to a GrPoint of a vertex's position or texture | |
| 180 * coordinate. | |
| 181 * @param vertices the vertex array | |
| 182 * @param vertexIndex the index of the vertex in the array | |
| 183 * @param vertexSize the size of each vertex in the array | |
| 184 * @param offset the offset in bytes of the vertex component. | |
| 185 * Defaults to zero (corresponding to vertex position) | |
| 186 * @return pointer to the vertex component as a GrPoint | |
| 187 */ | |
| 188 static SkPoint* GetVertexPoint(void* vertices, | |
| 189 int vertexIndex, | |
| 190 int vertexSize, | |
| 191 int offset = 0) { | |
| 192 intptr_t start = GrTCast<intptr_t>(vertices); | |
| 193 return GrTCast<SkPoint*>(start + offset + | |
| 194 vertexIndex * vertexSize); | |
| 195 } | |
| 196 static const SkPoint* GetVertexPoint(const void* vertices, | |
| 197 int vertexIndex, | |
| 198 int vertexSize, | |
| 199 int offset = 0) { | |
| 200 intptr_t start = GrTCast<intptr_t>(vertices); | |
| 201 return GrTCast<const SkPoint*>(start + offset + | |
| 202 vertexIndex * vertexSize); | |
| 203 } | |
| 204 | |
| 205 /** | |
| 206 * Gets a pointer to a GrColor inside a vertex within a vertex array. | |
| 207 * @param vertices the vetex array | |
| 208 * @param vertexIndex the index of the vertex in the array | |
| 209 * @param vertexSize the size of each vertex in the array | |
| 210 * @param offset the offset in bytes of the vertex color | |
| 211 * @return pointer to the vertex component as a GrColor | |
| 212 */ | |
| 213 static GrColor* GetVertexColor(void* vertices, | |
| 214 int vertexIndex, | |
| 215 int vertexSize, | |
| 216 int offset) { | |
| 217 intptr_t start = GrTCast<intptr_t>(vertices); | |
| 218 return GrTCast<GrColor*>(start + offset + | |
| 219 vertexIndex * vertexSize); | |
| 220 } | |
| 221 static const GrColor* GetVertexColor(const void* vertices, | |
| 222 int vertexIndex, | |
| 223 int vertexSize, | |
| 224 int offset) { | |
| 225 const intptr_t start = GrTCast<intptr_t>(vertices); | |
| 226 return GrTCast<const GrColor*>(start + offset + | |
| 227 vertexIndex * vertexSize); | |
| 228 } | |
| 229 | |
| 230 /// @} | 152 /// @} |
| 231 | 153 |
| 232 /** | 154 /** |
| 233 * Determines whether src alpha is guaranteed to be one for all src pixels | 155 * Determines whether src alpha is guaranteed to be one for all src pixels |
| 234 */ | 156 */ |
| 235 bool srcAlphaWillBeOne() const; | 157 bool srcAlphaWillBeOne() const; |
| 236 | 158 |
| 237 /** | 159 /** |
| 238 * Determines whether the output coverage is guaranteed to be one for all pi
xels hit by a draw. | 160 * Determines whether the output coverage is guaranteed to be one for all pi
xels hit by a draw. |
| 239 */ | 161 */ |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 class AutoRestoreEffects : public ::SkNoncopyable { | 279 class AutoRestoreEffects : public ::SkNoncopyable { |
| 358 public: | 280 public: |
| 359 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEf
fectCnt(0) {} | 281 AutoRestoreEffects() : fDrawState(NULL), fColorEffectCnt(0), fCoverageEf
fectCnt(0) {} |
| 360 | 282 |
| 361 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(
0), fCoverageEffectCnt(0) { | 283 AutoRestoreEffects(GrDrawState* ds) : fDrawState(NULL), fColorEffectCnt(
0), fCoverageEffectCnt(0) { |
| 362 this->set(ds); | 284 this->set(ds); |
| 363 } | 285 } |
| 364 | 286 |
| 365 ~AutoRestoreEffects() { this->set(NULL); } | 287 ~AutoRestoreEffects() { this->set(NULL); } |
| 366 | 288 |
| 367 void set(GrDrawState* ds) { | 289 void set(GrDrawState* ds); |
| 368 if (NULL != fDrawState) { | |
| 369 int m = fDrawState->fColorStages.count() - fColorEffectCnt; | |
| 370 SkASSERT(m >= 0); | |
| 371 fDrawState->fColorStages.pop_back_n(m); | |
| 372 | |
| 373 int n = fDrawState->fCoverageStages.count() - fCoverageEffectCnt
; | |
| 374 SkASSERT(n >= 0); | |
| 375 fDrawState->fCoverageStages.pop_back_n(n); | |
| 376 if (m + n > 0) { | |
| 377 fDrawState->invalidateBlendOptFlags(); | |
| 378 } | |
| 379 SkDEBUGCODE(--fDrawState->fBlockEffectRemovalCnt;) | |
| 380 } | |
| 381 fDrawState = ds; | |
| 382 if (NULL != ds) { | |
| 383 fColorEffectCnt = ds->fColorStages.count(); | |
| 384 fCoverageEffectCnt = ds->fCoverageStages.count(); | |
| 385 SkDEBUGCODE(++ds->fBlockEffectRemovalCnt;) | |
| 386 } | |
| 387 } | |
| 388 | 290 |
| 389 bool isSet() const { return NULL != fDrawState; } | 291 bool isSet() const { return NULL != fDrawState; } |
| 390 | 292 |
| 391 private: | 293 private: |
| 392 GrDrawState* fDrawState; | 294 GrDrawState* fDrawState; |
| 393 int fColorEffectCnt; | 295 int fColorEffectCnt; |
| 394 int fCoverageEffectCnt; | 296 int fCoverageEffectCnt; |
| 395 }; | 297 }; |
| 396 | 298 |
| 397 int numColorStages() const { return fColorStages.count(); } | 299 int numColorStages() const { return fColorStages.count(); } |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 /** Use the first draw state. */ | 755 /** Use the first draw state. */ |
| 854 kA_CombinedState, | 756 kA_CombinedState, |
| 855 /** Use the second draw state. */ | 757 /** Use the second draw state. */ |
| 856 kB_CombinedState, | 758 kB_CombinedState, |
| 857 }; | 759 }; |
| 858 | 760 |
| 859 /** This function determines whether the GrDrawStates used for two draws can
be combined into | 761 /** This function determines whether the GrDrawStates used for two draws can
be combined into |
| 860 a single GrDrawState. This is used to avoid storing redundant GrDrawStat
es and to determine | 762 a single GrDrawState. This is used to avoid storing redundant GrDrawStat
es and to determine |
| 861 if draws can be batched. The return value indicates whether combining is
possible and, if | 763 if draws can be batched. The return value indicates whether combining is
possible and, if |
| 862 so, which of the two inputs should be used. */ | 764 so, which of the two inputs should be used. */ |
| 863 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawSta
te& b) { | 765 static CombinedState CombineIfPossible(const GrDrawState& a, const GrDrawSta
te& b); |
| 864 bool usingVertexColors = a.hasColorVertexAttribute(); | |
| 865 if (!usingVertexColors && a.fColor != b.fColor) { | |
| 866 return kIncompatible_CombinedState; | |
| 867 } | |
| 868 | 766 |
| 869 if (a.fRenderTarget.get() != b.fRenderTarget.get() || | 767 GrDrawState& operator= (const GrDrawState& that); |
| 870 a.fColorStages.count() != b.fColorStages.count() || | |
| 871 a.fCoverageStages.count() != b.fCoverageStages.count() || | |
| 872 !a.fViewMatrix.cheapEqualTo(b.fViewMatrix) || | |
| 873 a.fSrcBlend != b.fSrcBlend || | |
| 874 a.fDstBlend != b.fDstBlend || | |
| 875 a.fBlendConstant != b.fBlendConstant || | |
| 876 a.fFlagBits != b.fFlagBits || | |
| 877 a.fVACount != b.fVACount || | |
| 878 memcmp(a.fVAPtr, b.fVAPtr, a.fVACount * sizeof(GrVertexAttrib)) || | |
| 879 a.fStencilSettings != b.fStencilSettings || | |
| 880 a.fDrawFace != b.fDrawFace) { | |
| 881 return kIncompatible_CombinedState; | |
| 882 } | |
| 883 | |
| 884 bool usingVertexCoverage = a.hasCoverageVertexAttribute(); | |
| 885 if (!usingVertexCoverage && a.fCoverage != b.fCoverage) { | |
| 886 return kIncompatible_CombinedState; | |
| 887 } | |
| 888 | |
| 889 bool explicitLocalCoords = a.hasLocalCoordAttribute(); | |
| 890 for (int i = 0; i < a.fColorStages.count(); i++) { | |
| 891 if (!GrEffectStage::AreCompatible(a.fColorStages[i], b.fColorStages[
i], | |
| 892 explicitLocalCoords)) { | |
| 893 return kIncompatible_CombinedState; | |
| 894 } | |
| 895 } | |
| 896 for (int i = 0; i < a.fCoverageStages.count(); i++) { | |
| 897 if (!GrEffectStage::AreCompatible(a.fCoverageStages[i], b.fCoverageS
tages[i], | |
| 898 explicitLocalCoords)) { | |
| 899 return kIncompatible_CombinedState; | |
| 900 } | |
| 901 } | |
| 902 SkASSERT(0 == memcmp(a.fFixedFunctionVertexAttribIndices, | |
| 903 b.fFixedFunctionVertexAttribIndices, | |
| 904 sizeof(a.fFixedFunctionVertexAttribIndices))); | |
| 905 return kAOrB_CombinedState; | |
| 906 } | |
| 907 | |
| 908 GrDrawState& operator= (const GrDrawState& that) { | |
| 909 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | |
| 910 this->setRenderTarget(that.fRenderTarget.get()); | |
| 911 fColor = that.fColor; | |
| 912 fViewMatrix = that.fViewMatrix; | |
| 913 fSrcBlend = that.fSrcBlend; | |
| 914 fDstBlend = that.fDstBlend; | |
| 915 fBlendConstant = that.fBlendConstant; | |
| 916 fFlagBits = that.fFlagBits; | |
| 917 fVACount = that.fVACount; | |
| 918 fVAPtr = that.fVAPtr; | |
| 919 fStencilSettings = that.fStencilSettings; | |
| 920 fCoverage = that.fCoverage; | |
| 921 fDrawFace = that.fDrawFace; | |
| 922 fColorStages = that.fColorStages; | |
| 923 fCoverageStages = that.fCoverageStages; | |
| 924 fOptSrcBlend = that.fOptSrcBlend; | |
| 925 fOptDstBlend = that.fOptDstBlend; | |
| 926 fBlendOptFlags = that.fBlendOptFlags; | |
| 927 | |
| 928 memcpy(fFixedFunctionVertexAttribIndices, | |
| 929 that.fFixedFunctionVertexAttribIndices, | |
| 930 sizeof(fFixedFunctionVertexAttribIndices)); | |
| 931 return *this; | |
| 932 } | |
| 933 | 768 |
| 934 private: | 769 private: |
| 935 | 770 |
| 936 void onReset(const SkMatrix* initialViewMatrix) { | 771 void onReset(const SkMatrix* initialViewMatrix); |
| 937 SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numTotalStages()); | |
| 938 fColorStages.reset(); | |
| 939 fCoverageStages.reset(); | |
| 940 | |
| 941 fRenderTarget.reset(NULL); | |
| 942 | |
| 943 this->setDefaultVertexAttribs(); | |
| 944 | |
| 945 fColor = 0xffffffff; | |
| 946 if (NULL == initialViewMatrix) { | |
| 947 fViewMatrix.reset(); | |
| 948 } else { | |
| 949 fViewMatrix = *initialViewMatrix; | |
| 950 } | |
| 951 fSrcBlend = kOne_GrBlendCoeff; | |
| 952 fDstBlend = kZero_GrBlendCoeff; | |
| 953 fBlendConstant = 0x0; | |
| 954 fFlagBits = 0x0; | |
| 955 fStencilSettings.setDisabled(); | |
| 956 fCoverage = 0xffffffff; | |
| 957 fDrawFace = kBoth_DrawFace; | |
| 958 | |
| 959 this->invalidateBlendOptFlags(); | |
| 960 } | |
| 961 | 772 |
| 962 BlendOptFlags calcBlendOpts(bool forceCoverage = false, | 773 BlendOptFlags calcBlendOpts(bool forceCoverage = false, |
| 963 GrBlendCoeff* srcCoeff = NULL, | 774 GrBlendCoeff* srcCoeff = NULL, |
| 964 GrBlendCoeff* dstCoeff = NULL) const; | 775 GrBlendCoeff* dstCoeff = NULL) const; |
| 965 | 776 |
| 966 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== | 777 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== |
| 967 SkAutoTUnref<GrRenderTarget> fRenderTarget; | 778 SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 968 GrColor fColor; | 779 GrColor fColor; |
| 969 SkMatrix fViewMatrix; | 780 SkMatrix fViewMatrix; |
| 970 GrBlendCoeff fSrcBlend; | 781 GrBlendCoeff fSrcBlend; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1000 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. | 811 * @param count the number of attributes being set, limited to kMaxVer
texAttribCnt. |
| 1001 */ | 812 */ |
| 1002 void setVertexAttribs(const GrVertexAttrib attribs[], int count); | 813 void setVertexAttribs(const GrVertexAttrib attribs[], int count); |
| 1003 | 814 |
| 1004 typedef SkRefCnt INHERITED; | 815 typedef SkRefCnt INHERITED; |
| 1005 }; | 816 }; |
| 1006 | 817 |
| 1007 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); | 818 GR_MAKE_BITFIELD_OPS(GrDrawState::BlendOptFlags); |
| 1008 | 819 |
| 1009 #endif | 820 #endif |
| OLD | NEW |