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 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 10 | #include "GrCaps.h" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 11 | #include "GrCoordTransform.h" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 12 | #include "GrFragmentProcessor.h" |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 13 | #include "GrPipeline.h" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 14 | #include "GrProcessor.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 15 | #include "GrShaderCaps.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLBlend.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 19 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 20 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLXferProcessor.h" |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 22 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 23 | bool GrCustomXfermode::IsSupportedMode(SkBlendMode mode) { |
| 24 | return (int)mode > (int)SkBlendMode::kLastCoeffMode && |
| 25 | (int)mode <= (int)SkBlendMode::kLastMode; |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Static helpers |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 32 | static constexpr GrBlendEquation hw_blend_equation(SkBlendMode mode) { |
| 33 | // In C++14 this could be a constexpr int variable. |
| 34 | #define EQ_OFFSET (kOverlay_GrBlendEquation - (int)SkBlendMode::kOverlay) |
| 35 | GR_STATIC_ASSERT(kOverlay_GrBlendEquation == (int)SkBlendMode::kOverlay + EQ_OFFSET); |
| 36 | GR_STATIC_ASSERT(kDarken_GrBlendEquation == (int)SkBlendMode::kDarken + EQ_OFFSET); |
| 37 | GR_STATIC_ASSERT(kLighten_GrBlendEquation == (int)SkBlendMode::kLighten + EQ_OFFSET); |
| 38 | GR_STATIC_ASSERT(kColorDodge_GrBlendEquation == (int)SkBlendMode::kColorDodge + EQ_OFFSET); |
| 39 | GR_STATIC_ASSERT(kColorBurn_GrBlendEquation == (int)SkBlendMode::kColorBurn + EQ_OFFSET); |
| 40 | GR_STATIC_ASSERT(kHardLight_GrBlendEquation == (int)SkBlendMode::kHardLight + EQ_OFFSET); |
| 41 | GR_STATIC_ASSERT(kSoftLight_GrBlendEquation == (int)SkBlendMode::kSoftLight + EQ_OFFSET); |
| 42 | GR_STATIC_ASSERT(kDifference_GrBlendEquation == (int)SkBlendMode::kDifference + EQ_OFFSET); |
| 43 | GR_STATIC_ASSERT(kExclusion_GrBlendEquation == (int)SkBlendMode::kExclusion + EQ_OFFSET); |
| 44 | GR_STATIC_ASSERT(kMultiply_GrBlendEquation == (int)SkBlendMode::kMultiply + EQ_OFFSET); |
| 45 | GR_STATIC_ASSERT(kHSLHue_GrBlendEquation == (int)SkBlendMode::kHue + EQ_OFFSET); |
| 46 | GR_STATIC_ASSERT(kHSLSaturation_GrBlendEquation == (int)SkBlendMode::kSaturation + EQ_OFFSET); |
| 47 | GR_STATIC_ASSERT(kHSLColor_GrBlendEquation == (int)SkBlendMode::kColor + EQ_OFFSET); |
| 48 | GR_STATIC_ASSERT(kHSLLuminosity_GrBlendEquation == (int)SkBlendMode::kLuminosity + EQ_OFFSET); |
Mike Klein | f2b35e4 | 2018-11-06 16:30:04 +0000 | [diff] [blame] | 49 | GR_STATIC_ASSERT(kGrBlendEquationCnt == (int)SkBlendMode::kLastMode + 1 + EQ_OFFSET); |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 50 | return static_cast<GrBlendEquation>((int)mode + EQ_OFFSET); |
| 51 | #undef EQ_OFFSET |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 54 | static bool can_use_hw_blend_equation(GrBlendEquation equation, |
| 55 | GrProcessorAnalysisCoverage coverage, const GrCaps& caps) { |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 56 | if (!caps.advancedBlendEquationSupport()) { |
| 57 | return false; |
| 58 | } |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 59 | if (GrProcessorAnalysisCoverage::kLCD == coverage) { |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 60 | return false; // LCD coverage must be applied after the blend equation. |
| 61 | } |
cdalton | 1dd0542 | 2015-06-12 09:01:18 -0700 | [diff] [blame] | 62 | if (caps.canUseAdvancedBlendEquation(equation)) { |
| 63 | return false; |
| 64 | } |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | // Xfer Processor |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 72 | class CustomXP : public GrXferProcessor { |
| 73 | public: |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 74 | CustomXP(SkBlendMode mode, GrBlendEquation hwBlendEquation) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 75 | : INHERITED(kCustomXP_ClassID) |
| 76 | , fMode(mode) |
| 77 | , fHWBlendEquation(hwBlendEquation) {} |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 78 | |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 79 | CustomXP(bool hasMixedSamples, SkBlendMode mode, GrProcessorAnalysisCoverage coverage) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 80 | : INHERITED(kCustomXP_ClassID, true, hasMixedSamples, coverage) |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 81 | , fMode(mode) |
Mike Klein | f2b35e4 | 2018-11-06 16:30:04 +0000 | [diff] [blame] | 82 | , fHWBlendEquation(static_cast<GrBlendEquation>(-1)) { |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 83 | } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | const char* name() const override { return "Custom Xfermode"; } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 86 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 87 | GrGLSLXferProcessor* createGLSLInstance() const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 88 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 89 | SkBlendMode mode() const { return fMode; } |
Mike Klein | f2b35e4 | 2018-11-06 16:30:04 +0000 | [diff] [blame] | 90 | bool hasHWBlendEquation() const { return -1 != static_cast<int>(fHWBlendEquation); } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 91 | |
| 92 | GrBlendEquation hwBlendEquation() const { |
| 93 | SkASSERT(this->hasHWBlendEquation()); |
| 94 | return fHWBlendEquation; |
| 95 | } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 96 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 97 | GrXferBarrierType xferBarrierType(const GrCaps&) const override; |
| 98 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 99 | private: |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 100 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 101 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 102 | void onGetBlendInfo(BlendInfo*) const override; |
| 103 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 104 | bool onIsEqual(const GrXferProcessor& xpBase) const override; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 105 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 106 | const SkBlendMode fMode; |
cdalton | f0df189 | 2015-06-05 13:26:20 -0700 | [diff] [blame] | 107 | const GrBlendEquation fHWBlendEquation; |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 108 | |
| 109 | typedef GrXferProcessor INHERITED; |
| 110 | }; |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 114 | class GLCustomXP : public GrGLSLXferProcessor { |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 115 | public: |
| 116 | GLCustomXP(const GrXferProcessor&) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 117 | ~GLCustomXP() override {} |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 118 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 119 | static void GenKey(const GrXferProcessor& p, const GrShaderCaps& caps, |
| 120 | GrProcessorKeyBuilder* b) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 121 | const CustomXP& xp = p.cast<CustomXP>(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 122 | uint32_t key = 0; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 123 | if (xp.hasHWBlendEquation()) { |
| 124 | SkASSERT(caps.advBlendEqInteraction() > 0); // 0 will mean !xp.hasHWBlendEquation(). |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 125 | key |= caps.advBlendEqInteraction(); |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 126 | GR_STATIC_ASSERT(GrShaderCaps::kLast_AdvBlendEqInteraction < 4); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 127 | } |
| 128 | if (!xp.hasHWBlendEquation() || caps.mustEnableSpecificAdvBlendEqs()) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 129 | key |= (int)xp.mode() << 3; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 130 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 131 | b->add32(key); |
| 132 | } |
| 133 | |
| 134 | private: |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 135 | void emitOutputsForBlendState(const EmitArgs& args) override { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 136 | const CustomXP& xp = args.fXP.cast<CustomXP>(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 137 | SkASSERT(xp.hasHWBlendEquation()); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 138 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 139 | GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
| 140 | fragBuilder->enableAdvancedBlendEquationIfNeeded(xp.hwBlendEquation()); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 141 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 142 | // Apply coverage by multiplying it into the src color before blending. Mixed samples will |
| 143 | // "just work" automatically. (See onGetOptimizations()) |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 144 | fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputPrimary, args.fInputCoverage, |
| 145 | args.fInputColor); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 146 | } |
| 147 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 148 | void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder, |
| 149 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 150 | const char* srcColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 151 | const char* srcCoverage, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 152 | const char* dstColor, |
| 153 | const char* outColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 154 | const char* outColorSecondary, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 155 | const GrXferProcessor& proc) override { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 156 | const CustomXP& xp = proc.cast<CustomXP>(); |
| 157 | SkASSERT(!xp.hasHWBlendEquation()); |
| 158 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 159 | GrGLSLBlend::AppendMode(fragBuilder, srcColor, dstColor, outColor, xp.mode()); |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 160 | |
| 161 | // Apply coverage. |
robertphillips | f42fca4 | 2016-01-27 05:00:04 -0800 | [diff] [blame] | 162 | INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor, |
| 163 | outColorSecondary, xp); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 164 | } |
| 165 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 166 | void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {} |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 167 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 168 | typedef GrGLSLXferProcessor INHERITED; |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /////////////////////////////////////////////////////////////////////////////// |
| 172 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 173 | void CustomXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 174 | GLCustomXP::GenKey(*this, caps, b); |
| 175 | } |
| 176 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 177 | GrGLSLXferProcessor* CustomXP::createGLSLInstance() const { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 178 | SkASSERT(this->willReadDstColor() != this->hasHWBlendEquation()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 179 | return new GLCustomXP(*this); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 180 | } |
| 181 | |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 182 | bool CustomXP::onIsEqual(const GrXferProcessor& other) const { |
| 183 | const CustomXP& s = other.cast<CustomXP>(); |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 184 | return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation; |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 187 | GrXferBarrierType CustomXP::xferBarrierType(const GrCaps& caps) const { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 188 | if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) { |
| 189 | return kBlend_GrXferBarrierType; |
| 190 | } |
| 191 | return kNone_GrXferBarrierType; |
| 192 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 193 | |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 194 | void CustomXP::onGetBlendInfo(BlendInfo* blendInfo) const { |
| 195 | if (this->hasHWBlendEquation()) { |
| 196 | blendInfo->fEquation = this->hwBlendEquation(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /////////////////////////////////////////////////////////////////////////////// |
| 201 | |
| 202 | // See the comment above GrXPFactory's definition about this warning suppression. |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 203 | #if defined(__GNUC__) |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 204 | #pragma GCC diagnostic push |
| 205 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" |
| 206 | #endif |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 207 | #if defined(__clang__) |
| 208 | #pragma clang diagnostic push |
| 209 | #pragma clang diagnostic ignored "-Wnon-virtual-dtor" |
| 210 | #endif |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 211 | class CustomXPFactory : public GrXPFactory { |
| 212 | public: |
| 213 | constexpr CustomXPFactory(SkBlendMode mode) |
| 214 | : fMode(mode), fHWBlendEquation(hw_blend_equation(mode)) {} |
| 215 | |
| 216 | private: |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 217 | sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&, |
| 218 | GrProcessorAnalysisCoverage, |
| 219 | bool hasMixedSamples, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 220 | const GrCaps&) const override; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 221 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 222 | AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&, |
| 223 | const GrProcessorAnalysisCoverage&, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 224 | const GrCaps&) const override; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 225 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 226 | GR_DECLARE_XP_FACTORY_TEST |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 227 | |
| 228 | SkBlendMode fMode; |
| 229 | GrBlendEquation fHWBlendEquation; |
| 230 | |
| 231 | typedef GrXPFactory INHERITED; |
| 232 | }; |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 233 | #if defined(__GNUC__) |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 234 | #pragma GCC diagnostic pop |
| 235 | #endif |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 236 | #if defined(__clang__) |
| 237 | #pragma clang diagnostic pop |
| 238 | #endif |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 239 | |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 240 | sk_sp<const GrXferProcessor> CustomXPFactory::makeXferProcessor( |
| 241 | const GrProcessorAnalysisColor&, |
| 242 | GrProcessorAnalysisCoverage coverage, |
| 243 | bool hasMixedSamples, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 244 | const GrCaps& caps) const { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 245 | SkASSERT(GrCustomXfermode::IsSupportedMode(fMode)); |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 246 | if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) { |
Brian Salomon | a076d87 | 2017-04-04 15:17:03 -0400 | [diff] [blame] | 247 | return sk_sp<GrXferProcessor>(new CustomXP(fMode, fHWBlendEquation)); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 248 | } |
Greg Daniel | 6ebe4b9 | 2017-05-19 10:56:46 -0400 | [diff] [blame] | 249 | return sk_sp<GrXferProcessor>(new CustomXP(hasMixedSamples, fMode, coverage)); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | GrXPFactory::AnalysisProperties CustomXPFactory::analysisProperties( |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 253 | const GrProcessorAnalysisColor&, const GrProcessorAnalysisCoverage& coverage, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 254 | const GrCaps& caps) const { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 255 | /* |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 256 | The general SVG blend equation is defined in the spec as follows: |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 257 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 258 | Dca' = B(Sc, Dc) * Sa * Da + Y * Sca * (1-Da) + Z * Dca * (1-Sa) |
| 259 | Da' = X * Sa * Da + Y * Sa * (1-Da) + Z * Da * (1-Sa) |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 260 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 261 | (Note that Sca, Dca indicate RGB vectors that are premultiplied by alpha, |
| 262 | and that B(Sc, Dc) is a mode-specific function that accepts non-multiplied |
| 263 | RGB colors.) |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 264 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 265 | For every blend mode supported by this class, i.e. the "advanced" blend |
| 266 | modes, X=Y=Z=1 and this equation reduces to the PDF blend equation. |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 267 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 268 | It can be shown that when X=Y=Z=1, these equations can modulate alpha for |
| 269 | coverage. |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 270 | |
| 271 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 272 | == Color == |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 273 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 274 | We substitute Y=Z=1 and define a blend() function that calculates Dca' in |
| 275 | terms of premultiplied alpha only: |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 276 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 277 | blend(Sca, Dca, Sa, Da) = {Dca : if Sa == 0, |
| 278 | Sca : if Da == 0, |
| 279 | B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa) : if |
| 280 | Sa,Da != 0} |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 281 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 282 | And for coverage modulation, we use a post blend src-over model: |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 283 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 284 | Dca'' = f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 285 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 286 | (Where f is the fractional coverage.) |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 287 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 288 | Next we show that canTweakAlphaForCoverage() is true by proving the |
| 289 | following relationship: |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 290 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 291 | blend(f*Sca, Dca, f*Sa, Da) == f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 292 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 293 | General case (f,Sa,Da != 0): |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 294 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 295 | f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 296 | = f * (B(Sca/Sa, Dca/Da) * Sa * Da + Sca * (1-Da) + Dca * (1-Sa)) + (1-f) * Dca [Sa,Da != |
| 297 | 0, definition of blend()] |
| 298 | = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + f*Dca * (1-Sa) + Dca - f*Dca |
| 299 | = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da + f*Dca - f*Dca * Sa + Dca - f*Dca |
| 300 | = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca - f*Sca * Da - f*Dca * Sa + Dca |
| 301 | = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) - f*Dca * Sa + Dca |
| 302 | = B(Sca/Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) |
| 303 | = B(f*Sca/f*Sa, Dca/Da) * f*Sa * Da + f*Sca * (1-Da) + Dca * (1 - f*Sa) [f!=0] |
| 304 | = blend(f*Sca, Dca, f*Sa, Da) [definition of blend()] |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 305 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 306 | Corner cases (Sa=0, Da=0, and f=0): |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 307 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 308 | Sa=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 309 | = f * Dca + (1-f) * Dca [Sa=0, definition of blend()] |
| 310 | = Dca |
| 311 | = blend(0, Dca, 0, Da) [definition of blend()] |
| 312 | = blend(f*Sca, Dca, f*Sa, Da) [Sa=0] |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 313 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 314 | Da=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 315 | = f * Sca + (1-f) * Dca [Da=0, definition of blend()] |
| 316 | = f * Sca [Da=0] |
| 317 | = blend(f*Sca, 0, f*Sa, 0) [definition of blend()] |
| 318 | = blend(f*Sca, Dca, f*Sa, Da) [Da=0] |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 319 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 320 | f=0: f * blend(Sca, Dca, Sa, Da) + (1-f) * Dca |
| 321 | = Dca [f=0] |
| 322 | = blend(0, Dca, 0, Da) [definition of blend()] |
| 323 | = blend(f*Sca, Dca, f*Sa, Da) [f=0] |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 324 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 325 | == Alpha == |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 326 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 327 | We substitute X=Y=Z=1 and define a blend() function that calculates Da': |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 328 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 329 | blend(Sa, Da) = Sa * Da + Sa * (1-Da) + Da * (1-Sa) |
| 330 | = Sa * Da + Sa - Sa * Da + Da - Da * Sa |
| 331 | = Sa + Da - Sa * Da |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 332 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 333 | We use the same model for coverage modulation as we did with color: |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 334 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 335 | Da'' = f * blend(Sa, Da) + (1-f) * Da |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 336 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 337 | And show that canTweakAlphaForCoverage() is true by proving the following |
| 338 | relationship: |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 339 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 340 | blend(f*Sa, Da) == f * blend(Sa, Da) + (1-f) * Da |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 341 | |
| 342 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 343 | f * blend(Sa, Da) + (1-f) * Da |
| 344 | = f * (Sa + Da - Sa * Da) + (1-f) * Da |
| 345 | = f*Sa + f*Da - f*Sa * Da + Da - f*Da |
| 346 | = f*Sa - f*Sa * Da + Da |
| 347 | = f*Sa + Da - f*Sa * Da |
| 348 | = blend(f*Sa, Da) |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 349 | */ |
| 350 | if (can_use_hw_blend_equation(fHWBlendEquation, coverage, caps)) { |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 351 | if (caps.blendEquationSupport() == GrCaps::kAdvancedCoherent_BlendEquationSupport) { |
| 352 | return AnalysisProperties::kCompatibleWithAlphaAsCoverage; |
| 353 | } else { |
| 354 | return AnalysisProperties::kCompatibleWithAlphaAsCoverage | |
| 355 | AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws; |
| 356 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 357 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 358 | return AnalysisProperties::kCompatibleWithAlphaAsCoverage | |
| 359 | AnalysisProperties::kReadsDstInShader; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 360 | } |
egdaniel | 41d4f09 | 2015-02-09 07:51:00 -0800 | [diff] [blame] | 361 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 362 | GR_DEFINE_XP_FACTORY_TEST(CustomXPFactory); |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 363 | #if GR_TEST_UTILS |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 364 | const GrXPFactory* CustomXPFactory::TestGet(GrProcessorTestData* d) { |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 365 | int mode = d->fRandom->nextRangeU((int)SkBlendMode::kLastCoeffMode + 1, |
| 366 | (int)SkBlendMode::kLastSeparableMode); |
egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 367 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 368 | return GrCustomXfermode::Get((SkBlendMode)mode); |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 369 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 370 | #endif |
egdaniel | 0063a9b | 2015-01-15 10:52:32 -0800 | [diff] [blame] | 371 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 372 | /////////////////////////////////////////////////////////////////////////////// |
| 373 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 374 | const GrXPFactory* GrCustomXfermode::Get(SkBlendMode mode) { |
| 375 | // If these objects are constructed as static constexpr by cl.exe (2015 SP2) the vtables are |
| 376 | // null. |
| 377 | #ifdef SK_BUILD_FOR_WIN |
| 378 | #define _CONSTEXPR_ |
| 379 | #else |
| 380 | #define _CONSTEXPR_ constexpr |
| 381 | #endif |
| 382 | static _CONSTEXPR_ const CustomXPFactory gOverlay(SkBlendMode::kOverlay); |
| 383 | static _CONSTEXPR_ const CustomXPFactory gDarken(SkBlendMode::kDarken); |
| 384 | static _CONSTEXPR_ const CustomXPFactory gLighten(SkBlendMode::kLighten); |
| 385 | static _CONSTEXPR_ const CustomXPFactory gColorDodge(SkBlendMode::kColorDodge); |
| 386 | static _CONSTEXPR_ const CustomXPFactory gColorBurn(SkBlendMode::kColorBurn); |
| 387 | static _CONSTEXPR_ const CustomXPFactory gHardLight(SkBlendMode::kHardLight); |
| 388 | static _CONSTEXPR_ const CustomXPFactory gSoftLight(SkBlendMode::kSoftLight); |
| 389 | static _CONSTEXPR_ const CustomXPFactory gDifference(SkBlendMode::kDifference); |
| 390 | static _CONSTEXPR_ const CustomXPFactory gExclusion(SkBlendMode::kExclusion); |
| 391 | static _CONSTEXPR_ const CustomXPFactory gMultiply(SkBlendMode::kMultiply); |
| 392 | static _CONSTEXPR_ const CustomXPFactory gHue(SkBlendMode::kHue); |
| 393 | static _CONSTEXPR_ const CustomXPFactory gSaturation(SkBlendMode::kSaturation); |
| 394 | static _CONSTEXPR_ const CustomXPFactory gColor(SkBlendMode::kColor); |
| 395 | static _CONSTEXPR_ const CustomXPFactory gLuminosity(SkBlendMode::kLuminosity); |
| 396 | #undef _CONSTEXPR_ |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 397 | switch (mode) { |
| 398 | case SkBlendMode::kOverlay: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 399 | return &gOverlay; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 400 | case SkBlendMode::kDarken: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 401 | return &gDarken; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 402 | case SkBlendMode::kLighten: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 403 | return &gLighten; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 404 | case SkBlendMode::kColorDodge: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 405 | return &gColorDodge; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 406 | case SkBlendMode::kColorBurn: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 407 | return &gColorBurn; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 408 | case SkBlendMode::kHardLight: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 409 | return &gHardLight; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 410 | case SkBlendMode::kSoftLight: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 411 | return &gSoftLight; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 412 | case SkBlendMode::kDifference: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 413 | return &gDifference; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 414 | case SkBlendMode::kExclusion: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 415 | return &gExclusion; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 416 | case SkBlendMode::kMultiply: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 417 | return &gMultiply; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 418 | case SkBlendMode::kHue: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 419 | return &gHue; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 420 | case SkBlendMode::kSaturation: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 421 | return &gSaturation; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 422 | case SkBlendMode::kColor: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 423 | return &gColor; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 424 | case SkBlendMode::kLuminosity: |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 425 | return &gLuminosity; |
Brian Salomon | c747bb6 | 2017-01-06 16:17:50 -0500 | [diff] [blame] | 426 | default: |
| 427 | SkASSERT(!GrCustomXfermode::IsSupportedMode(mode)); |
| 428 | return nullptr; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 429 | } |
| 430 | } |