blob: 070fa2ffe0d20cd9fee6fca98626a642e6dd80d7 [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"
Brian Salomon94efbf52016-11-29 13:43:05 -050016#include "GrShaderCaps.h"
egdaniel0063a9b2015-01-15 10:52:32 -080017#include "GrTexture.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLBlend.h"
egdaniel64c47282015-11-13 06:54:19 -080019#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080020#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070021#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080022#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080023#include "glsl/GrGLSLXferProcessor.h"
egdaniel0063a9b2015-01-15 10:52:32 -080024
Mike Reed7d954ad2016-10-28 15:42:34 -040025bool GrCustomXfermode::IsSupportedMode(SkBlendMode mode) {
26 return (int)mode > (int)SkBlendMode::kLastCoeffMode &&
27 (int)mode <= (int)SkBlendMode::kLastMode;
egdaniel0063a9b2015-01-15 10:52:32 -080028}
29
30///////////////////////////////////////////////////////////////////////////////
31// Static helpers
32///////////////////////////////////////////////////////////////////////////////
33
Brian Salomona1633922017-01-09 11:46:10 -050034static constexpr GrBlendEquation hw_blend_equation(SkBlendMode mode) {
35// In C++14 this could be a constexpr int variable.
36#define EQ_OFFSET (kOverlay_GrBlendEquation - (int)SkBlendMode::kOverlay)
37 GR_STATIC_ASSERT(kOverlay_GrBlendEquation == (int)SkBlendMode::kOverlay + EQ_OFFSET);
38 GR_STATIC_ASSERT(kDarken_GrBlendEquation == (int)SkBlendMode::kDarken + EQ_OFFSET);
39 GR_STATIC_ASSERT(kLighten_GrBlendEquation == (int)SkBlendMode::kLighten + EQ_OFFSET);
40 GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == (int)SkBlendMode::kColorDodge + EQ_OFFSET);
41 GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == (int)SkBlendMode::kColorBurn + EQ_OFFSET);
42 GR_STATIC_ASSERT(kHardLight_GrBlendEquation == (int)SkBlendMode::kHardLight + EQ_OFFSET);
43 GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == (int)SkBlendMode::kSoftLight + EQ_OFFSET);
44 GR_STATIC_ASSERT(kDifference_GrBlendEquation == (int)SkBlendMode::kDifference + EQ_OFFSET);
45 GR_STATIC_ASSERT(kExclusion_GrBlendEquation == (int)SkBlendMode::kExclusion + EQ_OFFSET);
46 GR_STATIC_ASSERT(kMultiply_GrBlendEquation == (int)SkBlendMode::kMultiply + EQ_OFFSET);
47 GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == (int)SkBlendMode::kHue + EQ_OFFSET);
48 GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == (int)SkBlendMode::kSaturation + EQ_OFFSET);
49 GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == (int)SkBlendMode::kColor + EQ_OFFSET);
50 GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == (int)SkBlendMode::kLuminosity + EQ_OFFSET);
51 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET);
52 return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET);
53#undef EQ_OFFSET
cdalton8917d622015-05-06 13:40:21 -070054}
55
cdalton1dd05422015-06-12 09:01:18 -070056static bool can_use_hw_blend_equation(GrBlendEquation equation,
Brian Salomon92aee3d2016-12-21 09:20:25 -050057 const GrPipelineAnalysis& analysis,
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 Salomon92aee3d2016-12-21 09:20:25 -050062 if (analysis.fUsesPLSDstRead) {
ethannicholas22793252016-01-30 09:59:10 -080063 return false;
64 }
Brian Salomonaab259e2017-01-17 10:44:34 -050065 if (analysis.fCoveragePOI.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 Salomon92aee3d2016-12-21 09:20:25 -0500303 if (analysis.fColorPOI.allStagesMultiplyInput()) {
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 -0700334 void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
335 GrXPFactory::InvariantBlendedColor*) const override;
336
337private:
338 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500339 const GrPipelineAnalysis&,
bsalomonae4738f2015-09-15 15:33:27 -0700340 bool hasMixedSamples,
341 const DstTexture*) const override;
342
Brian Salomon92aee3d2016-12-21 09:20:25 -0500343 bool onWillReadDstColor(const GrCaps&, const GrPipelineAnalysis&) const override;
bsalomonae4738f2015-09-15 15:33:27 -0700344
bsalomonae4738f2015-09-15 15:33:27 -0700345
346 GR_DECLARE_XP_FACTORY_TEST;
347
Mike Reed7d954ad2016-10-28 15:42:34 -0400348 SkBlendMode fMode;
349 GrBlendEquation fHWBlendEquation;
bsalomonae4738f2015-09-15 15:33:27 -0700350
351 typedef GrXPFactory INHERITED;
352};
Brian Salomona1633922017-01-09 11:46:10 -0500353#if defined(__GNUC__) || defined(__clang)
354#pragma GCC diagnostic pop
355#endif
egdaniel54f0e9d2015-01-16 06:29:47 -0800356
bsalomonae4738f2015-09-15 15:33:27 -0700357GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500358 const GrPipelineAnalysis& analysis,
bsalomonae4738f2015-09-15 15:33:27 -0700359 bool hasMixedSamples,
360 const DstTexture* dstTexture) const {
Brian Salomona1633922017-01-09 11:46:10 -0500361 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon92aee3d2016-12-21 09:20:25 -0500362 if (can_use_hw_blend_equation(fHWBlendEquation, analysis, caps)) {
cdaltonf0df1892015-06-05 13:26:20 -0700363 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700364 return new CustomXP(fMode, fHWBlendEquation);
cdaltonf0df1892015-06-05 13:26:20 -0700365 }
halcanary385fe4d2015-08-26 13:07:48 -0700366 return new CustomXP(dstTexture, hasMixedSamples, fMode);
egdaniel41d4f092015-02-09 07:51:00 -0800367}
368
ethannicholas22793252016-01-30 09:59:10 -0800369bool CustomXPFactory::onWillReadDstColor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500370 const GrPipelineAnalysis& analysis) const {
371 return !can_use_hw_blend_equation(fHWBlendEquation, analysis, caps);
cdalton8917d622015-05-06 13:40:21 -0700372}
egdaniel41d4f092015-02-09 07:51:00 -0800373
bsalomonae4738f2015-09-15 15:33:27 -0700374void CustomXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
375 InvariantBlendedColor* blendedColor) const {
cdalton1fa45722015-06-02 10:43:39 -0700376 blendedColor->fWillBlendWithDst = true;
377 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
egdaniel54f0e9d2015-01-16 06:29:47 -0800378}
379
bsalomonae4738f2015-09-15 15:33:27 -0700380GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Brian Salomona1633922017-01-09 11:46:10 -0500381const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500382 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
383 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800384
Brian Salomona1633922017-01-09 11:46:10 -0500385 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800386}
387
bsalomonae4738f2015-09-15 15:33:27 -0700388///////////////////////////////////////////////////////////////////////////////
389
Brian Salomona1633922017-01-09 11:46:10 -0500390const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
391 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
392 // null.
393#ifdef SK_BUILD_FOR_WIN
394#define _CONSTEXPR_
395#else
396#define _CONSTEXPR_ constexpr
397#endif
398 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
399 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
400 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
401 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
402 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
403 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
404 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
405 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
406 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
407 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
408 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
409 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
410 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
411 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
412#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500413 switch (mode) {
414 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500415 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500416 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500417 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500418 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500419 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500420 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500421 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500422 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500423 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500424 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500425 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500426 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500427 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500428 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500429 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500430 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500431 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500432 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500433 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500434 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500435 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500436 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500437 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500438 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500439 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500440 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500441 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500442 default:
443 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
444 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700445 }
446}