blob: 4c33b9b9ec54ffd8fc9e3367500984e6cc2439b5 [file] [log] [blame]
egdaniel0063a9b2015-01-15 10:52:32 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "effects/GrCustomXfermode.h"
egdaniel0063a9b2015-01-15 10:52:32 -08009
10#include "GrCoordTransform.h"
11#include "GrContext.h"
12#include "GrFragmentProcessor.h"
13#include "GrInvariantOutput.h"
ethannicholasde4166a2015-11-30 08:57:38 -080014#include "GrPipeline.h"
egdaniel0063a9b2015-01-15 10:52:32 -080015#include "GrProcessor.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050016#include "GrShaderCaps.h"
egdaniel0063a9b2015-01-15 10:52:32 -080017#include "GrTexture.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLBlend.h"
egdaniel64c47282015-11-13 06:54:19 -080019#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080020#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070021#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080022#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080023#include "glsl/GrGLSLXferProcessor.h"
egdaniel0063a9b2015-01-15 10:52:32 -080024
Mike Reed7d954ad2016-10-28 15:42:34 -040025bool GrCustomXfermode::IsSupportedMode(SkBlendMode mode) {
26 return (int)mode > (int)SkBlendMode::kLastCoeffMode &&
27 (int)mode <= (int)SkBlendMode::kLastMode;
egdaniel0063a9b2015-01-15 10:52:32 -080028}
29
30///////////////////////////////////////////////////////////////////////////////
31// Static helpers
32///////////////////////////////////////////////////////////////////////////////
33
Brian Salomona1633922017-01-09 11:46:10 -050034static constexpr GrBlendEquation hw_blend_equation(SkBlendMode mode) {
35// In C++14 this could be a constexpr int variable.
36#define EQ_OFFSET (kOverlay_GrBlendEquation - (int)SkBlendMode::kOverlay)
37 GR_STATIC_ASSERT(kOverlay_GrBlendEquation == (int)SkBlendMode::kOverlay + EQ_OFFSET);
38 GR_STATIC_ASSERT(kDarken_GrBlendEquation == (int)SkBlendMode::kDarken + EQ_OFFSET);
39 GR_STATIC_ASSERT(kLighten_GrBlendEquation == (int)SkBlendMode::kLighten + EQ_OFFSET);
40 GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == (int)SkBlendMode::kColorDodge + EQ_OFFSET);
41 GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == (int)SkBlendMode::kColorBurn + EQ_OFFSET);
42 GR_STATIC_ASSERT(kHardLight_GrBlendEquation == (int)SkBlendMode::kHardLight + EQ_OFFSET);
43 GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == (int)SkBlendMode::kSoftLight + EQ_OFFSET);
44 GR_STATIC_ASSERT(kDifference_GrBlendEquation == (int)SkBlendMode::kDifference + EQ_OFFSET);
45 GR_STATIC_ASSERT(kExclusion_GrBlendEquation == (int)SkBlendMode::kExclusion + EQ_OFFSET);
46 GR_STATIC_ASSERT(kMultiply_GrBlendEquation == (int)SkBlendMode::kMultiply + EQ_OFFSET);
47 GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == (int)SkBlendMode::kHue + EQ_OFFSET);
48 GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == (int)SkBlendMode::kSaturation + EQ_OFFSET);
49 GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == (int)SkBlendMode::kColor + EQ_OFFSET);
50 GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == (int)SkBlendMode::kLuminosity + EQ_OFFSET);
51 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET);
52 return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET);
53#undef EQ_OFFSET
cdalton8917d622015-05-06 13:40:21 -070054}
55
cdalton1dd05422015-06-12 09:01:18 -070056static bool can_use_hw_blend_equation(GrBlendEquation equation,
Brian Salomon5be6c952017-01-20 19:06:29 +000057 bool usePLSRead,
58 bool isLCDCoverage,
cdalton1dd05422015-06-12 09:01:18 -070059 const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070060 if (!caps.advancedBlendEquationSupport()) {
61 return false;
62 }
Brian Salomon5be6c952017-01-20 19:06:29 +000063 if (usePLSRead) {
ethannicholas22793252016-01-30 09:59:10 -080064 return false;
65 }
Brian Salomon5be6c952017-01-20 19:06:29 +000066 if (isLCDCoverage) {
cdaltonf0df1892015-06-05 13:26:20 -070067 return false; // LCD coverage must be applied after the blend equation.
68 }
cdalton1dd05422015-06-12 09:01:18 -070069 if (caps.canUseAdvancedBlendEquation(equation)) {
70 return false;
71 }
cdaltonf0df1892015-06-05 13:26:20 -070072 return true;
73}
74
egdaniel54f0e9d2015-01-16 06:29:47 -080075///////////////////////////////////////////////////////////////////////////////
76// Xfer Processor
77///////////////////////////////////////////////////////////////////////////////
78
egdaniel41d4f092015-02-09 07:51:00 -080079class CustomXP : public GrXferProcessor {
80public:
Mike Reed7d954ad2016-10-28 15:42:34 -040081 CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation)
cdaltonf0df1892015-06-05 13:26:20 -070082 : fMode(mode),
83 fHWBlendEquation(hwBlendEquation) {
84 this->initClassID<CustomXP>();
egdaniel41d4f092015-02-09 07:51:00 -080085 }
86
Mike Reed7d954ad2016-10-28 15:42:34 -040087 CustomXP(const DstTexture* dstTexture, bool hasMixedSamples, SkBlendMode mode)
cdalton86ae0a92015-06-08 15:11:04 -070088 : INHERITED(dstTexture, true, hasMixedSamples),
cdaltonf0df1892015-06-05 13:26:20 -070089 fMode(mode),
90 fHWBlendEquation(static_cast<GrBlendEquation>(-1)) {
91 this->initClassID<CustomXP>();
92 }
egdaniel41d4f092015-02-09 07:51:00 -080093
mtklein36352bf2015-03-25 18:17:31 -070094 const char* name() const override { return "Custom Xfermode"; }
egdaniel41d4f092015-02-09 07:51:00 -080095
egdaniel57d3b032015-11-13 11:57:27 -080096 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -080097
Mike Reed7d954ad2016-10-28 15:42:34 -040098 SkBlendMode mode() const { return fMode; }
bsalomonf7cc8772015-05-11 11:21:14 -070099 bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); }
cdalton8917d622015-05-06 13:40:21 -0700100
101 GrBlendEquation hwBlendEquation() const {
102 SkASSERT(this->hasHWBlendEquation());
103 return fHWBlendEquation;
104 }
egdaniel41d4f092015-02-09 07:51:00 -0800105
106private:
Brian Salomon92aee3d2016-12-21 09:20:25 -0500107 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineAnalysis&,
egdanielc19cdc22015-05-10 08:45:18 -0700108 bool doesStencilWrite,
109 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800110 const GrCaps& caps) const override;
egdanielc19cdc22015-05-10 08:45:18 -0700111
Brian Salomon94efbf52016-11-29 13:43:05 -0500112 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800113
bsalomoncb02b382015-08-12 11:14:50 -0700114 GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const override;
cdalton8917d622015-05-06 13:40:21 -0700115
116 void onGetBlendInfo(BlendInfo*) const override;
117
mtklein36352bf2015-03-25 18:17:31 -0700118 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800119
Mike Reed7d954ad2016-10-28 15:42:34 -0400120 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700121 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800122
123 typedef GrXferProcessor INHERITED;
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
egdanielfa4cc8b2015-11-13 08:34:52 -0800128class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800129public:
130 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700131 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800132
Brian Salomon94efbf52016-11-29 13:43:05 -0500133 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
134 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700135 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700136 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700137 if (xp.hasHWBlendEquation()) {
138 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700139 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500140 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700141 }
142 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400143 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700144 }
bsalomon50785a32015-02-06 07:02:37 -0800145 b->add32(key);
146 }
147
148private:
cdaltonedbb31f2015-06-08 12:14:44 -0700149 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700150 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700151 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800152
egdaniel4ca2e602015-11-18 08:01:26 -0800153 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
154 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700155
cdalton86ae0a92015-06-08 15:11:04 -0700156 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
157 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500158 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
159 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800160 }
161
egdaniel7ea439b2015-12-03 09:20:44 -0800162 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
163 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800164 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800165 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800166 const char* dstColor,
167 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800168 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800169 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700170 const CustomXP& xp = proc.cast<CustomXP>();
171 SkASSERT(!xp.hasHWBlendEquation());
172
egdaniel4ca2e602015-11-18 08:01:26 -0800173 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800174
175 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800176 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
177 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700178 }
179
egdaniel018fb622015-10-28 07:26:40 -0700180 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800181
egdanielfa4cc8b2015-11-13 08:34:52 -0800182 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800183};
184
185///////////////////////////////////////////////////////////////////////////////
186
Brian Salomon94efbf52016-11-29 13:43:05 -0500187void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800188 GLCustomXP::GenKey(*this, caps, b);
189}
190
egdaniel57d3b032015-11-13 11:57:27 -0800191GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700192 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700193 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800194}
195
egdaniel41d4f092015-02-09 07:51:00 -0800196bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
197 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700198 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800199}
200
Brian Salomon92aee3d2016-12-21 09:20:25 -0500201GrXferProcessor::OptFlags CustomXP::onGetOptimizations(const GrPipelineAnalysis& analysis,
egdaniel54f0e9d2015-01-16 06:29:47 -0800202 bool doesStencilWrite,
203 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800204 const GrCaps& caps) const {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500205 /*
206 Most the optimizations we do here are based on tweaking alpha for coverage.
cdalton8917d622015-05-06 13:40:21 -0700207
Brian Salomon92aee3d2016-12-21 09:20:25 -0500208 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700209
Brian Salomon92aee3d2016-12-21 09:20:25 -0500210 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
211 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700212
Brian Salomon92aee3d2016-12-21 09:20:25 -0500213 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
214 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
215 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700216
Brian Salomon92aee3d2016-12-21 09:20:25 -0500217 For every blend mode supported by this class, i.e. the "advanced" blend
218 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700219
Brian Salomon92aee3d2016-12-21 09:20:25 -0500220 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
221 coverage.
cdalton8917d622015-05-06 13:40:21 -0700222
223
Brian Salomon92aee3d2016-12-21 09:20:25 -0500224 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700225
Brian Salomon92aee3d2016-12-21 09:20:25 -0500226 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
227 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700228
Brian Salomon92aee3d2016-12-21 09:20:25 -0500229 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
230 Sca : if Da == 0,
231 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
232 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700233
Brian Salomon92aee3d2016-12-21 09:20:25 -0500234 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700235
Brian Salomon92aee3d2016-12-21 09:20:25 -0500236 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700237
Brian Salomon92aee3d2016-12-21 09:20:25 -0500238 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700239
Brian Salomon92aee3d2016-12-21 09:20:25 -0500240 Next we show that canTweakAlphaForCoverage() is true by proving the
241 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700242
Brian Salomon92aee3d2016-12-21 09:20:25 -0500243 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700244
Brian Salomon92aee3d2016-12-21 09:20:25 -0500245 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700246
Brian Salomon92aee3d2016-12-21 09:20:25 -0500247 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
248 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
249 0, definition of blend()]
250 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
251 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
252 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
253 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
254 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
255 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
256 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700257
Brian Salomon92aee3d2016-12-21 09:20:25 -0500258 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700259
Brian Salomon92aee3d2016-12-21 09:20:25 -0500260 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
261 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
262 = Dca
263 = blend(0, Dca, 0, Da) [definition of blend()]
264 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700265
Brian Salomon92aee3d2016-12-21 09:20:25 -0500266 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
267 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
268 = f * Sca [Da=0]
269 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
270 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700271
Brian Salomon92aee3d2016-12-21 09:20:25 -0500272 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
273 = Dca [f=0]
274 = blend(0, Dca, 0, Da) [definition of blend()]
275 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700276
Brian Salomon92aee3d2016-12-21 09:20:25 -0500277 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700278
Brian Salomon92aee3d2016-12-21 09:20:25 -0500279 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700280
Brian Salomon92aee3d2016-12-21 09:20:25 -0500281 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
282 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
283 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700284
Brian Salomon92aee3d2016-12-21 09:20:25 -0500285 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700286
Brian Salomon92aee3d2016-12-21 09:20:25 -0500287 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700288
Brian Salomon92aee3d2016-12-21 09:20:25 -0500289 And show that canTweakAlphaForCoverage() is true by proving the following
290 relationship:
cdalton8917d622015-05-06 13:40:21 -0700291
Brian Salomon92aee3d2016-12-21 09:20:25 -0500292 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700293
294
Brian Salomon92aee3d2016-12-21 09:20:25 -0500295 f * blend(Sa, Da) + (1-f) * Da
296 = f * (Sa + Da - Sa * Da) + (1-f) * Da
297 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
298 = f*Sa - f*Sa * Da + Da
299 = f*Sa + Da - f*Sa * Da
300 = blend(f*Sa, Da)
301 */
cdalton8917d622015-05-06 13:40:21 -0700302
bsalomon7765a472015-07-08 11:26:37 -0700303 OptFlags flags = kNone_OptFlags;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500304 if (analysis.fColorPOI.allStagesMultiplyInput()) {
cdalton32117702015-05-11 11:21:23 -0700305 flags |= kCanTweakAlphaForCoverage_OptFlag;
cdalton8917d622015-05-06 13:40:21 -0700306 }
cdalton8917d622015-05-06 13:40:21 -0700307 return flags;
308}
309
bsalomoncb02b382015-08-12 11:14:50 -0700310GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const {
cdalton8917d622015-05-06 13:40:21 -0700311 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
bsalomoncb02b382015-08-12 11:14:50 -0700312 return kBlend_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700313 }
bsalomoncb02b382015-08-12 11:14:50 -0700314 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700315}
316
317void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
318 if (this->hasHWBlendEquation()) {
319 blendInfo->fEquation = this->hwBlendEquation();
320 }
egdaniel54f0e9d2015-01-16 06:29:47 -0800321}
322
323///////////////////////////////////////////////////////////////////////////////
Brian Salomona1633922017-01-09 11:46:10 -0500324
325// See the comment above GrXPFactory's definition about this warning suppression.
326#if defined(__GNUC__) || defined(__clang)
327#pragma GCC diagnostic push
328#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
329#endif
bsalomonae4738f2015-09-15 15:33:27 -0700330class CustomXPFactory : public GrXPFactory {
331public:
Brian Salomona1633922017-01-09 11:46:10 -0500332 constexpr CustomXPFactory(SkBlendMode mode)
333 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800334
bsalomonae4738f2015-09-15 15:33:27 -0700335 void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
336 GrXPFactory::InvariantBlendedColor*) const override;
337
338private:
339 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500340 const GrPipelineAnalysis&,
bsalomonae4738f2015-09-15 15:33:27 -0700341 bool hasMixedSamples,
342 const DstTexture*) const override;
343
Brian Salomon5be6c952017-01-20 19:06:29 +0000344 bool willReadDstColor(const GrCaps&, ColorType, CoverageType) const override;
bsalomonae4738f2015-09-15 15:33:27 -0700345
346 GR_DECLARE_XP_FACTORY_TEST;
347
Mike Reed7d954ad2016-10-28 15:42:34 -0400348 SkBlendMode fMode;
349 GrBlendEquation fHWBlendEquation;
bsalomonae4738f2015-09-15 15:33:27 -0700350
351 typedef GrXPFactory INHERITED;
352};
Brian Salomona1633922017-01-09 11:46:10 -0500353#if defined(__GNUC__) || defined(__clang)
354#pragma GCC diagnostic pop
355#endif
egdaniel54f0e9d2015-01-16 06:29:47 -0800356
bsalomonae4738f2015-09-15 15:33:27 -0700357GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps,
Brian Salomon92aee3d2016-12-21 09:20:25 -0500358 const GrPipelineAnalysis& analysis,
bsalomonae4738f2015-09-15 15:33:27 -0700359 bool hasMixedSamples,
360 const DstTexture* dstTexture) const {
Brian Salomona1633922017-01-09 11:46:10 -0500361 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon5be6c952017-01-20 19:06:29 +0000362 if (can_use_hw_blend_equation(fHWBlendEquation, analysis.fUsesPLSDstRead,
363 analysis.fCoveragePOI.isLCDCoverage(), caps)) {
cdaltonf0df1892015-06-05 13:26:20 -0700364 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700365 return new CustomXP(fMode, fHWBlendEquation);
cdaltonf0df1892015-06-05 13:26:20 -0700366 }
halcanary385fe4d2015-08-26 13:07:48 -0700367 return new CustomXP(dstTexture, hasMixedSamples, fMode);
egdaniel41d4f092015-02-09 07:51:00 -0800368}
369
Brian Salomon5be6c952017-01-20 19:06:29 +0000370bool CustomXPFactory::willReadDstColor(const GrCaps& caps, ColorType colorType,
371 CoverageType coverageType) const {
372 // This should not be called if we're using PLS dst read.
373 static constexpr bool kUsesPLSRead = false;
374 return !can_use_hw_blend_equation(fHWBlendEquation, kUsesPLSRead,
375 CoverageType::kLCD == coverageType, caps);
cdalton8917d622015-05-06 13:40:21 -0700376}
egdaniel41d4f092015-02-09 07:51:00 -0800377
bsalomonae4738f2015-09-15 15:33:27 -0700378void CustomXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
379 InvariantBlendedColor* blendedColor) const {
cdalton1fa45722015-06-02 10:43:39 -0700380 blendedColor->fWillBlendWithDst = true;
381 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
egdaniel54f0e9d2015-01-16 06:29:47 -0800382}
383
bsalomonae4738f2015-09-15 15:33:27 -0700384GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500385#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500386const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500387 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
388 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800389
Brian Salomona1633922017-01-09 11:46:10 -0500390 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800391}
Hal Canary6f6961e2017-01-31 13:50:44 -0500392#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800393
bsalomonae4738f2015-09-15 15:33:27 -0700394///////////////////////////////////////////////////////////////////////////////
395
Brian Salomona1633922017-01-09 11:46:10 -0500396const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
397 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
398 // null.
399#ifdef SK_BUILD_FOR_WIN
400#define _CONSTEXPR_
401#else
402#define _CONSTEXPR_ constexpr
403#endif
404 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
405 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
406 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
407 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
408 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
409 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
410 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
411 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
412 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
413 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
414 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
415 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
416 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
417 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
418#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500419 switch (mode) {
420 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500421 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500422 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500423 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500424 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500425 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500426 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500427 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500428 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500429 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500430 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500431 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500432 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500433 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500434 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500435 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500436 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500437 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500438 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500439 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500440 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500441 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500442 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500443 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500444 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500445 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500446 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500447 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500448 default:
449 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
450 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700451 }
452}