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