blob: c8b931da73e0b6a6f84f39ec4834586dc71877b4 [file] [log] [blame]
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrTypes.h"
Mike Klein478c9e42015-07-01 16:35:59 -040010#include "../../src/utils/SkTLogic.h"
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000011
12#ifndef GrBlend_DEFINED
13#define GrBlend_DEFINED
14
cdaltonee0175f2015-06-12 08:21:26 -070015/**
16 * Equations for alpha-blending.
17 */
18enum GrBlendEquation {
19 // Basic blend equations.
20 kAdd_GrBlendEquation, //<! Cs*S + Cd*D
21 kSubtract_GrBlendEquation, //<! Cs*S - Cd*D
22 kReverseSubtract_GrBlendEquation, //<! Cd*D - Cs*S
23
24 // Advanced blend equations. These are described in the SVG and PDF specs.
25 kScreen_GrBlendEquation,
26 kOverlay_GrBlendEquation,
27 kDarken_GrBlendEquation,
28 kLighten_GrBlendEquation,
29 kColorDodge_GrBlendEquation,
30 kColorBurn_GrBlendEquation,
31 kHardLight_GrBlendEquation,
32 kSoftLight_GrBlendEquation,
33 kDifference_GrBlendEquation,
34 kExclusion_GrBlendEquation,
35 kMultiply_GrBlendEquation,
36 kHSLHue_GrBlendEquation,
37 kHSLSaturation_GrBlendEquation,
38 kHSLColor_GrBlendEquation,
39 kHSLLuminosity_GrBlendEquation,
40
41 kFirstAdvancedGrBlendEquation = kScreen_GrBlendEquation,
42 kLast_GrBlendEquation = kHSLLuminosity_GrBlendEquation
43};
44
45static const int kGrBlendEquationCnt = kLast_GrBlendEquation + 1;
46
47
48/**
49 * Coeffecients for alpha-blending.
50 */
51enum GrBlendCoeff {
52 kZero_GrBlendCoeff, //<! 0
53 kOne_GrBlendCoeff, //<! 1
54 kSC_GrBlendCoeff, //<! src color
55 kISC_GrBlendCoeff, //<! one minus src color
56 kDC_GrBlendCoeff, //<! dst color
57 kIDC_GrBlendCoeff, //<! one minus dst color
58 kSA_GrBlendCoeff, //<! src alpha
59 kISA_GrBlendCoeff, //<! one minus src alpha
60 kDA_GrBlendCoeff, //<! dst alpha
61 kIDA_GrBlendCoeff, //<! one minus dst alpha
62 kConstC_GrBlendCoeff, //<! constant color
63 kIConstC_GrBlendCoeff, //<! one minus constant color
64 kConstA_GrBlendCoeff, //<! constant color alpha
65 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
66 kS2C_GrBlendCoeff,
67 kIS2C_GrBlendCoeff,
68 kS2A_GrBlendCoeff,
69 kIS2A_GrBlendCoeff,
70
71 kLast_GrBlendCoeff = kIS2A_GrBlendCoeff
72};
73
74static const int kGrBlendCoeffCnt = kLast_GrBlendCoeff + 1;
75
76
cdalton6fd158e2015-05-27 15:08:33 -070077template<GrBlendCoeff Coeff>
78struct GrTBlendCoeffRefsSrc : SkTBool<kSC_GrBlendCoeff == Coeff ||
79 kISC_GrBlendCoeff == Coeff ||
80 kSA_GrBlendCoeff == Coeff ||
81 kISA_GrBlendCoeff == Coeff> {};
82
83#define GR_BLEND_COEFF_REFS_SRC(COEFF) \
84 GrTBlendCoeffRefsSrc<COEFF>::value
85
86inline bool GrBlendCoeffRefsSrc(GrBlendCoeff coeff) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000087 switch (coeff) {
88 case kSC_GrBlendCoeff:
89 case kISC_GrBlendCoeff:
90 case kSA_GrBlendCoeff:
91 case kISA_GrBlendCoeff:
92 return true;
93 default:
94 return false;
95 }
96}
97
cdalton6fd158e2015-05-27 15:08:33 -070098
99template<GrBlendCoeff Coeff>
100struct GrTBlendCoeffRefsDst : SkTBool<kDC_GrBlendCoeff == Coeff ||
101 kIDC_GrBlendCoeff == Coeff ||
102 kDA_GrBlendCoeff == Coeff ||
103 kIDA_GrBlendCoeff == Coeff> {};
104
105#define GR_BLEND_COEFF_REFS_DST(COEFF) \
106 GrTBlendCoeffRefsDst<COEFF>::value
107
108inline bool GrBlendCoeffRefsDst(GrBlendCoeff coeff) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000109 switch (coeff) {
110 case kDC_GrBlendCoeff:
111 case kIDC_GrBlendCoeff:
112 case kDA_GrBlendCoeff:
113 case kIDA_GrBlendCoeff:
114 return true;
115 default:
116 return false;
117 }
118}
119
cdalton6fd158e2015-05-27 15:08:33 -0700120
121template<GrBlendCoeff Coeff>
122struct GrTBlendCoeffRefsSrc2 : SkTBool<kS2C_GrBlendCoeff == Coeff ||
123 kIS2C_GrBlendCoeff == Coeff ||
124 kS2A_GrBlendCoeff == Coeff ||
125 kIS2A_GrBlendCoeff == Coeff> {};
126
127#define GR_BLEND_COEFF_REFS_SRC2(COEFF) \
128 GrTBlendCoeffRefsSrc2<COEFF>::value
129
130inline bool GrBlendCoeffRefsSrc2(GrBlendCoeff coeff) {
131 switch (coeff) {
132 case kS2C_GrBlendCoeff:
133 case kIS2C_GrBlendCoeff:
134 case kS2A_GrBlendCoeff:
135 case kIS2A_GrBlendCoeff:
136 return true;
137 default:
138 return false;
139 }
140}
141
142
143template<GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff>
144struct GrTBlendCoeffsUseSrcColor : SkTBool<kZero_GrBlendCoeff != SrcCoeff ||
145 GR_BLEND_COEFF_REFS_SRC(DstCoeff)> {};
146
147#define GR_BLEND_COEFFS_USE_SRC_COLOR(SRC_COEFF, DST_COEFF) \
148 GrTBlendCoeffsUseSrcColor<SRC_COEFF, DST_COEFF>::value
149
150
151template<GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff>
152struct GrTBlendCoeffsUseDstColor : SkTBool<GR_BLEND_COEFF_REFS_DST(SrcCoeff) ||
153 kZero_GrBlendCoeff != DstCoeff> {};
154
155#define GR_BLEND_COEFFS_USE_DST_COLOR(SRC_COEFF, DST_COEFF) \
156 GrTBlendCoeffsUseDstColor<SRC_COEFF, DST_COEFF>::value
157
158
159template<GrBlendEquation Equation>
160struct GrTBlendEquationIsAdvanced : SkTBool<Equation >= kFirstAdvancedGrBlendEquation> {};
161
162#define GR_BLEND_EQUATION_IS_ADVANCED(EQUATION) \
163 GrTBlendEquationIsAdvanced<EQUATION>::value
164
165inline bool GrBlendEquationIsAdvanced(GrBlendEquation equation) {
166 return equation >= kFirstAdvancedGrBlendEquation;
167}
168
169
170template<GrBlendEquation BlendEquation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff>
171struct GrTBlendModifiesDst : SkTBool<(kAdd_GrBlendEquation != BlendEquation &&
172 kReverseSubtract_GrBlendEquation != BlendEquation) ||
173 kZero_GrBlendCoeff != SrcCoeff ||
174 kOne_GrBlendCoeff != DstCoeff> {};
175
176#define GR_BLEND_MODIFIES_DST(EQUATION, SRC_COEFF, DST_COEFF) \
177 GrTBlendModifiesDst<EQUATION, SRC_COEFF, DST_COEFF>::value
178
179
180/**
181 * Advanced blend equations can always tweak alpha for coverage. (See GrCustomXfermode.cpp)
182 *
183 * For "add" and "reverse subtract" the blend equation with f=coverage is:
184 *
185 * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D
186 * = f * S * srcCoeff + D * (f * dstCoeff + (1 - f))
187 *
188 * (Let srcCoeff be negative for reverse subtract.) We can tweak alpha for coverage when the
189 * following relationship holds:
190 *
191 * (f*S) * srcCoeff' + D * dstCoeff' == f * S * srcCoeff + D * (f * dstCoeff + (1 - f))
192 *
193 * (Where srcCoeff' and dstCoeff' have any reference to S pre-multiplied by f.)
194 *
195 * It's easy to see this works for the src term as long as srcCoeff' == srcCoeff (meaning srcCoeff
196 * does not reference S). For the dst term, this will work as long as the following is true:
197 *|
198 * dstCoeff' == f * dstCoeff + (1 - f)
199 * dstCoeff' == 1 - f * (1 - dstCoeff)
200 *
201 * By inspection we can see this will work as long as dstCoeff has a 1, and any other term in
202 * dstCoeff references S.
203 */
204template<GrBlendEquation Equation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff>
205struct GrTBlendCanTweakAlphaForCoverage : SkTBool<GR_BLEND_EQUATION_IS_ADVANCED(Equation) ||
206 ((kAdd_GrBlendEquation == Equation ||
207 kReverseSubtract_GrBlendEquation == Equation) &&
208 !GR_BLEND_COEFF_REFS_SRC(SrcCoeff) &&
209 (kOne_GrBlendCoeff == DstCoeff ||
210 kISC_GrBlendCoeff == DstCoeff ||
211 kISA_GrBlendCoeff == DstCoeff))> {};
212
213#define GR_BLEND_CAN_TWEAK_ALPHA_FOR_COVERAGE(EQUATION, SRC_COEFF, DST_COEFF) \
214 GrTBlendCanTweakAlphaForCoverage<EQUATION, SRC_COEFF, DST_COEFF>::value
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000215
216#endif