blob: bc11d7e533212fc0174f3794e794bb058c7115ad [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"
ethannicholasde4166a2015-11-30 08:57:38 -080013#include "GrPipeline.h"
egdaniel0063a9b2015-01-15 10:52:32 -080014#include "GrProcessor.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "GrShaderCaps.h"
egdaniel0063a9b2015-01-15 10:52:32 -080016#include "GrTexture.h"
egdaniel64c47282015-11-13 06:54:19 -080017#include "glsl/GrGLSLBlend.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080019#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070020#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080021#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080022#include "glsl/GrGLSLXferProcessor.h"
egdaniel0063a9b2015-01-15 10:52:32 -080023
Mike Reed7d954ad2016-10-28 15:42:34 -040024bool GrCustomXfermode::IsSupportedMode(SkBlendMode mode) {
25 return (int)mode > (int)SkBlendMode::kLastCoeffMode &&
26 (int)mode <= (int)SkBlendMode::kLastMode;
egdaniel0063a9b2015-01-15 10:52:32 -080027}
28
29///////////////////////////////////////////////////////////////////////////////
30// Static helpers
31///////////////////////////////////////////////////////////////////////////////
32
Brian Salomona1633922017-01-09 11:46:10 -050033static constexpr GrBlendEquation hw_blend_equation(SkBlendMode mode) {
34// In C++14 this could be a constexpr int variable.
35#define EQ_OFFSET (kOverlay_GrBlendEquation - (int)SkBlendMode::kOverlay)
36 GR_STATIC_ASSERT(kOverlay_GrBlendEquation == (int)SkBlendMode::kOverlay + EQ_OFFSET);
37 GR_STATIC_ASSERT(kDarken_GrBlendEquation == (int)SkBlendMode::kDarken + EQ_OFFSET);
38 GR_STATIC_ASSERT(kLighten_GrBlendEquation == (int)SkBlendMode::kLighten + EQ_OFFSET);
39 GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == (int)SkBlendMode::kColorDodge + EQ_OFFSET);
40 GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == (int)SkBlendMode::kColorBurn + EQ_OFFSET);
41 GR_STATIC_ASSERT(kHardLight_GrBlendEquation == (int)SkBlendMode::kHardLight + EQ_OFFSET);
42 GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == (int)SkBlendMode::kSoftLight + EQ_OFFSET);
43 GR_STATIC_ASSERT(kDifference_GrBlendEquation == (int)SkBlendMode::kDifference + EQ_OFFSET);
44 GR_STATIC_ASSERT(kExclusion_GrBlendEquation == (int)SkBlendMode::kExclusion + EQ_OFFSET);
45 GR_STATIC_ASSERT(kMultiply_GrBlendEquation == (int)SkBlendMode::kMultiply + EQ_OFFSET);
46 GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == (int)SkBlendMode::kHue + EQ_OFFSET);
47 GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == (int)SkBlendMode::kSaturation + EQ_OFFSET);
48 GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == (int)SkBlendMode::kColor + EQ_OFFSET);
49 GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == (int)SkBlendMode::kLuminosity + EQ_OFFSET);
50 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET);
51 return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET);
52#undef EQ_OFFSET
cdalton8917d622015-05-06 13:40:21 -070053}
54
cdalton1dd05422015-06-12 09:01:18 -070055static bool can_use_hw_blend_equation(GrBlendEquation equation,
Brian Salomon5be6c952017-01-20 19:06:29 +000056 bool usePLSRead,
57 bool isLCDCoverage,
cdalton1dd05422015-06-12 09:01:18 -070058 const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070059 if (!caps.advancedBlendEquationSupport()) {
60 return false;
61 }
Brian Salomon5be6c952017-01-20 19:06:29 +000062 if (usePLSRead) {
ethannicholas22793252016-01-30 09:59:10 -080063 return false;
64 }
Brian Salomon5be6c952017-01-20 19:06:29 +000065 if (isLCDCoverage) {
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:
Mike Reed7d954ad2016-10-28 15:42:34 -040080 CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation)
cdaltonf0df1892015-06-05 13:26:20 -070081 : fMode(mode),
82 fHWBlendEquation(hwBlendEquation) {
83 this->initClassID<CustomXP>();
egdaniel41d4f092015-02-09 07:51:00 -080084 }
85
Mike Reed7d954ad2016-10-28 15:42:34 -040086 CustomXP(const DstTexture* dstTexture, bool hasMixedSamples, SkBlendMode mode)
cdalton86ae0a92015-06-08 15:11:04 -070087 : 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
Mike Reed7d954ad2016-10-28 15:42:34 -040097 SkBlendMode 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:
Brian Salomon92aee3d2016-12-21 09:20:25 -0500106 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&,
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
Brian Salomon94efbf52016-11-29 13:43:05 -0500111 void onGetGLSLProcessorKey(const GrShaderCaps& 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
Mike Reed7d954ad2016-10-28 15:42:34 -0400119 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700120 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
Brian Salomon94efbf52016-11-29 13:43:05 -0500132 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
133 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700134 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700135 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700136 if (xp.hasHWBlendEquation()) {
137 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700138 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500139 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700140 }
141 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400142 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700143 }
bsalomon50785a32015-02-06 07:02:37 -0800144 b->add32(key);
145 }
146
147private:
cdaltonedbb31f2015-06-08 12:14:44 -0700148 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700149 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700150 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800151
egdaniel4ca2e602015-11-18 08:01:26 -0800152 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
153 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700154
cdalton86ae0a92015-06-08 15:11:04 -0700155 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
156 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500157 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
158 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800159 }
160
egdaniel7ea439b2015-12-03 09:20:44 -0800161 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
162 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800163 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800164 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800165 const char* dstColor,
166 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800167 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800168 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700169 const CustomXP& xp = proc.cast<CustomXP>();
170 SkASSERT(!xp.hasHWBlendEquation());
171
egdaniel4ca2e602015-11-18 08:01:26 -0800172 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800173
174 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800175 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
176 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700177 }
178
egdaniel018fb622015-10-28 07:26:40 -0700179 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800180
egdanielfa4cc8b2015-11-13 08:34:52 -0800181 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800182};
183
184///////////////////////////////////////////////////////////////////////////////
185
Brian Salomon94efbf52016-11-29 13:43:05 -0500186void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800187 GLCustomXP::GenKey(*this, caps, b);
188}
189
egdaniel57d3b032015-11-13 11:57:27 -0800190GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700191 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700192 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800193}
194
egdaniel41d4f092015-02-09 07:51:00 -0800195bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
196 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700197 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800198}
199
Brian Salomon92aee3d2016-12-21 09:20:25 -0500200GrXferProcessor::OptFlags CustomXP::onGetOptimizations(const GrPipelineAnalysis& analysis,
egdaniel54f0e9d2015-01-16 06:29:47 -0800201 bool doesStencilWrite,
202 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800203 const GrCaps& caps) const {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500204 /*
205 Most the optimizations we do here are based on tweaking alpha for coverage.
cdalton8917d622015-05-06 13:40:21 -0700206
Brian Salomon92aee3d2016-12-21 09:20:25 -0500207 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700208
Brian Salomon92aee3d2016-12-21 09:20:25 -0500209 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
210 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700211
Brian Salomon92aee3d2016-12-21 09:20:25 -0500212 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
213 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
214 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700215
Brian Salomon92aee3d2016-12-21 09:20:25 -0500216 For every blend mode supported by this class, i.e. the "advanced" blend
217 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700218
Brian Salomon92aee3d2016-12-21 09:20:25 -0500219 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
220 coverage.
cdalton8917d622015-05-06 13:40:21 -0700221
222
Brian Salomon92aee3d2016-12-21 09:20:25 -0500223 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700224
Brian Salomon92aee3d2016-12-21 09:20:25 -0500225 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
226 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700227
Brian Salomon92aee3d2016-12-21 09:20:25 -0500228 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
229 Sca : if Da == 0,
230 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
231 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700232
Brian Salomon92aee3d2016-12-21 09:20:25 -0500233 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700234
Brian Salomon92aee3d2016-12-21 09:20:25 -0500235 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700236
Brian Salomon92aee3d2016-12-21 09:20:25 -0500237 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700238
Brian Salomon92aee3d2016-12-21 09:20:25 -0500239 Next we show that canTweakAlphaForCoverage() is true by proving the
240 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700241
Brian Salomon92aee3d2016-12-21 09:20:25 -0500242 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700243
Brian Salomon92aee3d2016-12-21 09:20:25 -0500244 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700245
Brian Salomon92aee3d2016-12-21 09:20:25 -0500246 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
247 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
248 0, definition of blend()]
249 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
250 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
251 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
252 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
253 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
254 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
255 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700256
Brian Salomon92aee3d2016-12-21 09:20:25 -0500257 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700258
Brian Salomon92aee3d2016-12-21 09:20:25 -0500259 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
260 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
261 = Dca
262 = blend(0, Dca, 0, Da) [definition of blend()]
263 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700264
Brian Salomon92aee3d2016-12-21 09:20:25 -0500265 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
266 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
267 = f * Sca [Da=0]
268 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
269 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700270
Brian Salomon92aee3d2016-12-21 09:20:25 -0500271 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
272 = Dca [f=0]
273 = blend(0, Dca, 0, Da) [definition of blend()]
274 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700275
Brian Salomon92aee3d2016-12-21 09:20:25 -0500276 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700277
Brian Salomon92aee3d2016-12-21 09:20:25 -0500278 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700279
Brian Salomon92aee3d2016-12-21 09:20:25 -0500280 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
281 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
282 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700283
Brian Salomon92aee3d2016-12-21 09:20:25 -0500284 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700285
Brian Salomon92aee3d2016-12-21 09:20:25 -0500286 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700287
Brian Salomon92aee3d2016-12-21 09:20:25 -0500288 And show that canTweakAlphaForCoverage() is true by proving the following
289 relationship:
cdalton8917d622015-05-06 13:40:21 -0700290
Brian Salomon92aee3d2016-12-21 09:20:25 -0500291 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700292
293
Brian Salomon92aee3d2016-12-21 09:20:25 -0500294 f * blend(Sa, Da) + (1-f) * Da
295 = f * (Sa + Da - Sa * Da) + (1-f) * Da
296 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
297 = f*Sa - f*Sa * Da + Da
298 = f*Sa + Da - f*Sa * Da
299 = blend(f*Sa, Da)
300 */
cdalton8917d622015-05-06 13:40:21 -0700301
bsalomon7765a472015-07-08 11:26:37 -0700302 OptFlags flags = kNone_OptFlags;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500303 if (analysis.fColorPOI.allProcessorsModulateByPremul()) {
cdalton32117702015-05-11 11:21:23 -0700304 flags |= kCanTweakAlphaForCoverage_OptFlag;
cdalton8917d622015-05-06 13:40:21 -0700305 }
cdalton8917d622015-05-06 13:40:21 -0700306 return flags;
307}
308
bsalomoncb02b382015-08-12 11:14:50 -0700309GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const {
cdalton8917d622015-05-06 13:40:21 -0700310 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
bsalomoncb02b382015-08-12 11:14:50 -0700311 return kBlend_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700312 }
bsalomoncb02b382015-08-12 11:14:50 -0700313 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700314}
315
316void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
317 if (this->hasHWBlendEquation()) {
318 blendInfo->fEquation = this->hwBlendEquation();
319 }
egdaniel54f0e9d2015-01-16 06:29:47 -0800320}
321
322///////////////////////////////////////////////////////////////////////////////
Brian Salomona1633922017-01-09 11:46:10 -0500323
324// See the comment above GrXPFactory's definition about this warning suppression.
325#if defined(__GNUC__) || defined(__clang)
326#pragma GCC diagnostic push
327#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
328#endif
bsalomonae4738f2015-09-15 15:33:27 -0700329class CustomXPFactory : public GrXPFactory {
330public:
Brian Salomona1633922017-01-09 11:46:10 -0500331 constexpr CustomXPFactory(SkBlendMode mode)
332 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800333
bsalomonae4738f2015-09-15 15:33:27 -0700334private:
335 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500336 const GrPipelineAnalysis&,
bsalomonae4738f2015-09-15 15:33:27 -0700337 bool hasMixedSamples,
338 const DstTexture*) const override;
339
Brian Salomon9a514982017-02-14 10:28:22 -0500340 bool isPreCoverageBlendedColorConstant(const GrProcOptInfo& colorInput,
341 GrColor* color) const override {
342 return false;
343 }
344
345 bool willReadsDst(const GrProcOptInfo&, const GrProcOptInfo&) const override { return true; }
346
347 bool willReadDstInShader(const GrCaps&, ColorType, CoverageType) const override;
bsalomonae4738f2015-09-15 15:33:27 -0700348
349 GR_DECLARE_XP_FACTORY_TEST;
350
Mike Reed7d954ad2016-10-28 15:42:34 -0400351 SkBlendMode fMode;
352 GrBlendEquation fHWBlendEquation;
bsalomonae4738f2015-09-15 15:33:27 -0700353
354 typedef GrXPFactory INHERITED;
355};
Brian Salomona1633922017-01-09 11:46:10 -0500356#if defined(__GNUC__) || defined(__clang)
357#pragma GCC diagnostic pop
358#endif
egdaniel54f0e9d2015-01-16 06:29:47 -0800359
bsalomonae4738f2015-09-15 15:33:27 -0700360GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500361 const GrPipelineAnalysis& analysis,
bsalomonae4738f2015-09-15 15:33:27 -0700362 bool hasMixedSamples,
363 const DstTexture* dstTexture) const {
Brian Salomona1633922017-01-09 11:46:10 -0500364 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon5be6c952017-01-20 19:06:29 +0000365 if (can_use_hw_blend_equation(fHWBlendEquation, analysis.fUsesPLSDstRead,
366 analysis.fCoveragePOI.isLCDCoverage(), caps)) {
cdaltonf0df1892015-06-05 13:26:20 -0700367 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700368 return new CustomXP(fMode, fHWBlendEquation);
cdaltonf0df1892015-06-05 13:26:20 -0700369 }
halcanary385fe4d2015-08-26 13:07:48 -0700370 return new CustomXP(dstTexture, hasMixedSamples, fMode);
egdaniel41d4f092015-02-09 07:51:00 -0800371}
372
Brian Salomon9a514982017-02-14 10:28:22 -0500373bool CustomXPFactory::willReadDstInShader(const GrCaps& caps, ColorType colorType,
374 CoverageType coverageType) const {
Brian Salomon5be6c952017-01-20 19:06:29 +0000375 // This should not be called if we're using PLS dst read.
376 static constexpr bool kUsesPLSRead = false;
377 return !can_use_hw_blend_equation(fHWBlendEquation, kUsesPLSRead,
378 CoverageType::kLCD == coverageType, caps);
cdalton8917d622015-05-06 13:40:21 -0700379}
egdaniel41d4f092015-02-09 07:51:00 -0800380
bsalomonae4738f2015-09-15 15:33:27 -0700381GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500382#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500383const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500384 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
385 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800386
Brian Salomona1633922017-01-09 11:46:10 -0500387 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800388}
Hal Canary6f6961e2017-01-31 13:50:44 -0500389#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800390
bsalomonae4738f2015-09-15 15:33:27 -0700391///////////////////////////////////////////////////////////////////////////////
392
Brian Salomona1633922017-01-09 11:46:10 -0500393const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
394 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
395 // null.
396#ifdef SK_BUILD_FOR_WIN
397#define _CONSTEXPR_
398#else
399#define _CONSTEXPR_ constexpr
400#endif
401 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
402 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
403 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
404 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
405 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
406 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
407 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
408 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
409 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
410 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
411 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
412 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
413 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
414 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
415#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500416 switch (mode) {
417 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500418 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500419 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500420 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500421 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500422 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500423 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500424 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500425 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500426 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500427 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500428 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500429 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500430 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500431 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500432 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500433 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500434 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500435 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500436 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500437 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500438 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500439 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500440 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500441 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500442 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500443 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500444 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500445 default:
446 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
447 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700448 }
449}