egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 1 | /* |
| 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" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 9 | |
| 10 | #include "GrCoordTransform.h" |
| 11 | #include "GrContext.h" |
| 12 | #include "GrFragmentProcessor.h" |
| 13 | #include "GrInvariantOutput.h" |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 14 | #include "GrPipeline.h" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 15 | #include "GrProcessor.h" |
| 16 | #include "GrTexture.h" |
| 17 | #include "GrTextureAccess.h" |
| 18 | #include "SkXfermode.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLBlend.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 20 | #include "glsl/GrGLSLCaps.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 22 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 23 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 25 | #include "glsl/GrGLSLXferProcessor.h" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 26 | |
| 27 | bool GrCustomXfermode::IsSupportedMode(SkXfermode::Mode mode) { |
| 28 | return mode > SkXfermode::kLastCoeffMode && mode <= SkXfermode::kLastMode; |
| 29 | } |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // Static helpers |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 35 | static GrBlendEquation hw_blend_equation(SkXfermode::Mode mode) { |
| 36 | enum { kOffset = kOverlay_GrBlendEquation - SkXfermode::kOverlay_Mode }; |
| 37 | return static_cast<GrBlendEquation>(mode + kOffset); |
| 38 | |
| 39 | GR_STATIC_ASSERT(kOverlay_GrBlendEquation == SkXfermode::kOverlay_Mode + kOffset); |
| 40 | GR_STATIC_ASSERT(kDarken_GrBlendEquation == SkXfermode::kDarken_Mode + kOffset); |
| 41 | GR_STATIC_ASSERT(kLighten_GrBlendEquation == SkXfermode::kLighten_Mode + kOffset); |
| 42 | GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == SkXfermode::kColorDodge_Mode + kOffset); |
| 43 | GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == SkXfermode::kColorBurn_Mode + kOffset); |
| 44 | GR_STATIC_ASSERT(kHardLight_GrBlendEquation == SkXfermode::kHardLight_Mode + kOffset); |
| 45 | GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == SkXfermode::kSoftLight_Mode + kOffset); |
| 46 | GR_STATIC_ASSERT(kDifference_GrBlendEquation == SkXfermode::kDifference_Mode + kOffset); |
| 47 | GR_STATIC_ASSERT(kExclusion_GrBlendEquation == SkXfermode::kExclusion_Mode + kOffset); |
| 48 | GR_STATIC_ASSERT(kMultiply_GrBlendEquation == SkXfermode::kMultiply_Mode + kOffset); |
| 49 | GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == SkXfermode::kHue_Mode + kOffset); |
| 50 | GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == SkXfermode::kSaturation_Mode + kOffset); |
| 51 | GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == SkXfermode::kColor_Mode + kOffset); |
| 52 | GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == SkXfermode::kLuminosity_Mode + kOffset); |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 53 | GR_STATIC_ASSERT(kGrBlendEquationCnt == SkXfermode::kLastMode + 1 + kOffset); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 54 | } |
| 55 | |
cdalton | 1dd0542 | 2015-06-12 09:01:18 -0700 | [diff] [blame] | 56 | static bool can_use_hw_blend_equation(GrBlendEquation equation, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 57 | const GrPipelineOptimizations& opt, |
cdalton | 1dd0542 | 2015-06-12 09:01:18 -0700 | [diff] [blame] | 58 | const GrCaps& caps) { |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 59 | if (!caps.advancedBlendEquationSupport()) { |
| 60 | return false; |
| 61 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 62 | if (opt.fOverrides.fUsePLSDstRead) { |
| 63 | return false; |
| 64 | } |
| 65 | if (opt.fCoveragePOI.isFourChannelOutput()) { |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 66 | return false; // LCD coverage must be applied after the blend equation. |
| 67 | } |
cdalton | 1dd0542 | 2015-06-12 09:01:18 -0700 | [diff] [blame] | 68 | if (caps.canUseAdvancedBlendEquation(equation)) { |
| 69 | return false; |
| 70 | } |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | // Xfer Processor |
| 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 78 | class CustomXP : public GrXferProcessor { |
| 79 | public: |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 80 | CustomXP(SkXfermode::Mode mode, GrBlendEquation hwBlendEquation) |
| 81 | : fMode(mode), |
| 82 | fHWBlendEquation(hwBlendEquation) { |
| 83 | this->initClassID<CustomXP>(); |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 84 | } |
| 85 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 86 | CustomXP(const DstTexture* dstTexture, bool hasMixedSamples, SkXfermode::Mode mode) |
| 87 | : INHERITED(dstTexture, true, hasMixedSamples), |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 88 | fMode(mode), |
| 89 | fHWBlendEquation(static_cast<GrBlendEquation>(-1)) { |
| 90 | this->initClassID<CustomXP>(); |
| 91 | } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 92 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | const char* name() const override { return "Custom Xfermode"; } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 94 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 95 | GrGLSLXferProcessor* createGLSLInstance() const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 96 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 97 | SkXfermode::Mode mode() const { return fMode; } |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 98 | bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 99 | |
| 100 | GrBlendEquation hwBlendEquation() const { |
| 101 | SkASSERT(this->hasHWBlendEquation()); |
| 102 | return fHWBlendEquation; |
| 103 | } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 104 | |
| 105 | private: |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 106 | GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations, |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 107 | bool doesStencilWrite, |
| 108 | GrColor* overrideColor, |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 109 | const GrCaps& caps) const override; |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 110 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 111 | void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 112 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 113 | GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const override; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 114 | |
| 115 | void onGetBlendInfo(BlendInfo*) const override; |
| 116 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 117 | bool onIsEqual(const GrXferProcessor& xpBase) const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 118 | |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 119 | const SkXfermode::Mode fMode; |
| 120 | const GrBlendEquation fHWBlendEquation; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 121 | |
| 122 | typedef GrXferProcessor INHERITED; |
| 123 | }; |
| 124 | |
| 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 127 | class GLCustomXP : public GrGLSLXferProcessor { |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 128 | public: |
| 129 | GLCustomXP(const GrXferProcessor&) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | ~GLCustomXP() override {} |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 131 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 132 | static void GenKey(const GrXferProcessor& p, const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) { |
| 133 | const CustomXP& xp = p.cast<CustomXP>(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 134 | uint32_t key = 0; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 135 | if (xp.hasHWBlendEquation()) { |
| 136 | SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation(). |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 137 | key |= caps.advBlendEqInteraction(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 138 | GR_STATIC_ASSERT(GrGLSLCaps::kLast_AdvBlendEqInteraction < 4); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 139 | } |
| 140 | if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 141 | key |= xp.mode() << 3; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 142 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 143 | b->add32(key); |
| 144 | } |
| 145 | |
| 146 | private: |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 147 | void emitOutputsForBlendState(const EmitArgs& args) override { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 148 | const CustomXP& xp = args.fXP.cast<CustomXP>(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 149 | SkASSERT(xp.hasHWBlendEquation()); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 150 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 151 | GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
| 152 | fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation()); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 153 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 154 | // Apply coverage by multiplying it into the src color before blending. Mixed samples will |
| 155 | // "just work" automatically. (See onGetOptimizations()) |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 156 | if (args.fInputCoverage) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 157 | fragBuilder->codeAppendf("%s = %s * %s;", |
| 158 | args.fOutputPrimary, args.fInputCoverage, args.fInputColor); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 159 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 160 | fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, args.fInputColor); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 161 | } |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 162 | } |
| 163 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 164 | void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder, |
| 165 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 166 | const char* srcColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 167 | const char* srcCoverage, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 168 | const char* dstColor, |
| 169 | const char* outColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 170 | const char* outColorSecondary, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 171 | const GrXferProcessor& proc) override { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 172 | const CustomXP& xp = proc.cast<CustomXP>(); |
| 173 | SkASSERT(!xp.hasHWBlendEquation()); |
| 174 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 175 | GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode()); |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 176 | |
| 177 | // Apply coverage. |
robertphillips | f42fca4 | 2016-01-27 05:00:04 -0800 | [diff] [blame] | 178 | INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor, |
| 179 | outColorSecondary, xp); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 180 | } |
| 181 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 182 | void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {} |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 183 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 184 | typedef GrGLSLXferProcessor INHERITED; |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /////////////////////////////////////////////////////////////////////////////// |
| 188 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 189 | void CustomXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 190 | GLCustomXP::GenKey(*this, caps, b); |
| 191 | } |
| 192 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 193 | GrGLSLXferProcessor* CustomXP::createGLSLInstance() const { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 194 | SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 195 | return new GLCustomXP(*this); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 196 | } |
| 197 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 198 | bool CustomXP::onIsEqual(const GrXferProcessor& other) const { |
| 199 | const CustomXP& s = other.cast<CustomXP>(); |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 200 | return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation; |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 201 | } |
| 202 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 203 | GrXferProcessor::OptFlags CustomXP::onGetOptimizations(const GrPipelineOptimizations& optimizations, |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 204 | bool doesStencilWrite, |
| 205 | GrColor* overrideColor, |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 206 | const GrCaps& caps) const { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 207 | /* |
| 208 | Most the optimizations we do here are based on tweaking alpha for coverage. |
| 209 | |
| 210 | The general SVG blend equation is defined in the spec as follows: |
| 211 | |
| 212 | Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa) |
| 213 | Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa) |
| 214 | |
| 215 | (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha, |
| 216 | and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied |
| 217 | RGB colors.) |
| 218 | |
| 219 | For every blend mode supported by this class, i.e. the "advanced" blend |
| 220 | modes, X=Y=Z=1 and this equation reduces to the PDF blend equation. |
| 221 | |
| 222 | It can be shown that when X=Y=Z=1, these equations can modulate alpha for |
| 223 | coverage. |
| 224 | |
| 225 | |
| 226 | == Color == |
| 227 | |
| 228 | We substitute Y=Z=1 and define a blend() function that calculates Dca' in |
| 229 | terms of premultiplied alpha only: |
| 230 | |
| 231 | blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0, |
| 232 | Sca : if Da == 0, |
| 233 | B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if Sa,Da != 0} |
| 234 | |
| 235 | And for coverage modulation, we use a post blend src-over model: |
| 236 | |
| 237 | Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 238 | |
| 239 | (Where f is the fractional coverage.) |
| 240 | |
| 241 | Next we show that canTweakAlphaForCoverage() is true by proving the |
| 242 | following relationship: |
| 243 | |
| 244 | blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 245 | |
| 246 | General case (f,Sa,Da != 0): |
| 247 | |
| 248 | f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 249 | = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da != 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()] |
| 257 | |
| 258 | Corner cases (Sa=0, Da=0, and f=0): |
| 259 | |
| 260 | 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] |
| 265 | |
| 266 | 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] |
| 271 | |
| 272 | 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] |
| 276 | |
| 277 | == Alpha == |
| 278 | |
| 279 | We substitute X=Y=Z=1 and define a blend() function that calculates Da': |
| 280 | |
| 281 | 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 |
| 284 | |
| 285 | We use the same model for coverage modulation as we did with color: |
| 286 | |
| 287 | Da'' = f * blend(Sa, Da) + (1-f) * Da |
| 288 | |
| 289 | And show that canTweakAlphaForCoverage() is true by proving the following |
| 290 | relationship: |
| 291 | |
| 292 | blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da |
| 293 | |
| 294 | |
| 295 | 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 | */ |
| 302 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 303 | OptFlags flags = kNone_OptFlags; |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 304 | if (optimizations.fColorPOI.allStagesMultiplyInput()) { |
cdalton | 3211770 | 2015-05-11 11:21:23 -0700 | [diff] [blame] | 305 | flags |= kCanTweakAlphaForCoverage_OptFlag; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 306 | } |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 307 | if (this->hasHWBlendEquation() && optimizations.fCoveragePOI.isSolidWhite()) { |
cdalton | 3211770 | 2015-05-11 11:21:23 -0700 | [diff] [blame] | 308 | flags |= kIgnoreCoverage_OptFlag; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 309 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 310 | return flags; |
| 311 | } |
| 312 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 313 | GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 314 | if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) { |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 315 | return kBlend_GrXferBarrierType; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 316 | } |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 317 | return kNone_GrXferBarrierType; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const { |
| 321 | if (this->hasHWBlendEquation()) { |
| 322 | blendInfo->fEquation = this->hwBlendEquation(); |
| 323 | } |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 327 | class CustomXPFactory : public GrXPFactory { |
| 328 | public: |
| 329 | CustomXPFactory(SkXfermode::Mode mode); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 330 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 331 | void getInvariantBlendedColor(const GrProcOptInfo& colorPOI, |
| 332 | GrXPFactory::InvariantBlendedColor*) const override; |
| 333 | |
| 334 | private: |
| 335 | GrXferProcessor* onCreateXferProcessor(const GrCaps& caps, |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 336 | const GrPipelineOptimizations& optimizations, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 337 | bool hasMixedSamples, |
| 338 | const DstTexture*) const override; |
| 339 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 340 | bool onWillReadDstColor(const GrCaps& caps, |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 341 | const GrPipelineOptimizations& optimizations, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 342 | bool hasMixedSamples) const override; |
| 343 | |
| 344 | bool onIsEqual(const GrXPFactory& xpfBase) const override { |
| 345 | const CustomXPFactory& xpf = xpfBase.cast<CustomXPFactory>(); |
| 346 | return fMode == xpf.fMode; |
| 347 | } |
| 348 | |
| 349 | GR_DECLARE_XP_FACTORY_TEST; |
| 350 | |
| 351 | SkXfermode::Mode fMode; |
| 352 | GrBlendEquation fHWBlendEquation; |
| 353 | |
| 354 | typedef GrXPFactory INHERITED; |
| 355 | }; |
| 356 | |
| 357 | CustomXPFactory::CustomXPFactory(SkXfermode::Mode mode) |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 358 | : fMode(mode), |
| 359 | fHWBlendEquation(hw_blend_equation(mode)) { |
| 360 | SkASSERT(GrCustomXfermode::IsSupportedMode(fMode)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 361 | this->initClassID<CustomXPFactory>(); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 362 | } |
| 363 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 364 | GrXferProcessor* CustomXPFactory::onCreateXferProcessor(const GrCaps& caps, |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 365 | const GrPipelineOptimizations& opt, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 366 | bool hasMixedSamples, |
| 367 | const DstTexture* dstTexture) const { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 368 | if (can_use_hw_blend_equation(fHWBlendEquation, opt, caps)) { |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 369 | SkASSERT(!dstTexture || !dstTexture->texture()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 370 | return new CustomXP(fMode, fHWBlendEquation); |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 371 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 372 | return new CustomXP(dstTexture, hasMixedSamples, fMode); |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 373 | } |
| 374 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 375 | bool CustomXPFactory::onWillReadDstColor(const GrCaps& caps, |
| 376 | const GrPipelineOptimizations& optimizations, |
| 377 | bool hasMixedSamples) const { |
| 378 | return !can_use_hw_blend_equation(fHWBlendEquation, optimizations, caps); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 379 | } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 380 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 381 | void CustomXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI, |
| 382 | InvariantBlendedColor* blendedColor) const { |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 383 | blendedColor->fWillBlendWithDst = true; |
| 384 | blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags; |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 385 | } |
| 386 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 387 | GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory); |
| 388 | const GrXPFactory* CustomXPFactory::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 389 | int mode = d->fRandom->nextRangeU(SkXfermode::kLastCoeffMode + 1, |
| 390 | SkXfermode::kLastSeparableMode); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 391 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 392 | return new CustomXPFactory(static_cast<SkXfermode::Mode>(mode)); |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 393 | } |
| 394 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 395 | /////////////////////////////////////////////////////////////////////////////// |
| 396 | |
egdaniel | f234272 | 2015-11-20 15:12:59 -0800 | [diff] [blame] | 397 | GrXPFactory* GrCustomXfermode::CreateXPFactory(SkXfermode::Mode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 398 | if (!GrCustomXfermode::IsSupportedMode(mode)) { |
| 399 | return nullptr; |
| 400 | } else { |
| 401 | return new CustomXPFactory(mode); |
| 402 | } |
| 403 | } |