egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
egdaniel | ced9010 | 2014-12-05 12:40:52 -0800 | [diff] [blame] | 8 | #include "effects/GrPorterDuffXferProcessor.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 9 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 10 | #include "GrBlend.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 12 | #include "GrPipeline.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 13 | #include "GrProcessor.h" |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 14 | #include "GrProcOptInfo.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 15 | #include "GrTypes.h" |
| 16 | #include "GrXferProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLBlend.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 19 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 20 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLXferProcessor.h" |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 22 | #include <utility> |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 23 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Wraps the shader outputs and HW blend state that comprise a Porter Duff blend mode with coverage. |
| 26 | */ |
| 27 | struct BlendFormula { |
| 28 | public: |
| 29 | /** |
| 30 | * Values the shader can write to primary and secondary outputs. These must all be modulated by |
| 31 | * coverage to support mixed samples. The XP will ignore the multiplies when not using coverage. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 32 | */ |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 33 | enum OutputType { |
| 34 | kNone_OutputType, //<! 0 |
| 35 | kCoverage_OutputType, //<! inputCoverage |
| 36 | kModulate_OutputType, //<! inputColor * inputCoverage |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 37 | kSAModulate_OutputType, //<! inputColor.a * inputCoverage |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 38 | kISAModulate_OutputType, //<! (1 - inputColor.a) * inputCoverage |
| 39 | kISCModulate_OutputType, //<! (1 - inputColor) * inputCoverage |
| 40 | |
| 41 | kLast_OutputType = kISCModulate_OutputType |
| 42 | }; |
| 43 | |
| 44 | enum Properties { |
| 45 | kModifiesDst_Property = 1, |
| 46 | kUsesDstColor_Property = 1 << 1, |
| 47 | kUsesInputColor_Property = 1 << 2, |
| 48 | kCanTweakAlphaForCoverage_Property = 1 << 3, |
| 49 | |
| 50 | kLast_Property = kCanTweakAlphaForCoverage_Property |
| 51 | }; |
| 52 | |
| 53 | BlendFormula& operator =(const BlendFormula& other) { |
| 54 | fData = other.fData; |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | bool operator ==(const BlendFormula& other) const { |
| 59 | return fData == other.fData; |
| 60 | } |
| 61 | |
| 62 | bool hasSecondaryOutput() const { return kNone_OutputType != fSecondaryOutputType; } |
| 63 | bool modifiesDst() const { return SkToBool(fProps & kModifiesDst_Property); } |
| 64 | bool usesDstColor() const { return SkToBool(fProps & kUsesDstColor_Property); } |
| 65 | bool usesInputColor() const { return SkToBool(fProps & kUsesInputColor_Property); } |
| 66 | bool canTweakAlphaForCoverage() const { |
| 67 | return SkToBool(fProps & kCanTweakAlphaForCoverage_Property); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Deduce the properties of a compile-time constant BlendFormula. |
| 72 | */ |
| 73 | template<OutputType PrimaryOut, OutputType SecondaryOut, |
| 74 | GrBlendEquation BlendEquation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff> |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 75 | struct get_properties : std::integral_constant<Properties, static_cast<Properties>( |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 76 | |
| 77 | (GR_BLEND_MODIFIES_DST(BlendEquation, SrcCoeff, DstCoeff) ? |
| 78 | kModifiesDst_Property : 0) | |
| 79 | |
| 80 | (GR_BLEND_COEFFS_USE_DST_COLOR(SrcCoeff, DstCoeff) ? |
| 81 | kUsesDstColor_Property : 0) | |
| 82 | |
| 83 | ((PrimaryOut >= kModulate_OutputType && GR_BLEND_COEFFS_USE_SRC_COLOR(SrcCoeff,DstCoeff)) || |
| 84 | (SecondaryOut >= kModulate_OutputType && GR_BLEND_COEFF_REFS_SRC2(DstCoeff)) ? |
| 85 | kUsesInputColor_Property : 0) | // We assert later that SrcCoeff doesn't ref src2. |
| 86 | |
| 87 | (kModulate_OutputType == PrimaryOut && |
| 88 | kNone_OutputType == SecondaryOut && |
| 89 | GR_BLEND_CAN_TWEAK_ALPHA_FOR_COVERAGE(BlendEquation, SrcCoeff, DstCoeff) ? |
| 90 | kCanTweakAlphaForCoverage_Property : 0))> { |
| 91 | |
| 92 | // The provided formula should already be optimized. |
| 93 | GR_STATIC_ASSERT((kNone_OutputType == PrimaryOut) == |
| 94 | !GR_BLEND_COEFFS_USE_SRC_COLOR(SrcCoeff, DstCoeff)); |
| 95 | GR_STATIC_ASSERT(!GR_BLEND_COEFF_REFS_SRC2(SrcCoeff)); |
| 96 | GR_STATIC_ASSERT((kNone_OutputType == SecondaryOut) == |
| 97 | !GR_BLEND_COEFF_REFS_SRC2(DstCoeff)); |
| 98 | GR_STATIC_ASSERT(PrimaryOut != SecondaryOut || kNone_OutputType == PrimaryOut); |
| 99 | GR_STATIC_ASSERT(kNone_OutputType != PrimaryOut || kNone_OutputType == SecondaryOut); |
| 100 | }; |
| 101 | |
| 102 | union { |
| 103 | struct { |
| 104 | // We allot the enums one more bit than they require because MSVC seems to sign-extend |
| 105 | // them when the top bit is set. (This is in violation of the C++03 standard 9.6/4) |
| 106 | OutputType fPrimaryOutputType : 4; |
| 107 | OutputType fSecondaryOutputType : 4; |
| 108 | GrBlendEquation fBlendEquation : 6; |
| 109 | GrBlendCoeff fSrcCoeff : 6; |
| 110 | GrBlendCoeff fDstCoeff : 6; |
| 111 | Properties fProps : 32 - (4 + 4 + 6 + 6 + 6); |
| 112 | }; |
| 113 | uint32_t fData; |
| 114 | }; |
| 115 | |
| 116 | GR_STATIC_ASSERT(kLast_OutputType < (1 << 3)); |
| 117 | GR_STATIC_ASSERT(kLast_GrBlendEquation < (1 << 5)); |
| 118 | GR_STATIC_ASSERT(kLast_GrBlendCoeff < (1 << 5)); |
| 119 | GR_STATIC_ASSERT(kLast_Property < (1 << 6)); |
| 120 | }; |
| 121 | |
| 122 | GR_STATIC_ASSERT(4 == sizeof(BlendFormula)); |
| 123 | |
| 124 | GR_MAKE_BITFIELD_OPS(BlendFormula::Properties); |
| 125 | |
| 126 | /** |
| 127 | * Initialize a compile-time constant BlendFormula and automatically deduce fProps. |
| 128 | */ |
| 129 | #define INIT_BLEND_FORMULA(PRIMARY_OUT, SECONDARY_OUT, BLEND_EQUATION, SRC_COEFF, DST_COEFF) \ |
| 130 | {{{PRIMARY_OUT, \ |
| 131 | SECONDARY_OUT, \ |
| 132 | BLEND_EQUATION, SRC_COEFF, DST_COEFF, \ |
| 133 | BlendFormula::get_properties<PRIMARY_OUT, SECONDARY_OUT, \ |
| 134 | BLEND_EQUATION, SRC_COEFF, DST_COEFF>::value}}} |
| 135 | |
| 136 | /** |
| 137 | * When there is no coverage, or the blend mode can tweak alpha for coverage, we use the standard |
| 138 | * Porter Duff formula. |
| 139 | */ |
| 140 | #define COEFF_FORMULA(SRC_COEFF, DST_COEFF) \ |
| 141 | INIT_BLEND_FORMULA(BlendFormula::kModulate_OutputType, \ |
| 142 | BlendFormula::kNone_OutputType, \ |
| 143 | kAdd_GrBlendEquation, SRC_COEFF, DST_COEFF) |
| 144 | |
| 145 | /** |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 146 | * Basic coeff formula similar to COEFF_FORMULA but we will make the src f*Sa. This is used in |
| 147 | * LCD dst-out. |
| 148 | */ |
| 149 | #define COEFF_FORMULA_SA_MODULATE(SRC_COEFF, DST_COEFF) \ |
| 150 | INIT_BLEND_FORMULA(BlendFormula::kSAModulate_OutputType, \ |
| 151 | BlendFormula::kNone_OutputType, \ |
| 152 | kAdd_GrBlendEquation, SRC_COEFF, DST_COEFF) |
| 153 | |
| 154 | /** |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 155 | * When the coeffs are (Zero, Zero), we clear the dst. This formula has its own macro so we can set |
| 156 | * the primary output type to none. |
| 157 | */ |
| 158 | #define DST_CLEAR_FORMULA \ |
| 159 | INIT_BLEND_FORMULA(BlendFormula::kNone_OutputType, \ |
| 160 | BlendFormula::kNone_OutputType, \ |
| 161 | kAdd_GrBlendEquation, kZero_GrBlendCoeff, kZero_GrBlendCoeff) |
| 162 | |
| 163 | /** |
| 164 | * When the coeffs are (Zero, One), we don't write to the dst at all. This formula has its own macro |
| 165 | * so we can set the primary output type to none. |
| 166 | */ |
| 167 | #define NO_DST_WRITE_FORMULA \ |
| 168 | INIT_BLEND_FORMULA(BlendFormula::kNone_OutputType, \ |
| 169 | BlendFormula::kNone_OutputType, \ |
| 170 | kAdd_GrBlendEquation, kZero_GrBlendCoeff, kOne_GrBlendCoeff) |
| 171 | |
| 172 | /** |
| 173 | * When there is coverage, the equation with f=coverage is: |
| 174 | * |
| 175 | * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D |
| 176 | * |
| 177 | * This can be rewritten as: |
| 178 | * |
| 179 | * D' = f * S * srcCoeff + D * (1 - [f * (1 - dstCoeff)]) |
| 180 | * |
| 181 | * To implement this formula, we output [f * (1 - dstCoeff)] for the secondary color and replace the |
| 182 | * HW dst coeff with IS2C. |
| 183 | * |
| 184 | * Xfer modes: dst-atop (Sa!=1) |
| 185 | */ |
| 186 | #define COVERAGE_FORMULA(ONE_MINUS_DST_COEFF_MODULATE_OUTPUT, SRC_COEFF) \ |
| 187 | INIT_BLEND_FORMULA(BlendFormula::kModulate_OutputType, \ |
| 188 | ONE_MINUS_DST_COEFF_MODULATE_OUTPUT, \ |
| 189 | kAdd_GrBlendEquation, SRC_COEFF, kIS2C_GrBlendCoeff) |
| 190 | |
| 191 | /** |
| 192 | * When there is coverage and the src coeff is Zero, the equation with f=coverage becomes: |
| 193 | * |
| 194 | * D' = f * D * dstCoeff + (1-f) * D |
| 195 | * |
| 196 | * This can be rewritten as: |
| 197 | * |
| 198 | * D' = D - D * [f * (1 - dstCoeff)] |
| 199 | * |
| 200 | * To implement this formula, we output [f * (1 - dstCoeff)] for the primary color and use a reverse |
| 201 | * subtract HW blend equation with coeffs of (DC, One). |
| 202 | * |
| 203 | * Xfer modes: clear, dst-out (Sa=1), dst-in (Sa!=1), modulate (Sc!=1) |
| 204 | */ |
| 205 | #define COVERAGE_SRC_COEFF_ZERO_FORMULA(ONE_MINUS_DST_COEFF_MODULATE_OUTPUT) \ |
| 206 | INIT_BLEND_FORMULA(ONE_MINUS_DST_COEFF_MODULATE_OUTPUT, \ |
| 207 | BlendFormula::kNone_OutputType, \ |
| 208 | kReverseSubtract_GrBlendEquation, kDC_GrBlendCoeff, kOne_GrBlendCoeff) |
| 209 | |
| 210 | /** |
| 211 | * When there is coverage and the dst coeff is Zero, the equation with f=coverage becomes: |
| 212 | * |
| 213 | * D' = f * S * srcCoeff + (1-f) * D |
| 214 | * |
| 215 | * To implement this formula, we output [f] for the secondary color and replace the HW dst coeff |
| 216 | * with IS2A. (Note that we can avoid dual source blending when Sa=1 by using ISA.) |
| 217 | * |
| 218 | * Xfer modes (Sa!=1): src, src-in, src-out |
| 219 | */ |
| 220 | #define COVERAGE_DST_COEFF_ZERO_FORMULA(SRC_COEFF) \ |
| 221 | INIT_BLEND_FORMULA(BlendFormula::kModulate_OutputType, \ |
| 222 | BlendFormula::kCoverage_OutputType, \ |
| 223 | kAdd_GrBlendEquation, SRC_COEFF, kIS2A_GrBlendCoeff) |
| 224 | |
| 225 | /** |
| 226 | * This table outlines the blend formulas we will use with each xfermode, with and without coverage, |
| 227 | * with and without an opaque input color. Optimization properties are deduced at compile time so we |
| 228 | * can make runtime decisions quickly. RGB coverage is not supported. |
| 229 | */ |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 230 | static const BlendFormula gBlendTable[2][2][(int)SkBlendMode::kLastCoeffMode + 1] = { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 231 | |
| 232 | /*>> No coverage, input color unknown <<*/ {{ |
| 233 | |
| 234 | /* clear */ DST_CLEAR_FORMULA, |
| 235 | /* src */ COEFF_FORMULA( kOne_GrBlendCoeff, kZero_GrBlendCoeff), |
| 236 | /* dst */ NO_DST_WRITE_FORMULA, |
| 237 | /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff), |
| 238 | /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 239 | /* src-in */ COEFF_FORMULA( kDA_GrBlendCoeff, kZero_GrBlendCoeff), |
| 240 | /* dst-in */ COEFF_FORMULA( kZero_GrBlendCoeff, kSA_GrBlendCoeff), |
| 241 | /* src-out */ COEFF_FORMULA( kIDA_GrBlendCoeff, kZero_GrBlendCoeff), |
| 242 | /* dst-out */ COEFF_FORMULA( kZero_GrBlendCoeff, kISA_GrBlendCoeff), |
| 243 | /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 244 | /* dst-atop */ COEFF_FORMULA( kIDA_GrBlendCoeff, kSA_GrBlendCoeff), |
| 245 | /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 246 | /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff), |
| 247 | /* modulate */ COEFF_FORMULA( kZero_GrBlendCoeff, kSC_GrBlendCoeff), |
| 248 | /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff), |
| 249 | |
| 250 | }, /*>> Has coverage, input color unknown <<*/ { |
| 251 | |
| 252 | /* clear */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType), |
| 253 | /* src */ COVERAGE_DST_COEFF_ZERO_FORMULA(kOne_GrBlendCoeff), |
| 254 | /* dst */ NO_DST_WRITE_FORMULA, |
| 255 | /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff), |
| 256 | /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 257 | /* src-in */ COVERAGE_DST_COEFF_ZERO_FORMULA(kDA_GrBlendCoeff), |
| 258 | /* dst-in */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISAModulate_OutputType), |
| 259 | /* src-out */ COVERAGE_DST_COEFF_ZERO_FORMULA(kIDA_GrBlendCoeff), |
| 260 | /* dst-out */ COEFF_FORMULA( kZero_GrBlendCoeff, kISA_GrBlendCoeff), |
| 261 | /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 262 | /* dst-atop */ COVERAGE_FORMULA(BlendFormula::kISAModulate_OutputType, kIDA_GrBlendCoeff), |
| 263 | /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 264 | /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff), |
| 265 | /* modulate */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISCModulate_OutputType), |
| 266 | /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff), |
| 267 | |
| 268 | }}, /*>> No coverage, input color opaque <<*/ {{ |
| 269 | |
| 270 | /* clear */ DST_CLEAR_FORMULA, |
| 271 | /* src */ COEFF_FORMULA( kOne_GrBlendCoeff, kZero_GrBlendCoeff), |
| 272 | /* dst */ NO_DST_WRITE_FORMULA, |
| 273 | /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kZero_GrBlendCoeff), |
| 274 | /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 275 | /* src-in */ COEFF_FORMULA( kDA_GrBlendCoeff, kZero_GrBlendCoeff), |
| 276 | /* dst-in */ NO_DST_WRITE_FORMULA, |
| 277 | /* src-out */ COEFF_FORMULA( kIDA_GrBlendCoeff, kZero_GrBlendCoeff), |
| 278 | /* dst-out */ DST_CLEAR_FORMULA, |
| 279 | /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kZero_GrBlendCoeff), |
| 280 | /* dst-atop */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 281 | /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kZero_GrBlendCoeff), |
| 282 | /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff), |
| 283 | /* modulate */ COEFF_FORMULA( kZero_GrBlendCoeff, kSC_GrBlendCoeff), |
| 284 | /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff), |
| 285 | |
| 286 | }, /*>> Has coverage, input color opaque <<*/ { |
| 287 | |
| 288 | /* clear */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType), |
| 289 | /* src */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff), |
| 290 | /* dst */ NO_DST_WRITE_FORMULA, |
| 291 | /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff), |
| 292 | /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 293 | /* src-in */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 294 | /* dst-in */ NO_DST_WRITE_FORMULA, |
| 295 | /* src-out */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 296 | /* dst-out */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType), |
| 297 | /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 298 | /* dst-atop */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 299 | /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff), |
| 300 | /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff), |
| 301 | /* modulate */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISCModulate_OutputType), |
| 302 | /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff), |
| 303 | }}}; |
| 304 | |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 305 | static const BlendFormula gLCDBlendTable[(int)SkBlendMode::kLastCoeffMode + 1] = { |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 306 | /* clear */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType), |
| 307 | /* src */ COVERAGE_FORMULA(BlendFormula::kCoverage_OutputType, kOne_GrBlendCoeff), |
| 308 | /* dst */ NO_DST_WRITE_FORMULA, |
| 309 | /* src-over */ COVERAGE_FORMULA(BlendFormula::kSAModulate_OutputType, kOne_GrBlendCoeff), |
| 310 | /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff), |
| 311 | /* src-in */ COVERAGE_FORMULA(BlendFormula::kCoverage_OutputType, kDA_GrBlendCoeff), |
| 312 | /* dst-in */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISAModulate_OutputType), |
| 313 | /* src-out */ COVERAGE_FORMULA(BlendFormula::kCoverage_OutputType, kIDA_GrBlendCoeff), |
| 314 | /* dst-out */ COEFF_FORMULA_SA_MODULATE( kZero_GrBlendCoeff, kISC_GrBlendCoeff), |
| 315 | /* src-atop */ COVERAGE_FORMULA(BlendFormula::kSAModulate_OutputType, kDA_GrBlendCoeff), |
| 316 | /* dst-atop */ COVERAGE_FORMULA(BlendFormula::kISAModulate_OutputType, kIDA_GrBlendCoeff), |
| 317 | /* xor */ COVERAGE_FORMULA(BlendFormula::kSAModulate_OutputType, kIDA_GrBlendCoeff), |
| 318 | /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff), |
| 319 | /* modulate */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISCModulate_OutputType), |
| 320 | /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff), |
| 321 | }; |
| 322 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 323 | static BlendFormula get_blend_formula(const GrProcOptInfo& colorPOI, |
| 324 | const GrProcOptInfo& coveragePOI, |
| 325 | bool hasMixedSamples, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 326 | SkBlendMode xfermode) { |
| 327 | SkASSERT((unsigned)xfermode <= (unsigned)SkBlendMode::kLastCoeffMode); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 328 | SkASSERT(!coveragePOI.isFourChannelOutput()); |
| 329 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 330 | bool conflatesCoverage = !coveragePOI.isSolidWhite() || hasMixedSamples; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 331 | return gBlendTable[colorPOI.isOpaque()][conflatesCoverage][(int)xfermode]; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 332 | } |
| 333 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 334 | static BlendFormula get_lcd_blend_formula(const GrProcOptInfo& coveragePOI, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 335 | SkBlendMode xfermode) { |
| 336 | SkASSERT((unsigned)xfermode <= (unsigned)SkBlendMode::kLastCoeffMode); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 337 | SkASSERT(coveragePOI.isFourChannelOutput()); |
| 338 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 339 | return gLCDBlendTable[(int)xfermode]; |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 340 | } |
| 341 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 342 | /////////////////////////////////////////////////////////////////////////////// |
| 343 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 344 | class PorterDuffXferProcessor : public GrXferProcessor { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 345 | public: |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 346 | PorterDuffXferProcessor(BlendFormula blendFormula) : fBlendFormula(blendFormula) { |
| 347 | this->initClassID<PorterDuffXferProcessor>(); |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 348 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 349 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 350 | const char* name() const override { return "Porter Duff"; } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 351 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 352 | GrGLSLXferProcessor* createGLSLInstance() const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 353 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 354 | BlendFormula getBlendFormula() const { return fBlendFormula; } |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 355 | |
| 356 | private: |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 357 | GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&, |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 358 | bool doesStencilWrite, |
| 359 | GrColor* overrideColor, |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 360 | const GrCaps&) const override; |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 361 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 362 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 363 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 364 | bool onHasSecondaryOutput() const override { return fBlendFormula.hasSecondaryOutput(); } |
| 365 | |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 366 | void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 367 | blendInfo->fEquation = fBlendFormula.fBlendEquation; |
| 368 | blendInfo->fSrcBlend = fBlendFormula.fSrcCoeff; |
| 369 | blendInfo->fDstBlend = fBlendFormula.fDstCoeff; |
| 370 | blendInfo->fWriteColor = fBlendFormula.modifiesDst(); |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 371 | } |
| 372 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 373 | bool onIsEqual(const GrXferProcessor& xpBase) const override { |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 374 | const PorterDuffXferProcessor& xp = xpBase.cast<PorterDuffXferProcessor>(); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 375 | return fBlendFormula == xp.fBlendFormula; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 376 | } |
| 377 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 378 | const BlendFormula fBlendFormula; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 379 | |
| 380 | typedef GrXferProcessor INHERITED; |
| 381 | }; |
| 382 | |
| 383 | /////////////////////////////////////////////////////////////////////////////// |
| 384 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 385 | static void append_color_output(const PorterDuffXferProcessor& xp, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 386 | GrGLSLXPFragmentBuilder* fragBuilder, |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 387 | BlendFormula::OutputType outputType, const char* output, |
| 388 | const char* inColor, const char* inCoverage) { |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame^] | 389 | SkASSERT(inCoverage); |
| 390 | SkASSERT(inColor); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 391 | switch (outputType) { |
| 392 | case BlendFormula::kNone_OutputType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 393 | fragBuilder->codeAppendf("%s = vec4(0.0);", output); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 394 | break; |
| 395 | case BlendFormula::kCoverage_OutputType: |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 396 | // We can have a coverage formula while not reading coverage if there are mixed samples. |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame^] | 397 | fragBuilder->codeAppendf("%s = %s;", output, inCoverage); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 398 | break; |
| 399 | case BlendFormula::kModulate_OutputType: |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame^] | 400 | fragBuilder->codeAppendf("%s = %s * %s;", output, inColor, inCoverage); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 401 | break; |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 402 | case BlendFormula::kSAModulate_OutputType: |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame^] | 403 | fragBuilder->codeAppendf("%s = %s.a * %s;", output, inColor, inCoverage); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 404 | break; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 405 | case BlendFormula::kISAModulate_OutputType: |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame^] | 406 | fragBuilder->codeAppendf("%s = (1.0 - %s.a) * %s;", output, inColor, inCoverage); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 407 | break; |
| 408 | case BlendFormula::kISCModulate_OutputType: |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame^] | 409 | fragBuilder->codeAppendf("%s = (vec4(1.0) - %s) * %s;", output, inColor, inCoverage); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 410 | break; |
| 411 | default: |
| 412 | SkFAIL("Unsupported output type."); |
| 413 | break; |
egdaniel | 3ad6570 | 2015-02-17 11:15:47 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 417 | class GLPorterDuffXferProcessor : public GrGLSLXferProcessor { |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 418 | public: |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 419 | static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) { |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 420 | const PorterDuffXferProcessor& xp = processor.cast<PorterDuffXferProcessor>(); |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 421 | b->add32(xp.getBlendFormula().fPrimaryOutputType | |
| 422 | (xp.getBlendFormula().fSecondaryOutputType << 3)); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 423 | GR_STATIC_ASSERT(BlendFormula::kLast_OutputType < 8); |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 424 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 425 | |
| 426 | private: |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 427 | void emitOutputsForBlendState(const EmitArgs& args) override { |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 428 | const PorterDuffXferProcessor& xp = args.fXP.cast<PorterDuffXferProcessor>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 429 | GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 430 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 431 | BlendFormula blendFormula = xp.getBlendFormula(); |
| 432 | if (blendFormula.hasSecondaryOutput()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 433 | append_color_output(xp, fragBuilder, blendFormula.fSecondaryOutputType, |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 434 | args.fOutputSecondary, args.fInputColor, args.fInputCoverage); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 435 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 436 | append_color_output(xp, fragBuilder, blendFormula.fPrimaryOutputType, |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 437 | args.fOutputPrimary, args.fInputColor, args.fInputCoverage); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 438 | } |
| 439 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 440 | void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {} |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 441 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 442 | typedef GrGLSLXferProcessor INHERITED; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | /////////////////////////////////////////////////////////////////////////////// |
| 446 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 447 | void PorterDuffXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps&, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 448 | GrProcessorKeyBuilder* b) const { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 449 | GLPorterDuffXferProcessor::GenKey(*this, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 450 | } |
| 451 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 452 | GrGLSLXferProcessor* PorterDuffXferProcessor::createGLSLInstance() const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 453 | return new GLPorterDuffXferProcessor; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 456 | GrXferProcessor::OptFlags PorterDuffXferProcessor::onGetOptimizations( |
| 457 | const GrPipelineAnalysis& analysis, |
| 458 | bool doesStencilWrite, |
| 459 | GrColor* overrideColor, |
| 460 | const GrCaps& caps) const { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 461 | GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 462 | if (!fBlendFormula.modifiesDst()) { |
| 463 | if (!doesStencilWrite) { |
| 464 | optFlags |= GrXferProcessor::kSkipDraw_OptFlag; |
| 465 | } |
| 466 | optFlags |= (GrXferProcessor::kIgnoreColor_OptFlag | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 467 | GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag); |
| 468 | } else { |
| 469 | if (!fBlendFormula.usesInputColor()) { |
| 470 | optFlags |= GrXferProcessor::kIgnoreColor_OptFlag; |
| 471 | } |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 472 | if (analysis.fColorPOI.allStagesMultiplyInput() && |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 473 | fBlendFormula.canTweakAlphaForCoverage() && |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 474 | !analysis.fCoveragePOI.isFourChannelOutput()) { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 475 | optFlags |= GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag; |
| 476 | } |
| 477 | } |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 478 | return optFlags; |
| 479 | } |
| 480 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 481 | /////////////////////////////////////////////////////////////////////////////// |
| 482 | |
| 483 | class ShaderPDXferProcessor : public GrXferProcessor { |
| 484 | public: |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 485 | ShaderPDXferProcessor(const DstTexture* dstTexture, |
| 486 | bool hasMixedSamples, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 487 | SkBlendMode xfermode) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 488 | : INHERITED(dstTexture, true, hasMixedSamples) |
| 489 | , fXfermode(xfermode) { |
| 490 | this->initClassID<ShaderPDXferProcessor>(); |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 491 | } |
| 492 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 493 | const char* name() const override { return "Porter Duff Shader"; } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 494 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 495 | GrGLSLXferProcessor* createGLSLInstance() const override; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 496 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 497 | SkBlendMode getXfermode() const { return fXfermode; } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 498 | |
| 499 | private: |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 500 | GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&, bool, GrColor*, |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 501 | const GrCaps&) const override { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 502 | return kNone_OptFlags; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 505 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 506 | |
| 507 | bool onIsEqual(const GrXferProcessor& xpBase) const override { |
| 508 | const ShaderPDXferProcessor& xp = xpBase.cast<ShaderPDXferProcessor>(); |
| 509 | return fXfermode == xp.fXfermode; |
| 510 | } |
| 511 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 512 | const SkBlendMode fXfermode; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 513 | |
| 514 | typedef GrXferProcessor INHERITED; |
| 515 | }; |
| 516 | |
| 517 | /////////////////////////////////////////////////////////////////////////////// |
| 518 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 519 | class GLShaderPDXferProcessor : public GrGLSLXferProcessor { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 520 | public: |
| 521 | static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) { |
| 522 | const ShaderPDXferProcessor& xp = processor.cast<ShaderPDXferProcessor>(); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 523 | b->add32((int)xp.getXfermode()); |
egdaniel | 3ad6570 | 2015-02-17 11:15:47 -0800 | [diff] [blame] | 524 | } |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 525 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 526 | private: |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 527 | void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder, |
| 528 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 529 | const char* srcColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 530 | const char* srcCoverage, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 531 | const char* dstColor, |
| 532 | const char* outColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 533 | const char* outColorSecondary, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 534 | const GrXferProcessor& proc) override { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 535 | const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 536 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 537 | GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.getXfermode()); |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 538 | |
| 539 | // Apply coverage. |
robertphillips | f42fca4 | 2016-01-27 05:00:04 -0800 | [diff] [blame] | 540 | INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor, |
| 541 | outColorSecondary, xp); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 542 | } |
| 543 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 544 | void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {} |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 545 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 546 | typedef GrGLSLXferProcessor INHERITED; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 547 | }; |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 548 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 549 | /////////////////////////////////////////////////////////////////////////////// |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 550 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 551 | void ShaderPDXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps&, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 552 | GrProcessorKeyBuilder* b) const { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 553 | GLShaderPDXferProcessor::GenKey(*this, b); |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 554 | } |
| 555 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 556 | GrGLSLXferProcessor* ShaderPDXferProcessor::createGLSLInstance() const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 557 | return new GLShaderPDXferProcessor; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 558 | } |
| 559 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 560 | /////////////////////////////////////////////////////////////////////////////// |
| 561 | |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 562 | class PDLCDXferProcessor : public GrXferProcessor { |
| 563 | public: |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 564 | static GrXferProcessor* Create(SkBlendMode xfermode, const GrProcOptInfo& colorPOI); |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 565 | |
| 566 | ~PDLCDXferProcessor() override; |
| 567 | |
| 568 | const char* name() const override { return "Porter Duff LCD"; } |
| 569 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 570 | GrGLSLXferProcessor* createGLSLInstance() const override; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 571 | |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 572 | private: |
| 573 | PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha); |
| 574 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 575 | GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&, |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 576 | bool doesStencilWrite, |
| 577 | GrColor* overrideColor, |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 578 | const GrCaps&) const override; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 579 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 580 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 581 | |
| 582 | void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override { |
| 583 | blendInfo->fSrcBlend = kConstC_GrBlendCoeff; |
| 584 | blendInfo->fDstBlend = kISC_GrBlendCoeff; |
| 585 | blendInfo->fBlendConstant = fBlendConstant; |
| 586 | } |
| 587 | |
| 588 | bool onIsEqual(const GrXferProcessor& xpBase) const override { |
| 589 | const PDLCDXferProcessor& xp = xpBase.cast<PDLCDXferProcessor>(); |
| 590 | if (fBlendConstant != xp.fBlendConstant || |
| 591 | fAlpha != xp.fAlpha) { |
| 592 | return false; |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | GrColor fBlendConstant; |
| 598 | uint8_t fAlpha; |
| 599 | |
| 600 | typedef GrXferProcessor INHERITED; |
| 601 | }; |
| 602 | |
| 603 | /////////////////////////////////////////////////////////////////////////////// |
| 604 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 605 | class GLPDLCDXferProcessor : public GrGLSLXferProcessor { |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 606 | public: |
| 607 | GLPDLCDXferProcessor(const GrProcessor&) {} |
| 608 | |
| 609 | virtual ~GLPDLCDXferProcessor() {} |
| 610 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 611 | static void GenKey(const GrProcessor& processor, const GrShaderCaps& caps, |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 612 | GrProcessorKeyBuilder* b) {} |
| 613 | |
| 614 | private: |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 615 | void emitOutputsForBlendState(const EmitArgs& args) override { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 616 | GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 617 | SkASSERT(args.fInputCoverage); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 618 | fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputColor, |
| 619 | args.fInputCoverage); |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 622 | void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {} |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 623 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 624 | typedef GrGLSLXferProcessor INHERITED; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | /////////////////////////////////////////////////////////////////////////////// |
| 628 | |
| 629 | PDLCDXferProcessor::PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha) |
| 630 | : fBlendConstant(blendConstant) |
| 631 | , fAlpha(alpha) { |
| 632 | this->initClassID<PDLCDXferProcessor>(); |
| 633 | } |
| 634 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 635 | GrXferProcessor* PDLCDXferProcessor::Create(SkBlendMode xfermode, |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 636 | const GrProcOptInfo& colorPOI) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 637 | if (SkBlendMode::kSrcOver != xfermode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 638 | return nullptr; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | if (kRGBA_GrColorComponentFlags != colorPOI.validFlags()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 642 | return nullptr; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 643 | } |
| 644 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 645 | GrColor blendConstant = GrUnpremulColor(colorPOI.color()); |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 646 | uint8_t alpha = GrColorUnpackA(blendConstant); |
| 647 | blendConstant |= (0xff << GrColor_SHIFT_A); |
| 648 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 649 | return new PDLCDXferProcessor(blendConstant, alpha); |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | PDLCDXferProcessor::~PDLCDXferProcessor() { |
| 653 | } |
| 654 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 655 | void PDLCDXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 656 | GrProcessorKeyBuilder* b) const { |
egdaniel | cc25297 | 2015-05-12 12:03:28 -0700 | [diff] [blame] | 657 | GLPDLCDXferProcessor::GenKey(*this, caps, b); |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 658 | } |
| 659 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 660 | GrGLSLXferProcessor* PDLCDXferProcessor::createGLSLInstance() const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 661 | return new GLPDLCDXferProcessor(*this); |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 664 | GrXferProcessor::OptFlags PDLCDXferProcessor::onGetOptimizations(const GrPipelineAnalysis&, |
| 665 | bool doesStencilWrite, |
| 666 | GrColor* overrideColor, |
| 667 | const GrCaps& caps) const { |
| 668 | // We want to force our primary output to be alpha * Coverage, where alpha is the alpha |
| 669 | // value of the blend the constant. We should already have valid blend coeff's if we are at |
| 670 | // a point where we have RGB coverage. We don't need any color stages since the known color |
| 671 | // output is already baked into the blendConstant. |
| 672 | *overrideColor = GrColorPackRGBA(fAlpha, fAlpha, fAlpha, fAlpha); |
| 673 | return GrXferProcessor::kOverrideColor_OptFlag; |
egdaniel | 0d5fd11 | 2015-05-12 06:11:35 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /////////////////////////////////////////////////////////////////////////////// |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 677 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 678 | GrPorterDuffXPFactory::GrPorterDuffXPFactory(SkBlendMode xfermode) |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 679 | : fXfermode(xfermode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 680 | SkASSERT((unsigned)fXfermode <= (unsigned)SkBlendMode::kLastCoeffMode); |
egdaniel | f234272 | 2015-11-20 15:12:59 -0800 | [diff] [blame] | 681 | this->initClassID<GrPorterDuffXPFactory>(); |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 684 | sk_sp<GrXPFactory> GrPorterDuffXPFactory::Make(SkBlendMode xfermode) { |
| 685 | static GrPorterDuffXPFactory gClearPDXPF(SkBlendMode::kClear); |
| 686 | static GrPorterDuffXPFactory gSrcPDXPF(SkBlendMode::kSrc); |
| 687 | static GrPorterDuffXPFactory gDstPDXPF(SkBlendMode::kDst); |
| 688 | static GrPorterDuffXPFactory gSrcOverPDXPF(SkBlendMode::kSrcOver); |
| 689 | static GrPorterDuffXPFactory gDstOverPDXPF(SkBlendMode::kDstOver); |
| 690 | static GrPorterDuffXPFactory gSrcInPDXPF(SkBlendMode::kSrcIn); |
| 691 | static GrPorterDuffXPFactory gDstInPDXPF(SkBlendMode::kDstIn); |
| 692 | static GrPorterDuffXPFactory gSrcOutPDXPF(SkBlendMode::kSrcOut); |
| 693 | static GrPorterDuffXPFactory gDstOutPDXPF(SkBlendMode::kDstOut); |
| 694 | static GrPorterDuffXPFactory gSrcATopPDXPF(SkBlendMode::kSrcATop); |
| 695 | static GrPorterDuffXPFactory gDstATopPDXPF(SkBlendMode::kDstATop); |
| 696 | static GrPorterDuffXPFactory gXorPDXPF(SkBlendMode::kXor); |
| 697 | static GrPorterDuffXPFactory gPlusPDXPF(SkBlendMode::kPlus); |
| 698 | static GrPorterDuffXPFactory gModulatePDXPF(SkBlendMode::kModulate); |
| 699 | static GrPorterDuffXPFactory gScreenPDXPF(SkBlendMode::kScreen); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 700 | |
egdaniel | f234272 | 2015-11-20 15:12:59 -0800 | [diff] [blame] | 701 | static GrPorterDuffXPFactory* gFactories[] = { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 702 | &gClearPDXPF, &gSrcPDXPF, &gDstPDXPF, &gSrcOverPDXPF, &gDstOverPDXPF, &gSrcInPDXPF, |
| 703 | &gDstInPDXPF, &gSrcOutPDXPF, &gDstOutPDXPF, &gSrcATopPDXPF, &gDstATopPDXPF, &gXorPDXPF, |
| 704 | &gPlusPDXPF, &gModulatePDXPF, &gScreenPDXPF |
| 705 | }; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 706 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFactories) == (int)SkBlendMode::kLastCoeffMode + 1); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 707 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 708 | if ((int)xfermode < 0 || (int)xfermode > (int)SkBlendMode::kLastCoeffMode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 709 | return nullptr; |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 710 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 711 | return sk_sp<GrXPFactory>(SkRef(gFactories[(int)xfermode])); |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 714 | GrXferProcessor* GrPorterDuffXPFactory::onCreateXferProcessor(const GrCaps& caps, |
| 715 | const GrPipelineAnalysis& analysis, |
| 716 | bool hasMixedSamples, |
| 717 | const DstTexture* dstTexture) const { |
| 718 | if (analysis.fUsesPLSDstRead) { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 719 | return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fXfermode); |
| 720 | } |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 721 | BlendFormula blendFormula; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 722 | if (analysis.fCoveragePOI.isFourChannelOutput()) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 723 | if (SkBlendMode::kSrcOver == fXfermode && |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 724 | kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() && |
egdaniel | e73f1f6 | 2015-09-22 10:10:55 -0700 | [diff] [blame] | 725 | !caps.shaderCaps()->dualSourceBlendingSupport() && |
| 726 | !caps.shaderCaps()->dstReadInShaderSupport()) { |
| 727 | // If we don't have dual source blending or in shader dst reads, we fall back to this |
| 728 | // trick for rendering SrcOver LCD text instead of doing a dst copy. |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 729 | SkASSERT(!dstTexture || !dstTexture->texture()); |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 730 | return PDLCDXferProcessor::Create(fXfermode, analysis.fColorPOI); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 731 | } |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 732 | blendFormula = get_lcd_blend_formula(analysis.fCoveragePOI, fXfermode); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 733 | } else { |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 734 | blendFormula = get_blend_formula(analysis.fColorPOI, analysis.fCoveragePOI, hasMixedSamples, |
| 735 | fXfermode); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 736 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 737 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 738 | if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 739 | return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fXfermode); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | SkASSERT(!dstTexture || !dstTexture->texture()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 743 | return new PorterDuffXferProcessor(blendFormula); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 744 | } |
| 745 | |
egdaniel | f234272 | 2015-11-20 15:12:59 -0800 | [diff] [blame] | 746 | void GrPorterDuffXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI, |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 747 | InvariantBlendedColor* blendedColor) const { |
| 748 | // Find the blended color info based on the formula that does not have coverage. |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 749 | BlendFormula colorFormula = gBlendTable[colorPOI.isOpaque()][0][(int)fXfermode]; |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 750 | if (colorFormula.usesDstColor()) { |
| 751 | blendedColor->fWillBlendWithDst = true; |
| 752 | blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags; |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 753 | return; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 754 | } |
| 755 | |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 756 | blendedColor->fWillBlendWithDst = false; |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 757 | |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 758 | SkASSERT(kAdd_GrBlendEquation == colorFormula.fBlendEquation); |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 759 | |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 760 | switch (colorFormula.fSrcCoeff) { |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 761 | case kZero_GrBlendCoeff: |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 762 | blendedColor->fKnownColor = 0; |
| 763 | blendedColor->fKnownColorFlags = kRGBA_GrColorComponentFlags; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 764 | return; |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 765 | |
| 766 | case kOne_GrBlendCoeff: |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 767 | blendedColor->fKnownColor = colorPOI.color(); |
| 768 | blendedColor->fKnownColorFlags = colorPOI.validFlags(); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 769 | return; |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 770 | |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 771 | default: |
| 772 | blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags; |
| 773 | return; |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 774 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 775 | } |
| 776 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 777 | bool GrPorterDuffXPFactory::onWillReadDstColor(const GrCaps& caps, |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 778 | const GrPipelineAnalysis& analysis) const { |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 779 | if (caps.shaderCaps()->dualSourceBlendingSupport()) { |
| 780 | return false; |
bungeman | c33db93 | 2015-05-22 17:55:26 -0700 | [diff] [blame] | 781 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 782 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 783 | // When we have four channel coverage we always need to read the dst in order to correctly |
| 784 | // blend. The one exception is when we are using srcover mode and we know the input color into |
| 785 | // the XP. |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 786 | if (analysis.fCoveragePOI.isFourChannelOutput()) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 787 | if (SkBlendMode::kSrcOver == fXfermode && |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 788 | kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() && |
egdaniel | e73f1f6 | 2015-09-22 10:10:55 -0700 | [diff] [blame] | 789 | !caps.shaderCaps()->dstReadInShaderSupport()) { |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 790 | return false; |
| 791 | } |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 792 | return get_lcd_blend_formula(analysis.fCoveragePOI, fXfermode).hasSecondaryOutput(); |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 793 | } |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 794 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 795 | // We fallback on the shader XP when the blend formula would use dual source blending but we |
| 796 | // don't have support for it. |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 797 | static const bool kHasMixedSamples = false; |
| 798 | SkASSERT(!caps.usesMixedSamples()); // We never use mixed samples without dual source blending. |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 799 | auto formula = get_blend_formula(analysis.fColorPOI, analysis.fCoveragePOI, kHasMixedSamples, |
| 800 | fXfermode); |
| 801 | return formula.hasSecondaryOutput(); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 802 | } |
| 803 | |
egdaniel | f234272 | 2015-11-20 15:12:59 -0800 | [diff] [blame] | 804 | GR_DEFINE_XP_FACTORY_TEST(GrPorterDuffXPFactory); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 805 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 806 | sk_sp<GrXPFactory> GrPorterDuffXPFactory::TestCreate(GrProcessorTestData* d) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 807 | SkBlendMode mode = SkBlendMode(d->fRandom->nextULessThan((int)SkBlendMode::kLastCoeffMode)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 808 | return GrPorterDuffXPFactory::Make(mode); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 809 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 810 | |
egdaniel | f234272 | 2015-11-20 15:12:59 -0800 | [diff] [blame] | 811 | void GrPorterDuffXPFactory::TestGetXPOutputTypes(const GrXferProcessor* xp, |
| 812 | int* outPrimary, |
| 813 | int* outSecondary) { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 814 | if (!!strcmp(xp->name(), "Porter Duff")) { |
| 815 | *outPrimary = *outSecondary = -1; |
| 816 | return; |
| 817 | } |
| 818 | BlendFormula blendFormula = static_cast<const PorterDuffXferProcessor*>(xp)->getBlendFormula(); |
| 819 | *outPrimary = blendFormula.fPrimaryOutputType; |
| 820 | *outSecondary = blendFormula.fSecondaryOutputType; |
| 821 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 822 | |
| 823 | |
| 824 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 825 | // SrcOver Global functions |
| 826 | //////////////////////////////////////////////////////////////////////////////////////////////// |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 827 | const GrXferProcessor& GrPorterDuffXPFactory::SimpleSrcOverXP() { |
| 828 | static BlendFormula gSrcOverBlendFormula = COEFF_FORMULA(kOne_GrBlendCoeff, |
| 829 | kISA_GrBlendCoeff); |
| 830 | static PorterDuffXferProcessor gSrcOverXP(gSrcOverBlendFormula); |
| 831 | return gSrcOverXP; |
| 832 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 833 | |
| 834 | GrXferProcessor* GrPorterDuffXPFactory::CreateSrcOverXferProcessor( |
| 835 | const GrCaps& caps, |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 836 | const GrPipelineAnalysis& analysis, |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 837 | bool hasMixedSamples, |
| 838 | const GrXferProcessor::DstTexture* dstTexture) { |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 839 | if (analysis.fUsesPLSDstRead) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 840 | return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, SkBlendMode::kSrcOver); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 841 | } |
egdaniel | abe795e | 2016-08-18 15:21:55 -0700 | [diff] [blame] | 842 | |
| 843 | // We want to not make an xfer processor if possible. Thus for the simple case where we are not |
| 844 | // doing lcd blending we will just use our global SimpleSrcOverXP. This slightly differs from |
| 845 | // the general case where we convert a src-over blend that has solid coverage and an opaque |
| 846 | // color to src-mode, which allows disabling of blending. |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 847 | if (!analysis.fCoveragePOI.isFourChannelOutput()) { |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 848 | // We return nullptr here, which our caller interprets as meaning "use SimpleSrcOverXP". |
| 849 | // We don't simply return the address of that XP here because our caller would have to unref |
| 850 | // it and since it is a global object and GrProgramElement's ref-cnting system is not thread |
| 851 | // safe. |
| 852 | return nullptr; |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 855 | if (kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() && |
egdaniel | abe795e | 2016-08-18 15:21:55 -0700 | [diff] [blame] | 856 | !caps.shaderCaps()->dualSourceBlendingSupport() && |
| 857 | !caps.shaderCaps()->dstReadInShaderSupport()) { |
| 858 | // If we don't have dual source blending or in shader dst reads, we fall |
| 859 | // back to this trick for rendering SrcOver LCD text instead of doing a |
| 860 | // dst copy. |
| 861 | SkASSERT(!dstTexture || !dstTexture->texture()); |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 862 | return PDLCDXferProcessor::Create(SkBlendMode::kSrcOver, analysis.fColorPOI); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 863 | } |
| 864 | |
egdaniel | abe795e | 2016-08-18 15:21:55 -0700 | [diff] [blame] | 865 | BlendFormula blendFormula; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 866 | blendFormula = get_lcd_blend_formula(analysis.fCoveragePOI, SkBlendMode::kSrcOver); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 867 | if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 868 | return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, SkBlendMode::kSrcOver); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | SkASSERT(!dstTexture || !dstTexture->texture()); |
| 872 | return new PorterDuffXferProcessor(blendFormula); |
| 873 | } |
| 874 | |
| 875 | bool GrPorterDuffXPFactory::SrcOverWillNeedDstTexture(const GrCaps& caps, |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 876 | const GrPipelineAnalysis& analysis) { |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 877 | if (caps.shaderCaps()->dstReadInShaderSupport() || |
| 878 | caps.shaderCaps()->dualSourceBlendingSupport()) { |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | // When we have four channel coverage we always need to read the dst in order to correctly |
| 883 | // blend. The one exception is when we are using srcover mode and we know the input color |
| 884 | // into the XP. |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 885 | if (analysis.fCoveragePOI.isFourChannelOutput()) { |
| 886 | if (kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() && |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 887 | !caps.shaderCaps()->dstReadInShaderSupport()) { |
| 888 | return false; |
| 889 | } |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 890 | auto formula = get_lcd_blend_formula(analysis.fCoveragePOI, SkBlendMode::kSrcOver); |
| 891 | return formula.hasSecondaryOutput(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 892 | } |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 893 | |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 894 | // We fallback on the shader XP when the blend formula would use dual source blending but we |
| 895 | // don't have support for it. |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 896 | static const bool kHasMixedSamples = false; |
| 897 | SkASSERT(!caps.usesMixedSamples()); // We never use mixed samples without dual source blending. |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 898 | auto formula = get_blend_formula(analysis.fColorPOI, analysis.fCoveragePOI, kHasMixedSamples, |
| 899 | SkBlendMode::kSrcOver); |
| 900 | return formula.hasSecondaryOutput(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 901 | } |