blob: 9891ee69c0cf5516d4b6b1cb6a531522c15368e7 [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);
49 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET);
50 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)
Greg Daniel6ebe4b92017-05-19 10:56:46 -040075 : fMode(mode)
76 , fHWBlendEquation(hwBlendEquation) {
cdaltonf0df1892015-06-05 13:26:20 -070077 this->initClassID<CustomXP>();
egdaniel41d4f092015-02-09 07:51:00 -080078 }
79
Greg Daniel6ebe4b92017-05-19 10:56:46 -040080 CustomXP(bool hasMixedSamples, SkBlendMode mode, GrProcessorAnalysisCoverage coverage)
81 : INHERITED(true, hasMixedSamples, coverage)
Brian Salomon18dfa982017-04-03 16:57:43 -040082 , fMode(mode)
83 , fHWBlendEquation(static_cast<GrBlendEquation>(-1)) {
cdaltonf0df1892015-06-05 13:26:20 -070084 this->initClassID<CustomXP>();
85 }
egdaniel41d4f092015-02-09 07:51:00 -080086
mtklein36352bf2015-03-25 18:17:31 -070087 const char* name() const override { return "Custom Xfermode"; }
egdaniel41d4f092015-02-09 07:51:00 -080088
egdaniel57d3b032015-11-13 11:57:27 -080089 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -080090
Mike Reed7d954ad2016-10-28 15:42:34 -040091 SkBlendMode mode() const { return fMode; }
bsalomonf7cc8772015-05-11 11:21:14 -070092 bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); }
cdalton8917d622015-05-06 13:40:21 -070093
94 GrBlendEquation hwBlendEquation() const {
95 SkASSERT(this->hasHWBlendEquation());
96 return fHWBlendEquation;
97 }
egdaniel41d4f092015-02-09 07:51:00 -080098
Brian Salomon18dfa982017-04-03 16:57:43 -040099 GrXferBarrierType xferBarrierType(const GrCaps&) const override;
100
egdaniel41d4f092015-02-09 07:51:00 -0800101private:
Brian Salomon94efbf52016-11-29 13:43:05 -0500102 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800103
cdalton8917d622015-05-06 13:40:21 -0700104 void onGetBlendInfo(BlendInfo*) const override;
105
mtklein36352bf2015-03-25 18:17:31 -0700106 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800107
Mike Reed7d954ad2016-10-28 15:42:34 -0400108 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700109 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800110
111 typedef GrXferProcessor INHERITED;
112};
113
114///////////////////////////////////////////////////////////////////////////////
115
egdanielfa4cc8b2015-11-13 08:34:52 -0800116class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800117public:
118 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700119 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800120
Brian Salomon94efbf52016-11-29 13:43:05 -0500121 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
122 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700123 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700124 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700125 if (xp.hasHWBlendEquation()) {
126 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700127 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500128 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700129 }
130 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400131 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700132 }
bsalomon50785a32015-02-06 07:02:37 -0800133 b->add32(key);
134 }
135
136private:
cdaltonedbb31f2015-06-08 12:14:44 -0700137 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700138 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700139 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800140
egdaniel4ca2e602015-11-18 08:01:26 -0800141 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
142 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700143
cdalton86ae0a92015-06-08 15:11:04 -0700144 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
145 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500146 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
147 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800148 }
149
egdaniel7ea439b2015-12-03 09:20:44 -0800150 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
151 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800152 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800153 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800154 const char* dstColor,
155 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800156 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800157 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700158 const CustomXP& xp = proc.cast<CustomXP>();
159 SkASSERT(!xp.hasHWBlendEquation());
160
egdaniel4ca2e602015-11-18 08:01:26 -0800161 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800162
163 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800164 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
165 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700166 }
167
egdaniel018fb622015-10-28 07:26:40 -0700168 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800169
egdanielfa4cc8b2015-11-13 08:34:52 -0800170 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800171};
172
173///////////////////////////////////////////////////////////////////////////////
174
Brian Salomon94efbf52016-11-29 13:43:05 -0500175void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800176 GLCustomXP::GenKey(*this, caps, b);
177}
178
egdaniel57d3b032015-11-13 11:57:27 -0800179GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700180 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700181 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800182}
183
egdaniel41d4f092015-02-09 07:51:00 -0800184bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
185 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700186 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800187}
188
Brian Salomon18dfa982017-04-03 16:57:43 -0400189GrXferBarrierType CustomXP::xferBarrierType(const GrCaps& caps) const {
Brian Salomon31853842017-03-28 16:32:05 -0400190 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
191 return kBlend_GrXferBarrierType;
192 }
193 return kNone_GrXferBarrierType;
194}
cdalton8917d622015-05-06 13:40:21 -0700195
Brian Salomon31853842017-03-28 16:32:05 -0400196void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
197 if (this->hasHWBlendEquation()) {
198 blendInfo->fEquation = this->hwBlendEquation();
199 }
200}
201
202///////////////////////////////////////////////////////////////////////////////
203
204// See the comment above GrXPFactory's definition about this warning suppression.
205#if defined(__GNUC__) || defined(__clang)
206#pragma GCC diagnostic push
207#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
208#endif
209class CustomXPFactory : public GrXPFactory {
210public:
211 constexpr CustomXPFactory(SkBlendMode mode)
212 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
213
214private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400215 sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
216 GrProcessorAnalysisCoverage,
217 bool hasMixedSamples,
218 const GrCaps&) const override;
Brian Salomon31853842017-03-28 16:32:05 -0400219
Brian Salomona811b122017-03-30 08:21:32 -0400220 AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
221 const GrProcessorAnalysisCoverage&,
Brian Salomon31853842017-03-28 16:32:05 -0400222 const GrCaps&) const override;
223
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400224 GR_DECLARE_XP_FACTORY_TEST
Brian Salomon31853842017-03-28 16:32:05 -0400225
226 SkBlendMode fMode;
227 GrBlendEquation fHWBlendEquation;
228
229 typedef GrXPFactory INHERITED;
230};
231#if defined(__GNUC__) || defined(__clang)
232#pragma GCC diagnostic pop
233#endif
234
Brian Salomond61c9d92017-04-10 10:54:25 -0400235sk_sp<const GrXferProcessor> CustomXPFactory::makeXferProcessor(
236 const GrProcessorAnalysisColor&,
237 GrProcessorAnalysisCoverage coverage,
238 bool hasMixedSamples,
239 const GrCaps& caps) const {
Brian Salomon31853842017-03-28 16:32:05 -0400240 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon1c6025c2017-03-29 14:25:04 -0400241 if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) {
Brian Salomona076d872017-04-04 15:17:03 -0400242 return sk_sp<GrXferProcessor>(new CustomXP(fMode, fHWBlendEquation));
Brian Salomon31853842017-03-28 16:32:05 -0400243 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400244 return sk_sp<GrXferProcessor>(new CustomXP(hasMixedSamples, fMode, coverage));
Brian Salomon31853842017-03-28 16:32:05 -0400245}
246
247GrXPFactory::AnalysisProperties CustomXPFactory::analysisProperties(
Brian Salomona811b122017-03-30 08:21:32 -0400248 const GrProcessorAnalysisColor&, const GrProcessorAnalysisCoverage& coverage,
Brian Salomon31853842017-03-28 16:32:05 -0400249 const GrCaps& caps) const {
250 /*
Brian Salomon92aee3d2016-12-21 09:20:25 -0500251 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700252
Brian Salomon92aee3d2016-12-21 09:20:25 -0500253 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
254 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700255
Brian Salomon92aee3d2016-12-21 09:20:25 -0500256 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
257 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
258 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700259
Brian Salomon92aee3d2016-12-21 09:20:25 -0500260 For every blend mode supported by this class, i.e. the "advanced" blend
261 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700262
Brian Salomon92aee3d2016-12-21 09:20:25 -0500263 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
264 coverage.
cdalton8917d622015-05-06 13:40:21 -0700265
266
Brian Salomon92aee3d2016-12-21 09:20:25 -0500267 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700268
Brian Salomon92aee3d2016-12-21 09:20:25 -0500269 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
270 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700271
Brian Salomon92aee3d2016-12-21 09:20:25 -0500272 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
273 Sca : if Da == 0,
274 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
275 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700276
Brian Salomon92aee3d2016-12-21 09:20:25 -0500277 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700278
Brian Salomon92aee3d2016-12-21 09:20:25 -0500279 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700280
Brian Salomon92aee3d2016-12-21 09:20:25 -0500281 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700282
Brian Salomon92aee3d2016-12-21 09:20:25 -0500283 Next we show that canTweakAlphaForCoverage() is true by proving the
284 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700285
Brian Salomon92aee3d2016-12-21 09:20:25 -0500286 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700287
Brian Salomon92aee3d2016-12-21 09:20:25 -0500288 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700289
Brian Salomon92aee3d2016-12-21 09:20:25 -0500290 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
291 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
292 0, definition of blend()]
293 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
294 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
295 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
296 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
297 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
298 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
299 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700300
Brian Salomon92aee3d2016-12-21 09:20:25 -0500301 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700302
Brian Salomon92aee3d2016-12-21 09:20:25 -0500303 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
304 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
305 = Dca
306 = blend(0, Dca, 0, Da) [definition of blend()]
307 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700308
Brian Salomon92aee3d2016-12-21 09:20:25 -0500309 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
310 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
311 = f * Sca [Da=0]
312 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
313 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700314
Brian Salomon92aee3d2016-12-21 09:20:25 -0500315 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
316 = Dca [f=0]
317 = blend(0, Dca, 0, Da) [definition of blend()]
318 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700319
Brian Salomon92aee3d2016-12-21 09:20:25 -0500320 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700321
Brian Salomon92aee3d2016-12-21 09:20:25 -0500322 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700323
Brian Salomon92aee3d2016-12-21 09:20:25 -0500324 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
325 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
326 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700327
Brian Salomon92aee3d2016-12-21 09:20:25 -0500328 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700329
Brian Salomon92aee3d2016-12-21 09:20:25 -0500330 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700331
Brian Salomon92aee3d2016-12-21 09:20:25 -0500332 And show that canTweakAlphaForCoverage() is true by proving the following
333 relationship:
cdalton8917d622015-05-06 13:40:21 -0700334
Brian Salomon92aee3d2016-12-21 09:20:25 -0500335 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700336
337
Brian Salomon92aee3d2016-12-21 09:20:25 -0500338 f * blend(Sa, Da) + (1-f) * Da
339 = f * (Sa + Da - Sa * Da) + (1-f) * Da
340 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
341 = f*Sa - f*Sa * Da + Da
342 = f*Sa + Da - f*Sa * Da
343 = blend(f*Sa, Da)
Brian Salomon31853842017-03-28 16:32:05 -0400344 */
345 if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) {
Brian Salomon4fc77402017-03-30 16:48:26 -0400346 if (caps.blendEquationSupport() == GrCaps::kAdvancedCoherent_BlendEquationSupport) {
347 return AnalysisProperties::kCompatibleWithAlphaAsCoverage;
348 } else {
349 return AnalysisProperties::kCompatibleWithAlphaAsCoverage |
350 AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws;
351 }
cdalton8917d622015-05-06 13:40:21 -0700352 }
Brian Salomon31853842017-03-28 16:32:05 -0400353 return AnalysisProperties::kCompatibleWithAlphaAsCoverage |
354 AnalysisProperties::kReadsDstInShader;
cdalton8917d622015-05-06 13:40:21 -0700355}
egdaniel41d4f092015-02-09 07:51:00 -0800356
bsalomonae4738f2015-09-15 15:33:27 -0700357GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500358#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500359const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500360 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
361 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800362
Brian Salomona1633922017-01-09 11:46:10 -0500363 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800364}
Hal Canary6f6961e2017-01-31 13:50:44 -0500365#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800366
bsalomonae4738f2015-09-15 15:33:27 -0700367///////////////////////////////////////////////////////////////////////////////
368
Brian Salomona1633922017-01-09 11:46:10 -0500369const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
370 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
371 // null.
372#ifdef SK_BUILD_FOR_WIN
373#define _CONSTEXPR_
374#else
375#define _CONSTEXPR_ constexpr
376#endif
377 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
378 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
379 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
380 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
381 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
382 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
383 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
384 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
385 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
386 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
387 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
388 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
389 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
390 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
391#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500392 switch (mode) {
393 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500394 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500395 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500396 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500397 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500398 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500399 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500400 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500401 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500402 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500403 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500404 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500405 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500406 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500407 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500408 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500409 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500410 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500411 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500412 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500413 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500414 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500415 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500416 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500417 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500418 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500419 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500420 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500421 default:
422 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
423 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700424 }
425}