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 |
| 70 template<GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
| 71 struct GrTBlendCoeffsUseSrcColor : SkTBool<kZero_GrBlendCoeff != SrcCoeff || |
| 72 GR_BLEND_COEFF_REFS_SRC(DstCoeff)> {}
; |
| 73 |
| 74 #define GR_BLEND_COEFFS_USE_SRC_COLOR(SRC_COEFF, DST_COEFF) \ |
| 75 GrTBlendCoeffsUseSrcColor<SRC_COEFF, DST_COEFF>::value |
| 76 |
| 77 |
| 78 template<GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
| 79 struct GrTBlendCoeffsUseDstColor : SkTBool<GR_BLEND_COEFF_REFS_DST(SrcCoeff) || |
| 80 kZero_GrBlendCoeff != DstCoeff> {}; |
| 81 |
| 82 #define GR_BLEND_COEFFS_USE_DST_COLOR(SRC_COEFF, DST_COEFF) \ |
| 83 GrTBlendCoeffsUseDstColor<SRC_COEFF, DST_COEFF>::value |
| 84 |
| 85 |
| 86 template<GrBlendEquation Equation> |
| 87 struct GrTBlendEquationIsAdvanced : SkTBool<Equation >= kFirstAdvancedGrBlendEqu
ation> {}; |
| 88 |
| 89 #define GR_BLEND_EQUATION_IS_ADVANCED(EQUATION) \ |
| 90 GrTBlendEquationIsAdvanced<EQUATION>::value |
| 91 |
| 92 inline bool GrBlendEquationIsAdvanced(GrBlendEquation equation) { |
| 93 return equation >= kFirstAdvancedGrBlendEquation; |
| 94 } |
| 95 |
| 96 |
| 97 template<GrBlendEquation BlendEquation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstC
oeff> |
| 98 struct GrTBlendModifiesDst : SkTBool<(kAdd_GrBlendEquation != BlendEquation && |
| 99 kReverseSubtract_GrBlendEquation != BlendE
quation) || |
| 100 kZero_GrBlendCoeff != SrcCoeff || |
| 101 kOne_GrBlendCoeff != DstCoeff> {}; |
| 102 |
| 103 #define GR_BLEND_MODIFIES_DST(EQUATION, SRC_COEFF, DST_COEFF) \ |
| 104 GrTBlendModifiesDst<EQUATION, SRC_COEFF, DST_COEFF>::value |
| 105 |
| 106 |
| 107 /** |
| 108 * Advanced blend equations can always tweak alpha for coverage. (See GrCustomXf
ermode.cpp) |
| 109 * |
| 110 * For "add" and "reverse subtract" the blend equation with f=coverage is: |
| 111 * |
| 112 * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D |
| 113 * = f * S * srcCoeff + D * (f * dstCoeff + (1 - f)) |
| 114 * |
| 115 * (Let srcCoeff be negative for reverse subtract.) We can tweak alpha for cover
age when the |
| 116 * following relationship holds: |
| 117 * |
| 118 * (f*S) * srcCoeff' + D * dstCoeff' == f * S * srcCoeff + D * (f * dstCoeff +
(1 - f)) |
| 119 * |
| 120 * (Where srcCoeff' and dstCoeff' have any reference to S pre-multiplied by f.) |
| 121 * |
| 122 * It's easy to see this works for the src term as long as srcCoeff' == srcCoeff
(meaning srcCoeff |
| 123 * does not reference S). For the dst term, this will work as long as the follow
ing is true: |
| 124 *| |
| 125 * dstCoeff' == f * dstCoeff + (1 - f) |
| 126 * dstCoeff' == 1 - f * (1 - dstCoeff) |
| 127 * |
| 128 * By inspection we can see this will work as long as dstCoeff has a 1, and any
other term in |
| 129 * dstCoeff references S. |
| 130 */ |
| 131 template<GrBlendEquation Equation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
| 132 struct GrTBlendCanTweakAlphaForCoverage : SkTBool<GR_BLEND_EQUATION_IS_ADVANCED(
Equation) || |
| 133 ((kAdd_GrBlendEquation == Equa
tion || |
| 134 kReverseSubtract_GrBlendEqua
tion == Equation) && |
| 135 !GR_BLEND_COEFF_REFS_SRC(SrcC
oeff) && |
| 136 (kOne_GrBlendCoeff == DstCoef
f || |
| 137 kISC_GrBlendCoeff == DstCoef
f || |
| 138 kISA_GrBlendCoeff == DstCoef
f))> {}; |
| 139 |
| 140 #define GR_BLEND_CAN_TWEAK_ALPHA_FOR_COVERAGE(EQUATION, SRC_COEFF, DST_COEFF) \ |
| 141 GrTBlendCanTweakAlphaForCoverage<EQUATION, SRC_COEFF, DST_COEFF>::value |
45 | 142 |
46 #endif | 143 #endif |
OLD | NEW |