blob: 31c42ebfd79a99af376be8d18a63d21adea65e10 [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
Robert Phillips296b1cc2017-03-15 10:42:12 -040010#include "GrCaps.h"
egdaniel0063a9b2015-01-15 10:52:32 -080011#include "GrCoordTransform.h"
egdaniel0063a9b2015-01-15 10:52:32 -080012#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
Brian Salomon42c456f2017-03-06 11:29:48 -050055static bool can_use_hw_blend_equation(GrBlendEquation equation, bool isLCDCoverage,
cdalton1dd05422015-06-12 09:01:18 -070056 const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070057 if (!caps.advancedBlendEquationSupport()) {
58 return false;
59 }
Brian Salomon5be6c952017-01-20 19:06:29 +000060 if (isLCDCoverage) {
cdaltonf0df1892015-06-05 13:26:20 -070061 return false; // LCD coverage must be applied after the blend equation.
62 }
cdalton1dd05422015-06-12 09:01:18 -070063 if (caps.canUseAdvancedBlendEquation(equation)) {
64 return false;
65 }
cdaltonf0df1892015-06-05 13:26:20 -070066 return true;
67}
68
egdaniel54f0e9d2015-01-16 06:29:47 -080069///////////////////////////////////////////////////////////////////////////////
70// Xfer Processor
71///////////////////////////////////////////////////////////////////////////////
72
egdaniel41d4f092015-02-09 07:51:00 -080073class CustomXP : public GrXferProcessor {
74public:
Mike Reed7d954ad2016-10-28 15:42:34 -040075 CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation)
cdaltonf0df1892015-06-05 13:26:20 -070076 : fMode(mode),
77 fHWBlendEquation(hwBlendEquation) {
78 this->initClassID<CustomXP>();
egdaniel41d4f092015-02-09 07:51:00 -080079 }
80
Mike Reed7d954ad2016-10-28 15:42:34 -040081 CustomXP(const DstTexture* dstTexture, bool hasMixedSamples, SkBlendMode mode)
cdalton86ae0a92015-06-08 15:11:04 -070082 : INHERITED(dstTexture, true, hasMixedSamples),
cdaltonf0df1892015-06-05 13:26:20 -070083 fMode(mode),
84 fHWBlendEquation(static_cast<GrBlendEquation>(-1)) {
85 this->initClassID<CustomXP>();
86 }
egdaniel41d4f092015-02-09 07:51:00 -080087
mtklein36352bf2015-03-25 18:17:31 -070088 const char* name() const override { return "Custom Xfermode"; }
egdaniel41d4f092015-02-09 07:51:00 -080089
egdaniel57d3b032015-11-13 11:57:27 -080090 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -080091
Mike Reed7d954ad2016-10-28 15:42:34 -040092 SkBlendMode mode() const { return fMode; }
bsalomonf7cc8772015-05-11 11:21:14 -070093 bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); }
cdalton8917d622015-05-06 13:40:21 -070094
95 GrBlendEquation hwBlendEquation() const {
96 SkASSERT(this->hasHWBlendEquation());
97 return fHWBlendEquation;
98 }
egdaniel41d4f092015-02-09 07:51:00 -080099
100private:
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500101 GrXferProcessor::OptFlags onGetOptimizations(const FragmentProcessorAnalysis&) const override;
egdanielc19cdc22015-05-10 08:45:18 -0700102
Brian Salomon94efbf52016-11-29 13:43:05 -0500103 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800104
bsalomoncb02b382015-08-12 11:14:50 -0700105 GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const override;
cdalton8917d622015-05-06 13:40:21 -0700106
107 void onGetBlendInfo(BlendInfo*) const override;
108
mtklein36352bf2015-03-25 18:17:31 -0700109 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800110
Mike Reed7d954ad2016-10-28 15:42:34 -0400111 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700112 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800113
114 typedef GrXferProcessor INHERITED;
115};
116
117///////////////////////////////////////////////////////////////////////////////
118
egdanielfa4cc8b2015-11-13 08:34:52 -0800119class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800120public:
121 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700122 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800123
Brian Salomon94efbf52016-11-29 13:43:05 -0500124 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
125 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700126 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700127 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700128 if (xp.hasHWBlendEquation()) {
129 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700130 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500131 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700132 }
133 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400134 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700135 }
bsalomon50785a32015-02-06 07:02:37 -0800136 b->add32(key);
137 }
138
139private:
cdaltonedbb31f2015-06-08 12:14:44 -0700140 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700141 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700142 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800143
egdaniel4ca2e602015-11-18 08:01:26 -0800144 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
145 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700146
cdalton86ae0a92015-06-08 15:11:04 -0700147 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
148 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500149 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
150 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800151 }
152
egdaniel7ea439b2015-12-03 09:20:44 -0800153 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
154 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800155 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800156 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800157 const char* dstColor,
158 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800159 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800160 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700161 const CustomXP& xp = proc.cast<CustomXP>();
162 SkASSERT(!xp.hasHWBlendEquation());
163
egdaniel4ca2e602015-11-18 08:01:26 -0800164 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800165
166 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800167 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
168 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700169 }
170
egdaniel018fb622015-10-28 07:26:40 -0700171 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800172
egdanielfa4cc8b2015-11-13 08:34:52 -0800173 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800174};
175
176///////////////////////////////////////////////////////////////////////////////
177
Brian Salomon94efbf52016-11-29 13:43:05 -0500178void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800179 GLCustomXP::GenKey(*this, caps, b);
180}
181
egdaniel57d3b032015-11-13 11:57:27 -0800182GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700183 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700184 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800185}
186
egdaniel41d4f092015-02-09 07:51:00 -0800187bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
188 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700189 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800190}
191
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500192GrXferProcessor::OptFlags CustomXP::onGetOptimizations(
193 const FragmentProcessorAnalysis& analysis) const {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500194 /*
195 Most the optimizations we do here are based on tweaking alpha for coverage.
cdalton8917d622015-05-06 13:40:21 -0700196
Brian Salomon92aee3d2016-12-21 09:20:25 -0500197 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700198
Brian Salomon92aee3d2016-12-21 09:20:25 -0500199 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
200 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700201
Brian Salomon92aee3d2016-12-21 09:20:25 -0500202 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
203 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
204 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700205
Brian Salomon92aee3d2016-12-21 09:20:25 -0500206 For every blend mode supported by this class, i.e. the "advanced" blend
207 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700208
Brian Salomon92aee3d2016-12-21 09:20:25 -0500209 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
210 coverage.
cdalton8917d622015-05-06 13:40:21 -0700211
212
Brian Salomon92aee3d2016-12-21 09:20:25 -0500213 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700214
Brian Salomon92aee3d2016-12-21 09:20:25 -0500215 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
216 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700217
Brian Salomon92aee3d2016-12-21 09:20:25 -0500218 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
219 Sca : if Da == 0,
220 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
221 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700222
Brian Salomon92aee3d2016-12-21 09:20:25 -0500223 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700224
Brian Salomon92aee3d2016-12-21 09:20:25 -0500225 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700226
Brian Salomon92aee3d2016-12-21 09:20:25 -0500227 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700228
Brian Salomon92aee3d2016-12-21 09:20:25 -0500229 Next we show that canTweakAlphaForCoverage() is true by proving the
230 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700231
Brian Salomon92aee3d2016-12-21 09:20:25 -0500232 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700233
Brian Salomon92aee3d2016-12-21 09:20:25 -0500234 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700235
Brian Salomon92aee3d2016-12-21 09:20:25 -0500236 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
237 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
238 0, definition of blend()]
239 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
240 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
241 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
242 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
243 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
244 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
245 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700246
Brian Salomon92aee3d2016-12-21 09:20:25 -0500247 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700248
Brian Salomon92aee3d2016-12-21 09:20:25 -0500249 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
250 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
251 = Dca
252 = blend(0, Dca, 0, Da) [definition of blend()]
253 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700254
Brian Salomon92aee3d2016-12-21 09:20:25 -0500255 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
256 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
257 = f * Sca [Da=0]
258 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
259 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700260
Brian Salomon92aee3d2016-12-21 09:20:25 -0500261 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
262 = Dca [f=0]
263 = blend(0, Dca, 0, Da) [definition of blend()]
264 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700265
Brian Salomon92aee3d2016-12-21 09:20:25 -0500266 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700267
Brian Salomon92aee3d2016-12-21 09:20:25 -0500268 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700269
Brian Salomon92aee3d2016-12-21 09:20:25 -0500270 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
271 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
272 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700273
Brian Salomon92aee3d2016-12-21 09:20:25 -0500274 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700275
Brian Salomon92aee3d2016-12-21 09:20:25 -0500276 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700277
Brian Salomon92aee3d2016-12-21 09:20:25 -0500278 And show that canTweakAlphaForCoverage() is true by proving the following
279 relationship:
cdalton8917d622015-05-06 13:40:21 -0700280
Brian Salomon92aee3d2016-12-21 09:20:25 -0500281 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700282
283
Brian Salomon92aee3d2016-12-21 09:20:25 -0500284 f * blend(Sa, Da) + (1-f) * Da
285 = f * (Sa + Da - Sa * Da) + (1-f) * Da
286 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
287 = f*Sa - f*Sa * Da + Da
288 = f*Sa + Da - f*Sa * Da
289 = blend(f*Sa, Da)
290 */
cdalton8917d622015-05-06 13:40:21 -0700291
bsalomon7765a472015-07-08 11:26:37 -0700292 OptFlags flags = kNone_OptFlags;
Brian Salomon5298dc82017-02-22 11:52:03 -0500293 if (analysis.isCompatibleWithCoverageAsAlpha()) {
cdalton32117702015-05-11 11:21:23 -0700294 flags |= kCanTweakAlphaForCoverage_OptFlag;
cdalton8917d622015-05-06 13:40:21 -0700295 }
cdalton8917d622015-05-06 13:40:21 -0700296 return flags;
297}
298
bsalomoncb02b382015-08-12 11:14:50 -0700299GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const {
cdalton8917d622015-05-06 13:40:21 -0700300 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
bsalomoncb02b382015-08-12 11:14:50 -0700301 return kBlend_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700302 }
bsalomoncb02b382015-08-12 11:14:50 -0700303 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700304}
305
306void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
307 if (this->hasHWBlendEquation()) {
308 blendInfo->fEquation = this->hwBlendEquation();
309 }
egdaniel54f0e9d2015-01-16 06:29:47 -0800310}
311
312///////////////////////////////////////////////////////////////////////////////
Brian Salomona1633922017-01-09 11:46:10 -0500313
314// See the comment above GrXPFactory's definition about this warning suppression.
315#if defined(__GNUC__) || defined(__clang)
316#pragma GCC diagnostic push
317#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
318#endif
bsalomonae4738f2015-09-15 15:33:27 -0700319class CustomXPFactory : public GrXPFactory {
320public:
Brian Salomona1633922017-01-09 11:46:10 -0500321 constexpr CustomXPFactory(SkBlendMode mode)
322 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800323
bsalomonae4738f2015-09-15 15:33:27 -0700324private:
325 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
Brian Salomon5298dc82017-02-22 11:52:03 -0500326 const FragmentProcessorAnalysis&,
bsalomonae4738f2015-09-15 15:33:27 -0700327 bool hasMixedSamples,
328 const DstTexture*) const override;
329
Brian Salomon5298dc82017-02-22 11:52:03 -0500330 bool willReadsDst(const FragmentProcessorAnalysis&) const override { return true; }
Brian Salomon9a514982017-02-14 10:28:22 -0500331
Brian Salomon42c456f2017-03-06 11:29:48 -0500332 bool willReadDstInShader(const GrCaps&, const FragmentProcessorAnalysis&) const override;
bsalomonae4738f2015-09-15 15:33:27 -0700333
Brian Salomon780b41f2017-03-13 10:36:40 -0400334 bool compatibleWithCoverageAsAlpha(bool colorIsOpaque) const override { return true; }
335
bsalomonae4738f2015-09-15 15:33:27 -0700336 GR_DECLARE_XP_FACTORY_TEST;
337
Mike Reed7d954ad2016-10-28 15:42:34 -0400338 SkBlendMode fMode;
339 GrBlendEquation fHWBlendEquation;
bsalomonae4738f2015-09-15 15:33:27 -0700340
341 typedef GrXPFactory INHERITED;
342};
Brian Salomona1633922017-01-09 11:46:10 -0500343#if defined(__GNUC__) || defined(__clang)
344#pragma GCC diagnostic pop
345#endif
egdaniel54f0e9d2015-01-16 06:29:47 -0800346
bsalomonae4738f2015-09-15 15:33:27 -0700347GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps,
Brian Salomon5298dc82017-02-22 11:52:03 -0500348 const FragmentProcessorAnalysis& analysis,
bsalomonae4738f2015-09-15 15:33:27 -0700349 bool hasMixedSamples,
350 const DstTexture* dstTexture) const {
Brian Salomona1633922017-01-09 11:46:10 -0500351 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon42c456f2017-03-06 11:29:48 -0500352 if (can_use_hw_blend_equation(fHWBlendEquation, analysis.hasLCDCoverage(), caps)) {
cdaltonf0df1892015-06-05 13:26:20 -0700353 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700354 return new CustomXP(fMode, fHWBlendEquation);
cdaltonf0df1892015-06-05 13:26:20 -0700355 }
halcanary385fe4d2015-08-26 13:07:48 -0700356 return new CustomXP(dstTexture, hasMixedSamples, fMode);
egdaniel41d4f092015-02-09 07:51:00 -0800357}
358
Brian Salomon42c456f2017-03-06 11:29:48 -0500359bool CustomXPFactory::willReadDstInShader(const GrCaps& caps,
360 const FragmentProcessorAnalysis& analysis) const {
361 return !can_use_hw_blend_equation(fHWBlendEquation, analysis.hasLCDCoverage(), caps);
cdalton8917d622015-05-06 13:40:21 -0700362}
egdaniel41d4f092015-02-09 07:51:00 -0800363
bsalomonae4738f2015-09-15 15:33:27 -0700364GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500365#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500366const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500367 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
368 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800369
Brian Salomona1633922017-01-09 11:46:10 -0500370 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800371}
Hal Canary6f6961e2017-01-31 13:50:44 -0500372#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800373
bsalomonae4738f2015-09-15 15:33:27 -0700374///////////////////////////////////////////////////////////////////////////////
375
Brian Salomona1633922017-01-09 11:46:10 -0500376const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
377 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
378 // null.
379#ifdef SK_BUILD_FOR_WIN
380#define _CONSTEXPR_
381#else
382#define _CONSTEXPR_ constexpr
383#endif
384 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
385 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
386 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
387 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
388 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
389 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
390 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
391 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
392 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
393 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
394 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
395 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
396 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
397 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
398#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500399 switch (mode) {
400 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500401 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500402 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500403 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500404 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500405 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500406 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500407 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500408 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500409 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500410 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500411 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500412 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500413 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500414 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500415 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500416 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500417 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500418 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500419 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500420 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500421 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500422 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500423 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500424 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500425 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500426 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500427 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500428 default:
429 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
430 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700431 }
432}