blob: dccec0c5991553eae61d3e9d76e765a2c43a9e56 [file] [log] [blame]
egdaniel0063a9b2015-01-15 10:52:32 -08001/*
2 * Copyright 2015 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
8#include "effects/GrCustomXfermode.h"
egdaniel0063a9b2015-01-15 10:52:32 -08009
10#include "GrCoordTransform.h"
11#include "GrContext.h"
12#include "GrFragmentProcessor.h"
13#include "GrInvariantOutput.h"
ethannicholasde4166a2015-11-30 08:57:38 -080014#include "GrPipeline.h"
egdaniel0063a9b2015-01-15 10:52:32 -080015#include "GrProcessor.h"
16#include "GrTexture.h"
17#include "GrTextureAccess.h"
18#include "SkXfermode.h"
egdaniel64c47282015-11-13 06:54:19 -080019#include "glsl/GrGLSLBlend.h"
jvanverthcba99b82015-06-24 06:59:57 -070020#include "glsl/GrGLSLCaps.h"
egdaniel64c47282015-11-13 06:54:19 -080021#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080022#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070023#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080024#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080025#include "glsl/GrGLSLXferProcessor.h"
egdaniel0063a9b2015-01-15 10:52:32 -080026
27bool GrCustomXfermode::IsSupportedMode(SkXfermode::Mode mode) {
28 return mode > SkXfermode::kLastCoeffMode && mode <= SkXfermode::kLastMode;
29}
30
31///////////////////////////////////////////////////////////////////////////////
32// Static helpers
33///////////////////////////////////////////////////////////////////////////////
34
cdalton8917d622015-05-06 13:40:21 -070035static GrBlendEquation hw_blend_equation(SkXfermode::Mode mode) {
36 enum { kOffset = kOverlay_GrBlendEquation - SkXfermode::kOverlay_Mode };
37 return static_cast<GrBlendEquation>(mode + kOffset);
38
39 GR_STATIC_ASSERT(kOverlay_GrBlendEquation == SkXfermode::kOverlay_Mode + kOffset);
40 GR_STATIC_ASSERT(kDarken_GrBlendEquation == SkXfermode::kDarken_Mode + kOffset);
41 GR_STATIC_ASSERT(kLighten_GrBlendEquation == SkXfermode::kLighten_Mode + kOffset);
42 GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == SkXfermode::kColorDodge_Mode + kOffset);
43 GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == SkXfermode::kColorBurn_Mode + kOffset);
44 GR_STATIC_ASSERT(kHardLight_GrBlendEquation == SkXfermode::kHardLight_Mode + kOffset);
45 GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == SkXfermode::kSoftLight_Mode + kOffset);
46 GR_STATIC_ASSERT(kDifference_GrBlendEquation == SkXfermode::kDifference_Mode + kOffset);
47 GR_STATIC_ASSERT(kExclusion_GrBlendEquation == SkXfermode::kExclusion_Mode + kOffset);
48 GR_STATIC_ASSERT(kMultiply_GrBlendEquation == SkXfermode::kMultiply_Mode + kOffset);
49 GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == SkXfermode::kHue_Mode + kOffset);
50 GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == SkXfermode::kSaturation_Mode + kOffset);
51 GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == SkXfermode::kColor_Mode + kOffset);
52 GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == SkXfermode::kLuminosity_Mode + kOffset);
bsalomonf7cc8772015-05-11 11:21:14 -070053 GR_STATIC_ASSERT(kGrBlendEquationCnt == SkXfermode::kLastMode + 1 + kOffset);
cdalton8917d622015-05-06 13:40:21 -070054}
55
cdalton1dd05422015-06-12 09:01:18 -070056static bool can_use_hw_blend_equation(GrBlendEquation equation,
ethannicholas22793252016-01-30 09:59:10 -080057 const GrPipelineOptimizations& opt,
cdalton1dd05422015-06-12 09:01:18 -070058 const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070059 if (!caps.advancedBlendEquationSupport()) {
60 return false;
61 }
ethannicholas22793252016-01-30 09:59:10 -080062 if (opt.fOverrides.fUsePLSDstRead) {
63 return false;
64 }
65 if (opt.fCoveragePOI.isFourChannelOutput()) {
cdaltonf0df1892015-06-05 13:26:20 -070066 return false; // LCD coverage must be applied after the blend equation.
67 }
cdalton1dd05422015-06-12 09:01:18 -070068 if (caps.canUseAdvancedBlendEquation(equation)) {
69 return false;
70 }
cdaltonf0df1892015-06-05 13:26:20 -070071 return true;
72}
73
egdaniel54f0e9d2015-01-16 06:29:47 -080074///////////////////////////////////////////////////////////////////////////////
75// Xfer Processor
76///////////////////////////////////////////////////////////////////////////////
77
egdaniel41d4f092015-02-09 07:51:00 -080078class CustomXP : public GrXferProcessor {
79public:
cdaltonf0df1892015-06-05 13:26:20 -070080 CustomXP(SkXfermode::Mode mode, GrBlendEquation hwBlendEquation)
81 : fMode(mode),
82 fHWBlendEquation(hwBlendEquation) {
83 this->initClassID<CustomXP>();
egdaniel41d4f092015-02-09 07:51:00 -080084 }
85
cdalton86ae0a92015-06-08 15:11:04 -070086 CustomXP(const DstTexture* dstTexture, bool hasMixedSamples, SkXfermode::Mode mode)
87 : INHERITED(dstTexture, true, hasMixedSamples),
cdaltonf0df1892015-06-05 13:26:20 -070088 fMode(mode),
89 fHWBlendEquation(static_cast<GrBlendEquation>(-1)) {
90 this->initClassID<CustomXP>();
91 }
egdaniel41d4f092015-02-09 07:51:00 -080092
mtklein36352bf2015-03-25 18:17:31 -070093 const char* name() const override { return "Custom Xfermode"; }
egdaniel41d4f092015-02-09 07:51:00 -080094
egdaniel57d3b032015-11-13 11:57:27 -080095 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -080096
egdaniel41d4f092015-02-09 07:51:00 -080097 SkXfermode::Mode mode() const { return fMode; }
bsalomonf7cc8772015-05-11 11:21:14 -070098 bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); }
cdalton8917d622015-05-06 13:40:21 -070099
100 GrBlendEquation hwBlendEquation() const {
101 SkASSERT(this->hasHWBlendEquation());
102 return fHWBlendEquation;
103 }
egdaniel41d4f092015-02-09 07:51:00 -0800104
105private:
ethannicholasde4166a2015-11-30 08:57:38 -0800106 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdanielc19cdc22015-05-10 08:45:18 -0700107 bool doesStencilWrite,
108 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800109 const GrCaps& caps) const override;
egdanielc19cdc22015-05-10 08:45:18 -0700110
egdaniel57d3b032015-11-13 11:57:27 -0800111 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800112
bsalomoncb02b382015-08-12 11:14:50 -0700113 GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const override;
cdalton8917d622015-05-06 13:40:21 -0700114
115 void onGetBlendInfo(BlendInfo*) const override;
116
mtklein36352bf2015-03-25 18:17:31 -0700117 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800118
cdaltonf0df1892015-06-05 13:26:20 -0700119 const SkXfermode::Mode fMode;
120 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800121
122 typedef GrXferProcessor INHERITED;
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
egdanielfa4cc8b2015-11-13 08:34:52 -0800127class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800128public:
129 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700130 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800131
cdalton8917d622015-05-06 13:40:21 -0700132 static void GenKey(const GrXferProcessor& p, const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) {
133 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700134 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700135 if (xp.hasHWBlendEquation()) {
136 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700137 key |= caps.advBlendEqInteraction();
cdaltonedbb31f2015-06-08 12:14:44 -0700138 GR_STATIC_ASSERT(GrGLSLCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700139 }
140 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
cdaltonedbb31f2015-06-08 12:14:44 -0700141 key |= xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700142 }
bsalomon50785a32015-02-06 07:02:37 -0800143 b->add32(key);
144 }
145
146private:
cdaltonedbb31f2015-06-08 12:14:44 -0700147 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700148 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700149 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800150
egdaniel4ca2e602015-11-18 08:01:26 -0800151 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
152 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700153
cdalton86ae0a92015-06-08 15:11:04 -0700154 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
155 // "just work" automatically. (See onGetOptimizations())
egdaniel56cf6dc2015-11-30 10:15:58 -0800156 if (args.fInputCoverage) {
egdaniel4ca2e602015-11-18 08:01:26 -0800157 fragBuilder->codeAppendf("%s = %s * %s;",
158 args.fOutputPrimary, args.fInputCoverage, args.fInputColor);
cdalton8917d622015-05-06 13:40:21 -0700159 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800160 fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, args.fInputColor);
cdalton8917d622015-05-06 13:40:21 -0700161 }
egdaniel54f0e9d2015-01-16 06:29:47 -0800162 }
163
egdaniel7ea439b2015-12-03 09:20:44 -0800164 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
165 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800166 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800167 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800168 const char* dstColor,
169 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800170 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800171 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700172 const CustomXP& xp = proc.cast<CustomXP>();
173 SkASSERT(!xp.hasHWBlendEquation());
174
egdaniel4ca2e602015-11-18 08:01:26 -0800175 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800176
177 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800178 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
179 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700180 }
181
egdaniel018fb622015-10-28 07:26:40 -0700182 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800183
egdanielfa4cc8b2015-11-13 08:34:52 -0800184 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800185};
186
187///////////////////////////////////////////////////////////////////////////////
188
egdaniel57d3b032015-11-13 11:57:27 -0800189void CustomXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800190 GLCustomXP::GenKey(*this, caps, b);
191}
192
egdaniel57d3b032015-11-13 11:57:27 -0800193GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700194 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700195 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800196}
197
egdaniel41d4f092015-02-09 07:51:00 -0800198bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
199 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700200 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800201}
202
ethannicholasde4166a2015-11-30 08:57:38 -0800203GrXferProcessor::OptFlags CustomXP::onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdaniel54f0e9d2015-01-16 06:29:47 -0800204 bool doesStencilWrite,
205 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800206 const GrCaps& caps) const {
cdalton8917d622015-05-06 13:40:21 -0700207 /*
208 Most the optimizations we do here are based on tweaking alpha for coverage.
209
210 The general SVG blend equation is defined in the spec as follows:
211
212 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
213 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
214
215 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
216 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
217 RGB colors.)
218
219 For every blend mode supported by this class, i.e. the "advanced" blend
220 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
221
222 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
223 coverage.
224
225
226 == Color ==
227
228 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
229 terms of premultiplied alpha only:
230
231 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
232 Sca : if Da == 0,
233 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if Sa,Da != 0}
234
235 And for coverage modulation, we use a post blend src-over model:
236
237 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
238
239 (Where f is the fractional coverage.)
240
241 Next we show that canTweakAlphaForCoverage() is true by proving the
242 following relationship:
243
244 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
245
246 General case (f,Sa,Da != 0):
247
248 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
249 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da != 0, definition of blend()]
250 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
251 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
252 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
253 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
254 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
255 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
256 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
257
258 Corner cases (Sa=0, Da=0, and f=0):
259
260 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
261 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
262 = Dca
263 = blend(0, Dca, 0, Da) [definition of blend()]
264 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
265
266 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
267 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
268 = f * Sca [Da=0]
269 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
270 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
271
272 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
273 = Dca [f=0]
274 = blend(0, Dca, 0, Da) [definition of blend()]
275 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
276
277 == Alpha ==
278
279 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
280
281 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
282 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
283 = Sa + Da - Sa * Da
284
285 We use the same model for coverage modulation as we did with color:
286
287 Da'' = f * blend(Sa, Da) + (1-f) * Da
288
289 And show that canTweakAlphaForCoverage() is true by proving the following
290 relationship:
291
292 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
293
294
295 f * blend(Sa, Da) + (1-f) * Da
296 = f * (Sa + Da - Sa * Da) + (1-f) * Da
297 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
298 = f*Sa - f*Sa * Da + Da
299 = f*Sa + Da - f*Sa * Da
300 = blend(f*Sa, Da)
301 */
302
bsalomon7765a472015-07-08 11:26:37 -0700303 OptFlags flags = kNone_OptFlags;
ethannicholasde4166a2015-11-30 08:57:38 -0800304 if (optimizations.fColorPOI.allStagesMultiplyInput()) {
cdalton32117702015-05-11 11:21:23 -0700305 flags |= kCanTweakAlphaForCoverage_OptFlag;
cdalton8917d622015-05-06 13:40:21 -0700306 }
ethannicholasde4166a2015-11-30 08:57:38 -0800307 if (this->hasHWBlendEquation() && optimizations.fCoveragePOI.isSolidWhite()) {
cdalton32117702015-05-11 11:21:23 -0700308 flags |= kIgnoreCoverage_OptFlag;
cdalton8917d622015-05-06 13:40:21 -0700309 }
cdalton8917d622015-05-06 13:40:21 -0700310 return flags;
311}
312
bsalomoncb02b382015-08-12 11:14:50 -0700313GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const {
cdalton8917d622015-05-06 13:40:21 -0700314 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
bsalomoncb02b382015-08-12 11:14:50 -0700315 return kBlend_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700316 }
bsalomoncb02b382015-08-12 11:14:50 -0700317 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700318}
319
320void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
321 if (this->hasHWBlendEquation()) {
322 blendInfo->fEquation = this->hwBlendEquation();
323 }
egdaniel54f0e9d2015-01-16 06:29:47 -0800324}
325
326///////////////////////////////////////////////////////////////////////////////
bsalomonae4738f2015-09-15 15:33:27 -0700327class CustomXPFactory : public GrXPFactory {
328public:
329 CustomXPFactory(SkXfermode::Mode mode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800330
bsalomonae4738f2015-09-15 15:33:27 -0700331 void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
332 GrXPFactory::InvariantBlendedColor*) const override;
333
334private:
335 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800336 const GrPipelineOptimizations& optimizations,
bsalomonae4738f2015-09-15 15:33:27 -0700337 bool hasMixedSamples,
338 const DstTexture*) const override;
339
ethannicholas22793252016-01-30 09:59:10 -0800340 bool onWillReadDstColor(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800341 const GrPipelineOptimizations& optimizations,
bsalomonae4738f2015-09-15 15:33:27 -0700342 bool hasMixedSamples) const override;
343
344 bool onIsEqual(const GrXPFactory& xpfBase) const override {
345 const CustomXPFactory& xpf = xpfBase.cast<CustomXPFactory>();
346 return fMode == xpf.fMode;
347 }
348
349 GR_DECLARE_XP_FACTORY_TEST;
350
351 SkXfermode::Mode fMode;
352 GrBlendEquation fHWBlendEquation;
353
354 typedef GrXPFactory INHERITED;
355};
356
357CustomXPFactory::CustomXPFactory(SkXfermode::Mode mode)
cdaltonf0df1892015-06-05 13:26:20 -0700358 : fMode(mode),
359 fHWBlendEquation(hw_blend_equation(mode)) {
360 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
bsalomonae4738f2015-09-15 15:33:27 -0700361 this->initClassID<CustomXPFactory>();
egdaniel54f0e9d2015-01-16 06:29:47 -0800362}
363
bsalomonae4738f2015-09-15 15:33:27 -0700364GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800365 const GrPipelineOptimizations& opt,
bsalomonae4738f2015-09-15 15:33:27 -0700366 bool hasMixedSamples,
367 const DstTexture* dstTexture) const {
ethannicholas22793252016-01-30 09:59:10 -0800368 if (can_use_hw_blend_equation(fHWBlendEquation, opt, caps)) {
cdaltonf0df1892015-06-05 13:26:20 -0700369 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700370 return new CustomXP(fMode, fHWBlendEquation);
cdaltonf0df1892015-06-05 13:26:20 -0700371 }
halcanary385fe4d2015-08-26 13:07:48 -0700372 return new CustomXP(dstTexture, hasMixedSamples, fMode);
egdaniel41d4f092015-02-09 07:51:00 -0800373}
374
ethannicholas22793252016-01-30 09:59:10 -0800375bool CustomXPFactory::onWillReadDstColor(const GrCaps& caps,
376 const GrPipelineOptimizations& optimizations,
377 bool hasMixedSamples) const {
378 return !can_use_hw_blend_equation(fHWBlendEquation, optimizations, caps);
cdalton8917d622015-05-06 13:40:21 -0700379}
egdaniel41d4f092015-02-09 07:51:00 -0800380
bsalomonae4738f2015-09-15 15:33:27 -0700381void CustomXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
382 InvariantBlendedColor* blendedColor) const {
cdalton1fa45722015-06-02 10:43:39 -0700383 blendedColor->fWillBlendWithDst = true;
384 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
egdaniel54f0e9d2015-01-16 06:29:47 -0800385}
386
bsalomonae4738f2015-09-15 15:33:27 -0700387GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
388const GrXPFactory* CustomXPFactory::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700389 int mode = d->fRandom->nextRangeU(SkXfermode::kLastCoeffMode + 1,
390 SkXfermode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800391
bsalomonae4738f2015-09-15 15:33:27 -0700392 return new CustomXPFactory(static_cast<SkXfermode::Mode>(mode));
egdaniel0063a9b2015-01-15 10:52:32 -0800393}
394
bsalomonae4738f2015-09-15 15:33:27 -0700395///////////////////////////////////////////////////////////////////////////////
396
egdanielf2342722015-11-20 15:12:59 -0800397GrXPFactory* GrCustomXfermode::CreateXPFactory(SkXfermode::Mode mode) {
bsalomonae4738f2015-09-15 15:33:27 -0700398 if (!GrCustomXfermode::IsSupportedMode(mode)) {
399 return nullptr;
400 } else {
401 return new CustomXPFactory(mode);
402 }
403}