blob: 5c352604c648f566e31d30a6b4b30974bb6c0bf4 [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"
egdaniel64c47282015-11-13 06:54:19 -080016#include "glsl/GrGLSLBlend.h"
egdaniel64c47282015-11-13 06:54:19 -080017#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080018#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070019#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080020#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080021#include "glsl/GrGLSLXferProcessor.h"
egdaniel0063a9b2015-01-15 10:52:32 -080022
Mike Reed7d954ad2016-10-28 15:42:34 -040023bool GrCustomXfermode::IsSupportedMode(SkBlendMode mode) {
24 return (int)mode > (int)SkBlendMode::kLastCoeffMode &&
25 (int)mode <= (int)SkBlendMode::kLastMode;
egdaniel0063a9b2015-01-15 10:52:32 -080026}
27
28///////////////////////////////////////////////////////////////////////////////
29// Static helpers
30///////////////////////////////////////////////////////////////////////////////
31
Brian Salomona1633922017-01-09 11:46:10 -050032static constexpr GrBlendEquation hw_blend_equation(SkBlendMode mode) {
33// In C++14 this could be a constexpr int variable.
34#define EQ_OFFSET (kOverlay_GrBlendEquation - (int)SkBlendMode::kOverlay)
35 GR_STATIC_ASSERT(kOverlay_GrBlendEquation == (int)SkBlendMode::kOverlay + EQ_OFFSET);
36 GR_STATIC_ASSERT(kDarken_GrBlendEquation == (int)SkBlendMode::kDarken + EQ_OFFSET);
37 GR_STATIC_ASSERT(kLighten_GrBlendEquation == (int)SkBlendMode::kLighten + EQ_OFFSET);
38 GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == (int)SkBlendMode::kColorDodge + EQ_OFFSET);
39 GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == (int)SkBlendMode::kColorBurn + EQ_OFFSET);
40 GR_STATIC_ASSERT(kHardLight_GrBlendEquation == (int)SkBlendMode::kHardLight + EQ_OFFSET);
41 GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == (int)SkBlendMode::kSoftLight + EQ_OFFSET);
42 GR_STATIC_ASSERT(kDifference_GrBlendEquation == (int)SkBlendMode::kDifference + EQ_OFFSET);
43 GR_STATIC_ASSERT(kExclusion_GrBlendEquation == (int)SkBlendMode::kExclusion + EQ_OFFSET);
44 GR_STATIC_ASSERT(kMultiply_GrBlendEquation == (int)SkBlendMode::kMultiply + EQ_OFFSET);
45 GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == (int)SkBlendMode::kHue + EQ_OFFSET);
46 GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == (int)SkBlendMode::kSaturation + EQ_OFFSET);
47 GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == (int)SkBlendMode::kColor + EQ_OFFSET);
48 GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == (int)SkBlendMode::kLuminosity + EQ_OFFSET);
Mike Kleinf2b35e42018-11-06 16:30:04 +000049 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET);
Brian Salomona1633922017-01-09 11:46:10 -050050 return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET);
51#undef EQ_OFFSET
cdalton8917d622015-05-06 13:40:21 -070052}
53
Brian Salomona811b122017-03-30 08:21:32 -040054static bool can_use_hw_blend_equation(GrBlendEquation equation,
55 GrProcessorAnalysisCoverage coverage, const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070056 if (!caps.advancedBlendEquationSupport()) {
57 return false;
58 }
Brian Salomona811b122017-03-30 08:21:32 -040059 if (GrProcessorAnalysisCoverage::kLCD == coverage) {
cdaltonf0df1892015-06-05 13:26:20 -070060 return false; // LCD coverage must be applied after the blend equation.
61 }
cdalton1dd05422015-06-12 09:01:18 -070062 if (caps.canUseAdvancedBlendEquation(equation)) {
63 return false;
64 }
cdaltonf0df1892015-06-05 13:26:20 -070065 return true;
66}
67
egdaniel54f0e9d2015-01-16 06:29:47 -080068///////////////////////////////////////////////////////////////////////////////
69// Xfer Processor
70///////////////////////////////////////////////////////////////////////////////
71
egdaniel41d4f092015-02-09 07:51:00 -080072class CustomXP : public GrXferProcessor {
73public:
Mike Reed7d954ad2016-10-28 15:42:34 -040074 CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation)
Ethan Nicholasabff9562017-10-09 10:54:08 -040075 : INHERITED(kCustomXP_ClassID)
76 , fMode(mode)
77 , fHWBlendEquation(hwBlendEquation) {}
egdaniel41d4f092015-02-09 07:51:00 -080078
Greg Daniel6ebe4b92017-05-19 10:56:46 -040079 CustomXP(bool hasMixedSamples, SkBlendMode mode, GrProcessorAnalysisCoverage coverage)
Ethan Nicholasabff9562017-10-09 10:54:08 -040080 : INHERITED(kCustomXP_ClassID, true, hasMixedSamples, coverage)
Brian Salomon18dfa982017-04-03 16:57:43 -040081 , fMode(mode)
Mike Kleinf2b35e42018-11-06 16:30:04 +000082 , fHWBlendEquation(static_cast<GrBlendEquation>(-1)) {
cdaltonf0df1892015-06-05 13:26:20 -070083 }
egdaniel41d4f092015-02-09 07:51:00 -080084
mtklein36352bf2015-03-25 18:17:31 -070085 const char* name() const override { return "Custom Xfermode"; }
egdaniel41d4f092015-02-09 07:51:00 -080086
egdaniel57d3b032015-11-13 11:57:27 -080087 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -080088
Mike Reed7d954ad2016-10-28 15:42:34 -040089 SkBlendMode mode() const { return fMode; }
Mike Kleinf2b35e42018-11-06 16:30:04 +000090 bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); }
cdalton8917d622015-05-06 13:40:21 -070091
92 GrBlendEquation hwBlendEquation() const {
93 SkASSERT(this->hasHWBlendEquation());
94 return fHWBlendEquation;
95 }
egdaniel41d4f092015-02-09 07:51:00 -080096
Brian Salomon18dfa982017-04-03 16:57:43 -040097 GrXferBarrierType xferBarrierType(const GrCaps&) const override;
98
egdaniel41d4f092015-02-09 07:51:00 -080099private:
Brian Salomon94efbf52016-11-29 13:43:05 -0500100 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800101
cdalton8917d622015-05-06 13:40:21 -0700102 void onGetBlendInfo(BlendInfo*) const override;
103
mtklein36352bf2015-03-25 18:17:31 -0700104 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800105
Mike Reed7d954ad2016-10-28 15:42:34 -0400106 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700107 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800108
109 typedef GrXferProcessor INHERITED;
110};
111
112///////////////////////////////////////////////////////////////////////////////
113
egdanielfa4cc8b2015-11-13 08:34:52 -0800114class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800115public:
116 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700117 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800118
Brian Salomon94efbf52016-11-29 13:43:05 -0500119 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
120 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700121 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700122 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700123 if (xp.hasHWBlendEquation()) {
124 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700125 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500126 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700127 }
128 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400129 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700130 }
bsalomon50785a32015-02-06 07:02:37 -0800131 b->add32(key);
132 }
133
134private:
cdaltonedbb31f2015-06-08 12:14:44 -0700135 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700136 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700137 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800138
egdaniel4ca2e602015-11-18 08:01:26 -0800139 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
140 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700141
cdalton86ae0a92015-06-08 15:11:04 -0700142 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
143 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500144 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
145 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800146 }
147
egdaniel7ea439b2015-12-03 09:20:44 -0800148 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
149 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800150 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800151 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800152 const char* dstColor,
153 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800154 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800155 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700156 const CustomXP& xp = proc.cast<CustomXP>();
157 SkASSERT(!xp.hasHWBlendEquation());
158
egdaniel4ca2e602015-11-18 08:01:26 -0800159 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800160
161 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800162 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
163 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700164 }
165
egdaniel018fb622015-10-28 07:26:40 -0700166 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800167
egdanielfa4cc8b2015-11-13 08:34:52 -0800168 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800169};
170
171///////////////////////////////////////////////////////////////////////////////
172
Brian Salomon94efbf52016-11-29 13:43:05 -0500173void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800174 GLCustomXP::GenKey(*this, caps, b);
175}
176
egdaniel57d3b032015-11-13 11:57:27 -0800177GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700178 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700179 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800180}
181
egdaniel41d4f092015-02-09 07:51:00 -0800182bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
183 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700184 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800185}
186
Brian Salomon18dfa982017-04-03 16:57:43 -0400187GrXferBarrierType CustomXP::xferBarrierType(const GrCaps& caps) const {
Brian Salomon31853842017-03-28 16:32:05 -0400188 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
189 return kBlend_GrXferBarrierType;
190 }
191 return kNone_GrXferBarrierType;
192}
cdalton8917d622015-05-06 13:40:21 -0700193
Brian Salomon31853842017-03-28 16:32:05 -0400194void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
195 if (this->hasHWBlendEquation()) {
196 blendInfo->fEquation = this->hwBlendEquation();
197 }
198}
199
200///////////////////////////////////////////////////////////////////////////////
201
202// See the comment above GrXPFactory's definition about this warning suppression.
Chris Dalton1ef80942017-12-04 12:01:30 -0700203#if defined(__GNUC__)
Brian Salomon31853842017-03-28 16:32:05 -0400204#pragma GCC diagnostic push
205#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
206#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700207#if defined(__clang__)
208#pragma clang diagnostic push
209#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
210#endif
Brian Salomon31853842017-03-28 16:32:05 -0400211class CustomXPFactory : public GrXPFactory {
212public:
213 constexpr CustomXPFactory(SkBlendMode mode)
214 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
215
216private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400217 sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
218 GrProcessorAnalysisCoverage,
219 bool hasMixedSamples,
Brian Osman532b3f92018-07-11 10:02:07 -0400220 const GrCaps&) const override;
Brian Salomon31853842017-03-28 16:32:05 -0400221
Brian Salomona811b122017-03-30 08:21:32 -0400222 AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
223 const GrProcessorAnalysisCoverage&,
Brian Osman532b3f92018-07-11 10:02:07 -0400224 const GrCaps&) const override;
Brian Salomon31853842017-03-28 16:32:05 -0400225
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400226 GR_DECLARE_XP_FACTORY_TEST
Brian Salomon31853842017-03-28 16:32:05 -0400227
228 SkBlendMode fMode;
229 GrBlendEquation fHWBlendEquation;
230
231 typedef GrXPFactory INHERITED;
232};
Chris Dalton1ef80942017-12-04 12:01:30 -0700233#if defined(__GNUC__)
Brian Salomon31853842017-03-28 16:32:05 -0400234#pragma GCC diagnostic pop
235#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700236#if defined(__clang__)
237#pragma clang diagnostic pop
238#endif
Brian Salomon31853842017-03-28 16:32:05 -0400239
Brian Salomond61c9d92017-04-10 10:54:25 -0400240sk_sp<const GrXferProcessor> CustomXPFactory::makeXferProcessor(
241 const GrProcessorAnalysisColor&,
242 GrProcessorAnalysisCoverage coverage,
243 bool hasMixedSamples,
Brian Osman532b3f92018-07-11 10:02:07 -0400244 const GrCaps& caps) const {
Brian Salomon31853842017-03-28 16:32:05 -0400245 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon1c6025c2017-03-29 14:25:04 -0400246 if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) {
Brian Salomona076d872017-04-04 15:17:03 -0400247 return sk_sp<GrXferProcessor>(new CustomXP(fMode, fHWBlendEquation));
Brian Salomon31853842017-03-28 16:32:05 -0400248 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400249 return sk_sp<GrXferProcessor>(new CustomXP(hasMixedSamples, fMode, coverage));
Brian Salomon31853842017-03-28 16:32:05 -0400250}
251
252GrXPFactory::AnalysisProperties CustomXPFactory::analysisProperties(
Brian Salomona811b122017-03-30 08:21:32 -0400253 const GrProcessorAnalysisColor&, const GrProcessorAnalysisCoverage& coverage,
Brian Osman532b3f92018-07-11 10:02:07 -0400254 const GrCaps& caps) const {
Brian Salomon31853842017-03-28 16:32:05 -0400255 /*
Brian Salomon92aee3d2016-12-21 09:20:25 -0500256 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700257
Brian Salomon92aee3d2016-12-21 09:20:25 -0500258 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
259 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700260
Brian Salomon92aee3d2016-12-21 09:20:25 -0500261 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
262 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
263 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700264
Brian Salomon92aee3d2016-12-21 09:20:25 -0500265 For every blend mode supported by this class, i.e. the "advanced" blend
266 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700267
Brian Salomon92aee3d2016-12-21 09:20:25 -0500268 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
269 coverage.
cdalton8917d622015-05-06 13:40:21 -0700270
271
Brian Salomon92aee3d2016-12-21 09:20:25 -0500272 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700273
Brian Salomon92aee3d2016-12-21 09:20:25 -0500274 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
275 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700276
Brian Salomon92aee3d2016-12-21 09:20:25 -0500277 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
278 Sca : if Da == 0,
279 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
280 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700281
Brian Salomon92aee3d2016-12-21 09:20:25 -0500282 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700283
Brian Salomon92aee3d2016-12-21 09:20:25 -0500284 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700285
Brian Salomon92aee3d2016-12-21 09:20:25 -0500286 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700287
Brian Salomon92aee3d2016-12-21 09:20:25 -0500288 Next we show that canTweakAlphaForCoverage() is true by proving the
289 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700290
Brian Salomon92aee3d2016-12-21 09:20:25 -0500291 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700292
Brian Salomon92aee3d2016-12-21 09:20:25 -0500293 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700294
Brian Salomon92aee3d2016-12-21 09:20:25 -0500295 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
296 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
297 0, definition of blend()]
298 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
299 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
300 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
301 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
302 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
303 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
304 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700305
Brian Salomon92aee3d2016-12-21 09:20:25 -0500306 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700307
Brian Salomon92aee3d2016-12-21 09:20:25 -0500308 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
309 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
310 = Dca
311 = blend(0, Dca, 0, Da) [definition of blend()]
312 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700313
Brian Salomon92aee3d2016-12-21 09:20:25 -0500314 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
315 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
316 = f * Sca [Da=0]
317 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
318 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700319
Brian Salomon92aee3d2016-12-21 09:20:25 -0500320 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
321 = Dca [f=0]
322 = blend(0, Dca, 0, Da) [definition of blend()]
323 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700324
Brian Salomon92aee3d2016-12-21 09:20:25 -0500325 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700326
Brian Salomon92aee3d2016-12-21 09:20:25 -0500327 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700328
Brian Salomon92aee3d2016-12-21 09:20:25 -0500329 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
330 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
331 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700332
Brian Salomon92aee3d2016-12-21 09:20:25 -0500333 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700334
Brian Salomon92aee3d2016-12-21 09:20:25 -0500335 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700336
Brian Salomon92aee3d2016-12-21 09:20:25 -0500337 And show that canTweakAlphaForCoverage() is true by proving the following
338 relationship:
cdalton8917d622015-05-06 13:40:21 -0700339
Brian Salomon92aee3d2016-12-21 09:20:25 -0500340 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700341
342
Brian Salomon92aee3d2016-12-21 09:20:25 -0500343 f * blend(Sa, Da) + (1-f) * Da
344 = f * (Sa + Da - Sa * Da) + (1-f) * Da
345 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
346 = f*Sa - f*Sa * Da + Da
347 = f*Sa + Da - f*Sa * Da
348 = blend(f*Sa, Da)
Brian Salomon31853842017-03-28 16:32:05 -0400349 */
350 if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) {
Brian Salomon4fc77402017-03-30 16:48:26 -0400351 if (caps.blendEquationSupport() == GrCaps::kAdvancedCoherent_BlendEquationSupport) {
352 return AnalysisProperties::kCompatibleWithAlphaAsCoverage;
353 } else {
354 return AnalysisProperties::kCompatibleWithAlphaAsCoverage |
355 AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws;
356 }
cdalton8917d622015-05-06 13:40:21 -0700357 }
Brian Salomon31853842017-03-28 16:32:05 -0400358 return AnalysisProperties::kCompatibleWithAlphaAsCoverage |
359 AnalysisProperties::kReadsDstInShader;
cdalton8917d622015-05-06 13:40:21 -0700360}
egdaniel41d4f092015-02-09 07:51:00 -0800361
bsalomonae4738f2015-09-15 15:33:27 -0700362GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500363#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500364const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500365 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
366 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800367
Brian Salomona1633922017-01-09 11:46:10 -0500368 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800369}
Hal Canary6f6961e2017-01-31 13:50:44 -0500370#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800371
bsalomonae4738f2015-09-15 15:33:27 -0700372///////////////////////////////////////////////////////////////////////////////
373
Brian Salomona1633922017-01-09 11:46:10 -0500374const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
375 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
376 // null.
377#ifdef SK_BUILD_FOR_WIN
378#define _CONSTEXPR_
379#else
380#define _CONSTEXPR_ constexpr
381#endif
382 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
383 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
384 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
385 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
386 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
387 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
388 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
389 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
390 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
391 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
392 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
393 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
394 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
395 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
396#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500397 switch (mode) {
398 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500399 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500400 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500401 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500402 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500403 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500404 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500405 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500406 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500407 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500408 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500409 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500410 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500411 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500412 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500413 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500414 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500415 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500416 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500417 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500418 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500419 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500420 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500421 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500422 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500423 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500424 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500425 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500426 default:
427 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
428 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700429 }
430}