commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 1 | |
| 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.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 9 | #ifndef GrBlend_DEFINED |
| 10 | #define GrBlend_DEFINED |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkTypes.h" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 13 | |
cdalton | ee0175f | 2015-06-12 08:21:26 -0700 | [diff] [blame] | 14 | /** |
| 15 | * Equations for alpha-blending. |
| 16 | */ |
| 17 | enum 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 | |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 40 | kIllegal_GrBlendEquation, |
| 41 | |
cdalton | ee0175f | 2015-06-12 08:21:26 -0700 | [diff] [blame] | 42 | kFirstAdvancedGrBlendEquation = kScreen_GrBlendEquation, |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 43 | kLast_GrBlendEquation = kIllegal_GrBlendEquation, |
cdalton | ee0175f | 2015-06-12 08:21:26 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static const int kGrBlendEquationCnt = kLast_GrBlendEquation + 1; |
| 47 | |
| 48 | |
| 49 | /** |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 50 | * Coefficients for alpha-blending. |
cdalton | ee0175f | 2015-06-12 08:21:26 -0700 | [diff] [blame] | 51 | */ |
| 52 | enum GrBlendCoeff { |
| 53 | kZero_GrBlendCoeff, //<! 0 |
| 54 | kOne_GrBlendCoeff, //<! 1 |
| 55 | kSC_GrBlendCoeff, //<! src color |
| 56 | kISC_GrBlendCoeff, //<! one minus src color |
| 57 | kDC_GrBlendCoeff, //<! dst color |
| 58 | kIDC_GrBlendCoeff, //<! one minus dst color |
| 59 | kSA_GrBlendCoeff, //<! src alpha |
| 60 | kISA_GrBlendCoeff, //<! one minus src alpha |
| 61 | kDA_GrBlendCoeff, //<! dst alpha |
| 62 | kIDA_GrBlendCoeff, //<! one minus dst alpha |
| 63 | kConstC_GrBlendCoeff, //<! constant color |
| 64 | kIConstC_GrBlendCoeff, //<! one minus constant color |
| 65 | kConstA_GrBlendCoeff, //<! constant color alpha |
| 66 | kIConstA_GrBlendCoeff, //<! one minus constant color alpha |
| 67 | kS2C_GrBlendCoeff, |
| 68 | kIS2C_GrBlendCoeff, |
| 69 | kS2A_GrBlendCoeff, |
| 70 | kIS2A_GrBlendCoeff, |
| 71 | |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 72 | kIllegal_GrBlendCoeff, |
| 73 | |
| 74 | kLast_GrBlendCoeff = kIllegal_GrBlendCoeff, |
cdalton | ee0175f | 2015-06-12 08:21:26 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static const int kGrBlendCoeffCnt = kLast_GrBlendCoeff + 1; |
| 78 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 79 | static constexpr bool GrBlendCoeffRefsSrc(const GrBlendCoeff coeff) { |
| 80 | return kSC_GrBlendCoeff == coeff || kISC_GrBlendCoeff == coeff || kSA_GrBlendCoeff == coeff || |
| 81 | kISA_GrBlendCoeff == coeff; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 84 | static constexpr bool GrBlendCoeffRefsDst(const GrBlendCoeff coeff) { |
| 85 | return kDC_GrBlendCoeff == coeff || kIDC_GrBlendCoeff == coeff || kDA_GrBlendCoeff == coeff || |
| 86 | kIDA_GrBlendCoeff == coeff; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 89 | static constexpr bool GrBlendCoeffRefsSrc2(const GrBlendCoeff coeff) { |
| 90 | return kS2C_GrBlendCoeff == coeff || kIS2C_GrBlendCoeff == coeff || |
| 91 | kS2A_GrBlendCoeff == coeff || kIS2A_GrBlendCoeff == coeff; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 94 | static constexpr bool GrBlendCoeffsUseSrcColor(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
| 95 | return kZero_GrBlendCoeff != srcCoeff || GrBlendCoeffRefsSrc(dstCoeff); |
| 96 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 97 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 98 | static constexpr bool GrBlendCoeffsUseDstColor(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { |
| 99 | return GrBlendCoeffRefsDst(srcCoeff) || kZero_GrBlendCoeff != dstCoeff; |
| 100 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 101 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 102 | static constexpr bool GrBlendEquationIsAdvanced(GrBlendEquation equation) { |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 103 | return equation >= kFirstAdvancedGrBlendEquation |
| 104 | && equation != kIllegal_GrBlendEquation; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 107 | static constexpr bool GrBlendModifiesDst(GrBlendEquation equation, GrBlendCoeff srcCoeff, |
| 108 | GrBlendCoeff dstCoeff) { |
| 109 | return (kAdd_GrBlendEquation != equation && kReverseSubtract_GrBlendEquation != equation) || |
| 110 | kZero_GrBlendCoeff != srcCoeff || kOne_GrBlendCoeff != dstCoeff; |
| 111 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Advanced blend equations can always tweak alpha for coverage. (See GrCustomXfermode.cpp) |
| 115 | * |
| 116 | * For "add" and "reverse subtract" the blend equation with f=coverage is: |
| 117 | * |
| 118 | * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D |
| 119 | * = f * S * srcCoeff + D * (f * dstCoeff + (1 - f)) |
| 120 | * |
| 121 | * (Let srcCoeff be negative for reverse subtract.) We can tweak alpha for coverage when the |
| 122 | * following relationship holds: |
| 123 | * |
| 124 | * (f*S) * srcCoeff' + D * dstCoeff' == f * S * srcCoeff + D * (f * dstCoeff + (1 - f)) |
| 125 | * |
| 126 | * (Where srcCoeff' and dstCoeff' have any reference to S pre-multiplied by f.) |
| 127 | * |
| 128 | * It's easy to see this works for the src term as long as srcCoeff' == srcCoeff (meaning srcCoeff |
| 129 | * does not reference S). For the dst term, this will work as long as the following is true: |
| 130 | *| |
| 131 | * dstCoeff' == f * dstCoeff + (1 - f) |
| 132 | * dstCoeff' == 1 - f * (1 - dstCoeff) |
| 133 | * |
| 134 | * By inspection we can see this will work as long as dstCoeff has a 1, and any other term in |
| 135 | * dstCoeff references S. |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 136 | * |
| 137 | * Moreover, if the blend doesn't modify the dst at all then it is ok to arbitrarily modify the src |
| 138 | * color so folding in coverage is allowed. |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 139 | */ |
Brian Salomon | 7b29749 | 2017-04-10 11:17:14 -0400 | [diff] [blame] | 140 | static constexpr bool GrBlendAllowsCoverageAsAlpha(GrBlendEquation equation, |
| 141 | GrBlendCoeff srcCoeff, |
| 142 | GrBlendCoeff dstCoeff) { |
| 143 | return GrBlendEquationIsAdvanced(equation) || |
| 144 | !GrBlendModifiesDst(equation, srcCoeff, dstCoeff) || |
| 145 | ((kAdd_GrBlendEquation == equation || kReverseSubtract_GrBlendEquation == equation) && |
| 146 | !GrBlendCoeffRefsSrc(srcCoeff) && |
| 147 | (kOne_GrBlendCoeff == dstCoeff || kISC_GrBlendCoeff == dstCoeff || |
| 148 | kISA_GrBlendCoeff == dstCoeff)); |
| 149 | } |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 150 | |
| 151 | #endif |