blob: ab75ee215f9635fb0891bc4e568964f6085eec20 [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
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +00009#ifndef GrBlend_DEFINED
10#define GrBlend_DEFINED
11
Brian Osman653c6b52018-09-20 11:04:00 -040012#include "SkTypes.h"
bsalomonae4738f2015-09-15 15:33:27 -070013
cdaltonee0175f2015-06-12 08:21:26 -070014/**
15 * Equations for alpha-blending.
16 */
17enum GrBlendEquation {
18 // Basic blend equations.
19 kAdd_GrBlendEquation, //<! Cs*S + Cd*D
20 kSubtract_GrBlendEquation, //<! Cs*S - Cd*D
21 kReverseSubtract_GrBlendEquation, //<! Cd*D - Cs*S
22
23 // Advanced blend equations. These are described in the SVG and PDF specs.
24 kScreen_GrBlendEquation,
25 kOverlay_GrBlendEquation,
26 kDarken_GrBlendEquation,
27 kLighten_GrBlendEquation,
28 kColorDodge_GrBlendEquation,
29 kColorBurn_GrBlendEquation,
30 kHardLight_GrBlendEquation,
31 kSoftLight_GrBlendEquation,
32 kDifference_GrBlendEquation,
33 kExclusion_GrBlendEquation,
34 kMultiply_GrBlendEquation,
35 kHSLHue_GrBlendEquation,
36 kHSLSaturation_GrBlendEquation,
37 kHSLColor_GrBlendEquation,
38 kHSLLuminosity_GrBlendEquation,
39
40 kFirstAdvancedGrBlendEquation = kScreen_GrBlendEquation,
41 kLast_GrBlendEquation = kHSLLuminosity_GrBlendEquation
42};
43
44static const int kGrBlendEquationCnt = kLast_GrBlendEquation + 1;
45
46
47/**
bsalomonae4738f2015-09-15 15:33:27 -070048 * Coefficients for alpha-blending.
cdaltonee0175f2015-06-12 08:21:26 -070049 */
50enum GrBlendCoeff {
51 kZero_GrBlendCoeff, //<! 0
52 kOne_GrBlendCoeff, //<! 1
53 kSC_GrBlendCoeff, //<! src color
54 kISC_GrBlendCoeff, //<! one minus src color
55 kDC_GrBlendCoeff, //<! dst color
56 kIDC_GrBlendCoeff, //<! one minus dst color
57 kSA_GrBlendCoeff, //<! src alpha
58 kISA_GrBlendCoeff, //<! one minus src alpha
59 kDA_GrBlendCoeff, //<! dst alpha
60 kIDA_GrBlendCoeff, //<! one minus dst alpha
61 kConstC_GrBlendCoeff, //<! constant color
62 kIConstC_GrBlendCoeff, //<! one minus constant color
63 kConstA_GrBlendCoeff, //<! constant color alpha
64 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
65 kS2C_GrBlendCoeff,
66 kIS2C_GrBlendCoeff,
67 kS2A_GrBlendCoeff,
68 kIS2A_GrBlendCoeff,
69
70 kLast_GrBlendCoeff = kIS2A_GrBlendCoeff
71};
72
73static const int kGrBlendCoeffCnt = kLast_GrBlendCoeff + 1;
74
Brian Salomon7b297492017-04-10 11:17:14 -040075static constexpr bool GrBlendCoeffRefsSrc(const GrBlendCoeff coeff) {
76 return kSC_GrBlendCoeff == coeff || kISC_GrBlendCoeff == coeff || kSA_GrBlendCoeff == coeff ||
77 kISA_GrBlendCoeff == coeff;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000078}
79
Brian Salomon7b297492017-04-10 11:17:14 -040080static constexpr bool GrBlendCoeffRefsDst(const GrBlendCoeff coeff) {
81 return kDC_GrBlendCoeff == coeff || kIDC_GrBlendCoeff == coeff || kDA_GrBlendCoeff == coeff ||
82 kIDA_GrBlendCoeff == coeff;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000083}
84
Brian Salomon7b297492017-04-10 11:17:14 -040085static constexpr bool GrBlendCoeffRefsSrc2(const GrBlendCoeff coeff) {
86 return kS2C_GrBlendCoeff == coeff || kIS2C_GrBlendCoeff == coeff ||
87 kS2A_GrBlendCoeff == coeff || kIS2A_GrBlendCoeff == coeff;
cdalton6fd158e2015-05-27 15:08:33 -070088}
89
Brian Salomon7b297492017-04-10 11:17:14 -040090static constexpr bool GrBlendCoeffsUseSrcColor(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
91 return kZero_GrBlendCoeff != srcCoeff || GrBlendCoeffRefsSrc(dstCoeff);
92}
cdalton6fd158e2015-05-27 15:08:33 -070093
Brian Salomon7b297492017-04-10 11:17:14 -040094static constexpr bool GrBlendCoeffsUseDstColor(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
95 return GrBlendCoeffRefsDst(srcCoeff) || kZero_GrBlendCoeff != dstCoeff;
96}
cdalton6fd158e2015-05-27 15:08:33 -070097
Brian Salomon7b297492017-04-10 11:17:14 -040098static constexpr bool GrBlendEquationIsAdvanced(GrBlendEquation equation) {
cdalton6fd158e2015-05-27 15:08:33 -070099 return equation >= kFirstAdvancedGrBlendEquation;
100}
101
Brian Salomon7b297492017-04-10 11:17:14 -0400102static constexpr bool GrBlendModifiesDst(GrBlendEquation equation, GrBlendCoeff srcCoeff,
103 GrBlendCoeff dstCoeff) {
104 return (kAdd_GrBlendEquation != equation && kReverseSubtract_GrBlendEquation != equation) ||
105 kZero_GrBlendCoeff != srcCoeff || kOne_GrBlendCoeff != dstCoeff;
106}
cdalton6fd158e2015-05-27 15:08:33 -0700107
108/**
109 * Advanced blend equations can always tweak alpha for coverage. (See GrCustomXfermode.cpp)
110 *
111 * For "add" and "reverse subtract" the blend equation with f=coverage is:
112 *
113 * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D
114 * = f * S * srcCoeff + D * (f * dstCoeff + (1 - f))
115 *
116 * (Let srcCoeff be negative for reverse subtract.) We can tweak alpha for coverage when the
117 * following relationship holds:
118 *
119 * (f*S) * srcCoeff' + D * dstCoeff' == f * S * srcCoeff + D * (f * dstCoeff + (1 - f))
120 *
121 * (Where srcCoeff' and dstCoeff' have any reference to S pre-multiplied by f.)
122 *
123 * It's easy to see this works for the src term as long as srcCoeff' == srcCoeff (meaning srcCoeff
124 * does not reference S). For the dst term, this will work as long as the following is true:
125 *|
126 * dstCoeff' == f * dstCoeff + (1 - f)
127 * dstCoeff' == 1 - f * (1 - dstCoeff)
128 *
129 * By inspection we can see this will work as long as dstCoeff has a 1, and any other term in
130 * dstCoeff references S.
Brian Salomon31853842017-03-28 16:32:05 -0400131 *
132 * Moreover, if the blend doesn't modify the dst at all then it is ok to arbitrarily modify the src
133 * color so folding in coverage is allowed.
cdalton6fd158e2015-05-27 15:08:33 -0700134 */
Brian Salomon7b297492017-04-10 11:17:14 -0400135static constexpr bool GrBlendAllowsCoverageAsAlpha(GrBlendEquation equation,
136 GrBlendCoeff srcCoeff,
137 GrBlendCoeff dstCoeff) {
138 return GrBlendEquationIsAdvanced(equation) ||
139 !GrBlendModifiesDst(equation, srcCoeff, dstCoeff) ||
140 ((kAdd_GrBlendEquation == equation || kReverseSubtract_GrBlendEquation == equation) &&
141 !GrBlendCoeffRefsSrc(srcCoeff) &&
142 (kOne_GrBlendCoeff == dstCoeff || kISC_GrBlendCoeff == dstCoeff ||
143 kISA_GrBlendCoeff == dstCoeff));
144}
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000145
146#endif