OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "GrTypes.h" | 9 #include "GrTypes.h" |
10 #include "GrColor.h" | 10 #include "SkTLogic.h" |
11 #include "GrXferProcessor.h" | 11 #include "GrXferProcessor.h" |
12 | 12 |
13 #ifndef GrBlend_DEFINED | 13 #ifndef GrBlend_DEFINED |
14 #define GrBlend_DEFINED | 14 #define GrBlend_DEFINED |
15 | 15 |
16 static inline bool GrBlendCoeffRefsSrc(GrBlendCoeff coeff) { | 16 template<GrBlendCoeff Coeff> |
| 17 struct GrTBlendCoeffRefsSrc : SkTBool<kSC_GrBlendCoeff == Coeff || |
| 18 kISC_GrBlendCoeff == Coeff || |
| 19 kSA_GrBlendCoeff == Coeff || |
| 20 kISA_GrBlendCoeff == Coeff> {}; |
| 21 |
| 22 #define GR_BLEND_COEFF_REFS_SRC(COEFF) \ |
| 23 GrTBlendCoeffRefsSrc<COEFF>::value |
| 24 |
| 25 inline bool GrBlendCoeffRefsSrc(GrBlendCoeff coeff) { |
17 switch (coeff) { | 26 switch (coeff) { |
18 case kSC_GrBlendCoeff: | 27 case kSC_GrBlendCoeff: |
19 case kISC_GrBlendCoeff: | 28 case kISC_GrBlendCoeff: |
20 case kSA_GrBlendCoeff: | 29 case kSA_GrBlendCoeff: |
21 case kISA_GrBlendCoeff: | 30 case kISA_GrBlendCoeff: |
22 return true; | 31 return true; |
23 default: | 32 default: |
24 return false; | 33 return false; |
25 } | 34 } |
26 } | 35 } |
27 | 36 |
28 static inline bool GrBlendCoeffRefsDst(GrBlendCoeff coeff) { | 37 |
| 38 template<GrBlendCoeff Coeff> |
| 39 struct GrTBlendCoeffRefsDst : SkTBool<kDC_GrBlendCoeff == Coeff || |
| 40 kIDC_GrBlendCoeff == Coeff || |
| 41 kDA_GrBlendCoeff == Coeff || |
| 42 kIDA_GrBlendCoeff == Coeff> {}; |
| 43 |
| 44 #define GR_BLEND_COEFF_REFS_DST(COEFF) \ |
| 45 GrTBlendCoeffRefsDst<COEFF>::value |
| 46 |
| 47 inline bool GrBlendCoeffRefsDst(GrBlendCoeff coeff) { |
29 switch (coeff) { | 48 switch (coeff) { |
30 case kDC_GrBlendCoeff: | 49 case kDC_GrBlendCoeff: |
31 case kIDC_GrBlendCoeff: | 50 case kIDC_GrBlendCoeff: |
32 case kDA_GrBlendCoeff: | 51 case kDA_GrBlendCoeff: |
33 case kIDA_GrBlendCoeff: | 52 case kIDA_GrBlendCoeff: |
34 return true; | 53 return true; |
35 default: | 54 default: |
36 return false; | 55 return false; |
37 } | 56 } |
38 } | 57 } |
39 | 58 |
40 GrColor GrSimplifyBlend(GrBlendCoeff* srcCoeff, | 59 |
41 GrBlendCoeff* dstCoeff, | 60 template<GrBlendCoeff Coeff> |
42 GrColor srcColor, uint32_t srcCompFlags, | 61 struct GrTBlendCoeffRefsSrc2 : SkTBool<kS2C_GrBlendCoeff == Coeff || |
43 GrColor dstColor, uint32_t dstCompFlags, | 62 kIS2C_GrBlendCoeff == Coeff || |
44 GrColor constantColor); | 63 kS2A_GrBlendCoeff == Coeff || |
| 64 kIS2A_GrBlendCoeff == Coeff> {}; |
| 65 |
| 66 #define GR_BLEND_COEFF_REFS_SRC2(COEFF) \ |
| 67 GrTBlendCoeffRefsSrc2<COEFF>::value |
| 68 |
| 69 inline bool GrBlendCoeffRefsSrc2(GrBlendCoeff coeff) { |
| 70 switch (coeff) { |
| 71 case kS2C_GrBlendCoeff: |
| 72 case kIS2C_GrBlendCoeff: |
| 73 case kS2A_GrBlendCoeff: |
| 74 case kIS2A_GrBlendCoeff: |
| 75 return true; |
| 76 default: |
| 77 return false; |
| 78 } |
| 79 } |
| 80 |
| 81 |
| 82 template<GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
| 83 struct GrTBlendCoeffsUseSrcColor : SkTBool<kZero_GrBlendCoeff != SrcCoeff || |
| 84 GR_BLEND_COEFF_REFS_SRC(DstCoeff)> {}
; |
| 85 |
| 86 #define GR_BLEND_COEFFS_USE_SRC_COLOR(SRC_COEFF, DST_COEFF) \ |
| 87 GrTBlendCoeffsUseSrcColor<SRC_COEFF, DST_COEFF>::value |
| 88 |
| 89 |
| 90 template<GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
| 91 struct GrTBlendCoeffsUseDstColor : SkTBool<GR_BLEND_COEFF_REFS_DST(SrcCoeff) || |
| 92 kZero_GrBlendCoeff != DstCoeff> {}; |
| 93 |
| 94 #define GR_BLEND_COEFFS_USE_DST_COLOR(SRC_COEFF, DST_COEFF) \ |
| 95 GrTBlendCoeffsUseDstColor<SRC_COEFF, DST_COEFF>::value |
| 96 |
| 97 |
| 98 template<GrBlendEquation Equation> |
| 99 struct GrTBlendEquationIsAdvanced : SkTBool<Equation >= kFirstAdvancedGrBlendEqu
ation> {}; |
| 100 |
| 101 #define GR_BLEND_EQUATION_IS_ADVANCED(EQUATION) \ |
| 102 GrTBlendEquationIsAdvanced<EQUATION>::value |
| 103 |
| 104 inline bool GrBlendEquationIsAdvanced(GrBlendEquation equation) { |
| 105 return equation >= kFirstAdvancedGrBlendEquation; |
| 106 } |
| 107 |
| 108 |
| 109 template<GrBlendEquation BlendEquation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstC
oeff> |
| 110 struct GrTBlendModifiesDst : SkTBool<(kAdd_GrBlendEquation != BlendEquation && |
| 111 kReverseSubtract_GrBlendEquation != BlendE
quation) || |
| 112 kZero_GrBlendCoeff != SrcCoeff || |
| 113 kOne_GrBlendCoeff != DstCoeff> {}; |
| 114 |
| 115 #define GR_BLEND_MODIFIES_DST(EQUATION, SRC_COEFF, DST_COEFF) \ |
| 116 GrTBlendModifiesDst<EQUATION, SRC_COEFF, DST_COEFF>::value |
| 117 |
| 118 |
| 119 /** |
| 120 * Advanced blend equations can always tweak alpha for coverage. (See GrCustomXf
ermode.cpp) |
| 121 * |
| 122 * For "add" and "reverse subtract" the blend equation with f=coverage is: |
| 123 * |
| 124 * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D |
| 125 * = f * S * srcCoeff + D * (f * dstCoeff + (1 - f)) |
| 126 * |
| 127 * (Let srcCoeff be negative for reverse subtract.) We can tweak alpha for cover
age when the |
| 128 * following relationship holds: |
| 129 * |
| 130 * (f*S) * srcCoeff' + D * dstCoeff' == f * S * srcCoeff + D * (f * dstCoeff +
(1 - f)) |
| 131 * |
| 132 * (Where srcCoeff' and dstCoeff' have any reference to S pre-multiplied by f.) |
| 133 * |
| 134 * It's easy to see this works for the src term as long as srcCoeff' == srcCoeff
(meaning srcCoeff |
| 135 * does not reference S). For the dst term, this will work as long as the follow
ing is true: |
| 136 *| |
| 137 * dstCoeff' == f * dstCoeff + (1 - f) |
| 138 * dstCoeff' == 1 - f * (1 - dstCoeff) |
| 139 * |
| 140 * By inspection we can see this will work as long as dstCoeff has a 1, and any
other term in |
| 141 * dstCoeff references S. |
| 142 */ |
| 143 template<GrBlendEquation Equation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
| 144 struct GrTBlendCanTweakAlphaForCoverage : SkTBool<GR_BLEND_EQUATION_IS_ADVANCED(
Equation) || |
| 145 ((kAdd_GrBlendEquation == Equa
tion || |
| 146 kReverseSubtract_GrBlendEqua
tion == Equation) && |
| 147 !GR_BLEND_COEFF_REFS_SRC(SrcC
oeff) && |
| 148 (kOne_GrBlendCoeff == DstCoef
f || |
| 149 kISC_GrBlendCoeff == DstCoef
f || |
| 150 kISA_GrBlendCoeff == DstCoef
f))> {}; |
| 151 |
| 152 #define GR_BLEND_CAN_TWEAK_ALPHA_FOR_COVERAGE(EQUATION, SRC_COEFF, DST_COEFF) \ |
| 153 GrTBlendCanTweakAlphaForCoverage<EQUATION, SRC_COEFF, DST_COEFF>::value |
45 | 154 |
46 #endif | 155 #endif |
OLD | NEW |