blob: 4af53458c1e4007c2925fcece3176dbff6713db5 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrCustomXfermode.h"
egdaniel0063a9b2015-01-15 10:52:32 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCaps.h"
11#include "src/gpu/GrCoordTransform.h"
12#include "src/gpu/GrFragmentProcessor.h"
13#include "src/gpu/GrPipeline.h"
14#include "src/gpu/GrProcessor.h"
15#include "src/gpu/GrShaderCaps.h"
16#include "src/gpu/glsl/GrGLSLBlend.h"
17#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
18#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
19#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
20#include "src/gpu/glsl/GrGLSLUniformHandler.h"
21#include "src/gpu/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 Klein36743362018-11-06 08:23:30 -050049
50 // There's an illegal GrBlendEquation that corresponds to no SkBlendMode, hence the extra +1.
51 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + 1 + EQ_OFFSET);
52
Brian Salomona1633922017-01-09 11:46:10 -050053 return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET);
54#undef EQ_OFFSET
cdalton8917d622015-05-06 13:40:21 -070055}
56
Brian Salomona811b122017-03-30 08:21:32 -040057static bool can_use_hw_blend_equation(GrBlendEquation equation,
58 GrProcessorAnalysisCoverage coverage, const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070059 if (!caps.advancedBlendEquationSupport()) {
60 return false;
61 }
Brian Salomona811b122017-03-30 08:21:32 -040062 if (GrProcessorAnalysisCoverage::kLCD == coverage) {
cdaltonf0df1892015-06-05 13:26:20 -070063 return false; // LCD coverage must be applied after the blend equation.
64 }
Chris Dalton302d5be2019-01-22 23:41:25 -070065 if (caps.isAdvancedBlendEquationBlacklisted(equation)) {
cdalton1dd05422015-06-12 09:01:18 -070066 return false;
67 }
cdaltonf0df1892015-06-05 13:26:20 -070068 return true;
69}
70
egdaniel54f0e9d2015-01-16 06:29:47 -080071///////////////////////////////////////////////////////////////////////////////
72// Xfer Processor
73///////////////////////////////////////////////////////////////////////////////
74
egdaniel41d4f092015-02-09 07:51:00 -080075class CustomXP : public GrXferProcessor {
76public:
Mike Reed7d954ad2016-10-28 15:42:34 -040077 CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation)
Ethan Nicholasabff9562017-10-09 10:54:08 -040078 : INHERITED(kCustomXP_ClassID)
79 , fMode(mode)
80 , fHWBlendEquation(hwBlendEquation) {}
egdaniel41d4f092015-02-09 07:51:00 -080081
Greg Daniel6ebe4b92017-05-19 10:56:46 -040082 CustomXP(bool hasMixedSamples, SkBlendMode mode, GrProcessorAnalysisCoverage coverage)
Ethan Nicholasabff9562017-10-09 10:54:08 -040083 : INHERITED(kCustomXP_ClassID, true, hasMixedSamples, coverage)
Brian Salomon18dfa982017-04-03 16:57:43 -040084 , fMode(mode)
Mike Klein36743362018-11-06 08:23:30 -050085 , fHWBlendEquation(kIllegal_GrBlendEquation) {
cdaltonf0df1892015-06-05 13:26:20 -070086 }
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; }
Mike Klein36743362018-11-06 08:23:30 -050093 bool hasHWBlendEquation() const { return kIllegal_GrBlendEquation != 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
Brian Salomon18dfa982017-04-03 16:57:43 -0400100 GrXferBarrierType xferBarrierType(const GrCaps&) const override;
101
egdaniel41d4f092015-02-09 07:51:00 -0800102private:
Brian Salomon94efbf52016-11-29 13:43:05 -0500103 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800104
cdalton8917d622015-05-06 13:40:21 -0700105 void onGetBlendInfo(BlendInfo*) const override;
106
mtklein36352bf2015-03-25 18:17:31 -0700107 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800108
Mike Reed7d954ad2016-10-28 15:42:34 -0400109 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700110 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800111
112 typedef GrXferProcessor INHERITED;
113};
114
115///////////////////////////////////////////////////////////////////////////////
116
egdanielfa4cc8b2015-11-13 08:34:52 -0800117class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800118public:
119 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700120 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800121
Brian Salomon94efbf52016-11-29 13:43:05 -0500122 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
123 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700124 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700125 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700126 if (xp.hasHWBlendEquation()) {
127 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700128 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500129 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700130 }
131 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400132 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700133 }
bsalomon50785a32015-02-06 07:02:37 -0800134 b->add32(key);
135 }
136
137private:
cdaltonedbb31f2015-06-08 12:14:44 -0700138 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700139 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700140 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800141
egdaniel4ca2e602015-11-18 08:01:26 -0800142 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
143 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700144
cdalton86ae0a92015-06-08 15:11:04 -0700145 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
146 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500147 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
148 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800149 }
150
egdaniel7ea439b2015-12-03 09:20:44 -0800151 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
152 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800153 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800154 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800155 const char* dstColor,
156 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800157 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800158 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700159 const CustomXP& xp = proc.cast<CustomXP>();
160 SkASSERT(!xp.hasHWBlendEquation());
161
egdaniel4ca2e602015-11-18 08:01:26 -0800162 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800163
164 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800165 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
166 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700167 }
168
egdaniel018fb622015-10-28 07:26:40 -0700169 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800170
egdanielfa4cc8b2015-11-13 08:34:52 -0800171 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800172};
173
174///////////////////////////////////////////////////////////////////////////////
175
Brian Salomon94efbf52016-11-29 13:43:05 -0500176void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800177 GLCustomXP::GenKey(*this, caps, b);
178}
179
egdaniel57d3b032015-11-13 11:57:27 -0800180GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700181 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700182 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800183}
184
egdaniel41d4f092015-02-09 07:51:00 -0800185bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
186 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700187 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800188}
189
Brian Salomon18dfa982017-04-03 16:57:43 -0400190GrXferBarrierType CustomXP::xferBarrierType(const GrCaps& caps) const {
Brian Salomon31853842017-03-28 16:32:05 -0400191 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
192 return kBlend_GrXferBarrierType;
193 }
194 return kNone_GrXferBarrierType;
195}
cdalton8917d622015-05-06 13:40:21 -0700196
Brian Salomon31853842017-03-28 16:32:05 -0400197void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
198 if (this->hasHWBlendEquation()) {
199 blendInfo->fEquation = this->hwBlendEquation();
200 }
201}
202
203///////////////////////////////////////////////////////////////////////////////
204
205// See the comment above GrXPFactory's definition about this warning suppression.
Chris Dalton1ef80942017-12-04 12:01:30 -0700206#if defined(__GNUC__)
Brian Salomon31853842017-03-28 16:32:05 -0400207#pragma GCC diagnostic push
208#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
209#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700210#if defined(__clang__)
211#pragma clang diagnostic push
212#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
213#endif
Brian Salomon31853842017-03-28 16:32:05 -0400214class CustomXPFactory : public GrXPFactory {
215public:
216 constexpr CustomXPFactory(SkBlendMode mode)
217 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
218
219private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400220 sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
221 GrProcessorAnalysisCoverage,
222 bool hasMixedSamples,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400223 const GrCaps&,
224 GrClampType) const override;
Brian Salomon31853842017-03-28 16:32:05 -0400225
Brian Salomona811b122017-03-30 08:21:32 -0400226 AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
227 const GrProcessorAnalysisCoverage&,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400228 const GrCaps&,
229 GrClampType) const override;
Brian Salomon31853842017-03-28 16:32:05 -0400230
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400231 GR_DECLARE_XP_FACTORY_TEST
Brian Salomon31853842017-03-28 16:32:05 -0400232
233 SkBlendMode fMode;
234 GrBlendEquation fHWBlendEquation;
235
236 typedef GrXPFactory INHERITED;
237};
Chris Dalton1ef80942017-12-04 12:01:30 -0700238#if defined(__GNUC__)
Brian Salomon31853842017-03-28 16:32:05 -0400239#pragma GCC diagnostic pop
240#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700241#if defined(__clang__)
242#pragma clang diagnostic pop
243#endif
Brian Salomon31853842017-03-28 16:32:05 -0400244
Brian Salomond61c9d92017-04-10 10:54:25 -0400245sk_sp<const GrXferProcessor> CustomXPFactory::makeXferProcessor(
246 const GrProcessorAnalysisColor&,
247 GrProcessorAnalysisCoverage coverage,
248 bool hasMixedSamples,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400249 const GrCaps& caps,
250 GrClampType clampType) const {
Brian Salomon31853842017-03-28 16:32:05 -0400251 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon1c6025c2017-03-29 14:25:04 -0400252 if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) {
Brian Salomona076d872017-04-04 15:17:03 -0400253 return sk_sp<GrXferProcessor>(new CustomXP(fMode, fHWBlendEquation));
Brian Salomon31853842017-03-28 16:32:05 -0400254 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400255 return sk_sp<GrXferProcessor>(new CustomXP(hasMixedSamples, fMode, coverage));
Brian Salomon31853842017-03-28 16:32:05 -0400256}
257
258GrXPFactory::AnalysisProperties CustomXPFactory::analysisProperties(
Brian Salomona811b122017-03-30 08:21:32 -0400259 const GrProcessorAnalysisColor&, const GrProcessorAnalysisCoverage& coverage,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400260 const GrCaps& caps, GrClampType clampType) const {
Brian Salomon31853842017-03-28 16:32:05 -0400261 /*
Brian Salomon92aee3d2016-12-21 09:20:25 -0500262 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700263
Brian Salomon92aee3d2016-12-21 09:20:25 -0500264 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
265 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700266
Brian Salomon92aee3d2016-12-21 09:20:25 -0500267 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
268 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
269 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700270
Brian Salomon92aee3d2016-12-21 09:20:25 -0500271 For every blend mode supported by this class, i.e. the "advanced" blend
272 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700273
Brian Salomon92aee3d2016-12-21 09:20:25 -0500274 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
275 coverage.
cdalton8917d622015-05-06 13:40:21 -0700276
277
Brian Salomon92aee3d2016-12-21 09:20:25 -0500278 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700279
Brian Salomon92aee3d2016-12-21 09:20:25 -0500280 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
281 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700282
Brian Salomon92aee3d2016-12-21 09:20:25 -0500283 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
284 Sca : if Da == 0,
285 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
286 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700287
Brian Salomon92aee3d2016-12-21 09:20:25 -0500288 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700289
Brian Salomon92aee3d2016-12-21 09:20:25 -0500290 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700291
Brian Salomon92aee3d2016-12-21 09:20:25 -0500292 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700293
Brian Salomon92aee3d2016-12-21 09:20:25 -0500294 Next we show that canTweakAlphaForCoverage() is true by proving the
295 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700296
Brian Salomon92aee3d2016-12-21 09:20:25 -0500297 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700298
Brian Salomon92aee3d2016-12-21 09:20:25 -0500299 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700300
Brian Salomon92aee3d2016-12-21 09:20:25 -0500301 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
302 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
303 0, definition of blend()]
304 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
305 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
306 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
307 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
308 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
309 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
310 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700311
Brian Salomon92aee3d2016-12-21 09:20:25 -0500312 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700313
Brian Salomon92aee3d2016-12-21 09:20:25 -0500314 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
315 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
316 = Dca
317 = blend(0, Dca, 0, Da) [definition of blend()]
318 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700319
Brian Salomon92aee3d2016-12-21 09:20:25 -0500320 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
321 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
322 = f * Sca [Da=0]
323 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
324 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700325
Brian Salomon92aee3d2016-12-21 09:20:25 -0500326 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
327 = Dca [f=0]
328 = blend(0, Dca, 0, Da) [definition of blend()]
329 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700330
Brian Salomon92aee3d2016-12-21 09:20:25 -0500331 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700332
Brian Salomon92aee3d2016-12-21 09:20:25 -0500333 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700334
Brian Salomon92aee3d2016-12-21 09:20:25 -0500335 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
336 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
337 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700338
Brian Salomon92aee3d2016-12-21 09:20:25 -0500339 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700340
Brian Salomon92aee3d2016-12-21 09:20:25 -0500341 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700342
Brian Salomon92aee3d2016-12-21 09:20:25 -0500343 And show that canTweakAlphaForCoverage() is true by proving the following
344 relationship:
cdalton8917d622015-05-06 13:40:21 -0700345
Brian Salomon92aee3d2016-12-21 09:20:25 -0500346 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700347
348
Brian Salomon92aee3d2016-12-21 09:20:25 -0500349 f * blend(Sa, Da) + (1-f) * Da
350 = f * (Sa + Da - Sa * Da) + (1-f) * Da
351 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
352 = f*Sa - f*Sa * Da + Da
353 = f*Sa + Da - f*Sa * Da
354 = blend(f*Sa, Da)
Brian Salomon31853842017-03-28 16:32:05 -0400355 */
356 if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) {
Brian Salomon4fc77402017-03-30 16:48:26 -0400357 if (caps.blendEquationSupport() == GrCaps::kAdvancedCoherent_BlendEquationSupport) {
Brian Osman605c6d52019-03-15 12:10:35 -0400358 return AnalysisProperties::kCompatibleWithCoverageAsAlpha;
Brian Salomon4fc77402017-03-30 16:48:26 -0400359 } else {
Brian Osman605c6d52019-03-15 12:10:35 -0400360 return AnalysisProperties::kCompatibleWithCoverageAsAlpha |
Chris Dalton945ee652019-01-23 09:10:36 -0700361 AnalysisProperties::kRequiresNonOverlappingDraws;
Brian Salomon4fc77402017-03-30 16:48:26 -0400362 }
cdalton8917d622015-05-06 13:40:21 -0700363 }
Brian Osman605c6d52019-03-15 12:10:35 -0400364 return AnalysisProperties::kCompatibleWithCoverageAsAlpha |
Brian Salomon31853842017-03-28 16:32:05 -0400365 AnalysisProperties::kReadsDstInShader;
cdalton8917d622015-05-06 13:40:21 -0700366}
egdaniel41d4f092015-02-09 07:51:00 -0800367
bsalomonae4738f2015-09-15 15:33:27 -0700368GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500369#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500370const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500371 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
372 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800373
Brian Salomona1633922017-01-09 11:46:10 -0500374 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800375}
Hal Canary6f6961e2017-01-31 13:50:44 -0500376#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800377
bsalomonae4738f2015-09-15 15:33:27 -0700378///////////////////////////////////////////////////////////////////////////////
379
Brian Salomona1633922017-01-09 11:46:10 -0500380const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
381 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
382 // null.
383#ifdef SK_BUILD_FOR_WIN
384#define _CONSTEXPR_
385#else
386#define _CONSTEXPR_ constexpr
387#endif
388 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
389 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
390 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
391 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
392 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
393 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
394 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
395 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
396 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
397 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
398 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
399 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
400 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
401 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
402#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500403 switch (mode) {
404 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500405 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500406 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500407 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500408 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500409 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500410 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500411 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500412 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500413 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500414 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500415 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500416 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500417 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500418 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500419 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500420 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500421 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500422 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500423 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500424 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500425 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500426 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500427 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500428 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500429 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500430 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500431 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500432 default:
433 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
434 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700435 }
436}