blob: 7dd7be6b0b27b02d5c725b5f637bea907ac70b04 [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
Brian Salomon5be6c952017-01-20 19:06:29 +0000323static BlendFormula get_blend_formula(bool isOpaque,
324 bool hasCoverage,
cdalton86ae0a92015-06-08 15:11:04 -0700325 bool hasMixedSamples,
Mike Reed7d954ad2016-10-28 15:42:34 -0400326 SkBlendMode xfermode) {
327 SkASSERT((unsigned)xfermode <= (unsigned)SkBlendMode::kLastCoeffMode);
Brian Salomon5be6c952017-01-20 19:06:29 +0000328 bool conflatesCoverage = hasCoverage || hasMixedSamples;
329 return gBlendTable[isOpaque][conflatesCoverage][(int)xfermode];
egdaniel95131432014-12-09 11:15:43 -0800330}
331
Brian Salomon5be6c952017-01-20 19:06:29 +0000332static BlendFormula get_lcd_blend_formula(SkBlendMode xfermode) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400333 SkASSERT((unsigned)xfermode <= (unsigned)SkBlendMode::kLastCoeffMode);
egdaniel723b0502015-09-15 09:31:40 -0700334
Mike Reed7d954ad2016-10-28 15:42:34 -0400335 return gLCDBlendTable[(int)xfermode];
egdaniel723b0502015-09-15 09:31:40 -0700336}
337
cdalton6fd158e2015-05-27 15:08:33 -0700338///////////////////////////////////////////////////////////////////////////////
339
egdaniel41d4f092015-02-09 07:51:00 -0800340class PorterDuffXferProcessor : public GrXferProcessor {
egdaniel378092f2014-12-03 10:40:13 -0800341public:
cdalton86ae0a92015-06-08 15:11:04 -0700342 PorterDuffXferProcessor(BlendFormula blendFormula) : fBlendFormula(blendFormula) {
343 this->initClassID<PorterDuffXferProcessor>();
egdaniel41d4f092015-02-09 07:51:00 -0800344 }
egdaniel378092f2014-12-03 10:40:13 -0800345
mtklein36352bf2015-03-25 18:17:31 -0700346 const char* name() const override { return "Porter Duff"; }
egdaniel41d4f092015-02-09 07:51:00 -0800347
egdaniel57d3b032015-11-13 11:57:27 -0800348 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -0800349
cdalton6fd158e2015-05-27 15:08:33 -0700350 BlendFormula getBlendFormula() const { return fBlendFormula; }
cdaltonf4f2b442015-04-23 09:40:23 -0700351
352private:
Brian Salomon5298dc82017-02-22 11:52:03 -0500353 GrXferProcessor::OptFlags onGetOptimizations(const FragmentProcessorAnalysis&,
egdanielc19cdc22015-05-10 08:45:18 -0700354 bool doesStencilWrite,
355 GrColor* overrideColor,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500356 const GrCaps&) const override;
egdanielc19cdc22015-05-10 08:45:18 -0700357
Brian Salomon94efbf52016-11-29 13:43:05 -0500358 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
cdaltonf4f2b442015-04-23 09:40:23 -0700359
cdaltonedbb31f2015-06-08 12:14:44 -0700360 bool onHasSecondaryOutput() const override { return fBlendFormula.hasSecondaryOutput(); }
361
cdaltonf4f2b442015-04-23 09:40:23 -0700362 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override {
cdalton6fd158e2015-05-27 15:08:33 -0700363 blendInfo->fEquation = fBlendFormula.fBlendEquation;
364 blendInfo->fSrcBlend = fBlendFormula.fSrcCoeff;
365 blendInfo->fDstBlend = fBlendFormula.fDstCoeff;
366 blendInfo->fWriteColor = fBlendFormula.modifiesDst();
egdaniel41d4f092015-02-09 07:51:00 -0800367 }
368
mtklein36352bf2015-03-25 18:17:31 -0700369 bool onIsEqual(const GrXferProcessor& xpBase) const override {
egdaniel41d4f092015-02-09 07:51:00 -0800370 const PorterDuffXferProcessor& xp = xpBase.cast<PorterDuffXferProcessor>();
cdalton6fd158e2015-05-27 15:08:33 -0700371 return fBlendFormula == xp.fBlendFormula;
egdaniel41d4f092015-02-09 07:51:00 -0800372 }
373
cdalton6fd158e2015-05-27 15:08:33 -0700374 const BlendFormula fBlendFormula;
egdaniel41d4f092015-02-09 07:51:00 -0800375
376 typedef GrXferProcessor INHERITED;
377};
378
379///////////////////////////////////////////////////////////////////////////////
380
egdaniel2d721d32015-11-11 13:06:05 -0800381static void append_color_output(const PorterDuffXferProcessor& xp,
egdaniel4ca2e602015-11-18 08:01:26 -0800382 GrGLSLXPFragmentBuilder* fragBuilder,
cdalton6fd158e2015-05-27 15:08:33 -0700383 BlendFormula::OutputType outputType, const char* output,
384 const char* inColor, const char* inCoverage) {
Brian Salomon8c852be2017-01-04 10:44:42 -0500385 SkASSERT(inCoverage);
386 SkASSERT(inColor);
cdalton6fd158e2015-05-27 15:08:33 -0700387 switch (outputType) {
388 case BlendFormula::kNone_OutputType:
egdaniel4ca2e602015-11-18 08:01:26 -0800389 fragBuilder->codeAppendf("%s = vec4(0.0);", output);
cdalton6fd158e2015-05-27 15:08:33 -0700390 break;
391 case BlendFormula::kCoverage_OutputType:
cdalton86ae0a92015-06-08 15:11:04 -0700392 // We can have a coverage formula while not reading coverage if there are mixed samples.
Brian Salomon8c852be2017-01-04 10:44:42 -0500393 fragBuilder->codeAppendf("%s = %s;", output, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700394 break;
395 case BlendFormula::kModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500396 fragBuilder->codeAppendf("%s = %s * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700397 break;
egdaniel723b0502015-09-15 09:31:40 -0700398 case BlendFormula::kSAModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500399 fragBuilder->codeAppendf("%s = %s.a * %s;", output, inColor, inCoverage);
egdaniel723b0502015-09-15 09:31:40 -0700400 break;
cdalton6fd158e2015-05-27 15:08:33 -0700401 case BlendFormula::kISAModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500402 fragBuilder->codeAppendf("%s = (1.0 - %s.a) * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700403 break;
404 case BlendFormula::kISCModulate_OutputType:
Brian Salomon8c852be2017-01-04 10:44:42 -0500405 fragBuilder->codeAppendf("%s = (vec4(1.0) - %s) * %s;", output, inColor, inCoverage);
cdalton6fd158e2015-05-27 15:08:33 -0700406 break;
407 default:
408 SkFAIL("Unsupported output type.");
409 break;
egdaniel3ad65702015-02-17 11:15:47 -0800410 }
411}
412
egdanielfa4cc8b2015-11-13 08:34:52 -0800413class GLPorterDuffXferProcessor : public GrGLSLXferProcessor {
egdaniel41d4f092015-02-09 07:51:00 -0800414public:
cdalton6fd158e2015-05-27 15:08:33 -0700415 static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
egdaniel41d4f092015-02-09 07:51:00 -0800416 const PorterDuffXferProcessor& xp = processor.cast<PorterDuffXferProcessor>();
egdaniel56cf6dc2015-11-30 10:15:58 -0800417 b->add32(xp.getBlendFormula().fPrimaryOutputType |
418 (xp.getBlendFormula().fSecondaryOutputType << 3));
cdalton6fd158e2015-05-27 15:08:33 -0700419 GR_STATIC_ASSERT(BlendFormula::kLast_OutputType < 8);
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400420 }
bsalomon50785a32015-02-06 07:02:37 -0800421
422private:
cdaltonedbb31f2015-06-08 12:14:44 -0700423 void emitOutputsForBlendState(const EmitArgs& args) override {
egdaniel41d4f092015-02-09 07:51:00 -0800424 const PorterDuffXferProcessor& xp = args.fXP.cast<PorterDuffXferProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800425 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
bungemanc33db932015-05-22 17:55:26 -0700426
cdalton6fd158e2015-05-27 15:08:33 -0700427 BlendFormula blendFormula = xp.getBlendFormula();
428 if (blendFormula.hasSecondaryOutput()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800429 append_color_output(xp, fragBuilder, blendFormula.fSecondaryOutputType,
cdalton6fd158e2015-05-27 15:08:33 -0700430 args.fOutputSecondary, args.fInputColor, args.fInputCoverage);
egdanielc2304142014-12-11 13:15:13 -0800431 }
egdaniel4ca2e602015-11-18 08:01:26 -0800432 append_color_output(xp, fragBuilder, blendFormula.fPrimaryOutputType,
cdalton6fd158e2015-05-27 15:08:33 -0700433 args.fOutputPrimary, args.fInputColor, args.fInputCoverage);
egdaniel378092f2014-12-03 10:40:13 -0800434 }
435
egdaniel018fb622015-10-28 07:26:40 -0700436 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel378092f2014-12-03 10:40:13 -0800437
egdanielfa4cc8b2015-11-13 08:34:52 -0800438 typedef GrGLSLXferProcessor INHERITED;
egdaniel378092f2014-12-03 10:40:13 -0800439};
440
441///////////////////////////////////////////////////////////////////////////////
442
Brian Salomon94efbf52016-11-29 13:43:05 -0500443void PorterDuffXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps&,
egdaniel57d3b032015-11-13 11:57:27 -0800444 GrProcessorKeyBuilder* b) const {
cdalton6fd158e2015-05-27 15:08:33 -0700445 GLPorterDuffXferProcessor::GenKey(*this, b);
joshualitteb2a6762014-12-04 11:35:33 -0800446}
447
egdaniel57d3b032015-11-13 11:57:27 -0800448GrGLSLXferProcessor* PorterDuffXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700449 return new GLPorterDuffXferProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800450}
451
Brian Salomon92aee3d2016-12-21 09:20:25 -0500452GrXferProcessor::OptFlags PorterDuffXferProcessor::onGetOptimizations(
Brian Salomon5298dc82017-02-22 11:52:03 -0500453 const FragmentProcessorAnalysis& analysis,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500454 bool doesStencilWrite,
455 GrColor* overrideColor,
456 const GrCaps& caps) const {
bsalomon7765a472015-07-08 11:26:37 -0700457 GrXferProcessor::OptFlags optFlags = GrXferProcessor::kNone_OptFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700458 if (!fBlendFormula.modifiesDst()) {
cdalton6fd158e2015-05-27 15:08:33 -0700459 optFlags |= (GrXferProcessor::kIgnoreColor_OptFlag |
cdalton6fd158e2015-05-27 15:08:33 -0700460 GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag);
461 } else {
462 if (!fBlendFormula.usesInputColor()) {
463 optFlags |= GrXferProcessor::kIgnoreColor_OptFlag;
464 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500465 if (analysis.isCompatibleWithCoverageAsAlpha() &&
Brian Salomoneb628292017-02-15 14:12:26 -0500466 fBlendFormula.canTweakAlphaForCoverage()) {
cdalton6fd158e2015-05-27 15:08:33 -0700467 optFlags |= GrXferProcessor::kCanTweakAlphaForCoverage_OptFlag;
468 }
469 }
bungemanc33db932015-05-22 17:55:26 -0700470 return optFlags;
471}
472
cdalton6fd158e2015-05-27 15:08:33 -0700473///////////////////////////////////////////////////////////////////////////////
474
475class ShaderPDXferProcessor : public GrXferProcessor {
476public:
cdalton86ae0a92015-06-08 15:11:04 -0700477 ShaderPDXferProcessor(const DstTexture* dstTexture,
478 bool hasMixedSamples,
Mike Reed7d954ad2016-10-28 15:42:34 -0400479 SkBlendMode xfermode)
cdalton86ae0a92015-06-08 15:11:04 -0700480 : INHERITED(dstTexture, true, hasMixedSamples)
481 , fXfermode(xfermode) {
482 this->initClassID<ShaderPDXferProcessor>();
bungemanc33db932015-05-22 17:55:26 -0700483 }
484
cdalton6fd158e2015-05-27 15:08:33 -0700485 const char* name() const override { return "Porter Duff Shader"; }
cdalton6fd158e2015-05-27 15:08:33 -0700486
egdaniel57d3b032015-11-13 11:57:27 -0800487 GrGLSLXferProcessor* createGLSLInstance() const override;
cdalton6fd158e2015-05-27 15:08:33 -0700488
Mike Reed7d954ad2016-10-28 15:42:34 -0400489 SkBlendMode getXfermode() const { return fXfermode; }
cdalton6fd158e2015-05-27 15:08:33 -0700490
491private:
Brian Salomon5298dc82017-02-22 11:52:03 -0500492 GrXferProcessor::OptFlags onGetOptimizations(const FragmentProcessorAnalysis&, bool, GrColor*,
egdaniel56cf6dc2015-11-30 10:15:58 -0800493 const GrCaps&) const override {
bsalomon7765a472015-07-08 11:26:37 -0700494 return kNone_OptFlags;
cdalton6fd158e2015-05-27 15:08:33 -0700495 }
496
Brian Salomon94efbf52016-11-29 13:43:05 -0500497 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
cdalton6fd158e2015-05-27 15:08:33 -0700498
499 bool onIsEqual(const GrXferProcessor& xpBase) const override {
500 const ShaderPDXferProcessor& xp = xpBase.cast<ShaderPDXferProcessor>();
501 return fXfermode == xp.fXfermode;
502 }
503
Mike Reed7d954ad2016-10-28 15:42:34 -0400504 const SkBlendMode fXfermode;
cdalton6fd158e2015-05-27 15:08:33 -0700505
506 typedef GrXferProcessor INHERITED;
507};
508
509///////////////////////////////////////////////////////////////////////////////
510
egdanielfa4cc8b2015-11-13 08:34:52 -0800511class GLShaderPDXferProcessor : public GrGLSLXferProcessor {
cdalton6fd158e2015-05-27 15:08:33 -0700512public:
513 static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
514 const ShaderPDXferProcessor& xp = processor.cast<ShaderPDXferProcessor>();
Mike Reed7d954ad2016-10-28 15:42:34 -0400515 b->add32((int)xp.getXfermode());
egdaniel3ad65702015-02-17 11:15:47 -0800516 }
egdaniel87509242014-12-17 13:37:13 -0800517
cdalton6fd158e2015-05-27 15:08:33 -0700518private:
egdaniel7ea439b2015-12-03 09:20:44 -0800519 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
520 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800521 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800522 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800523 const char* dstColor,
524 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800525 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800526 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700527 const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>();
egdaniel95131432014-12-09 11:15:43 -0800528
egdaniel4ca2e602015-11-18 08:01:26 -0800529 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.getXfermode());
egdanielf34b2932015-12-01 13:54:06 -0800530
531 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800532 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
533 outColorSecondary, xp);
egdaniel95131432014-12-09 11:15:43 -0800534 }
535
egdaniel018fb622015-10-28 07:26:40 -0700536 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
bungemanc33db932015-05-22 17:55:26 -0700537
egdanielfa4cc8b2015-11-13 08:34:52 -0800538 typedef GrGLSLXferProcessor INHERITED;
cdalton6fd158e2015-05-27 15:08:33 -0700539};
bungemanc33db932015-05-22 17:55:26 -0700540
cdalton6fd158e2015-05-27 15:08:33 -0700541///////////////////////////////////////////////////////////////////////////////
bungemanc33db932015-05-22 17:55:26 -0700542
Brian Salomon94efbf52016-11-29 13:43:05 -0500543void ShaderPDXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps&,
egdaniel57d3b032015-11-13 11:57:27 -0800544 GrProcessorKeyBuilder* b) const {
cdalton6fd158e2015-05-27 15:08:33 -0700545 GLShaderPDXferProcessor::GenKey(*this, b);
bungemanc33db932015-05-22 17:55:26 -0700546}
547
egdaniel57d3b032015-11-13 11:57:27 -0800548GrGLSLXferProcessor* ShaderPDXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700549 return new GLShaderPDXferProcessor;
egdanielc2304142014-12-11 13:15:13 -0800550}
551
egdaniel378092f2014-12-03 10:40:13 -0800552///////////////////////////////////////////////////////////////////////////////
553
egdaniel0d5fd112015-05-12 06:11:35 -0700554class PDLCDXferProcessor : public GrXferProcessor {
555public:
Brian Salomon5298dc82017-02-22 11:52:03 -0500556 static GrXferProcessor* Create(SkBlendMode xfermode, const FragmentProcessorAnalysis& analysis);
egdaniel0d5fd112015-05-12 06:11:35 -0700557
558 ~PDLCDXferProcessor() override;
559
560 const char* name() const override { return "Porter Duff LCD"; }
561
egdaniel57d3b032015-11-13 11:57:27 -0800562 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700563
egdaniel0d5fd112015-05-12 06:11:35 -0700564private:
565 PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha);
566
Brian Salomon5298dc82017-02-22 11:52:03 -0500567 GrXferProcessor::OptFlags onGetOptimizations(const FragmentProcessorAnalysis&,
egdaniel0d5fd112015-05-12 06:11:35 -0700568 bool doesStencilWrite,
569 GrColor* overrideColor,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500570 const GrCaps&) const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700571
Brian Salomon94efbf52016-11-29 13:43:05 -0500572 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel0d5fd112015-05-12 06:11:35 -0700573
574 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override {
575 blendInfo->fSrcBlend = kConstC_GrBlendCoeff;
576 blendInfo->fDstBlend = kISC_GrBlendCoeff;
577 blendInfo->fBlendConstant = fBlendConstant;
578 }
579
580 bool onIsEqual(const GrXferProcessor& xpBase) const override {
581 const PDLCDXferProcessor& xp = xpBase.cast<PDLCDXferProcessor>();
582 if (fBlendConstant != xp.fBlendConstant ||
583 fAlpha != xp.fAlpha) {
584 return false;
585 }
586 return true;
587 }
588
589 GrColor fBlendConstant;
590 uint8_t fAlpha;
591
592 typedef GrXferProcessor INHERITED;
593};
594
595///////////////////////////////////////////////////////////////////////////////
596
egdanielfa4cc8b2015-11-13 08:34:52 -0800597class GLPDLCDXferProcessor : public GrGLSLXferProcessor {
egdaniel0d5fd112015-05-12 06:11:35 -0700598public:
599 GLPDLCDXferProcessor(const GrProcessor&) {}
600
601 virtual ~GLPDLCDXferProcessor() {}
602
Brian Salomon94efbf52016-11-29 13:43:05 -0500603 static void GenKey(const GrProcessor& processor, const GrShaderCaps& caps,
egdaniel0d5fd112015-05-12 06:11:35 -0700604 GrProcessorKeyBuilder* b) {}
605
606private:
cdaltonedbb31f2015-06-08 12:14:44 -0700607 void emitOutputsForBlendState(const EmitArgs& args) override {
egdaniel4ca2e602015-11-18 08:01:26 -0800608 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
egdaniel56cf6dc2015-11-30 10:15:58 -0800609 SkASSERT(args.fInputCoverage);
egdaniel4ca2e602015-11-18 08:01:26 -0800610 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputColor,
611 args.fInputCoverage);
egdaniel0d5fd112015-05-12 06:11:35 -0700612 }
613
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400614 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel0d5fd112015-05-12 06:11:35 -0700615
egdanielfa4cc8b2015-11-13 08:34:52 -0800616 typedef GrGLSLXferProcessor INHERITED;
egdaniel0d5fd112015-05-12 06:11:35 -0700617};
618
619///////////////////////////////////////////////////////////////////////////////
620
621PDLCDXferProcessor::PDLCDXferProcessor(GrColor blendConstant, uint8_t alpha)
622 : fBlendConstant(blendConstant)
623 , fAlpha(alpha) {
624 this->initClassID<PDLCDXferProcessor>();
625}
626
Brian Salomon5298dc82017-02-22 11:52:03 -0500627GrXferProcessor* PDLCDXferProcessor::Create(SkBlendMode xfermode,
628 const FragmentProcessorAnalysis& analysis) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400629 if (SkBlendMode::kSrcOver != xfermode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700630 return nullptr;
egdaniel0d5fd112015-05-12 06:11:35 -0700631 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500632 GrColor blendConstant;
Brian Salomon5298dc82017-02-22 11:52:03 -0500633 if (!analysis.hasKnownOutputColor(&blendConstant)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700634 return nullptr;
egdaniel0d5fd112015-05-12 06:11:35 -0700635 }
Brian Salomonafb41032017-03-01 13:59:10 -0500636 blendConstant = GrUnpremulColor(blendConstant);
egdaniel0d5fd112015-05-12 06:11:35 -0700637 uint8_t alpha = GrColorUnpackA(blendConstant);
638 blendConstant |= (0xff << GrColor_SHIFT_A);
halcanary385fe4d2015-08-26 13:07:48 -0700639 return new PDLCDXferProcessor(blendConstant, alpha);
egdaniel0d5fd112015-05-12 06:11:35 -0700640}
641
642PDLCDXferProcessor::~PDLCDXferProcessor() {
643}
644
Brian Salomon94efbf52016-11-29 13:43:05 -0500645void PDLCDXferProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800646 GrProcessorKeyBuilder* b) const {
egdanielcc252972015-05-12 12:03:28 -0700647 GLPDLCDXferProcessor::GenKey(*this, caps, b);
egdaniel0d5fd112015-05-12 06:11:35 -0700648}
649
egdaniel57d3b032015-11-13 11:57:27 -0800650GrGLSLXferProcessor* PDLCDXferProcessor::createGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700651 return new GLPDLCDXferProcessor(*this);
egdaniel0d5fd112015-05-12 06:11:35 -0700652}
653
Brian Salomon5298dc82017-02-22 11:52:03 -0500654GrXferProcessor::OptFlags PDLCDXferProcessor::onGetOptimizations(const FragmentProcessorAnalysis&,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500655 bool doesStencilWrite,
656 GrColor* overrideColor,
657 const GrCaps& caps) const {
658 // We want to force our primary output to be alpha * Coverage, where alpha is the alpha
659 // value of the blend the constant. We should already have valid blend coeff's if we are at
660 // a point where we have RGB coverage. We don't need any color stages since the known color
661 // output is already baked into the blendConstant.
662 *overrideColor = GrColorPackRGBA(fAlpha, fAlpha, fAlpha, fAlpha);
663 return GrXferProcessor::kOverrideColor_OptFlag;
egdaniel0d5fd112015-05-12 06:11:35 -0700664}
665
666///////////////////////////////////////////////////////////////////////////////
cdalton6fd158e2015-05-27 15:08:33 -0700667
Brian Salomona1633922017-01-09 11:46:10 -0500668constexpr GrPorterDuffXPFactory::GrPorterDuffXPFactory(SkBlendMode xfermode)
669 : fBlendMode(xfermode) {}
egdaniel915187b2014-12-05 12:58:28 -0800670
Brian Salomona1633922017-01-09 11:46:10 -0500671const GrXPFactory* GrPorterDuffXPFactory::Get(SkBlendMode blendMode) {
672 SkASSERT((unsigned)blendMode <= (unsigned)SkBlendMode::kLastCoeffMode);
cdalton6fd158e2015-05-27 15:08:33 -0700673
Brian Salomona1633922017-01-09 11:46:10 -0500674 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
675 // null.
676#ifdef SK_BUILD_FOR_WIN
677#define _CONSTEXPR_
678#else
679#define _CONSTEXPR_ constexpr
680#endif
681 static _CONSTEXPR_ const GrPorterDuffXPFactory gClearPDXPF(SkBlendMode::kClear);
682 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcPDXPF(SkBlendMode::kSrc);
683 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstPDXPF(SkBlendMode::kDst);
684 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcOverPDXPF(SkBlendMode::kSrcOver);
685 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstOverPDXPF(SkBlendMode::kDstOver);
686 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcInPDXPF(SkBlendMode::kSrcIn);
687 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstInPDXPF(SkBlendMode::kDstIn);
688 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcOutPDXPF(SkBlendMode::kSrcOut);
689 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstOutPDXPF(SkBlendMode::kDstOut);
690 static _CONSTEXPR_ const GrPorterDuffXPFactory gSrcATopPDXPF(SkBlendMode::kSrcATop);
691 static _CONSTEXPR_ const GrPorterDuffXPFactory gDstATopPDXPF(SkBlendMode::kDstATop);
692 static _CONSTEXPR_ const GrPorterDuffXPFactory gXorPDXPF(SkBlendMode::kXor);
693 static _CONSTEXPR_ const GrPorterDuffXPFactory gPlusPDXPF(SkBlendMode::kPlus);
694 static _CONSTEXPR_ const GrPorterDuffXPFactory gModulatePDXPF(SkBlendMode::kModulate);
695 static _CONSTEXPR_ const GrPorterDuffXPFactory gScreenPDXPF(SkBlendMode::kScreen);
696#undef _CONSTEXPR_
cdalton6fd158e2015-05-27 15:08:33 -0700697
Brian Salomona1633922017-01-09 11:46:10 -0500698 switch (blendMode) {
699 case SkBlendMode::kClear:
700 return &gClearPDXPF;
701 case SkBlendMode::kSrc:
702 return &gSrcPDXPF;
703 case SkBlendMode::kDst:
704 return &gDstPDXPF;
705 case SkBlendMode::kSrcOver:
706 return &gSrcOverPDXPF;
707 case SkBlendMode::kDstOver:
708 return &gDstOverPDXPF;
709 case SkBlendMode::kSrcIn:
710 return &gSrcInPDXPF;
711 case SkBlendMode::kDstIn:
712 return &gDstInPDXPF;
713 case SkBlendMode::kSrcOut:
714 return &gSrcOutPDXPF;
715 case SkBlendMode::kDstOut:
716 return &gDstOutPDXPF;
717 case SkBlendMode::kSrcATop:
718 return &gSrcATopPDXPF;
719 case SkBlendMode::kDstATop:
720 return &gDstATopPDXPF;
721 case SkBlendMode::kXor:
722 return &gXorPDXPF;
723 case SkBlendMode::kPlus:
724 return &gPlusPDXPF;
725 case SkBlendMode::kModulate:
726 return &gModulatePDXPF;
727 case SkBlendMode::kScreen:
728 return &gScreenPDXPF;
729 default:
730 SkFAIL("Unexpected blend mode.");
731 return nullptr;
egdanielc016fb82014-12-03 11:41:54 -0800732 }
733}
734
Brian Salomon5298dc82017-02-22 11:52:03 -0500735GrXferProcessor* GrPorterDuffXPFactory::onCreateXferProcessor(
736 const GrCaps& caps,
737 const FragmentProcessorAnalysis& analysis,
738 bool hasMixedSamples,
739 const DstTexture* dstTexture) const {
740 if (analysis.usesPLSDstRead()) {
Brian Salomona1633922017-01-09 11:46:10 -0500741 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fBlendMode);
ethannicholas22793252016-01-30 09:59:10 -0800742 }
egdaniel723b0502015-09-15 09:31:40 -0700743 BlendFormula blendFormula;
Brian Salomon5298dc82017-02-22 11:52:03 -0500744 if (analysis.hasLCDCoverage()) {
745 if (SkBlendMode::kSrcOver == fBlendMode && analysis.hasKnownOutputColor() &&
egdaniele73f1f62015-09-22 10:10:55 -0700746 !caps.shaderCaps()->dualSourceBlendingSupport() &&
747 !caps.shaderCaps()->dstReadInShaderSupport()) {
748 // If we don't have dual source blending or in shader dst reads, we fall back to this
749 // trick for rendering SrcOver LCD text instead of doing a dst copy.
egdaniel723b0502015-09-15 09:31:40 -0700750 SkASSERT(!dstTexture || !dstTexture->texture());
Brian Salomon5298dc82017-02-22 11:52:03 -0500751 return PDLCDXferProcessor::Create(fBlendMode, analysis);
egdaniel723b0502015-09-15 09:31:40 -0700752 }
Brian Salomon5be6c952017-01-20 19:06:29 +0000753 blendFormula = get_lcd_blend_formula(fBlendMode);
egdaniel723b0502015-09-15 09:31:40 -0700754 } else {
Brian Salomon5298dc82017-02-22 11:52:03 -0500755 blendFormula = get_blend_formula(analysis.isOutputColorOpaque(), analysis.hasCoverage(),
756 hasMixedSamples, fBlendMode);
egdaniel95131432014-12-09 11:15:43 -0800757 }
cdalton6fd158e2015-05-27 15:08:33 -0700758
cdalton6fd158e2015-05-27 15:08:33 -0700759 if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) {
Brian Salomona1633922017-01-09 11:46:10 -0500760 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, fBlendMode);
cdalton6fd158e2015-05-27 15:08:33 -0700761 }
762
763 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700764 return new PorterDuffXferProcessor(blendFormula);
egdaniel378092f2014-12-03 10:40:13 -0800765}
egdaniel95131432014-12-09 11:15:43 -0800766
Brian Salomon5298dc82017-02-22 11:52:03 -0500767bool GrPorterDuffXPFactory::willReadsDst(const FragmentProcessorAnalysis& analysis) const {
768 BlendFormula colorFormula = gBlendTable[analysis.isOutputColorOpaque()][0][(int)fBlendMode];
Brian Salomon9a514982017-02-14 10:28:22 -0500769 SkASSERT(kAdd_GrBlendEquation == colorFormula.fBlendEquation);
Brian Salomon5298dc82017-02-22 11:52:03 -0500770 return (colorFormula.usesDstColor() || analysis.hasCoverage());
Brian Salomon9a514982017-02-14 10:28:22 -0500771}
772
Brian Salomon5298dc82017-02-22 11:52:03 -0500773bool GrPorterDuffXPFactory::onWillReadDstInShader(const GrCaps& caps,
774 const FragmentProcessorAnalysis& analysis) const {
cdalton86ae0a92015-06-08 15:11:04 -0700775 if (caps.shaderCaps()->dualSourceBlendingSupport()) {
776 return false;
bungemanc33db932015-05-22 17:55:26 -0700777 }
halcanary9d524f22016-03-29 09:03:52 -0700778
egdaniel723b0502015-09-15 09:31:40 -0700779 // When we have four channel coverage we always need to read the dst in order to correctly
780 // blend. The one exception is when we are using srcover mode and we know the input color into
781 // the XP.
Brian Salomon5298dc82017-02-22 11:52:03 -0500782 if (analysis.hasLCDCoverage()) {
783 if (SkBlendMode::kSrcOver == fBlendMode && analysis.hasKnownOutputColor() &&
egdaniele73f1f62015-09-22 10:10:55 -0700784 !caps.shaderCaps()->dstReadInShaderSupport()) {
egdaniel723b0502015-09-15 09:31:40 -0700785 return false;
786 }
Brian Salomon5be6c952017-01-20 19:06:29 +0000787 return get_lcd_blend_formula(fBlendMode).hasSecondaryOutput();
cdalton86ae0a92015-06-08 15:11:04 -0700788 }
cdalton3ccf2e72016-05-06 09:41:16 -0700789
cdalton6fd158e2015-05-27 15:08:33 -0700790 // We fallback on the shader XP when the blend formula would use dual source blending but we
791 // don't have support for it.
cdalton3ccf2e72016-05-06 09:41:16 -0700792 static const bool kHasMixedSamples = false;
793 SkASSERT(!caps.usesMixedSamples()); // We never use mixed samples without dual source blending.
Brian Salomon5298dc82017-02-22 11:52:03 -0500794 auto formula = get_blend_formula(analysis.isOutputColorOpaque(), analysis.hasCoverage(),
795 kHasMixedSamples, fBlendMode);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500796 return formula.hasSecondaryOutput();
bsalomon50785a32015-02-06 07:02:37 -0800797}
798
egdanielf2342722015-11-20 15:12:59 -0800799GR_DEFINE_XP_FACTORY_TEST(GrPorterDuffXPFactory);
egdanielc2304142014-12-11 13:15:13 -0800800
Hal Canary6f6961e2017-01-31 13:50:44 -0500801#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500802const GrXPFactory* GrPorterDuffXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400803 SkBlendMode mode = SkBlendMode(d->fRandom->nextULessThan((int)SkBlendMode::kLastCoeffMode));
Brian Salomona1633922017-01-09 11:46:10 -0500804 return GrPorterDuffXPFactory::Get(mode);
egdanielc2304142014-12-11 13:15:13 -0800805}
Hal Canary6f6961e2017-01-31 13:50:44 -0500806#endif
egdaniel95131432014-12-09 11:15:43 -0800807
egdanielf2342722015-11-20 15:12:59 -0800808void GrPorterDuffXPFactory::TestGetXPOutputTypes(const GrXferProcessor* xp,
809 int* outPrimary,
810 int* outSecondary) {
cdalton6fd158e2015-05-27 15:08:33 -0700811 if (!!strcmp(xp->name(), "Porter Duff")) {
812 *outPrimary = *outSecondary = -1;
813 return;
814 }
815 BlendFormula blendFormula = static_cast<const PorterDuffXferProcessor*>(xp)->getBlendFormula();
816 *outPrimary = blendFormula.fPrimaryOutputType;
817 *outSecondary = blendFormula.fSecondaryOutputType;
818}
egdanielc4b72722015-11-23 13:20:41 -0800819
egdanielc4b72722015-11-23 13:20:41 -0800820////////////////////////////////////////////////////////////////////////////////////////////////
821// SrcOver Global functions
822////////////////////////////////////////////////////////////////////////////////////////////////
bsalomon2047b782015-12-21 13:12:54 -0800823const GrXferProcessor& GrPorterDuffXPFactory::SimpleSrcOverXP() {
824 static BlendFormula gSrcOverBlendFormula = COEFF_FORMULA(kOne_GrBlendCoeff,
825 kISA_GrBlendCoeff);
826 static PorterDuffXferProcessor gSrcOverXP(gSrcOverBlendFormula);
827 return gSrcOverXP;
828}
egdanielc4b72722015-11-23 13:20:41 -0800829
830GrXferProcessor* GrPorterDuffXPFactory::CreateSrcOverXferProcessor(
831 const GrCaps& caps,
Brian Salomon5298dc82017-02-22 11:52:03 -0500832 const FragmentProcessorAnalysis& analysis,
egdanielc4b72722015-11-23 13:20:41 -0800833 bool hasMixedSamples,
834 const GrXferProcessor::DstTexture* dstTexture) {
Brian Salomon5298dc82017-02-22 11:52:03 -0500835 if (analysis.usesPLSDstRead()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400836 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, SkBlendMode::kSrcOver);
ethannicholas22793252016-01-30 09:59:10 -0800837 }
egdanielabe795e2016-08-18 15:21:55 -0700838
839 // We want to not make an xfer processor if possible. Thus for the simple case where we are not
840 // doing lcd blending we will just use our global SimpleSrcOverXP. This slightly differs from
841 // the general case where we convert a src-over blend that has solid coverage and an opaque
842 // color to src-mode, which allows disabling of blending.
Brian Salomon5298dc82017-02-22 11:52:03 -0500843 if (!analysis.hasLCDCoverage()) {
bsalomon2047b782015-12-21 13:12:54 -0800844 // We return nullptr here, which our caller interprets as meaning "use SimpleSrcOverXP".
845 // We don't simply return the address of that XP here because our caller would have to unref
846 // it and since it is a global object and GrProgramElement's ref-cnting system is not thread
847 // safe.
848 return nullptr;
egdaniel56cf6dc2015-11-30 10:15:58 -0800849 }
850
Brian Salomon5298dc82017-02-22 11:52:03 -0500851 if (analysis.hasKnownOutputColor() && !caps.shaderCaps()->dualSourceBlendingSupport() &&
egdanielabe795e2016-08-18 15:21:55 -0700852 !caps.shaderCaps()->dstReadInShaderSupport()) {
853 // If we don't have dual source blending or in shader dst reads, we fall
854 // back to this trick for rendering SrcOver LCD text instead of doing a
855 // dst copy.
856 SkASSERT(!dstTexture || !dstTexture->texture());
Brian Salomon5298dc82017-02-22 11:52:03 -0500857 return PDLCDXferProcessor::Create(SkBlendMode::kSrcOver, analysis);
egdanielc4b72722015-11-23 13:20:41 -0800858 }
859
egdanielabe795e2016-08-18 15:21:55 -0700860 BlendFormula blendFormula;
Brian Salomon5be6c952017-01-20 19:06:29 +0000861 blendFormula = get_lcd_blend_formula(SkBlendMode::kSrcOver);
egdanielc4b72722015-11-23 13:20:41 -0800862 if (blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->dualSourceBlendingSupport()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400863 return new ShaderPDXferProcessor(dstTexture, hasMixedSamples, SkBlendMode::kSrcOver);
egdanielc4b72722015-11-23 13:20:41 -0800864 }
865
866 SkASSERT(!dstTexture || !dstTexture->texture());
867 return new PorterDuffXferProcessor(blendFormula);
868}
869
csmartdalton119fb2b2017-02-08 14:41:05 -0500870sk_sp<GrXferProcessor> GrPorterDuffXPFactory::CreateNoCoverageXP(SkBlendMode blendmode) {
871 BlendFormula formula = get_blend_formula(false, false, false, blendmode);
872 return sk_make_sp<PorterDuffXferProcessor>(formula);
873}
874
Brian Salomon5298dc82017-02-22 11:52:03 -0500875bool GrPorterDuffXPFactory::WillSrcOverReadDst(const FragmentProcessorAnalysis& analysis) {
876 return analysis.hasCoverage() || !analysis.isOutputColorOpaque();
Brian Salomon9a514982017-02-14 10:28:22 -0500877}
878
879bool GrPorterDuffXPFactory::IsSrcOverPreCoverageBlendedColorConstant(
880 const GrProcOptInfo& colorInput, GrColor* color) {
881 if (!colorInput.isOpaque()) {
882 return false;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500883 }
Brian Salomon9a514982017-02-14 10:28:22 -0500884 return colorInput.hasKnownOutputColor(color);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500885}
886
Brian Salomon5298dc82017-02-22 11:52:03 -0500887bool GrPorterDuffXPFactory::WillSrcOverNeedDstTexture(const GrCaps& caps,
888 const FragmentProcessorAnalysis& analysis) {
egdanielc4b72722015-11-23 13:20:41 -0800889 if (caps.shaderCaps()->dstReadInShaderSupport() ||
890 caps.shaderCaps()->dualSourceBlendingSupport()) {
891 return false;
892 }
893
894 // When we have four channel coverage we always need to read the dst in order to correctly
895 // blend. The one exception is when we are using srcover mode and we know the input color
896 // into the XP.
Brian Salomon5298dc82017-02-22 11:52:03 -0500897 if (analysis.hasLCDCoverage()) {
898 if (analysis.hasKnownOutputColor() && !caps.shaderCaps()->dstReadInShaderSupport()) {
egdanielc4b72722015-11-23 13:20:41 -0800899 return false;
900 }
Brian Salomon5be6c952017-01-20 19:06:29 +0000901 auto formula = get_lcd_blend_formula(SkBlendMode::kSrcOver);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500902 return formula.hasSecondaryOutput();
egdanielc4b72722015-11-23 13:20:41 -0800903 }
cdalton3ccf2e72016-05-06 09:41:16 -0700904
egdanielc4b72722015-11-23 13:20:41 -0800905 // We fallback on the shader XP when the blend formula would use dual source blending but we
906 // don't have support for it.
cdalton3ccf2e72016-05-06 09:41:16 -0700907 static const bool kHasMixedSamples = false;
Brian Salomon5298dc82017-02-22 11:52:03 -0500908 bool isOpaque = analysis.isOutputColorOpaque();
909 bool hasCoverage = analysis.hasCoverage();
cdalton3ccf2e72016-05-06 09:41:16 -0700910 SkASSERT(!caps.usesMixedSamples()); // We never use mixed samples without dual source blending.
Brian Salomon5be6c952017-01-20 19:06:29 +0000911 auto formula =
912 get_blend_formula(isOpaque, hasCoverage, kHasMixedSamples, SkBlendMode::kSrcOver);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500913 return formula.hasSecondaryOutput();
egdanielc4b72722015-11-23 13:20:41 -0800914}