blob: ccbab73f86e2bc27458bb21fee0e186d75e4120f [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"
ethannicholasde4166a2015-11-30 08:57:38 -080013#include "GrPipeline.h"
egdaniel0063a9b2015-01-15 10:52:32 -080014#include "GrProcessor.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "GrShaderCaps.h"
egdaniel0063a9b2015-01-15 10:52:32 -080016#include "GrTexture.h"
egdaniel64c47282015-11-13 06:54:19 -080017#include "glsl/GrGLSLBlend.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080019#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070020#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080021#include "glsl/GrGLSLUniformHandler.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080022#include "glsl/GrGLSLXferProcessor.h"
egdaniel0063a9b2015-01-15 10:52:32 -080023
Mike Reed7d954ad2016-10-28 15:42:34 -040024bool GrCustomXfermode::IsSupportedMode(SkBlendMode mode) {
25 return (int)mode > (int)SkBlendMode::kLastCoeffMode &&
26 (int)mode <= (int)SkBlendMode::kLastMode;
egdaniel0063a9b2015-01-15 10:52:32 -080027}
28
29///////////////////////////////////////////////////////////////////////////////
30// Static helpers
31///////////////////////////////////////////////////////////////////////////////
32
Brian Salomona1633922017-01-09 11:46:10 -050033static constexpr GrBlendEquation hw_blend_equation(SkBlendMode mode) {
34// In C++14 this could be a constexpr int variable.
35#define EQ_OFFSET (kOverlay_GrBlendEquation - (int)SkBlendMode::kOverlay)
36 GR_STATIC_ASSERT(kOverlay_GrBlendEquation == (int)SkBlendMode::kOverlay + EQ_OFFSET);
37 GR_STATIC_ASSERT(kDarken_GrBlendEquation == (int)SkBlendMode::kDarken + EQ_OFFSET);
38 GR_STATIC_ASSERT(kLighten_GrBlendEquation == (int)SkBlendMode::kLighten + EQ_OFFSET);
39 GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == (int)SkBlendMode::kColorDodge + EQ_OFFSET);
40 GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == (int)SkBlendMode::kColorBurn + EQ_OFFSET);
41 GR_STATIC_ASSERT(kHardLight_GrBlendEquation == (int)SkBlendMode::kHardLight + EQ_OFFSET);
42 GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == (int)SkBlendMode::kSoftLight + EQ_OFFSET);
43 GR_STATIC_ASSERT(kDifference_GrBlendEquation == (int)SkBlendMode::kDifference + EQ_OFFSET);
44 GR_STATIC_ASSERT(kExclusion_GrBlendEquation == (int)SkBlendMode::kExclusion + EQ_OFFSET);
45 GR_STATIC_ASSERT(kMultiply_GrBlendEquation == (int)SkBlendMode::kMultiply + EQ_OFFSET);
46 GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == (int)SkBlendMode::kHue + EQ_OFFSET);
47 GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == (int)SkBlendMode::kSaturation + EQ_OFFSET);
48 GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == (int)SkBlendMode::kColor + EQ_OFFSET);
49 GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == (int)SkBlendMode::kLuminosity + EQ_OFFSET);
50 GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET);
51 return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET);
52#undef EQ_OFFSET
cdalton8917d622015-05-06 13:40:21 -070053}
54
cdalton1dd05422015-06-12 09:01:18 -070055static bool can_use_hw_blend_equation(GrBlendEquation equation,
Brian Salomon5be6c952017-01-20 19:06:29 +000056 bool usePLSRead,
57 bool isLCDCoverage,
cdalton1dd05422015-06-12 09:01:18 -070058 const GrCaps& caps) {
cdaltonf0df1892015-06-05 13:26:20 -070059 if (!caps.advancedBlendEquationSupport()) {
60 return false;
61 }
Brian Salomon5be6c952017-01-20 19:06:29 +000062 if (usePLSRead) {
ethannicholas22793252016-01-30 09:59:10 -080063 return false;
64 }
Brian Salomon5be6c952017-01-20 19:06:29 +000065 if (isLCDCoverage) {
cdaltonf0df1892015-06-05 13:26:20 -070066 return false; // LCD coverage must be applied after the blend equation.
67 }
cdalton1dd05422015-06-12 09:01:18 -070068 if (caps.canUseAdvancedBlendEquation(equation)) {
69 return false;
70 }
cdaltonf0df1892015-06-05 13:26:20 -070071 return true;
72}
73
egdaniel54f0e9d2015-01-16 06:29:47 -080074///////////////////////////////////////////////////////////////////////////////
75// Xfer Processor
76///////////////////////////////////////////////////////////////////////////////
77
egdaniel41d4f092015-02-09 07:51:00 -080078class CustomXP : public GrXferProcessor {
79public:
Mike Reed7d954ad2016-10-28 15:42:34 -040080 CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation)
cdaltonf0df1892015-06-05 13:26:20 -070081 : fMode(mode),
82 fHWBlendEquation(hwBlendEquation) {
83 this->initClassID<CustomXP>();
egdaniel41d4f092015-02-09 07:51:00 -080084 }
85
Mike Reed7d954ad2016-10-28 15:42:34 -040086 CustomXP(const DstTexture* dstTexture, bool hasMixedSamples, SkBlendMode mode)
cdalton86ae0a92015-06-08 15:11:04 -070087 : INHERITED(dstTexture, true, hasMixedSamples),
cdaltonf0df1892015-06-05 13:26:20 -070088 fMode(mode),
89 fHWBlendEquation(static_cast<GrBlendEquation>(-1)) {
90 this->initClassID<CustomXP>();
91 }
egdaniel41d4f092015-02-09 07:51:00 -080092
mtklein36352bf2015-03-25 18:17:31 -070093 const char* name() const override { return "Custom Xfermode"; }
egdaniel41d4f092015-02-09 07:51:00 -080094
egdaniel57d3b032015-11-13 11:57:27 -080095 GrGLSLXferProcessor* createGLSLInstance() const override;
egdaniel41d4f092015-02-09 07:51:00 -080096
Mike Reed7d954ad2016-10-28 15:42:34 -040097 SkBlendMode mode() const { return fMode; }
bsalomonf7cc8772015-05-11 11:21:14 -070098 bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); }
cdalton8917d622015-05-06 13:40:21 -070099
100 GrBlendEquation hwBlendEquation() const {
101 SkASSERT(this->hasHWBlendEquation());
102 return fHWBlendEquation;
103 }
egdaniel41d4f092015-02-09 07:51:00 -0800104
105private:
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500106 GrXferProcessor::OptFlags onGetOptimizations(const FragmentProcessorAnalysis&) const override;
egdanielc19cdc22015-05-10 08:45:18 -0700107
Brian Salomon94efbf52016-11-29 13:43:05 -0500108 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800109
bsalomoncb02b382015-08-12 11:14:50 -0700110 GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const override;
cdalton8917d622015-05-06 13:40:21 -0700111
112 void onGetBlendInfo(BlendInfo*) const override;
113
mtklein36352bf2015-03-25 18:17:31 -0700114 bool onIsEqual(const GrXferProcessor& xpBase) const override;
egdaniel41d4f092015-02-09 07:51:00 -0800115
Mike Reed7d954ad2016-10-28 15:42:34 -0400116 const SkBlendMode fMode;
cdaltonf0df1892015-06-05 13:26:20 -0700117 const GrBlendEquation fHWBlendEquation;
egdaniel41d4f092015-02-09 07:51:00 -0800118
119 typedef GrXferProcessor INHERITED;
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
egdanielfa4cc8b2015-11-13 08:34:52 -0800124class GLCustomXP : public GrGLSLXferProcessor {
egdaniel54f0e9d2015-01-16 06:29:47 -0800125public:
126 GLCustomXP(const GrXferProcessor&) {}
mtklein36352bf2015-03-25 18:17:31 -0700127 ~GLCustomXP() override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800128
Brian Salomon94efbf52016-11-29 13:43:05 -0500129 static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps,
130 GrProcessorKeyBuilder* b) {
cdalton8917d622015-05-06 13:40:21 -0700131 const CustomXP& xp = p.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700132 uint32_t key = 0;
cdalton8917d622015-05-06 13:40:21 -0700133 if (xp.hasHWBlendEquation()) {
134 SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation().
cdaltonedbb31f2015-06-08 12:14:44 -0700135 key |= caps.advBlendEqInteraction();
Brian Salomon94efbf52016-11-29 13:43:05 -0500136 GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4);
cdalton8917d622015-05-06 13:40:21 -0700137 }
138 if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400139 key |= (int)xp.mode() << 3;
cdalton8917d622015-05-06 13:40:21 -0700140 }
bsalomon50785a32015-02-06 07:02:37 -0800141 b->add32(key);
142 }
143
144private:
cdaltonedbb31f2015-06-08 12:14:44 -0700145 void emitOutputsForBlendState(const EmitArgs& args) override {
cdalton8917d622015-05-06 13:40:21 -0700146 const CustomXP& xp = args.fXP.cast<CustomXP>();
cdaltonedbb31f2015-06-08 12:14:44 -0700147 SkASSERT(xp.hasHWBlendEquation());
egdaniel54f0e9d2015-01-16 06:29:47 -0800148
egdaniel4ca2e602015-11-18 08:01:26 -0800149 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
150 fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation());
cdaltonedbb31f2015-06-08 12:14:44 -0700151
cdalton86ae0a92015-06-08 15:11:04 -0700152 // Apply coverage by multiplying it into the src color before blending. Mixed samples will
153 // "just work" automatically. (See onGetOptimizations())
Brian Salomon8c852be2017-01-04 10:44:42 -0500154 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage,
155 args.fInputColor);
egdaniel54f0e9d2015-01-16 06:29:47 -0800156 }
157
egdaniel7ea439b2015-12-03 09:20:44 -0800158 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
159 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800160 const char* srcColor,
egdanielf34b2932015-12-01 13:54:06 -0800161 const char* srcCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -0800162 const char* dstColor,
163 const char* outColor,
egdanielf34b2932015-12-01 13:54:06 -0800164 const char* outColorSecondary,
egdaniel4ca2e602015-11-18 08:01:26 -0800165 const GrXferProcessor& proc) override {
cdaltonedbb31f2015-06-08 12:14:44 -0700166 const CustomXP& xp = proc.cast<CustomXP>();
167 SkASSERT(!xp.hasHWBlendEquation());
168
egdaniel4ca2e602015-11-18 08:01:26 -0800169 GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode());
egdanielf34b2932015-12-01 13:54:06 -0800170
171 // Apply coverage.
robertphillipsf42fca42016-01-27 05:00:04 -0800172 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
173 outColorSecondary, xp);
cdaltonedbb31f2015-06-08 12:14:44 -0700174 }
175
egdaniel018fb622015-10-28 07:26:40 -0700176 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800177
egdanielfa4cc8b2015-11-13 08:34:52 -0800178 typedef GrGLSLXferProcessor INHERITED;
egdaniel54f0e9d2015-01-16 06:29:47 -0800179};
180
181///////////////////////////////////////////////////////////////////////////////
182
Brian Salomon94efbf52016-11-29 13:43:05 -0500183void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel54f0e9d2015-01-16 06:29:47 -0800184 GLCustomXP::GenKey(*this, caps, b);
185}
186
egdaniel57d3b032015-11-13 11:57:27 -0800187GrGLSLXferProcessor* CustomXP::createGLSLInstance() const {
cdalton8917d622015-05-06 13:40:21 -0700188 SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation());
halcanary385fe4d2015-08-26 13:07:48 -0700189 return new GLCustomXP(*this);
egdaniel54f0e9d2015-01-16 06:29:47 -0800190}
191
egdaniel41d4f092015-02-09 07:51:00 -0800192bool CustomXP::onIsEqual(const GrXferProcessor& other) const {
193 const CustomXP& s = other.cast<CustomXP>();
egdanielc19cdc22015-05-10 08:45:18 -0700194 return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
egdaniel54f0e9d2015-01-16 06:29:47 -0800195}
196
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500197GrXferProcessor::OptFlags CustomXP::onGetOptimizations(
198 const FragmentProcessorAnalysis& analysis) const {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500199 /*
200 Most the optimizations we do here are based on tweaking alpha for coverage.
cdalton8917d622015-05-06 13:40:21 -0700201
Brian Salomon92aee3d2016-12-21 09:20:25 -0500202 The general SVG blend equation is defined in the spec as follows:
cdalton8917d622015-05-06 13:40:21 -0700203
Brian Salomon92aee3d2016-12-21 09:20:25 -0500204 Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa)
205 Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa)
cdalton8917d622015-05-06 13:40:21 -0700206
Brian Salomon92aee3d2016-12-21 09:20:25 -0500207 (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha,
208 and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied
209 RGB colors.)
cdalton8917d622015-05-06 13:40:21 -0700210
Brian Salomon92aee3d2016-12-21 09:20:25 -0500211 For every blend mode supported by this class, i.e. the "advanced" blend
212 modes, X=Y=Z=1 and this equation reduces to the PDF blend equation.
cdalton8917d622015-05-06 13:40:21 -0700213
Brian Salomon92aee3d2016-12-21 09:20:25 -0500214 It can be shown that when X=Y=Z=1, these equations can modulate alpha for
215 coverage.
cdalton8917d622015-05-06 13:40:21 -0700216
217
Brian Salomon92aee3d2016-12-21 09:20:25 -0500218 == Color ==
cdalton8917d622015-05-06 13:40:21 -0700219
Brian Salomon92aee3d2016-12-21 09:20:25 -0500220 We substitute Y=Z=1 and define a blend() function that calculates Dca' in
221 terms of premultiplied alpha only:
cdalton8917d622015-05-06 13:40:21 -0700222
Brian Salomon92aee3d2016-12-21 09:20:25 -0500223 blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0,
224 Sca : if Da == 0,
225 B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if
226 Sa,Da != 0}
cdalton8917d622015-05-06 13:40:21 -0700227
Brian Salomon92aee3d2016-12-21 09:20:25 -0500228 And for coverage modulation, we use a post blend src-over model:
cdalton8917d622015-05-06 13:40:21 -0700229
Brian Salomon92aee3d2016-12-21 09:20:25 -0500230 Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700231
Brian Salomon92aee3d2016-12-21 09:20:25 -0500232 (Where f is the fractional coverage.)
cdalton8917d622015-05-06 13:40:21 -0700233
Brian Salomon92aee3d2016-12-21 09:20:25 -0500234 Next we show that canTweakAlphaForCoverage() is true by proving the
235 following relationship:
cdalton8917d622015-05-06 13:40:21 -0700236
Brian Salomon92aee3d2016-12-21 09:20:25 -0500237 blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
cdalton8917d622015-05-06 13:40:21 -0700238
Brian Salomon92aee3d2016-12-21 09:20:25 -0500239 General case (f,Sa,Da != 0):
cdalton8917d622015-05-06 13:40:21 -0700240
Brian Salomon92aee3d2016-12-21 09:20:25 -0500241 f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
242 = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da !=
243 0, definition of blend()]
244 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca
245 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca
246 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca
247 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca
248 = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa)
249 = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0]
250 = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()]
cdalton8917d622015-05-06 13:40:21 -0700251
Brian Salomon92aee3d2016-12-21 09:20:25 -0500252 Corner cases (Sa=0, Da=0, and f=0):
cdalton8917d622015-05-06 13:40:21 -0700253
Brian Salomon92aee3d2016-12-21 09:20:25 -0500254 Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
255 = f * Dca + (1-f) * Dca [Sa=0, definition of blend()]
256 = Dca
257 = blend(0, Dca, 0, Da) [definition of blend()]
258 = blend(f*Sca, Dca, f*Sa, Da) [Sa=0]
cdalton8917d622015-05-06 13:40:21 -0700259
Brian Salomon92aee3d2016-12-21 09:20:25 -0500260 Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
261 = f * Sca + (1-f) * Dca [Da=0, definition of blend()]
262 = f * Sca [Da=0]
263 = blend(f*Sca, 0, f*Sa, 0) [definition of blend()]
264 = blend(f*Sca, Dca, f*Sa, Da) [Da=0]
cdalton8917d622015-05-06 13:40:21 -0700265
Brian Salomon92aee3d2016-12-21 09:20:25 -0500266 f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca
267 = Dca [f=0]
268 = blend(0, Dca, 0, Da) [definition of blend()]
269 = blend(f*Sca, Dca, f*Sa, Da) [f=0]
cdalton8917d622015-05-06 13:40:21 -0700270
Brian Salomon92aee3d2016-12-21 09:20:25 -0500271 == Alpha ==
cdalton8917d622015-05-06 13:40:21 -0700272
Brian Salomon92aee3d2016-12-21 09:20:25 -0500273 We substitute X=Y=Z=1 and define a blend() function that calculates Da':
cdalton8917d622015-05-06 13:40:21 -0700274
Brian Salomon92aee3d2016-12-21 09:20:25 -0500275 blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa)
276 = Sa * Da + Sa - Sa * Da + Da - Da * Sa
277 = Sa + Da - Sa * Da
cdalton8917d622015-05-06 13:40:21 -0700278
Brian Salomon92aee3d2016-12-21 09:20:25 -0500279 We use the same model for coverage modulation as we did with color:
cdalton8917d622015-05-06 13:40:21 -0700280
Brian Salomon92aee3d2016-12-21 09:20:25 -0500281 Da'' = f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700282
Brian Salomon92aee3d2016-12-21 09:20:25 -0500283 And show that canTweakAlphaForCoverage() is true by proving the following
284 relationship:
cdalton8917d622015-05-06 13:40:21 -0700285
Brian Salomon92aee3d2016-12-21 09:20:25 -0500286 blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da
cdalton8917d622015-05-06 13:40:21 -0700287
288
Brian Salomon92aee3d2016-12-21 09:20:25 -0500289 f * blend(Sa, Da) + (1-f) * Da
290 = f * (Sa + Da - Sa * Da) + (1-f) * Da
291 = f*Sa + f*Da - f*Sa * Da + Da - f*Da
292 = f*Sa - f*Sa * Da + Da
293 = f*Sa + Da - f*Sa * Da
294 = blend(f*Sa, Da)
295 */
cdalton8917d622015-05-06 13:40:21 -0700296
bsalomon7765a472015-07-08 11:26:37 -0700297 OptFlags flags = kNone_OptFlags;
Brian Salomon5298dc82017-02-22 11:52:03 -0500298 if (analysis.isCompatibleWithCoverageAsAlpha()) {
cdalton32117702015-05-11 11:21:23 -0700299 flags |= kCanTweakAlphaForCoverage_OptFlag;
cdalton8917d622015-05-06 13:40:21 -0700300 }
cdalton8917d622015-05-06 13:40:21 -0700301 return flags;
302}
303
bsalomoncb02b382015-08-12 11:14:50 -0700304GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const {
cdalton8917d622015-05-06 13:40:21 -0700305 if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
bsalomoncb02b382015-08-12 11:14:50 -0700306 return kBlend_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700307 }
bsalomoncb02b382015-08-12 11:14:50 -0700308 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700309}
310
311void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const {
312 if (this->hasHWBlendEquation()) {
313 blendInfo->fEquation = this->hwBlendEquation();
314 }
egdaniel54f0e9d2015-01-16 06:29:47 -0800315}
316
317///////////////////////////////////////////////////////////////////////////////
Brian Salomona1633922017-01-09 11:46:10 -0500318
319// See the comment above GrXPFactory's definition about this warning suppression.
320#if defined(__GNUC__) || defined(__clang)
321#pragma GCC diagnostic push
322#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
323#endif
bsalomonae4738f2015-09-15 15:33:27 -0700324class CustomXPFactory : public GrXPFactory {
325public:
Brian Salomona1633922017-01-09 11:46:10 -0500326 constexpr CustomXPFactory(SkBlendMode mode)
327 : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {}
egdaniel54f0e9d2015-01-16 06:29:47 -0800328
bsalomonae4738f2015-09-15 15:33:27 -0700329private:
330 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
Brian Salomon5298dc82017-02-22 11:52:03 -0500331 const FragmentProcessorAnalysis&,
bsalomonae4738f2015-09-15 15:33:27 -0700332 bool hasMixedSamples,
333 const DstTexture*) const override;
334
Brian Salomon5298dc82017-02-22 11:52:03 -0500335 bool willReadsDst(const FragmentProcessorAnalysis&) const override { return true; }
Brian Salomon9a514982017-02-14 10:28:22 -0500336
Brian Salomon5298dc82017-02-22 11:52:03 -0500337 bool onWillReadDstInShader(const GrCaps&, const FragmentProcessorAnalysis&) const override;
bsalomonae4738f2015-09-15 15:33:27 -0700338
339 GR_DECLARE_XP_FACTORY_TEST;
340
Mike Reed7d954ad2016-10-28 15:42:34 -0400341 SkBlendMode fMode;
342 GrBlendEquation fHWBlendEquation;
bsalomonae4738f2015-09-15 15:33:27 -0700343
344 typedef GrXPFactory INHERITED;
345};
Brian Salomona1633922017-01-09 11:46:10 -0500346#if defined(__GNUC__) || defined(__clang)
347#pragma GCC diagnostic pop
348#endif
egdaniel54f0e9d2015-01-16 06:29:47 -0800349
bsalomonae4738f2015-09-15 15:33:27 -0700350GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps,
Brian Salomon5298dc82017-02-22 11:52:03 -0500351 const FragmentProcessorAnalysis& analysis,
bsalomonae4738f2015-09-15 15:33:27 -0700352 bool hasMixedSamples,
353 const DstTexture* dstTexture) const {
Brian Salomona1633922017-01-09 11:46:10 -0500354 SkASSERT(GrCustomXfermode::IsSupportedMode(fMode));
Brian Salomon5298dc82017-02-22 11:52:03 -0500355 if (can_use_hw_blend_equation(fHWBlendEquation, analysis.usesPLSDstRead(),
356 analysis.hasLCDCoverage(), caps)) {
cdaltonf0df1892015-06-05 13:26:20 -0700357 SkASSERT(!dstTexture || !dstTexture->texture());
halcanary385fe4d2015-08-26 13:07:48 -0700358 return new CustomXP(fMode, fHWBlendEquation);
cdaltonf0df1892015-06-05 13:26:20 -0700359 }
halcanary385fe4d2015-08-26 13:07:48 -0700360 return new CustomXP(dstTexture, hasMixedSamples, fMode);
egdaniel41d4f092015-02-09 07:51:00 -0800361}
362
Brian Salomon5298dc82017-02-22 11:52:03 -0500363bool CustomXPFactory::onWillReadDstInShader(const GrCaps& caps,
364 const FragmentProcessorAnalysis& analysis) const {
Brian Salomon5be6c952017-01-20 19:06:29 +0000365 // This should not be called if we're using PLS dst read.
366 static constexpr bool kUsesPLSRead = false;
Brian Salomon5298dc82017-02-22 11:52:03 -0500367 return !can_use_hw_blend_equation(fHWBlendEquation, kUsesPLSRead, analysis.hasLCDCoverage(),
368 caps);
cdalton8917d622015-05-06 13:40:21 -0700369}
egdaniel41d4f092015-02-09 07:51:00 -0800370
bsalomonae4738f2015-09-15 15:33:27 -0700371GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory);
Hal Canary6f6961e2017-01-31 13:50:44 -0500372#if GR_TEST_UTILS
Brian Salomona1633922017-01-09 11:46:10 -0500373const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) {
Mike Reedd4706732016-11-15 16:44:34 -0500374 int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1,
375 (int)SkBlendMode::kLastSeparableMode);
egdaniel54f0e9d2015-01-16 06:29:47 -0800376
Brian Salomona1633922017-01-09 11:46:10 -0500377 return GrCustomXfermode::Get((SkBlendMode)mode);
egdaniel0063a9b2015-01-15 10:52:32 -0800378}
Hal Canary6f6961e2017-01-31 13:50:44 -0500379#endif
egdaniel0063a9b2015-01-15 10:52:32 -0800380
bsalomonae4738f2015-09-15 15:33:27 -0700381///////////////////////////////////////////////////////////////////////////////
382
Brian Salomona1633922017-01-09 11:46:10 -0500383const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) {
384 // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are
385 // null.
386#ifdef SK_BUILD_FOR_WIN
387#define _CONSTEXPR_
388#else
389#define _CONSTEXPR_ constexpr
390#endif
391 static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay);
392 static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken);
393 static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten);
394 static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge);
395 static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn);
396 static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight);
397 static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight);
398 static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference);
399 static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion);
400 static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply);
401 static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue);
402 static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation);
403 static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor);
404 static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity);
405#undef _CONSTEXPR_
Brian Salomonc747bb62017-01-06 16:17:50 -0500406 switch (mode) {
407 case SkBlendMode::kOverlay:
Brian Salomona1633922017-01-09 11:46:10 -0500408 return &gOverlay;
Brian Salomonc747bb62017-01-06 16:17:50 -0500409 case SkBlendMode::kDarken:
Brian Salomona1633922017-01-09 11:46:10 -0500410 return &gDarken;
Brian Salomonc747bb62017-01-06 16:17:50 -0500411 case SkBlendMode::kLighten:
Brian Salomona1633922017-01-09 11:46:10 -0500412 return &gLighten;
Brian Salomonc747bb62017-01-06 16:17:50 -0500413 case SkBlendMode::kColorDodge:
Brian Salomona1633922017-01-09 11:46:10 -0500414 return &gColorDodge;
Brian Salomonc747bb62017-01-06 16:17:50 -0500415 case SkBlendMode::kColorBurn:
Brian Salomona1633922017-01-09 11:46:10 -0500416 return &gColorBurn;
Brian Salomonc747bb62017-01-06 16:17:50 -0500417 case SkBlendMode::kHardLight:
Brian Salomona1633922017-01-09 11:46:10 -0500418 return &gHardLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500419 case SkBlendMode::kSoftLight:
Brian Salomona1633922017-01-09 11:46:10 -0500420 return &gSoftLight;
Brian Salomonc747bb62017-01-06 16:17:50 -0500421 case SkBlendMode::kDifference:
Brian Salomona1633922017-01-09 11:46:10 -0500422 return &gDifference;
Brian Salomonc747bb62017-01-06 16:17:50 -0500423 case SkBlendMode::kExclusion:
Brian Salomona1633922017-01-09 11:46:10 -0500424 return &gExclusion;
Brian Salomonc747bb62017-01-06 16:17:50 -0500425 case SkBlendMode::kMultiply:
Brian Salomona1633922017-01-09 11:46:10 -0500426 return &gMultiply;
Brian Salomonc747bb62017-01-06 16:17:50 -0500427 case SkBlendMode::kHue:
Brian Salomona1633922017-01-09 11:46:10 -0500428 return &gHue;
Brian Salomonc747bb62017-01-06 16:17:50 -0500429 case SkBlendMode::kSaturation:
Brian Salomona1633922017-01-09 11:46:10 -0500430 return &gSaturation;
Brian Salomonc747bb62017-01-06 16:17:50 -0500431 case SkBlendMode::kColor:
Brian Salomona1633922017-01-09 11:46:10 -0500432 return &gColor;
Brian Salomonc747bb62017-01-06 16:17:50 -0500433 case SkBlendMode::kLuminosity:
Brian Salomona1633922017-01-09 11:46:10 -0500434 return &gLuminosity;
Brian Salomonc747bb62017-01-06 16:17:50 -0500435 default:
436 SkASSERT(!GrCustomXfermode::IsSupportedMode(mode));
437 return nullptr;
bsalomonae4738f2015-09-15 15:33:27 -0700438 }
439}