blob: 88275c2bbed85ce370821e9b3a1c8257f3c2c872 [file] [log] [blame]
egdaniel378092f2014-12-03 10:40:13 -08001/*
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
egdanielced90102014-12-05 12:40:52 -08008#include "effects/GrPorterDuffXferProcessor.h"
egdaniel378092f2014-12-03 10:40:13 -08009
egdaniel95131432014-12-09 11:15:43 -080010#include "GrBlend.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
ethannicholasde4166a2015-11-30 08:57:38 -080012#include "GrPipeline.h"
egdaniel378092f2014-12-03 10:40:13 -080013#include "GrProcessor.h"
egdaniel87509242014-12-17 13:37:13 -080014#include "GrProcOptInfo.h"
egdaniel378092f2014-12-03 10:40:13 -080015#include "GrTypes.h"
16#include "GrXferProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080017#include "glsl/GrGLSLBlend.h"
egdaniel2d721d32015-11-11 13:06:05 -080018#include "glsl/GrGLSLFragmentShaderBuilder.h"
19#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070020#include "glsl/GrGLSLProgramDataManager.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080021#include "glsl/GrGLSLXferProcessor.h"
egdaniel378092f2014-12-03 10:40:13 -080022
cdalton6fd158e2015-05-27 15:08:33 -070023/**
24 * Wraps the shader outputs and HW blend state that comprise a Porter Duff blend mode with coverage.
25 */
26struct BlendFormula {
27public:
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.
egdaniel95131432014-12-09 11:15:43 -080031 */
cdalton6fd158e2015-05-27 15:08:33 -070032 enum OutputType {
33 kNone_OutputType, //<! 0
34 kCoverage_OutputType, //<! inputCoverage
35 kModulate_OutputType, //<! inputColor * inputCoverage
egdaniel723b0502015-09-15 09:31:40 -070036 kSAModulate_OutputType, //<! inputColor.a * inputCoverage
cdalton6fd158e2015-05-27 15:08:33 -070037 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>
bungeman761cf612015-08-28 07:09:20 -070074 struct get_properties : skstd::integral_constant<Properties, static_cast<Properties>(
cdalton6fd158e2015-05-27 15:08:33 -070075
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
121GR_STATIC_ASSERT(4 == sizeof(BlendFormula));
122
123GR_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/**
egdaniel723b0502015-09-15 09:31:40 -0700145 * 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/**
cdalton6fd158e2015-05-27 15:08:33 -0700154 * 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 */
229static 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
egdaniel723b0502015-09-15 09:31:40 -0700304static 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
cdalton86ae0a92015-06-08 15:11:04 -0700322static BlendFormula get_blend_formula(const GrProcOptInfo& colorPOI,
323 const GrProcOptInfo& coveragePOI,
324 bool hasMixedSamples,
325 SkXfermode::Mode xfermode) {
cdalton6fd158e2015-05-27 15:08:33 -0700326 SkASSERT(xfermode >= 0 && xfermode <= SkXfermode::kLastCoeffMode);
327 SkASSERT(!coveragePOI.isFourChannelOutput());
328
cdalton86ae0a92015-06-08 15:11:04 -0700329 bool conflatesCoverage = !coveragePOI.isSolidWhite() || hasMixedSamples;
330 return gBlendTable[colorPOI.isOpaque()][conflatesCoverage][xfermode];
egdaniel95131432014-12-09 11:15:43 -0800331}
332
egdaniel723b0502015-09-15 09:31:40 -0700333static 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
cdalton6fd158e2015-05-27 15:08:33 -0700341///////////////////////////////////////////////////////////////////////////////
342
egdaniel41d4f092015-02-09 07:51:00 -0800343class PorterDuffXferProcessor : public GrXferProcessor {
egdaniel378092f2014-12-03 10:40:13 -0800344public:
cdalton86ae0a92015-06-08 15:11:04 -0700345 PorterDuffXferProcessor(BlendFormula blendFormula) : fBlendFormula(blendFormula) {
346 this->initClassID<PorterDuffXferProcessor>();
egdaniel41d4f092015-02-09 07:51:00 -0800347 }
egdaniel378092f2014-12-03 10:40:13 -0800348
mtklein36352bf2015-03-25 18:17:31 -0700349 const char* name() const override { return "Porter Duff"; }
egdaniel41d4f092015-02-09 07:51:00 -0800350
egdaniel57d3b032015-11-13 11:57:27 -0800351 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -0800352
cdalton6fd158e2015-05-27 15:08:33 -0700353 BlendFormula getBlendFormula() const { return fBlendFormula; }
cdaltonf4f2b442015-04-23 09:40:23 -0700354
355private:
ethannicholasde4166a2015-11-30 08:57:38 -0800356 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdanielc19cdc22015-05-10 08:45:18 -0700357 bool doesStencilWrite,
358 GrColor* overrideColor,
bsalomon4b91f762015-05-19 09:29:46 -0700359 const GrCaps& caps) override;
egdanielc19cdc22015-05-10 08:45:18 -0700360
egdaniel57d3b032015-11-13 11:57:27 -0800361 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
cdaltonf4f2b442015-04-23 09:40:23 -0700362
cdaltonedbb31f2015-06-08 12:14:44 -0700363 bool onHasSecondaryOutput() const override { return fBlendFormula.hasSecondaryOutput(); }
364
cdaltonf4f2b442015-04-23 09:40:23 -0700365 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override {
cdalton6fd158e2015-05-27 15:08:33 -0700366 blendInfo->fEquation = fBlendFormula.fBlendEquation;
367 blendInfo->fSrcBlend = fBlendFormula.fSrcCoeff;
368 blendInfo->fDstBlend = fBlendFormula.fDstCoeff;
369 blendInfo->fWriteColor = fBlendFormula.modifiesDst();
egdaniel41d4f092015-02-09 07:51:00 -0800370 }
371
mtklein36352bf2015-03-25 18:17:31 -0700372 bool onIsEqual(const GrXferProcessor& xpBase) const override {
egdaniel41d4f092015-02-09 07:51:00 -0800373 const PorterDuffXferProcessor& xp = xpBase.cast<PorterDuffXferProcessor>();
cdalton6fd158e2015-05-27 15:08:33 -0700374 return fBlendFormula == xp.fBlendFormula;
egdaniel41d4f092015-02-09 07:51:00 -0800375 }
376
cdalton6fd158e2015-05-27 15:08:33 -0700377 const BlendFormula fBlendFormula;
egdaniel41d4f092015-02-09 07:51:00 -0800378
379 typedef GrXferProcessor INHERITED;
380};
381
382///////////////////////////////////////////////////////////////////////////////
383
egdaniel2d721d32015-11-11 13:06:05 -0800384static void append_color_output(const PorterDuffXferProcessor& xp,
egdaniel4ca2e602015-11-18 08:01:26 -0800385 GrGLSLXPFragmentBuilder* fragBuilder,
cdalton6fd158e2015-05-27 15:08:33 -0700386 BlendFormula::OutputType outputType, const char* output,
387 const char* inColor, const char* inCoverage) {
388 switch (outputType) {
389 case BlendFormula::kNone_OutputType:
egdaniel4ca2e602015-11-18 08:01:26 -0800390 fragBuilder->codeAppendf("%s = vec4(0.0);", output);
cdalton6fd158e2015-05-27 15:08:33 -0700391 break;
392 case BlendFormula::kCoverage_OutputType:
cdalton86ae0a92015-06-08 15:11:04 -0700393 // We can have a coverage formula while not reading coverage if there are mixed samples.
egdaniel4ca2e602015-11-18 08:01:26 -0800394 fragBuilder->codeAppendf("%s = %s;",
cdalton6fd158e2015-05-27 15:08:33 -0700395 output, xp.readsCoverage() ? inCoverage : "vec4(1.0)");
396 break;
397 case BlendFormula::kModulate_OutputType:
398 if (xp.readsCoverage()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800399 fragBuilder->codeAppendf("%s = %s * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700400 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800401 fragBuilder->codeAppendf("%s = %s;", output, inColor);
cdalton6fd158e2015-05-27 15:08:33 -0700402 }
403 break;
egdaniel723b0502015-09-15 09:31:40 -0700404 case BlendFormula::kSAModulate_OutputType:
405 if (xp.readsCoverage()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800406 fragBuilder->codeAppendf("%s = %s.a * %s;", output, inColor, inCoverage);
egdaniel723b0502015-09-15 09:31:40 -0700407 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800408 fragBuilder->codeAppendf("%s = %s;", output, inColor);
egdaniel723b0502015-09-15 09:31:40 -0700409 }
410 break;
cdalton6fd158e2015-05-27 15:08:33 -0700411 case BlendFormula::kISAModulate_OutputType:
412 if (xp.readsCoverage()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800413 fragBuilder->codeAppendf("%s = (1.0 - %s.a) * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700414 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800415 fragBuilder->codeAppendf("%s = vec4(1.0 - %s.a);", output, inColor);
cdalton6fd158e2015-05-27 15:08:33 -0700416 }
417 break;
418 case BlendFormula::kISCModulate_OutputType:
419 if (xp.readsCoverage()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800420 fragBuilder->codeAppendf("%s = (vec4(1.0) - %s) * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700421 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800422 fragBuilder->codeAppendf("%s = vec4(1.0) - %s;", output, inColor);
cdalton6fd158e2015-05-27 15:08:33 -0700423 }
424 break;
425 default:
426 SkFAIL("Unsupported output type.");
427 break;
egdaniel3ad65702015-02-17 11:15:47 -0800428 }
429}
430
egdanielfa4cc8b2015-11-13 08:34:52 -0800431class GLPorterDuffXferProcessor : public GrGLSLXferProcessor {
egdaniel41d4f092015-02-09 07:51:00 -0800432public:
cdalton6fd158e2015-05-27 15:08:33 -0700433 static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
egdaniel41d4f092015-02-09 07:51:00 -0800434 const PorterDuffXferProcessor& xp = processor.cast<PorterDuffXferProcessor>();
cdalton6fd158e2015-05-27 15:08:33 -0700435 b->add32(SkToInt(xp.readsCoverage()) |
436 (xp.getBlendFormula().fPrimaryOutputType << 1) |
437 (xp.getBlendFormula().fSecondaryOutputType << 4));
438 GR_STATIC_ASSERT(BlendFormula::kLast_OutputType < 8);
bsalomon50785a32015-02-06 07:02:37 -0800439 };
440
441private:
cdaltonedbb31f2015-06-08 12:14:44 -0700442 void emitOutputsForBlendState(const EmitArgs& args) override {
egdaniel41d4f092015-02-09 07:51:00 -0800443 const PorterDuffXferProcessor& xp = args.fXP.cast<PorterDuffXferProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800444 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
bungemanc33db932015-05-22 17:55:26 -0700445
cdalton6fd158e2015-05-27 15:08:33 -0700446 BlendFormula blendFormula = xp.getBlendFormula();
447 if (blendFormula.hasSecondaryOutput()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800448 append_color_output(xp, fragBuilder, blendFormula.fSecondaryOutputType,
cdalton6fd158e2015-05-27 15:08:33 -0700449 args.fOutputSecondary, args.fInputColor, args.fInputCoverage);
egdanielc2304142014-12-11 13:15:13 -0800450 }
egdaniel4ca2e602015-11-18 08:01:26 -0800451 append_color_output(xp, fragBuilder, blendFormula.fPrimaryOutputType,
cdalton6fd158e2015-05-27 15:08:33 -0700452 args.fOutputPrimary, args.fInputColor, args.fInputCoverage);
egdaniel378092f2014-12-03 10:40:13 -0800453 }
454
egdaniel018fb622015-10-28 07:26:40 -0700455 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel378092f2014-12-03 10:40:13 -0800456
egdanielfa4cc8b2015-11-13 08:34:52 -0800457 typedef GrGLSLXferProcessor INHERITED;
egdaniel378092f2014-12-03 10:40:13 -0800458};
459
460///////////////////////////////////////////////////////////////////////////////
461
egdaniel57d3b032015-11-13 11:57:27 -0800462void PorterDuffXferProcessor::onGetGLSLProcessorKey(const GrGLSLCaps&,
463 GrProcessorKeyBuilder* b) const {
cdalton6fd158e2015-05-27 15:08:33 -0700464 GLPorterDuffXferProcessor::GenKey(*this, b);
joshualitteb2a6762014-12-04 11:35:33 -0800465}
466
egdaniel57d3b032015-11-13 11:57:27 -0800467GrGLSLXferProcessor* PorterDuffXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700468 return new GLPorterDuffXferProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800469}
470
egdaniel95131432014-12-09 11:15:43 -0800471GrXferProcessor::OptFlags
ethannicholasde4166a2015-11-30 08:57:38 -0800472PorterDuffXferProcessor::onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdanielc19cdc22015-05-10 08:45:18 -0700473 bool doesStencilWrite,
474 GrColor* overrideColor,
bsalomon4b91f762015-05-19 09:29:46 -0700475 const GrCaps& caps) {
bsalomon7765a472015-07-08 11:26:37 -0700476 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700477 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 }
ethannicholasde4166a2015-11-30 08:57:38 -0800488 if (optimizations.fCoveragePOI.isSolidWhite()) {
cdalton6fd158e2015-05-27 15:08:33 -0700489 optFlags |= GrXferProcessor::kIgnoreCoverage_OptFlag;
490 }
ethannicholasde4166a2015-11-30 08:57:38 -0800491 if (optimizations.fColorPOI.allStagesMultiplyInput() &&
egdaniel723b0502015-09-15 09:31:40 -0700492 fBlendFormula.canTweakAlphaForCoverage() &&
ethannicholasde4166a2015-11-30 08:57:38 -0800493 !optimizations.fCoveragePOI.isFourChannelOutput()) {
cdalton6fd158e2015-05-27 15:08:33 -0700494 optFlags |= GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag;
495 }
496 }
bungemanc33db932015-05-22 17:55:26 -0700497 return optFlags;
498}
499
cdalton6fd158e2015-05-27 15:08:33 -0700500///////////////////////////////////////////////////////////////////////////////
501
502class ShaderPDXferProcessor : public GrXferProcessor {
503public:
cdalton86ae0a92015-06-08 15:11:04 -0700504 ShaderPDXferProcessor(const DstTexture* dstTexture,
505 bool hasMixedSamples,
506 SkXfermode::Mode xfermode)
507 : INHERITED(dstTexture, true, hasMixedSamples)
508 , fXfermode(xfermode) {
509 this->initClassID<ShaderPDXferProcessor>();
bungemanc33db932015-05-22 17:55:26 -0700510 }
511
cdalton6fd158e2015-05-27 15:08:33 -0700512 const char* name() const override { return "Porter Duff Shader"; }
cdalton6fd158e2015-05-27 15:08:33 -0700513
egdaniel57d3b032015-11-13 11:57:27 -0800514 GrGLSLXferProcessor* createGLSLInstance() const override;
cdalton6fd158e2015-05-27 15:08:33 -0700515
516 SkXfermode::Mode getXfermode() const { return fXfermode; }
517
518private:
ethannicholasde4166a2015-11-30 08:57:38 -0800519 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations&, bool, GrColor*,
520 const GrCaps&) override {
bsalomon7765a472015-07-08 11:26:37 -0700521 return kNone_OptFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700522 }
523
egdaniel57d3b032015-11-13 11:57:27 -0800524 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
cdalton6fd158e2015-05-27 15:08:33 -0700525
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
egdanielfa4cc8b2015-11-13 08:34:52 -0800538class GLShaderPDXferProcessor : public GrGLSLXferProcessor {
cdalton6fd158e2015-05-27 15:08:33 -0700539public:
540 static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
541 const ShaderPDXferProcessor& xp = processor.cast<ShaderPDXferProcessor>();
542 b->add32(xp.getXfermode());
egdaniel3ad65702015-02-17 11:15:47 -0800543 }
egdaniel87509242014-12-17 13:37:13 -0800544
cdalton6fd158e2015-05-27 15:08:33 -0700545private:
egdaniel4ca2e602015-11-18 08:01:26 -0800546 void emitBlendCodeForDstRead(GrGLSLXPBuilder* pb,
547 GrGLSLXPFragmentBuilder* fragBuilder,
548 const char* srcColor,
549 const char* dstColor,
550 const char* outColor,
551 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700552 const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>();
egdaniel95131432014-12-09 11:15:43 -0800553
egdaniel4ca2e602015-11-18 08:01:26 -0800554 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.getXfermode());
egdaniel95131432014-12-09 11:15:43 -0800555 }
556
egdaniel018fb622015-10-28 07:26:40 -0700557 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
bungemanc33db932015-05-22 17:55:26 -0700558
egdanielfa4cc8b2015-11-13 08:34:52 -0800559 typedef GrGLSLXferProcessor INHERITED;
cdalton6fd158e2015-05-27 15:08:33 -0700560};
bungemanc33db932015-05-22 17:55:26 -0700561
cdalton6fd158e2015-05-27 15:08:33 -0700562///////////////////////////////////////////////////////////////////////////////
bungemanc33db932015-05-22 17:55:26 -0700563
egdaniel57d3b032015-11-13 11:57:27 -0800564void ShaderPDXferProcessor::onGetGLSLProcessorKey(const GrGLSLCaps&,
565 GrProcessorKeyBuilder* b) const {
cdalton6fd158e2015-05-27 15:08:33 -0700566 GLShaderPDXferProcessor::GenKey(*this, b);
bungemanc33db932015-05-22 17:55:26 -0700567}
568
egdaniel57d3b032015-11-13 11:57:27 -0800569GrGLSLXferProcessor* ShaderPDXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700570 return new GLShaderPDXferProcessor;
egdanielc2304142014-12-11 13:15:13 -0800571}
572
egdaniel378092f2014-12-03 10:40:13 -0800573///////////////////////////////////////////////////////////////////////////////
574
egdaniel0d5fd112015-05-12 06:11:35 -0700575class PDLCDXferProcessor : public GrXferProcessor {
576public:
cdalton6fd158e2015-05-27 15:08:33 -0700577 static GrXferProcessor* Create(SkXfermode::Mode xfermode, const GrProcOptInfo& colorPOI);
egdaniel0d5fd112015-05-12 06:11:35 -0700578
579 ~PDLCDXferProcessor() override;
580
581 const char* name() const override { return "Porter Duff LCD"; }
582
egdaniel57d3b032015-11-13 11:57:27 -0800583 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700584
egdaniel0d5fd112015-05-12 06:11:35 -0700585private:
586 PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha);
587
ethannicholasde4166a2015-11-30 08:57:38 -0800588 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdaniel0d5fd112015-05-12 06:11:35 -0700589 bool doesStencilWrite,
590 GrColor* overrideColor,
bsalomon4b91f762015-05-19 09:29:46 -0700591 const GrCaps& caps) override;
egdaniel0d5fd112015-05-12 06:11:35 -0700592
egdaniel57d3b032015-11-13 11:57:27 -0800593 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700594
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
egdanielfa4cc8b2015-11-13 08:34:52 -0800618class GLPDLCDXferProcessor : public GrGLSLXferProcessor {
egdaniel0d5fd112015-05-12 06:11:35 -0700619public:
620 GLPDLCDXferProcessor(const GrProcessor&) {}
621
622 virtual ~GLPDLCDXferProcessor() {}
623
624 static void GenKey(const GrProcessor& processor, const GrGLSLCaps& caps,
625 GrProcessorKeyBuilder* b) {}
626
627private:
cdaltonedbb31f2015-06-08 12:14:44 -0700628 void emitOutputsForBlendState(const EmitArgs& args) override {
egdaniel4ca2e602015-11-18 08:01:26 -0800629 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
630 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputColor,
631 args.fInputCoverage);
egdaniel0d5fd112015-05-12 06:11:35 -0700632 }
633
egdaniel018fb622015-10-28 07:26:40 -0700634 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {};
egdaniel0d5fd112015-05-12 06:11:35 -0700635
egdanielfa4cc8b2015-11-13 08:34:52 -0800636 typedef GrGLSLXferProcessor INHERITED;
egdaniel0d5fd112015-05-12 06:11:35 -0700637};
638
639///////////////////////////////////////////////////////////////////////////////
640
641PDLCDXferProcessor::PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha)
642 : fBlendConstant(blendConstant)
643 , fAlpha(alpha) {
644 this->initClassID<PDLCDXferProcessor>();
645}
646
cdalton6fd158e2015-05-27 15:08:33 -0700647GrXferProcessor* PDLCDXferProcessor::Create(SkXfermode::Mode xfermode,
egdaniel0d5fd112015-05-12 06:11:35 -0700648 const GrProcOptInfo& colorPOI) {
cdalton6fd158e2015-05-27 15:08:33 -0700649 if (SkXfermode::kSrcOver_Mode != xfermode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700650 return nullptr;
egdaniel0d5fd112015-05-12 06:11:35 -0700651 }
652
653 if (kRGBA_GrColorComponentFlags != colorPOI.validFlags()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700654 return nullptr;
egdaniel0d5fd112015-05-12 06:11:35 -0700655 }
656
bsalomonae4738f2015-09-15 15:33:27 -0700657 GrColor blendConstant = GrUnpremulColor(colorPOI.color());
egdaniel0d5fd112015-05-12 06:11:35 -0700658 uint8_t alpha = GrColorUnpackA(blendConstant);
659 blendConstant |= (0xff << GrColor_SHIFT_A);
660
halcanary385fe4d2015-08-26 13:07:48 -0700661 return new PDLCDXferProcessor(blendConstant, alpha);
egdaniel0d5fd112015-05-12 06:11:35 -0700662}
663
664PDLCDXferProcessor::~PDLCDXferProcessor() {
665}
666
egdaniel57d3b032015-11-13 11:57:27 -0800667void PDLCDXferProcessor::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
668 GrProcessorKeyBuilder* b) const {
egdanielcc252972015-05-12 12:03:28 -0700669 GLPDLCDXferProcessor::GenKey(*this, caps, b);
egdaniel0d5fd112015-05-12 06:11:35 -0700670}
671
egdaniel57d3b032015-11-13 11:57:27 -0800672GrGLSLXferProcessor* PDLCDXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700673 return new GLPDLCDXferProcessor(*this);
egdaniel0d5fd112015-05-12 06:11:35 -0700674}
675
676GrXferProcessor::OptFlags
ethannicholasde4166a2015-11-30 08:57:38 -0800677PDLCDXferProcessor::onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdaniel0d5fd112015-05-12 06:11:35 -0700678 bool doesStencilWrite,
679 GrColor* overrideColor,
bsalomon4b91f762015-05-19 09:29:46 -0700680 const GrCaps& caps) {
egdaniel0d5fd112015-05-12 06:11:35 -0700681 // 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///////////////////////////////////////////////////////////////////////////////
cdalton6fd158e2015-05-27 15:08:33 -0700690
egdanielf2342722015-11-20 15:12:59 -0800691GrPorterDuffXPFactory::GrPorterDuffXPFactory(SkXfermode::Mode xfermode)
cdalton6fd158e2015-05-27 15:08:33 -0700692 : fXfermode(xfermode) {
cdalton1fa45722015-06-02 10:43:39 -0700693 SkASSERT(fXfermode <= SkXfermode::kLastCoeffMode);
egdanielf2342722015-11-20 15:12:59 -0800694 this->initClassID<GrPorterDuffXPFactory>();
egdaniel915187b2014-12-05 12:58:28 -0800695}
696
egdanielf2342722015-11-20 15:12:59 -0800697GrXPFactory* 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);
cdalton6fd158e2015-05-27 15:08:33 -0700713
egdanielf2342722015-11-20 15:12:59 -0800714 static GrPorterDuffXPFactory* gFactories[] = {
cdalton6fd158e2015-05-27 15:08:33 -0700715 &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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700722 return nullptr;
egdanielc016fb82014-12-03 11:41:54 -0800723 }
cdalton6fd158e2015-05-27 15:08:33 -0700724 return SkRef(gFactories[xfermode]);
egdanielc016fb82014-12-03 11:41:54 -0800725}
726
bsalomon50785a32015-02-06 07:02:37 -0800727GrXferProcessor*
egdanielf2342722015-11-20 15:12:59 -0800728GrPorterDuffXPFactory::onCreateXferProcessor(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800729 const GrPipelineOptimizations& optimizations,
cdalton86ae0a92015-06-08 15:11:04 -0700730 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700731 const DstTexture* dstTexture) const {
egdaniel723b0502015-09-15 09:31:40 -0700732 BlendFormula blendFormula;
ethannicholasde4166a2015-11-30 08:57:38 -0800733 if (optimizations.fCoveragePOI.isFourChannelOutput()) {
egdaniel723b0502015-09-15 09:31:40 -0700734 if (SkXfermode::kSrcOver_Mode == fXfermode &&
ethannicholasde4166a2015-11-30 08:57:38 -0800735 kRGBA_GrColorComponentFlags == optimizations.fColorPOI.validFlags() &&
egdaniele73f1f62015-09-22 10:10:55 -0700736 !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.
egdaniel723b0502015-09-15 09:31:40 -0700740 SkASSERT(!dstTexture || !dstTexture->texture());
ethannicholasde4166a2015-11-30 08:57:38 -0800741 return PDLCDXferProcessor::Create(fXfermode, optimizations.fColorPOI);
egdaniel723b0502015-09-15 09:31:40 -0700742 }
ethannicholasde4166a2015-11-30 08:57:38 -0800743 blendFormula = get_lcd_blend_formula(optimizations.fCoveragePOI, fXfermode);
egdaniel723b0502015-09-15 09:31:40 -0700744 } else {
ethannicholasde4166a2015-11-30 08:57:38 -0800745 blendFormula = get_blend_formula(optimizations.fColorPOI, optimizations.fCoveragePOI,
746 hasMixedSamples, fXfermode);
egdaniel95131432014-12-09 11:15:43 -0800747 }
cdalton6fd158e2015-05-27 15:08:33 -0700748
cdalton6fd158e2015-05-27 15:08:33 -0700749 if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700750 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fXfermode);
cdalton6fd158e2015-05-27 15:08:33 -0700751 }
752
753 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700754 return new PorterDuffXferProcessor(blendFormula);
egdaniel378092f2014-12-03 10:40:13 -0800755}
756
egdanielf2342722015-11-20 15:12:59 -0800757void GrPorterDuffXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
cdalton1fa45722015-06-02 10:43:39 -0700758 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;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800764 return;
egdaniel95131432014-12-09 11:15:43 -0800765 }
766
cdalton1fa45722015-06-02 10:43:39 -0700767 blendedColor->fWillBlendWithDst = false;
bungemanc33db932015-05-22 17:55:26 -0700768
cdalton1fa45722015-06-02 10:43:39 -0700769 SkASSERT(kAdd_GrBlendEquation == colorFormula.fBlendEquation);
bungemanc33db932015-05-22 17:55:26 -0700770
cdalton1fa45722015-06-02 10:43:39 -0700771 switch (colorFormula.fSrcCoeff) {
egdaniel9e4ecdc2014-12-18 12:44:55 -0800772 case kZero_GrBlendCoeff:
cdalton1fa45722015-06-02 10:43:39 -0700773 blendedColor->fKnownColor = 0;
774 blendedColor->fKnownColorFlags = kRGBA_GrColorComponentFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700775 return;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800776
777 case kOne_GrBlendCoeff:
cdalton1fa45722015-06-02 10:43:39 -0700778 blendedColor->fKnownColor = colorPOI.color();
779 blendedColor->fKnownColorFlags = colorPOI.validFlags();
cdalton6fd158e2015-05-27 15:08:33 -0700780 return;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800781
cdalton1fa45722015-06-02 10:43:39 -0700782 default:
783 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
784 return;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800785 }
egdaniel95131432014-12-09 11:15:43 -0800786}
787
egdanielf2342722015-11-20 15:12:59 -0800788bool GrPorterDuffXPFactory::willReadDstColor(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800789 const GrPipelineOptimizations& optimizations,
cdalton86ae0a92015-06-08 15:11:04 -0700790 bool hasMixedSamples) const {
791 if (caps.shaderCaps()->dualSourceBlendingSupport()) {
792 return false;
bungemanc33db932015-05-22 17:55:26 -0700793 }
egdaniel723b0502015-09-15 09:31:40 -0700794
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.
ethannicholasde4166a2015-11-30 08:57:38 -0800798 if (optimizations.fCoveragePOI.isFourChannelOutput()) {
egdaniel723b0502015-09-15 09:31:40 -0700799 if (SkXfermode::kSrcOver_Mode == fXfermode &&
ethannicholasde4166a2015-11-30 08:57:38 -0800800 kRGBA_GrColorComponentFlags == optimizations.fColorPOI.validFlags() &&
egdaniele73f1f62015-09-22 10:10:55 -0700801 !caps.shaderCaps()->dstReadInShaderSupport()) {
egdaniel723b0502015-09-15 09:31:40 -0700802 return false;
803 }
ethannicholasde4166a2015-11-30 08:57:38 -0800804 return get_lcd_blend_formula(optimizations.fCoveragePOI, fXfermode).hasSecondaryOutput();
cdalton86ae0a92015-06-08 15:11:04 -0700805 }
cdalton6fd158e2015-05-27 15:08:33 -0700806 // We fallback on the shader XP when the blend formula would use dual source blending but we
807 // don't have support for it.
ethannicholasde4166a2015-11-30 08:57:38 -0800808 return get_blend_formula(optimizations.fColorPOI, optimizations.fCoveragePOI, hasMixedSamples,
809 fXfermode).hasSecondaryOutput();
bsalomon50785a32015-02-06 07:02:37 -0800810}
811
egdanielf2342722015-11-20 15:12:59 -0800812GR_DEFINE_XP_FACTORY_TEST(GrPorterDuffXPFactory);
egdanielc2304142014-12-11 13:15:13 -0800813
egdanielf2342722015-11-20 15:12:59 -0800814const GrXPFactory* GrPorterDuffXPFactory::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700815 SkXfermode::Mode mode = SkXfermode::Mode(d->fRandom->nextULessThan(SkXfermode::kLastCoeffMode));
egdanielf2342722015-11-20 15:12:59 -0800816 return GrPorterDuffXPFactory::Create(mode);
egdanielc2304142014-12-11 13:15:13 -0800817}
egdaniel95131432014-12-09 11:15:43 -0800818
egdanielf2342722015-11-20 15:12:59 -0800819void GrPorterDuffXPFactory::TestGetXPOutputTypes(const GrXferProcessor* xp,
820 int* outPrimary,
821 int* outSecondary) {
cdalton6fd158e2015-05-27 15:08:33 -0700822 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}
egdanielc4b72722015-11-23 13:20:41 -0800830
831
832////////////////////////////////////////////////////////////////////////////////////////////////
833// SrcOver Global functions
834////////////////////////////////////////////////////////////////////////////////////////////////
835
836GrXferProcessor* GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
837 const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800838 const GrPipelineOptimizations& optimizations,
egdanielc4b72722015-11-23 13:20:41 -0800839 bool hasMixedSamples,
840 const GrXferProcessor::DstTexture* dstTexture) {
841 BlendFormula blendFormula;
ethannicholasde4166a2015-11-30 08:57:38 -0800842 if (optimizations.fCoveragePOI.isFourChannelOutput()) {
843 if (kRGBA_GrColorComponentFlags == optimizations.fColorPOI.validFlags() &&
egdanielc4b72722015-11-23 13:20:41 -0800844 !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());
ethannicholasde4166a2015-11-30 08:57:38 -0800850 return PDLCDXferProcessor::Create(SkXfermode::kSrcOver_Mode, optimizations.fColorPOI);
egdanielc4b72722015-11-23 13:20:41 -0800851 }
ethannicholasde4166a2015-11-30 08:57:38 -0800852 blendFormula = get_lcd_blend_formula(optimizations.fCoveragePOI, SkXfermode::kSrcOver_Mode);
egdanielc4b72722015-11-23 13:20:41 -0800853 } else {
ethannicholasde4166a2015-11-30 08:57:38 -0800854 blendFormula = get_blend_formula(optimizations.fColorPOI, optimizations.fCoveragePOI,
855 hasMixedSamples, SkXfermode::kSrcOver_Mode);
egdanielc4b72722015-11-23 13:20:41 -0800856 }
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
866bool GrPorterDuffXPFactory::SrcOverWillNeedDstTexture(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800867 const GrPipelineOptimizations& optimizations,
egdanielc4b72722015-11-23 13:20:41 -0800868 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.
ethannicholasde4166a2015-11-30 08:57:38 -0800877 if (optimizations.fCoveragePOI.isFourChannelOutput()) {
878 if (kRGBA_GrColorComponentFlags == optimizations.fColorPOI.validFlags() &&
egdanielc4b72722015-11-23 13:20:41 -0800879 !caps.shaderCaps()->dstReadInShaderSupport()) {
880 return false;
881 }
ethannicholasde4166a2015-11-30 08:57:38 -0800882 return get_lcd_blend_formula(optimizations.fCoveragePOI,
883 SkXfermode::kSrcOver_Mode).hasSecondaryOutput();
egdanielc4b72722015-11-23 13:20:41 -0800884 }
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.
ethannicholasde4166a2015-11-30 08:57:38 -0800887 return get_blend_formula(optimizations.fColorPOI, optimizations.fCoveragePOI,
egdanielc4b72722015-11-23 13:20:41 -0800888 hasMixedSamples, SkXfermode::kSrcOver_Mode).hasSecondaryOutput();
889}
890