blob: a813a532cd65c04fbaf59543c4dfec6066dead6f [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"
egdaniel018fb622015-10-28 07:26:40 -070019#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080020#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080021#include "glsl/GrGLSLXferProcessor.h"
bungeman221524d2016-01-05 14:59:40 -080022#include <utility>
egdaniel378092f2014-12-03 10:40:13 -080023
cdalton6fd158e2015-05-27 15:08:33 -070024/**
25 * Wraps the shader outputs and HW blend state that comprise a Porter Duff blend mode with coverage.
26 */
27struct BlendFormula {
28public:
29 /**
30 * Values the shader can write to primary and secondary outputs. These must all be modulated by
31 * coverage to support mixed samples. The XP will ignore the multiplies when not using coverage.
egdaniel95131432014-12-09 11:15:43 -080032 */
cdalton6fd158e2015-05-27 15:08:33 -070033 enum OutputType {
34 kNone_OutputType, //<! 0
35 kCoverage_OutputType, //<! inputCoverage
36 kModulate_OutputType, //<! inputColor * inputCoverage
egdaniel723b0502015-09-15 09:31:40 -070037 kSAModulate_OutputType, //<! inputColor.a * inputCoverage
cdalton6fd158e2015-05-27 15:08:33 -070038 kISAModulate_OutputType, //<! (1 - inputColor.a) * inputCoverage
39 kISCModulate_OutputType, //<! (1 - inputColor) * inputCoverage
40
41 kLast_OutputType = kISCModulate_OutputType
42 };
43
44 enum Properties {
45 kModifiesDst_Property = 1,
46 kUsesDstColor_Property = 1 << 1,
47 kUsesInputColor_Property = 1 << 2,
48 kCanTweakAlphaForCoverage_Property = 1 << 3,
49
50 kLast_Property = kCanTweakAlphaForCoverage_Property
51 };
52
53 BlendFormula& operator =(const BlendFormula& other) {
54 fData = other.fData;
55 return *this;
56 }
57
58 bool operator ==(const BlendFormula& other) const {
59 return fData == other.fData;
60 }
61
62 bool hasSecondaryOutput() const { return kNone_OutputType != fSecondaryOutputType; }
63 bool modifiesDst() const { return SkToBool(fProps & kModifiesDst_Property); }
64 bool usesDstColor() const { return SkToBool(fProps & kUsesDstColor_Property); }
65 bool usesInputColor() const { return SkToBool(fProps & kUsesInputColor_Property); }
66 bool canTweakAlphaForCoverage() const {
67 return SkToBool(fProps & kCanTweakAlphaForCoverage_Property);
68 }
69
70 /**
71 * Deduce the properties of a compile-time constant BlendFormula.
72 */
73 template<OutputType PrimaryOut, OutputType SecondaryOut,
74 GrBlendEquation BlendEquation, GrBlendCoeff SrcCoeff, GrBlendCoeff DstCoeff>
bungeman221524d2016-01-05 14:59:40 -080075 struct get_properties : std::integral_constant<Properties, static_cast<Properties>(
cdalton6fd158e2015-05-27 15:08:33 -070076
77 (GR_BLEND_MODIFIES_DST(BlendEquation, SrcCoeff, DstCoeff) ?
78 kModifiesDst_Property : 0) |
79
80 (GR_BLEND_COEFFS_USE_DST_COLOR(SrcCoeff, DstCoeff) ?
81 kUsesDstColor_Property : 0) |
82
83 ((PrimaryOut >= kModulate_OutputType && GR_BLEND_COEFFS_USE_SRC_COLOR(SrcCoeff,DstCoeff)) ||
84 (SecondaryOut >= kModulate_OutputType && GR_BLEND_COEFF_REFS_SRC2(DstCoeff)) ?
85 kUsesInputColor_Property : 0) | // We assert later that SrcCoeff doesn't ref src2.
86
87 (kModulate_OutputType == PrimaryOut &&
88 kNone_OutputType == SecondaryOut &&
89 GR_BLEND_CAN_TWEAK_ALPHA_FOR_COVERAGE(BlendEquation, SrcCoeff, DstCoeff) ?
90 kCanTweakAlphaForCoverage_Property : 0))> {
91
92 // The provided formula should already be optimized.
93 GR_STATIC_ASSERT((kNone_OutputType == PrimaryOut) ==
94 !GR_BLEND_COEFFS_USE_SRC_COLOR(SrcCoeff, DstCoeff));
95 GR_STATIC_ASSERT(!GR_BLEND_COEFF_REFS_SRC2(SrcCoeff));
96 GR_STATIC_ASSERT((kNone_OutputType == SecondaryOut) ==
97 !GR_BLEND_COEFF_REFS_SRC2(DstCoeff));
98 GR_STATIC_ASSERT(PrimaryOut != SecondaryOut || kNone_OutputType == PrimaryOut);
99 GR_STATIC_ASSERT(kNone_OutputType != PrimaryOut || kNone_OutputType == SecondaryOut);
100 };
101
102 union {
103 struct {
104 // We allot the enums one more bit than they require because MSVC seems to sign-extend
105 // them when the top bit is set. (This is in violation of the C++03 standard 9.6/4)
106 OutputType fPrimaryOutputType : 4;
107 OutputType fSecondaryOutputType : 4;
108 GrBlendEquation fBlendEquation : 6;
109 GrBlendCoeff fSrcCoeff : 6;
110 GrBlendCoeff fDstCoeff : 6;
111 Properties fProps : 32 - (4 + 4 + 6 + 6 + 6);
112 };
113 uint32_t fData;
114 };
115
116 GR_STATIC_ASSERT(kLast_OutputType < (1 << 3));
117 GR_STATIC_ASSERT(kLast_GrBlendEquation < (1 << 5));
118 GR_STATIC_ASSERT(kLast_GrBlendCoeff < (1 << 5));
119 GR_STATIC_ASSERT(kLast_Property < (1 << 6));
120};
121
122GR_STATIC_ASSERT(4 == sizeof(BlendFormula));
123
124GR_MAKE_BITFIELD_OPS(BlendFormula::Properties);
125
126/**
127 * Initialize a compile-time constant BlendFormula and automatically deduce fProps.
128 */
129#define INIT_BLEND_FORMULA(PRIMARY_OUT, SECONDARY_OUT, BLEND_EQUATION, SRC_COEFF, DST_COEFF) \
130 {{{PRIMARY_OUT, \
131 SECONDARY_OUT, \
132 BLEND_EQUATION, SRC_COEFF, DST_COEFF, \
133 BlendFormula::get_properties<PRIMARY_OUT, SECONDARY_OUT, \
134 BLEND_EQUATION, SRC_COEFF, DST_COEFF>::value}}}
135
136/**
137 * When there is no coverage, or the blend mode can tweak alpha for coverage, we use the standard
138 * Porter Duff formula.
139 */
140#define COEFF_FORMULA(SRC_COEFF, DST_COEFF) \
141 INIT_BLEND_FORMULA(BlendFormula::kModulate_OutputType, \
142 BlendFormula::kNone_OutputType, \
143 kAdd_GrBlendEquation, SRC_COEFF, DST_COEFF)
144
145/**
egdaniel723b0502015-09-15 09:31:40 -0700146 * Basic coeff formula similar to COEFF_FORMULA but we will make the src f*Sa. This is used in
147 * LCD dst-out.
148 */
149#define COEFF_FORMULA_SA_MODULATE(SRC_COEFF, DST_COEFF) \
150 INIT_BLEND_FORMULA(BlendFormula::kSAModulate_OutputType, \
151 BlendFormula::kNone_OutputType, \
152 kAdd_GrBlendEquation, SRC_COEFF, DST_COEFF)
153
154/**
cdalton6fd158e2015-05-27 15:08:33 -0700155 * When the coeffs are (Zero, Zero), we clear the dst. This formula has its own macro so we can set
156 * the primary output type to none.
157 */
158#define DST_CLEAR_FORMULA \
159 INIT_BLEND_FORMULA(BlendFormula::kNone_OutputType, \
160 BlendFormula::kNone_OutputType, \
161 kAdd_GrBlendEquation, kZero_GrBlendCoeff, kZero_GrBlendCoeff)
162
163/**
164 * When the coeffs are (Zero, One), we don't write to the dst at all. This formula has its own macro
165 * so we can set the primary output type to none.
166 */
167#define NO_DST_WRITE_FORMULA \
168 INIT_BLEND_FORMULA(BlendFormula::kNone_OutputType, \
169 BlendFormula::kNone_OutputType, \
170 kAdd_GrBlendEquation, kZero_GrBlendCoeff, kOne_GrBlendCoeff)
171
172/**
173 * When there is coverage, the equation with f=coverage is:
174 *
175 * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D
176 *
177 * This can be rewritten as:
178 *
179 * D' = f * S * srcCoeff + D * (1 - [f * (1 - dstCoeff)])
180 *
181 * To implement this formula, we output [f * (1 - dstCoeff)] for the secondary color and replace the
182 * HW dst coeff with IS2C.
183 *
184 * Xfer modes: dst-atop (Sa!=1)
185 */
186#define COVERAGE_FORMULA(ONE_MINUS_DST_COEFF_MODULATE_OUTPUT, SRC_COEFF) \
187 INIT_BLEND_FORMULA(BlendFormula::kModulate_OutputType, \
188 ONE_MINUS_DST_COEFF_MODULATE_OUTPUT, \
189 kAdd_GrBlendEquation, SRC_COEFF, kIS2C_GrBlendCoeff)
190
191/**
192 * When there is coverage and the src coeff is Zero, the equation with f=coverage becomes:
193 *
194 * D' = f * D * dstCoeff + (1-f) * D
195 *
196 * This can be rewritten as:
197 *
198 * D' = D - D * [f * (1 - dstCoeff)]
199 *
200 * To implement this formula, we output [f * (1 - dstCoeff)] for the primary color and use a reverse
201 * subtract HW blend equation with coeffs of (DC, One).
202 *
203 * Xfer modes: clear, dst-out (Sa=1), dst-in (Sa!=1), modulate (Sc!=1)
204 */
205#define COVERAGE_SRC_COEFF_ZERO_FORMULA(ONE_MINUS_DST_COEFF_MODULATE_OUTPUT) \
206 INIT_BLEND_FORMULA(ONE_MINUS_DST_COEFF_MODULATE_OUTPUT, \
207 BlendFormula::kNone_OutputType, \
208 kReverseSubtract_GrBlendEquation, kDC_GrBlendCoeff, kOne_GrBlendCoeff)
209
210/**
211 * When there is coverage and the dst coeff is Zero, the equation with f=coverage becomes:
212 *
213 * D' = f * S * srcCoeff + (1-f) * D
214 *
215 * To implement this formula, we output [f] for the secondary color and replace the HW dst coeff
216 * with IS2A. (Note that we can avoid dual source blending when Sa=1 by using ISA.)
217 *
218 * Xfer modes (Sa!=1): src, src-in, src-out
219 */
220#define COVERAGE_DST_COEFF_ZERO_FORMULA(SRC_COEFF) \
221 INIT_BLEND_FORMULA(BlendFormula::kModulate_OutputType, \
222 BlendFormula::kCoverage_OutputType, \
223 kAdd_GrBlendEquation, SRC_COEFF, kIS2A_GrBlendCoeff)
224
225/**
226 * This table outlines the blend formulas we will use with each xfermode, with and without coverage,
227 * with and without an opaque input color. Optimization properties are deduced at compile time so we
228 * can make runtime decisions quickly. RGB coverage is not supported.
229 */
Mike Reedd4706732016-11-15 16:44:34 -0500230static const BlendFormula gBlendTable[2][2][(int)SkBlendMode::kLastCoeffMode + 1] = {
cdalton6fd158e2015-05-27 15:08:33 -0700231
232 /*>> No coverage, input color unknown <<*/ {{
233
234 /* clear */ DST_CLEAR_FORMULA,
235 /* src */ COEFF_FORMULA( kOne_GrBlendCoeff, kZero_GrBlendCoeff),
236 /* dst */ NO_DST_WRITE_FORMULA,
237 /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff),
238 /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
239 /* src-in */ COEFF_FORMULA( kDA_GrBlendCoeff, kZero_GrBlendCoeff),
240 /* dst-in */ COEFF_FORMULA( kZero_GrBlendCoeff, kSA_GrBlendCoeff),
241 /* src-out */ COEFF_FORMULA( kIDA_GrBlendCoeff, kZero_GrBlendCoeff),
242 /* dst-out */ COEFF_FORMULA( kZero_GrBlendCoeff, kISA_GrBlendCoeff),
243 /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff),
244 /* dst-atop */ COEFF_FORMULA( kIDA_GrBlendCoeff, kSA_GrBlendCoeff),
245 /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff),
246 /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff),
247 /* modulate */ COEFF_FORMULA( kZero_GrBlendCoeff, kSC_GrBlendCoeff),
248 /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff),
249
250 }, /*>> Has coverage, input color unknown <<*/ {
251
252 /* clear */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType),
253 /* src */ COVERAGE_DST_COEFF_ZERO_FORMULA(kOne_GrBlendCoeff),
254 /* dst */ NO_DST_WRITE_FORMULA,
255 /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff),
256 /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
257 /* src-in */ COVERAGE_DST_COEFF_ZERO_FORMULA(kDA_GrBlendCoeff),
258 /* dst-in */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISAModulate_OutputType),
259 /* src-out */ COVERAGE_DST_COEFF_ZERO_FORMULA(kIDA_GrBlendCoeff),
260 /* dst-out */ COEFF_FORMULA( kZero_GrBlendCoeff, kISA_GrBlendCoeff),
261 /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff),
262 /* dst-atop */ COVERAGE_FORMULA(BlendFormula::kISAModulate_OutputType, kIDA_GrBlendCoeff),
263 /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff),
264 /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff),
265 /* modulate */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISCModulate_OutputType),
266 /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff),
267
268 }}, /*>> No coverage, input color opaque <<*/ {{
269
270 /* clear */ DST_CLEAR_FORMULA,
271 /* src */ COEFF_FORMULA( kOne_GrBlendCoeff, kZero_GrBlendCoeff),
272 /* dst */ NO_DST_WRITE_FORMULA,
273 /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kZero_GrBlendCoeff),
274 /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
275 /* src-in */ COEFF_FORMULA( kDA_GrBlendCoeff, kZero_GrBlendCoeff),
276 /* dst-in */ NO_DST_WRITE_FORMULA,
277 /* src-out */ COEFF_FORMULA( kIDA_GrBlendCoeff, kZero_GrBlendCoeff),
278 /* dst-out */ DST_CLEAR_FORMULA,
279 /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kZero_GrBlendCoeff),
280 /* dst-atop */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
281 /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kZero_GrBlendCoeff),
282 /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff),
283 /* modulate */ COEFF_FORMULA( kZero_GrBlendCoeff, kSC_GrBlendCoeff),
284 /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff),
285
286 }, /*>> Has coverage, input color opaque <<*/ {
287
288 /* clear */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType),
289 /* src */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff),
290 /* dst */ NO_DST_WRITE_FORMULA,
291 /* src-over */ COEFF_FORMULA( kOne_GrBlendCoeff, kISA_GrBlendCoeff),
292 /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
293 /* src-in */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff),
294 /* dst-in */ NO_DST_WRITE_FORMULA,
295 /* src-out */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff),
296 /* dst-out */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType),
297 /* src-atop */ COEFF_FORMULA( kDA_GrBlendCoeff, kISA_GrBlendCoeff),
298 /* dst-atop */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
299 /* xor */ COEFF_FORMULA( kIDA_GrBlendCoeff, kISA_GrBlendCoeff),
300 /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff),
301 /* modulate */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISCModulate_OutputType),
302 /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff),
303}}};
304
Mike Reedd4706732016-11-15 16:44:34 -0500305static const BlendFormula gLCDBlendTable[(int)SkBlendMode::kLastCoeffMode + 1] = {
egdaniel723b0502015-09-15 09:31:40 -0700306 /* clear */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kCoverage_OutputType),
307 /* src */ COVERAGE_FORMULA(BlendFormula::kCoverage_OutputType, kOne_GrBlendCoeff),
308 /* dst */ NO_DST_WRITE_FORMULA,
309 /* src-over */ COVERAGE_FORMULA(BlendFormula::kSAModulate_OutputType, kOne_GrBlendCoeff),
310 /* dst-over */ COEFF_FORMULA( kIDA_GrBlendCoeff, kOne_GrBlendCoeff),
311 /* src-in */ COVERAGE_FORMULA(BlendFormula::kCoverage_OutputType, kDA_GrBlendCoeff),
312 /* dst-in */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISAModulate_OutputType),
313 /* src-out */ COVERAGE_FORMULA(BlendFormula::kCoverage_OutputType, kIDA_GrBlendCoeff),
314 /* dst-out */ COEFF_FORMULA_SA_MODULATE( kZero_GrBlendCoeff, kISC_GrBlendCoeff),
315 /* src-atop */ COVERAGE_FORMULA(BlendFormula::kSAModulate_OutputType, kDA_GrBlendCoeff),
316 /* dst-atop */ COVERAGE_FORMULA(BlendFormula::kISAModulate_OutputType, kIDA_GrBlendCoeff),
317 /* xor */ COVERAGE_FORMULA(BlendFormula::kSAModulate_OutputType, kIDA_GrBlendCoeff),
318 /* plus */ COEFF_FORMULA( kOne_GrBlendCoeff, kOne_GrBlendCoeff),
319 /* modulate */ COVERAGE_SRC_COEFF_ZERO_FORMULA(BlendFormula::kISCModulate_OutputType),
320 /* screen */ COEFF_FORMULA( kOne_GrBlendCoeff, kISC_GrBlendCoeff),
321};
322
cdalton86ae0a92015-06-08 15:11:04 -0700323static BlendFormula get_blend_formula(const GrProcOptInfo& colorPOI,
324 const GrProcOptInfo& coveragePOI,
325 bool hasMixedSamples,
Mike Reed7d954ad2016-10-28 15:42:34 -0400326 SkBlendMode xfermode) {
327 SkASSERT((unsigned)xfermode <= (unsigned)SkBlendMode::kLastCoeffMode);
cdalton6fd158e2015-05-27 15:08:33 -0700328 SkASSERT(!coveragePOI.isFourChannelOutput());
329
cdalton86ae0a92015-06-08 15:11:04 -0700330 bool conflatesCoverage = !coveragePOI.isSolidWhite() || hasMixedSamples;
Mike Reed7d954ad2016-10-28 15:42:34 -0400331 return gBlendTable[colorPOI.isOpaque()][conflatesCoverage][(int)xfermode];
egdaniel95131432014-12-09 11:15:43 -0800332}
333
egdaniel723b0502015-09-15 09:31:40 -0700334static BlendFormula get_lcd_blend_formula(const GrProcOptInfo& coveragePOI,
Mike Reed7d954ad2016-10-28 15:42:34 -0400335 SkBlendMode xfermode) {
336 SkASSERT((unsigned)xfermode <= (unsigned)SkBlendMode::kLastCoeffMode);
egdaniel723b0502015-09-15 09:31:40 -0700337 SkASSERT(coveragePOI.isFourChannelOutput());
338
Mike Reed7d954ad2016-10-28 15:42:34 -0400339 return gLCDBlendTable[(int)xfermode];
egdaniel723b0502015-09-15 09:31:40 -0700340}
341
cdalton6fd158e2015-05-27 15:08:33 -0700342///////////////////////////////////////////////////////////////////////////////
343
egdaniel41d4f092015-02-09 07:51:00 -0800344class PorterDuffXferProcessor : public GrXferProcessor {
egdaniel378092f2014-12-03 10:40:13 -0800345public:
cdalton86ae0a92015-06-08 15:11:04 -0700346 PorterDuffXferProcessor(BlendFormula blendFormula) : fBlendFormula(blendFormula) {
347 this->initClassID<PorterDuffXferProcessor>();
egdaniel41d4f092015-02-09 07:51:00 -0800348 }
egdaniel378092f2014-12-03 10:40:13 -0800349
mtklein36352bf2015-03-25 18:17:31 -0700350 const char* name() const override { return "Porter Duff"; }
egdaniel41d4f092015-02-09 07:51:00 -0800351
egdaniel57d3b032015-11-13 11:57:27 -0800352 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -0800353
cdalton6fd158e2015-05-27 15:08:33 -0700354 BlendFormula getBlendFormula() const { return fBlendFormula; }
cdaltonf4f2b442015-04-23 09:40:23 -0700355
356private:
Brian Salomon92aee3d2016-12-21 09:20:25 -0500357 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&,
egdanielc19cdc22015-05-10 08:45:18 -0700358 bool doesStencilWrite,
359 GrColor* overrideColor,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500360 const GrCaps&) const override;
egdanielc19cdc22015-05-10 08:45:18 -0700361
Brian Salomon94efbf52016-11-29 13:43:05 -0500362 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
cdaltonf4f2b442015-04-23 09:40:23 -0700363
cdaltonedbb31f2015-06-08 12:14:44 -0700364 bool onHasSecondaryOutput() const override { return fBlendFormula.hasSecondaryOutput(); }
365
cdaltonf4f2b442015-04-23 09:40:23 -0700366 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override {
cdalton6fd158e2015-05-27 15:08:33 -0700367 blendInfo->fEquation = fBlendFormula.fBlendEquation;
368 blendInfo->fSrcBlend = fBlendFormula.fSrcCoeff;
369 blendInfo->fDstBlend = fBlendFormula.fDstCoeff;
370 blendInfo->fWriteColor = fBlendFormula.modifiesDst();
egdaniel41d4f092015-02-09 07:51:00 -0800371 }
372
mtklein36352bf2015-03-25 18:17:31 -0700373 bool onIsEqual(const GrXferProcessor& xpBase) const override {
egdaniel41d4f092015-02-09 07:51:00 -0800374 const PorterDuffXferProcessor& xp = xpBase.cast<PorterDuffXferProcessor>();
cdalton6fd158e2015-05-27 15:08:33 -0700375 return fBlendFormula == xp.fBlendFormula;
egdaniel41d4f092015-02-09 07:51:00 -0800376 }
377
cdalton6fd158e2015-05-27 15:08:33 -0700378 const BlendFormula fBlendFormula;
egdaniel41d4f092015-02-09 07:51:00 -0800379
380 typedef GrXferProcessor INHERITED;
381};
382
383///////////////////////////////////////////////////////////////////////////////
384
egdaniel2d721d32015-11-11 13:06:05 -0800385static void append_color_output(const PorterDuffXferProcessor& xp,
egdaniel4ca2e602015-11-18 08:01:26 -0800386 GrGLSLXPFragmentBuilder* fragBuilder,
cdalton6fd158e2015-05-27 15:08:33 -0700387 BlendFormula::OutputType outputType, const char* output,
388 const char* inColor, const char* inCoverage) {
Brian Salomon8c852be2017-01-04 10:44:42 -0500389 SkASSERT(inCoverage);
390 SkASSERT(inColor);
cdalton6fd158e2015-05-27 15:08:33 -0700391 switch (outputType) {
392 case BlendFormula::kNone_OutputType:
egdaniel4ca2e602015-11-18 08:01:26 -0800393 fragBuilder->codeAppendf("%s = vec4(0.0);", output);
cdalton6fd158e2015-05-27 15:08:33 -0700394 break;
395 case BlendFormula::kCoverage_OutputType:
cdalton86ae0a92015-06-08 15:11:04 -0700396 // We can have a coverage formula while not reading coverage if there are mixed samples.
Brian Salomon8c852be2017-01-04 10:44:42 -0500397 fragBuilder->codeAppendf("%s = %s;", output, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700398 break;
399 case BlendFormula::kModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500400 fragBuilder->codeAppendf("%s = %s * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700401 break;
egdaniel723b0502015-09-15 09:31:40 -0700402 case BlendFormula::kSAModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500403 fragBuilder->codeAppendf("%s = %s.a * %s;", output, inColor, inCoverage);
egdaniel723b0502015-09-15 09:31:40 -0700404 break;
cdalton6fd158e2015-05-27 15:08:33 -0700405 case BlendFormula::kISAModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500406 fragBuilder->codeAppendf("%s = (1.0 - %s.a) * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700407 break;
408 case BlendFormula::kISCModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500409 fragBuilder->codeAppendf("%s = (vec4(1.0) - %s) * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700410 break;
411 default:
412 SkFAIL("Unsupported output type.");
413 break;
egdaniel3ad65702015-02-17 11:15:47 -0800414 }
415}
416
egdanielfa4cc8b2015-11-13 08:34:52 -0800417class GLPorterDuffXferProcessor : public GrGLSLXferProcessor {
egdaniel41d4f092015-02-09 07:51:00 -0800418public:
cdalton6fd158e2015-05-27 15:08:33 -0700419 static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
egdaniel41d4f092015-02-09 07:51:00 -0800420 const PorterDuffXferProcessor& xp = processor.cast<PorterDuffXferProcessor>();
egdaniel56cf6dc2015-11-30 10:15:58 -0800421 b->add32(xp.getBlendFormula().fPrimaryOutputType |
422 (xp.getBlendFormula().fSecondaryOutputType << 3));
cdalton6fd158e2015-05-27 15:08:33 -0700423 GR_STATIC_ASSERT(BlendFormula::kLast_OutputType < 8);
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400424 }
bsalomon50785a32015-02-06 07:02:37 -0800425
426private:
cdaltonedbb31f2015-06-08 12:14:44 -0700427 void emitOutputsForBlendState(const EmitArgs& args) override {
egdaniel41d4f092015-02-09 07:51:00 -0800428 const PorterDuffXferProcessor& xp = args.fXP.cast<PorterDuffXferProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800429 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
bungemanc33db932015-05-22 17:55:26 -0700430
cdalton6fd158e2015-05-27 15:08:33 -0700431 BlendFormula blendFormula = xp.getBlendFormula();
432 if (blendFormula.hasSecondaryOutput()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800433 append_color_output(xp, fragBuilder, blendFormula.fSecondaryOutputType,
cdalton6fd158e2015-05-27 15:08:33 -0700434 args.fOutputSecondary, args.fInputColor, args.fInputCoverage);
egdanielc2304142014-12-11 13:15:13 -0800435 }
egdaniel4ca2e602015-11-18 08:01:26 -0800436 append_color_output(xp, fragBuilder, blendFormula.fPrimaryOutputType,
cdalton6fd158e2015-05-27 15:08:33 -0700437 args.fOutputPrimary, args.fInputColor, args.fInputCoverage);
egdaniel378092f2014-12-03 10:40:13 -0800438 }
439
egdaniel018fb622015-10-28 07:26:40 -0700440 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel378092f2014-12-03 10:40:13 -0800441
egdanielfa4cc8b2015-11-13 08:34:52 -0800442 typedef GrGLSLXferProcessor INHERITED;
egdaniel378092f2014-12-03 10:40:13 -0800443};
444
445///////////////////////////////////////////////////////////////////////////////
446
Brian Salomon94efbf52016-11-29 13:43:05 -0500447void PorterDuffXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps&,
egdaniel57d3b032015-11-13 11:57:27 -0800448 GrProcessorKeyBuilder* b) const {
cdalton6fd158e2015-05-27 15:08:33 -0700449 GLPorterDuffXferProcessor::GenKey(*this, b);
joshualitteb2a6762014-12-04 11:35:33 -0800450}
451
egdaniel57d3b032015-11-13 11:57:27 -0800452GrGLSLXferProcessor* PorterDuffXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700453 return new GLPorterDuffXferProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800454}
455
Brian Salomon92aee3d2016-12-21 09:20:25 -0500456GrXferProcessor::OptFlags PorterDuffXferProcessor::onGetOptimizations(
457 const GrPipelineAnalysis& analysis,
458 bool doesStencilWrite,
459 GrColor* overrideColor,
460 const GrCaps& caps) const {
bsalomon7765a472015-07-08 11:26:37 -0700461 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700462 if (!fBlendFormula.modifiesDst()) {
463 if (!doesStencilWrite) {
464 optFlags |= GrXferProcessor::kSkipDraw_OptFlag;
465 }
466 optFlags |= (GrXferProcessor::kIgnoreColor_OptFlag |
cdalton6fd158e2015-05-27 15:08:33 -0700467 GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag);
468 } else {
469 if (!fBlendFormula.usesInputColor()) {
470 optFlags |= GrXferProcessor::kIgnoreColor_OptFlag;
471 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500472 if (analysis.fColorPOI.allStagesMultiplyInput() &&
egdaniel723b0502015-09-15 09:31:40 -0700473 fBlendFormula.canTweakAlphaForCoverage() &&
Brian Salomon92aee3d2016-12-21 09:20:25 -0500474 !analysis.fCoveragePOI.isFourChannelOutput()) {
cdalton6fd158e2015-05-27 15:08:33 -0700475 optFlags |= GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag;
476 }
477 }
bungemanc33db932015-05-22 17:55:26 -0700478 return optFlags;
479}
480
cdalton6fd158e2015-05-27 15:08:33 -0700481///////////////////////////////////////////////////////////////////////////////
482
483class ShaderPDXferProcessor : public GrXferProcessor {
484public:
cdalton86ae0a92015-06-08 15:11:04 -0700485 ShaderPDXferProcessor(const DstTexture* dstTexture,
486 bool hasMixedSamples,
Mike Reed7d954ad2016-10-28 15:42:34 -0400487 SkBlendMode xfermode)
cdalton86ae0a92015-06-08 15:11:04 -0700488 : INHERITED(dstTexture, true, hasMixedSamples)
489 , fXfermode(xfermode) {
490 this->initClassID<ShaderPDXferProcessor>();
bungemanc33db932015-05-22 17:55:26 -0700491 }
492
cdalton6fd158e2015-05-27 15:08:33 -0700493 const char* name() const override { return "Porter Duff Shader"; }
cdalton6fd158e2015-05-27 15:08:33 -0700494
egdaniel57d3b032015-11-13 11:57:27 -0800495 GrGLSLXferProcessor* createGLSLInstance() const override;
cdalton6fd158e2015-05-27 15:08:33 -0700496
Mike Reed7d954ad2016-10-28 15:42:34 -0400497 SkBlendMode getXfermode() const { return fXfermode; }
cdalton6fd158e2015-05-27 15:08:33 -0700498
499private:
Brian Salomon92aee3d2016-12-21 09:20:25 -0500500 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&, bool, GrColor*,
egdaniel56cf6dc2015-11-30 10:15:58 -0800501 const GrCaps&) const override {
bsalomon7765a472015-07-08 11:26:37 -0700502 return kNone_OptFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700503 }
504
Brian Salomon94efbf52016-11-29 13:43:05 -0500505 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
cdalton6fd158e2015-05-27 15:08:33 -0700506
507 bool onIsEqual(const GrXferProcessor& xpBase) const override {
508 const ShaderPDXferProcessor& xp = xpBase.cast<ShaderPDXferProcessor>();
509 return fXfermode == xp.fXfermode;
510 }
511
Mike Reed7d954ad2016-10-28 15:42:34 -0400512 const SkBlendMode fXfermode;
cdalton6fd158e2015-05-27 15:08:33 -0700513
514 typedef GrXferProcessor INHERITED;
515};
516
517///////////////////////////////////////////////////////////////////////////////
518
egdanielfa4cc8b2015-11-13 08:34:52 -0800519class GLShaderPDXferProcessor : public GrGLSLXferProcessor {
cdalton6fd158e2015-05-27 15:08:33 -0700520public:
521 static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
522 const ShaderPDXferProcessor& xp = processor.cast<ShaderPDXferProcessor>();
Mike Reed7d954ad2016-10-28 15:42:34 -0400523 b->add32((int)xp.getXfermode());
egdaniel3ad65702015-02-17 11:15:47 -0800524 }
egdaniel87509242014-12-17 13:37:13 -0800525
cdalton6fd158e2015-05-27 15:08:33 -0700526private:
egdaniel7ea439b2015-12-03 09:20:44 -0800527 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
528 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800529 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800530 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800531 const char* dstColor,
532 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800533 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800534 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700535 const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>();
egdaniel95131432014-12-09 11:15:43 -0800536
egdaniel4ca2e602015-11-18 08:01:26 -0800537 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.getXfermode());
egdanielf34b2932015-12-01 13:54:06 -0800538
539 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800540 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
541 outColorSecondary, xp);
egdaniel95131432014-12-09 11:15:43 -0800542 }
543
egdaniel018fb622015-10-28 07:26:40 -0700544 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
bungemanc33db932015-05-22 17:55:26 -0700545
egdanielfa4cc8b2015-11-13 08:34:52 -0800546 typedef GrGLSLXferProcessor INHERITED;
cdalton6fd158e2015-05-27 15:08:33 -0700547};
bungemanc33db932015-05-22 17:55:26 -0700548
cdalton6fd158e2015-05-27 15:08:33 -0700549///////////////////////////////////////////////////////////////////////////////
bungemanc33db932015-05-22 17:55:26 -0700550
Brian Salomon94efbf52016-11-29 13:43:05 -0500551void ShaderPDXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps&,
egdaniel57d3b032015-11-13 11:57:27 -0800552 GrProcessorKeyBuilder* b) const {
cdalton6fd158e2015-05-27 15:08:33 -0700553 GLShaderPDXferProcessor::GenKey(*this, b);
bungemanc33db932015-05-22 17:55:26 -0700554}
555
egdaniel57d3b032015-11-13 11:57:27 -0800556GrGLSLXferProcessor* ShaderPDXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700557 return new GLShaderPDXferProcessor;
egdanielc2304142014-12-11 13:15:13 -0800558}
559
egdaniel378092f2014-12-03 10:40:13 -0800560///////////////////////////////////////////////////////////////////////////////
561
egdaniel0d5fd112015-05-12 06:11:35 -0700562class PDLCDXferProcessor : public GrXferProcessor {
563public:
Mike Reed7d954ad2016-10-28 15:42:34 -0400564 static GrXferProcessor* Create(SkBlendMode xfermode, const GrProcOptInfo& colorPOI);
egdaniel0d5fd112015-05-12 06:11:35 -0700565
566 ~PDLCDXferProcessor() override;
567
568 const char* name() const override { return "Porter Duff LCD"; }
569
egdaniel57d3b032015-11-13 11:57:27 -0800570 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700571
egdaniel0d5fd112015-05-12 06:11:35 -0700572private:
573 PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha);
574
Brian Salomon92aee3d2016-12-21 09:20:25 -0500575 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&,
egdaniel0d5fd112015-05-12 06:11:35 -0700576 bool doesStencilWrite,
577 GrColor* overrideColor,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500578 const GrCaps&) const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700579
Brian Salomon94efbf52016-11-29 13:43:05 -0500580 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700581
582 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override {
583 blendInfo->fSrcBlend = kConstC_GrBlendCoeff;
584 blendInfo->fDstBlend = kISC_GrBlendCoeff;
585 blendInfo->fBlendConstant = fBlendConstant;
586 }
587
588 bool onIsEqual(const GrXferProcessor& xpBase) const override {
589 const PDLCDXferProcessor& xp = xpBase.cast<PDLCDXferProcessor>();
590 if (fBlendConstant != xp.fBlendConstant ||
591 fAlpha != xp.fAlpha) {
592 return false;
593 }
594 return true;
595 }
596
597 GrColor fBlendConstant;
598 uint8_t fAlpha;
599
600 typedef GrXferProcessor INHERITED;
601};
602
603///////////////////////////////////////////////////////////////////////////////
604
egdanielfa4cc8b2015-11-13 08:34:52 -0800605class GLPDLCDXferProcessor : public GrGLSLXferProcessor {
egdaniel0d5fd112015-05-12 06:11:35 -0700606public:
607 GLPDLCDXferProcessor(const GrProcessor&) {}
608
609 virtual ~GLPDLCDXferProcessor() {}
610
Brian Salomon94efbf52016-11-29 13:43:05 -0500611 static void GenKey(const GrProcessor& processor, const GrShaderCaps& caps,
egdaniel0d5fd112015-05-12 06:11:35 -0700612 GrProcessorKeyBuilder* b) {}
613
614private:
cdaltonedbb31f2015-06-08 12:14:44 -0700615 void emitOutputsForBlendState(const EmitArgs& args) override {
egdaniel4ca2e602015-11-18 08:01:26 -0800616 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
egdaniel56cf6dc2015-11-30 10:15:58 -0800617 SkASSERT(args.fInputCoverage);
egdaniel4ca2e602015-11-18 08:01:26 -0800618 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputColor,
619 args.fInputCoverage);
egdaniel0d5fd112015-05-12 06:11:35 -0700620 }
621
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400622 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel0d5fd112015-05-12 06:11:35 -0700623
egdanielfa4cc8b2015-11-13 08:34:52 -0800624 typedef GrGLSLXferProcessor INHERITED;
egdaniel0d5fd112015-05-12 06:11:35 -0700625};
626
627///////////////////////////////////////////////////////////////////////////////
628
629PDLCDXferProcessor::PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha)
630 : fBlendConstant(blendConstant)
631 , fAlpha(alpha) {
632 this->initClassID<PDLCDXferProcessor>();
633}
634
Mike Reed7d954ad2016-10-28 15:42:34 -0400635GrXferProcessor* PDLCDXferProcessor::Create(SkBlendMode xfermode,
egdaniel0d5fd112015-05-12 06:11:35 -0700636 const GrProcOptInfo& colorPOI) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400637 if (SkBlendMode::kSrcOver != xfermode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700638 return nullptr;
egdaniel0d5fd112015-05-12 06:11:35 -0700639 }
640
641 if (kRGBA_GrColorComponentFlags != colorPOI.validFlags()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700642 return nullptr;
egdaniel0d5fd112015-05-12 06:11:35 -0700643 }
644
bsalomonae4738f2015-09-15 15:33:27 -0700645 GrColor blendConstant = GrUnpremulColor(colorPOI.color());
egdaniel0d5fd112015-05-12 06:11:35 -0700646 uint8_t alpha = GrColorUnpackA(blendConstant);
647 blendConstant |= (0xff << GrColor_SHIFT_A);
648
halcanary385fe4d2015-08-26 13:07:48 -0700649 return new PDLCDXferProcessor(blendConstant, alpha);
egdaniel0d5fd112015-05-12 06:11:35 -0700650}
651
652PDLCDXferProcessor::~PDLCDXferProcessor() {
653}
654
Brian Salomon94efbf52016-11-29 13:43:05 -0500655void PDLCDXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800656 GrProcessorKeyBuilder* b) const {
egdanielcc252972015-05-12 12:03:28 -0700657 GLPDLCDXferProcessor::GenKey(*this, caps, b);
egdaniel0d5fd112015-05-12 06:11:35 -0700658}
659
egdaniel57d3b032015-11-13 11:57:27 -0800660GrGLSLXferProcessor* PDLCDXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700661 return new GLPDLCDXferProcessor(*this);
egdaniel0d5fd112015-05-12 06:11:35 -0700662}
663
Brian Salomon92aee3d2016-12-21 09:20:25 -0500664GrXferProcessor::OptFlags PDLCDXferProcessor::onGetOptimizations(const GrPipelineAnalysis&,
665 bool doesStencilWrite,
666 GrColor* overrideColor,
667 const GrCaps& caps) const {
668 // We want to force our primary output to be alpha * Coverage, where alpha is the alpha
669 // value of the blend the constant. We should already have valid blend coeff's if we are at
670 // a point where we have RGB coverage. We don't need any color stages since the known color
671 // output is already baked into the blendConstant.
672 *overrideColor = GrColorPackRGBA(fAlpha, fAlpha, fAlpha, fAlpha);
673 return GrXferProcessor::kOverrideColor_OptFlag;
egdaniel0d5fd112015-05-12 06:11:35 -0700674}
675
676///////////////////////////////////////////////////////////////////////////////
cdalton6fd158e2015-05-27 15:08:33 -0700677
Brian Salomona1633922017-01-09 11:46:10 -0500678constexpr GrPorterDuffXPFactory::GrPorterDuffXPFactory(SkBlendMode xfermode)
679 : fBlendMode(xfermode) {}
egdaniel915187b2014-12-05 12:58:28 -0800680
Brian Salomona1633922017-01-09 11:46:10 -0500681const GrXPFactory* GrPorterDuffXPFactory::Get(SkBlendMode blendMode) {
682 SkASSERT((unsigned)blendMode <= (unsigned)SkBlendMode::kLastCoeffMode);
cdalton6fd158e2015-05-27 15:08:33 -0700683
Brian Salomona1633922017-01-09 11:46:10 -0500684 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
685 // null.
686#ifdef SK_BUILD_FOR_WIN
687#define _CONSTEXPR_
688#else
689#define _CONSTEXPR_ constexpr
690#endif
691 static _CONSTEXPR_ const GrPorterDuffXPFactory gClearPDXPF(SkBlendMode::kClear);
692 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcPDXPF(SkBlendMode::kSrc);
693 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstPDXPF(SkBlendMode::kDst);
694 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcOverPDXPF(SkBlendMode::kSrcOver);
695 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstOverPDXPF(SkBlendMode::kDstOver);
696 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcInPDXPF(SkBlendMode::kSrcIn);
697 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstInPDXPF(SkBlendMode::kDstIn);
698 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcOutPDXPF(SkBlendMode::kSrcOut);
699 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstOutPDXPF(SkBlendMode::kDstOut);
700 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcATopPDXPF(SkBlendMode::kSrcATop);
701 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstATopPDXPF(SkBlendMode::kDstATop);
702 static _CONSTEXPR_ const GrPorterDuffXPFactory gXorPDXPF(SkBlendMode::kXor);
703 static _CONSTEXPR_ const GrPorterDuffXPFactory gPlusPDXPF(SkBlendMode::kPlus);
704 static _CONSTEXPR_ const GrPorterDuffXPFactory gModulatePDXPF(SkBlendMode::kModulate);
705 static _CONSTEXPR_ const GrPorterDuffXPFactory gScreenPDXPF(SkBlendMode::kScreen);
706#undef _CONSTEXPR_
cdalton6fd158e2015-05-27 15:08:33 -0700707
Brian Salomona1633922017-01-09 11:46:10 -0500708 switch (blendMode) {
709 case SkBlendMode::kClear:
710 return &gClearPDXPF;
711 case SkBlendMode::kSrc:
712 return &gSrcPDXPF;
713 case SkBlendMode::kDst:
714 return &gDstPDXPF;
715 case SkBlendMode::kSrcOver:
716 return &gSrcOverPDXPF;
717 case SkBlendMode::kDstOver:
718 return &gDstOverPDXPF;
719 case SkBlendMode::kSrcIn:
720 return &gSrcInPDXPF;
721 case SkBlendMode::kDstIn:
722 return &gDstInPDXPF;
723 case SkBlendMode::kSrcOut:
724 return &gSrcOutPDXPF;
725 case SkBlendMode::kDstOut:
726 return &gDstOutPDXPF;
727 case SkBlendMode::kSrcATop:
728 return &gSrcATopPDXPF;
729 case SkBlendMode::kDstATop:
730 return &gDstATopPDXPF;
731 case SkBlendMode::kXor:
732 return &gXorPDXPF;
733 case SkBlendMode::kPlus:
734 return &gPlusPDXPF;
735 case SkBlendMode::kModulate:
736 return &gModulatePDXPF;
737 case SkBlendMode::kScreen:
738 return &gScreenPDXPF;
739 default:
740 SkFAIL("Unexpected blend mode.");
741 return nullptr;
egdanielc016fb82014-12-03 11:41:54 -0800742 }
743}
744
Brian Salomon92aee3d2016-12-21 09:20:25 -0500745GrXferProcessor* GrPorterDuffXPFactory::onCreateXferProcessor(const GrCaps& caps,
746 const GrPipelineAnalysis& analysis,
747 bool hasMixedSamples,
748 const DstTexture* dstTexture) const {
749 if (analysis.fUsesPLSDstRead) {
Brian Salomona1633922017-01-09 11:46:10 -0500750 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fBlendMode);
ethannicholas22793252016-01-30 09:59:10 -0800751 }
egdaniel723b0502015-09-15 09:31:40 -0700752 BlendFormula blendFormula;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500753 if (analysis.fCoveragePOI.isFourChannelOutput()) {
Brian Salomona1633922017-01-09 11:46:10 -0500754 if (SkBlendMode::kSrcOver == fBlendMode &&
Brian Salomon92aee3d2016-12-21 09:20:25 -0500755 kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() &&
egdaniele73f1f62015-09-22 10:10:55 -0700756 !caps.shaderCaps()->dualSourceBlendingSupport() &&
757 !caps.shaderCaps()->dstReadInShaderSupport()) {
758 // If we don't have dual source blending or in shader dst reads, we fall back to this
759 // trick for rendering SrcOver LCD text instead of doing a dst copy.
egdaniel723b0502015-09-15 09:31:40 -0700760 SkASSERT(!dstTexture || !dstTexture->texture());
Brian Salomona1633922017-01-09 11:46:10 -0500761 return PDLCDXferProcessor::Create(fBlendMode, analysis.fColorPOI);
egdaniel723b0502015-09-15 09:31:40 -0700762 }
Brian Salomona1633922017-01-09 11:46:10 -0500763 blendFormula = get_lcd_blend_formula(analysis.fCoveragePOI, fBlendMode);
egdaniel723b0502015-09-15 09:31:40 -0700764 } else {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500765 blendFormula = get_blend_formula(analysis.fColorPOI, analysis.fCoveragePOI, hasMixedSamples,
Brian Salomona1633922017-01-09 11:46:10 -0500766 fBlendMode);
egdaniel95131432014-12-09 11:15:43 -0800767 }
cdalton6fd158e2015-05-27 15:08:33 -0700768
cdalton6fd158e2015-05-27 15:08:33 -0700769 if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) {
Brian Salomona1633922017-01-09 11:46:10 -0500770 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fBlendMode);
cdalton6fd158e2015-05-27 15:08:33 -0700771 }
772
773 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700774 return new PorterDuffXferProcessor(blendFormula);
egdaniel378092f2014-12-03 10:40:13 -0800775}
776
egdanielf2342722015-11-20 15:12:59 -0800777void GrPorterDuffXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
cdalton1fa45722015-06-02 10:43:39 -0700778 InvariantBlendedColor* blendedColor) const {
779 // Find the blended color info based on the formula that does not have coverage.
Brian Salomona1633922017-01-09 11:46:10 -0500780 BlendFormula colorFormula = gBlendTable[colorPOI.isOpaque()][0][(int)fBlendMode];
cdalton1fa45722015-06-02 10:43:39 -0700781 if (colorFormula.usesDstColor()) {
782 blendedColor->fWillBlendWithDst = true;
783 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800784 return;
egdaniel95131432014-12-09 11:15:43 -0800785 }
786
cdalton1fa45722015-06-02 10:43:39 -0700787 blendedColor->fWillBlendWithDst = false;
bungemanc33db932015-05-22 17:55:26 -0700788
cdalton1fa45722015-06-02 10:43:39 -0700789 SkASSERT(kAdd_GrBlendEquation == colorFormula.fBlendEquation);
bungemanc33db932015-05-22 17:55:26 -0700790
cdalton1fa45722015-06-02 10:43:39 -0700791 switch (colorFormula.fSrcCoeff) {
egdaniel9e4ecdc2014-12-18 12:44:55 -0800792 case kZero_GrBlendCoeff:
cdalton1fa45722015-06-02 10:43:39 -0700793 blendedColor->fKnownColor = 0;
794 blendedColor->fKnownColorFlags = kRGBA_GrColorComponentFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700795 return;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800796
797 case kOne_GrBlendCoeff:
cdalton1fa45722015-06-02 10:43:39 -0700798 blendedColor->fKnownColor = colorPOI.color();
799 blendedColor->fKnownColorFlags = colorPOI.validFlags();
cdalton6fd158e2015-05-27 15:08:33 -0700800 return;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800801
cdalton1fa45722015-06-02 10:43:39 -0700802 default:
803 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
804 return;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800805 }
egdaniel95131432014-12-09 11:15:43 -0800806}
807
ethannicholas22793252016-01-30 09:59:10 -0800808bool GrPorterDuffXPFactory::onWillReadDstColor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500809 const GrPipelineAnalysis& analysis) const {
cdalton86ae0a92015-06-08 15:11:04 -0700810 if (caps.shaderCaps()->dualSourceBlendingSupport()) {
811 return false;
bungemanc33db932015-05-22 17:55:26 -0700812 }
halcanary9d524f22016-03-29 09:03:52 -0700813
egdaniel723b0502015-09-15 09:31:40 -0700814 // When we have four channel coverage we always need to read the dst in order to correctly
815 // blend. The one exception is when we are using srcover mode and we know the input color into
816 // the XP.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500817 if (analysis.fCoveragePOI.isFourChannelOutput()) {
Brian Salomona1633922017-01-09 11:46:10 -0500818 if (SkBlendMode::kSrcOver == fBlendMode &&
Brian Salomon92aee3d2016-12-21 09:20:25 -0500819 kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() &&
egdaniele73f1f62015-09-22 10:10:55 -0700820 !caps.shaderCaps()->dstReadInShaderSupport()) {
egdaniel723b0502015-09-15 09:31:40 -0700821 return false;
822 }
Brian Salomona1633922017-01-09 11:46:10 -0500823 return get_lcd_blend_formula(analysis.fCoveragePOI, fBlendMode).hasSecondaryOutput();
cdalton86ae0a92015-06-08 15:11:04 -0700824 }
cdalton3ccf2e72016-05-06 09:41:16 -0700825
cdalton6fd158e2015-05-27 15:08:33 -0700826 // We fallback on the shader XP when the blend formula would use dual source blending but we
827 // don't have support for it.
cdalton3ccf2e72016-05-06 09:41:16 -0700828 static const bool kHasMixedSamples = false;
829 SkASSERT(!caps.usesMixedSamples()); // We never use mixed samples without dual source blending.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500830 auto formula = get_blend_formula(analysis.fColorPOI, analysis.fCoveragePOI, kHasMixedSamples,
Brian Salomona1633922017-01-09 11:46:10 -0500831 fBlendMode);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500832 return formula.hasSecondaryOutput();
bsalomon50785a32015-02-06 07:02:37 -0800833}
834
egdanielf2342722015-11-20 15:12:59 -0800835GR_DEFINE_XP_FACTORY_TEST(GrPorterDuffXPFactory);
egdanielc2304142014-12-11 13:15:13 -0800836
Brian Salomona1633922017-01-09 11:46:10 -0500837const GrXPFactory* GrPorterDuffXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400838 SkBlendMode mode = SkBlendMode(d->fRandom->nextULessThan((int)SkBlendMode::kLastCoeffMode));
Brian Salomona1633922017-01-09 11:46:10 -0500839 return GrPorterDuffXPFactory::Get(mode);
egdanielc2304142014-12-11 13:15:13 -0800840}
egdaniel95131432014-12-09 11:15:43 -0800841
egdanielf2342722015-11-20 15:12:59 -0800842void GrPorterDuffXPFactory::TestGetXPOutputTypes(const GrXferProcessor* xp,
843 int* outPrimary,
844 int* outSecondary) {
cdalton6fd158e2015-05-27 15:08:33 -0700845 if (!!strcmp(xp->name(), "Porter Duff")) {
846 *outPrimary = *outSecondary = -1;
847 return;
848 }
849 BlendFormula blendFormula = static_cast<const PorterDuffXferProcessor*>(xp)->getBlendFormula();
850 *outPrimary = blendFormula.fPrimaryOutputType;
851 *outSecondary = blendFormula.fSecondaryOutputType;
852}
egdanielc4b72722015-11-23 13:20:41 -0800853
854
855////////////////////////////////////////////////////////////////////////////////////////////////
856// SrcOver Global functions
857////////////////////////////////////////////////////////////////////////////////////////////////
bsalomon2047b782015-12-21 13:12:54 -0800858const GrXferProcessor& GrPorterDuffXPFactory::SimpleSrcOverXP() {
859 static BlendFormula gSrcOverBlendFormula = COEFF_FORMULA(kOne_GrBlendCoeff,
860 kISA_GrBlendCoeff);
861 static PorterDuffXferProcessor gSrcOverXP(gSrcOverBlendFormula);
862 return gSrcOverXP;
863}
egdanielc4b72722015-11-23 13:20:41 -0800864
865GrXferProcessor* GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
866 const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500867 const GrPipelineAnalysis& analysis,
egdanielc4b72722015-11-23 13:20:41 -0800868 bool hasMixedSamples,
869 const GrXferProcessor::DstTexture* dstTexture) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500870 if (analysis.fUsesPLSDstRead) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400871 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, SkBlendMode::kSrcOver);
ethannicholas22793252016-01-30 09:59:10 -0800872 }
egdanielabe795e2016-08-18 15:21:55 -0700873
874 // We want to not make an xfer processor if possible. Thus for the simple case where we are not
875 // doing lcd blending we will just use our global SimpleSrcOverXP. This slightly differs from
876 // the general case where we convert a src-over blend that has solid coverage and an opaque
877 // color to src-mode, which allows disabling of blending.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500878 if (!analysis.fCoveragePOI.isFourChannelOutput()) {
bsalomon2047b782015-12-21 13:12:54 -0800879 // We return nullptr here, which our caller interprets as meaning "use SimpleSrcOverXP".
880 // We don't simply return the address of that XP here because our caller would have to unref
881 // it and since it is a global object and GrProgramElement's ref-cnting system is not thread
882 // safe.
883 return nullptr;
egdaniel56cf6dc2015-11-30 10:15:58 -0800884 }
885
Brian Salomon92aee3d2016-12-21 09:20:25 -0500886 if (kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() &&
egdanielabe795e2016-08-18 15:21:55 -0700887 !caps.shaderCaps()->dualSourceBlendingSupport() &&
888 !caps.shaderCaps()->dstReadInShaderSupport()) {
889 // If we don't have dual source blending or in shader dst reads, we fall
890 // back to this trick for rendering SrcOver LCD text instead of doing a
891 // dst copy.
892 SkASSERT(!dstTexture || !dstTexture->texture());
Brian Salomon92aee3d2016-12-21 09:20:25 -0500893 return PDLCDXferProcessor::Create(SkBlendMode::kSrcOver, analysis.fColorPOI);
egdanielc4b72722015-11-23 13:20:41 -0800894 }
895
egdanielabe795e2016-08-18 15:21:55 -0700896 BlendFormula blendFormula;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500897 blendFormula = get_lcd_blend_formula(analysis.fCoveragePOI, SkBlendMode::kSrcOver);
egdanielc4b72722015-11-23 13:20:41 -0800898 if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400899 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, SkBlendMode::kSrcOver);
egdanielc4b72722015-11-23 13:20:41 -0800900 }
901
902 SkASSERT(!dstTexture || !dstTexture->texture());
903 return new PorterDuffXferProcessor(blendFormula);
904}
905
906bool GrPorterDuffXPFactory::SrcOverWillNeedDstTexture(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500907 const GrPipelineAnalysis& analysis) {
egdanielc4b72722015-11-23 13:20:41 -0800908 if (caps.shaderCaps()->dstReadInShaderSupport() ||
909 caps.shaderCaps()->dualSourceBlendingSupport()) {
910 return false;
911 }
912
913 // When we have four channel coverage we always need to read the dst in order to correctly
914 // blend. The one exception is when we are using srcover mode and we know the input color
915 // into the XP.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500916 if (analysis.fCoveragePOI.isFourChannelOutput()) {
917 if (kRGBA_GrColorComponentFlags == analysis.fColorPOI.validFlags() &&
egdanielc4b72722015-11-23 13:20:41 -0800918 !caps.shaderCaps()->dstReadInShaderSupport()) {
919 return false;
920 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500921 auto formula = get_lcd_blend_formula(analysis.fCoveragePOI, SkBlendMode::kSrcOver);
922 return formula.hasSecondaryOutput();
egdanielc4b72722015-11-23 13:20:41 -0800923 }
cdalton3ccf2e72016-05-06 09:41:16 -0700924
egdanielc4b72722015-11-23 13:20:41 -0800925 // We fallback on the shader XP when the blend formula would use dual source blending but we
926 // don't have support for it.
cdalton3ccf2e72016-05-06 09:41:16 -0700927 static const bool kHasMixedSamples = false;
928 SkASSERT(!caps.usesMixedSamples()); // We never use mixed samples without dual source blending.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500929 auto formula = get_blend_formula(analysis.fColorPOI, analysis.fCoveragePOI, kHasMixedSamples,
930 SkBlendMode::kSrcOver);
931 return formula.hasSecondaryOutput();
egdanielc4b72722015-11-23 13:20:41 -0800932}