wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 1 | /* |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 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 | */ |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 7 | |
Robert Phillips | 54cbcd7 | 2017-04-20 17:20:24 -0400 | [diff] [blame] | 8 | #include "GrXfermodeFragmentProcessor.h" |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 9 | |
Robert Phillips | 54cbcd7 | 2017-04-20 17:20:24 -0400 | [diff] [blame] | 10 | #include "GrConstColorProcessor.h" |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 13 | #include "glsl/GrGLSLBlend.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 15 | #include "SkGr.h" |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 16 | #include "SkXfermodePriv.h" |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 17 | |
Brian Salomon | 246a3c2 | 2017-02-10 16:16:21 -0500 | [diff] [blame] | 18 | // Some of the cpu implementations of blend modes differ too much from the GPU enough that |
| 19 | // we can't use the cpu implementation to implement constantOutputForConstantInput. |
| 20 | static inline bool does_cpu_blend_impl_match_gpu(SkBlendMode mode) { |
| 21 | // The non-seperable modes differ too much. So does SoftLight. ColorBurn differs too much on our |
| 22 | // test iOS device (but we just disable it across the aboard since it may happen on untested |
| 23 | // GPUs). |
| 24 | return mode <= SkBlendMode::kLastSeparableMode && mode != SkBlendMode::kSoftLight && |
| 25 | mode != SkBlendMode::kColorBurn; |
| 26 | } |
| 27 | |
| 28 | ////////////////////////////////////////////////////////////////////////////// |
| 29 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 30 | class ComposeTwoFragmentProcessor : public GrFragmentProcessor { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 31 | public: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 32 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> src, |
| 33 | sk_sp<GrFragmentProcessor> dst, |
| 34 | SkBlendMode mode) { |
| 35 | return sk_sp<GrFragmentProcessor>(new ComposeTwoFragmentProcessor(std::move(src), |
| 36 | std::move(dst), mode)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 37 | } |
| 38 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 39 | const char* name() const override { return "ComposeTwo"; } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 40 | |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 41 | SkString dumpInfo() const override { |
| 42 | SkString str; |
| 43 | |
| 44 | str.appendf("Mode: %s", SkBlendMode_Name(fMode)); |
| 45 | |
| 46 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 47 | str.appendf(" [%s %s]", |
| 48 | this->childProcessor(i).name(), this->childProcessor(i).dumpInfo().c_str()); |
| 49 | } |
| 50 | return str; |
| 51 | } |
| 52 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 53 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 54 | b->add32((int)fMode); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 57 | SkBlendMode getMode() const { return fMode; } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 58 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 59 | private: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 60 | ComposeTwoFragmentProcessor(sk_sp<GrFragmentProcessor> src, |
| 61 | sk_sp<GrFragmentProcessor> dst, |
| 62 | SkBlendMode mode) |
| 63 | : INHERITED(OptFlags(src.get(), dst.get(), mode)) |
| 64 | , fMode(mode) { |
| 65 | this->initClassID<ComposeTwoFragmentProcessor>(); |
| 66 | SkDEBUGCODE(int shaderAChildIndex = )this->registerChildProcessor(std::move(src)); |
| 67 | SkDEBUGCODE(int shaderBChildIndex = )this->registerChildProcessor(std::move(dst)); |
| 68 | SkASSERT(0 == shaderAChildIndex); |
| 69 | SkASSERT(1 == shaderBChildIndex); |
| 70 | } |
| 71 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 72 | static OptimizationFlags OptFlags(const GrFragmentProcessor* src, |
| 73 | const GrFragmentProcessor* dst, SkBlendMode mode) { |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 74 | OptimizationFlags flags; |
| 75 | switch (mode) { |
| 76 | case SkBlendMode::kClear: |
| 77 | case SkBlendMode::kSrc: |
| 78 | case SkBlendMode::kDst: |
| 79 | SkFAIL("Should never create clear, src, or dst compose two FP."); |
| 80 | flags = kNone_OptimizationFlags; |
| 81 | break; |
| 82 | |
| 83 | // Produces opaque if both src and dst are opaque. |
| 84 | case SkBlendMode::kSrcIn: |
| 85 | case SkBlendMode::kDstIn: |
| 86 | case SkBlendMode::kModulate: |
| 87 | flags = src->preservesOpaqueInput() && dst->preservesOpaqueInput() |
| 88 | ? kPreservesOpaqueInput_OptimizationFlag |
| 89 | : kNone_OptimizationFlags; |
| 90 | break; |
| 91 | |
| 92 | // Produces zero when both are opaque, indeterminate if one is opaque. |
| 93 | case SkBlendMode::kSrcOut: |
| 94 | case SkBlendMode::kDstOut: |
| 95 | case SkBlendMode::kXor: |
| 96 | flags = kNone_OptimizationFlags; |
| 97 | break; |
| 98 | |
| 99 | // Is opaque if the dst is opaque. |
| 100 | case SkBlendMode::kSrcATop: |
| 101 | flags = dst->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 102 | : kNone_OptimizationFlags; |
| 103 | break; |
| 104 | |
| 105 | // DstATop is the converse of kSrcATop. Screen is also opaque if the src is a opaque. |
| 106 | case SkBlendMode::kDstATop: |
| 107 | case SkBlendMode::kScreen: |
| 108 | flags = src->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 109 | : kNone_OptimizationFlags; |
| 110 | break; |
| 111 | |
| 112 | // These modes are all opaque if either src or dst is opaque. All the advanced modes |
| 113 | // compute alpha as src-over. |
| 114 | case SkBlendMode::kSrcOver: |
| 115 | case SkBlendMode::kDstOver: |
| 116 | case SkBlendMode::kPlus: |
| 117 | case SkBlendMode::kOverlay: |
| 118 | case SkBlendMode::kDarken: |
| 119 | case SkBlendMode::kLighten: |
| 120 | case SkBlendMode::kColorDodge: |
| 121 | case SkBlendMode::kColorBurn: |
| 122 | case SkBlendMode::kHardLight: |
| 123 | case SkBlendMode::kSoftLight: |
| 124 | case SkBlendMode::kDifference: |
| 125 | case SkBlendMode::kExclusion: |
| 126 | case SkBlendMode::kMultiply: |
| 127 | case SkBlendMode::kHue: |
| 128 | case SkBlendMode::kSaturation: |
| 129 | case SkBlendMode::kColor: |
| 130 | case SkBlendMode::kLuminosity: |
| 131 | flags = src->preservesOpaqueInput() || dst->preservesOpaqueInput() |
| 132 | ? kPreservesOpaqueInput_OptimizationFlag |
| 133 | : kNone_OptimizationFlags; |
| 134 | break; |
| 135 | } |
Brian Salomon | 246a3c2 | 2017-02-10 16:16:21 -0500 | [diff] [blame] | 136 | if (does_cpu_blend_impl_match_gpu(mode) && src->hasConstantOutputForConstantInput() && |
| 137 | dst->hasConstantOutputForConstantInput()) { |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 138 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 139 | } |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 140 | return flags; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 141 | } |
| 142 | |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 143 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 144 | const ComposeTwoFragmentProcessor& cs = other.cast<ComposeTwoFragmentProcessor>(); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 145 | return fMode == cs.fMode; |
| 146 | } |
| 147 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 148 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 149 | float alpha = input.fRGBA[3]; |
| 150 | input = input.opaque(); |
| 151 | GrColor4f srcColor = ConstantOutputForConstantInput(this->childProcessor(0), input); |
| 152 | GrColor4f dstColor = ConstantOutputForConstantInput(this->childProcessor(1), input); |
| 153 | SkPM4f src = GrColor4fToSkPM4f(srcColor); |
| 154 | SkPM4f dst = GrColor4fToSkPM4f(dstColor); |
Mike Reed | f066ac9 | 2017-06-09 14:36:59 -0400 | [diff] [blame] | 155 | SkPM4f res = SkBlendMode_Apply(fMode, src, dst); |
| 156 | return SkPM4fToGrColor4f(res).mulByScalar(alpha); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 157 | } |
| 158 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 159 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 160 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 161 | SkBlendMode fMode; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 162 | |
| 163 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 164 | |
| 165 | typedef GrFragmentProcessor INHERITED; |
| 166 | }; |
| 167 | |
| 168 | ///////////////////////////////////////////////////////////////////// |
| 169 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 170 | class GLComposeTwoFragmentProcessor : public GrGLSLFragmentProcessor { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 171 | public: |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 172 | void emitCode(EmitArgs&) override; |
| 173 | |
| 174 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 175 | typedef GrGLSLFragmentProcessor INHERITED; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | ///////////////////////////////////////////////////////////////////// |
| 179 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 180 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ComposeTwoFragmentProcessor); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 181 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 182 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 183 | sk_sp<GrFragmentProcessor> ComposeTwoFragmentProcessor::TestCreate(GrProcessorTestData* d) { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 184 | // Create two random frag procs. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 185 | sk_sp<GrFragmentProcessor> fpA(GrProcessorUnitTest::MakeChildFP(d)); |
| 186 | sk_sp<GrFragmentProcessor> fpB(GrProcessorUnitTest::MakeChildFP(d)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 187 | |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 188 | SkBlendMode mode; |
| 189 | do { |
| 190 | mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0, (int)SkBlendMode::kLastMode)); |
| 191 | } while (SkBlendMode::kClear == mode || SkBlendMode::kSrc == mode || SkBlendMode::kDst == mode); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 192 | return sk_sp<GrFragmentProcessor>( |
| 193 | new ComposeTwoFragmentProcessor(std::move(fpA), std::move(fpB), mode)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 194 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 195 | #endif |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 196 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 197 | GrGLSLFragmentProcessor* ComposeTwoFragmentProcessor::onCreateGLSLInstance() const{ |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 198 | return new GLComposeTwoFragmentProcessor; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | ///////////////////////////////////////////////////////////////////// |
| 202 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 203 | void GLComposeTwoFragmentProcessor::emitCode(EmitArgs& args) { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 204 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 205 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 206 | const ComposeTwoFragmentProcessor& cs = args.fFp.cast<ComposeTwoFragmentProcessor>(); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 207 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 208 | const char* inputColor = nullptr; |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 209 | if (args.fInputColor) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 210 | inputColor = "inputColor"; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 211 | fragBuilder->codeAppendf("vec4 inputColor = vec4(%s.rgb, 1.0);", args.fInputColor); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 212 | } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 213 | |
| 214 | // declare outputColor and emit the code for each of the two children |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 215 | SkString srcColor("xfer_src"); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 216 | this->emitChild(0, inputColor, &srcColor, args); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 217 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 218 | SkString dstColor("xfer_dst"); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 219 | this->emitChild(1, inputColor, &dstColor, args); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 220 | |
| 221 | // emit blend code |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 222 | SkBlendMode mode = cs.getMode(); |
Mike Reed | cde9031 | 2017-06-07 23:05:45 -0400 | [diff] [blame] | 223 | fragBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkBlendMode_Name(mode)); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 224 | GrGLSLBlend::AppendMode(fragBuilder, |
| 225 | srcColor.c_str(), |
| 226 | dstColor.c_str(), |
| 227 | args.fOutputColor, |
| 228 | mode); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 229 | |
| 230 | // re-multiply the output color by the input color's alpha |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 231 | if (args.fInputColor) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 232 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 233 | } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 234 | } |
| 235 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 236 | sk_sp<GrFragmentProcessor> GrXfermodeFragmentProcessor::MakeFromTwoProcessors( |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 237 | sk_sp<GrFragmentProcessor> src, sk_sp<GrFragmentProcessor> dst, SkBlendMode mode) { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 238 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 239 | case SkBlendMode::kClear: |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 240 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 241 | GrConstColorProcessor::kIgnore_InputMode); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 242 | case SkBlendMode::kSrc: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 243 | return src; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 244 | case SkBlendMode::kDst: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 245 | return dst; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 246 | default: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 247 | return ComposeTwoFragmentProcessor::Make(std::move(src), std::move(dst), mode); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | ////////////////////////////////////////////////////////////////////////////// |
| 252 | |
| 253 | class ComposeOneFragmentProcessor : public GrFragmentProcessor { |
| 254 | public: |
| 255 | enum Child { |
| 256 | kDst_Child, |
| 257 | kSrc_Child, |
| 258 | }; |
| 259 | |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 260 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> fp, SkBlendMode mode, |
| 261 | Child child) { |
| 262 | if (!fp) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | return sk_sp<GrFragmentProcessor>(new ComposeOneFragmentProcessor(std::move(fp), |
| 266 | mode, child)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | const char* name() const override { return "ComposeOne"; } |
| 270 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 271 | SkString dumpInfo() const override { |
| 272 | SkString str; |
| 273 | |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 274 | str.appendf("Mode: %s, Child: %s", |
| 275 | SkBlendMode_Name(fMode), kDst_Child == fChild ? "Dst" : "Src"); |
| 276 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 277 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 278 | str.appendf(" [%s %s]", |
| 279 | this->childProcessor(i).name(), this->childProcessor(i).dumpInfo().c_str()); |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 280 | } |
| 281 | return str; |
| 282 | } |
| 283 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 284 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 285 | GR_STATIC_ASSERT(((int)SkBlendMode::kLastMode & SK_MaxU16) == (int)SkBlendMode::kLastMode); |
| 286 | b->add32((int)fMode | (fChild << 16)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 289 | SkBlendMode mode() const { return fMode; } |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 290 | |
| 291 | Child child() const { return fChild; } |
| 292 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 293 | private: |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 294 | OptimizationFlags OptFlags(const GrFragmentProcessor* fp, SkBlendMode mode, Child child) { |
| 295 | OptimizationFlags flags; |
| 296 | switch (mode) { |
| 297 | case SkBlendMode::kClear: |
| 298 | SkFAIL("Should never create clear compose one FP."); |
| 299 | flags = kNone_OptimizationFlags; |
| 300 | break; |
| 301 | |
| 302 | case SkBlendMode::kSrc: |
| 303 | SkASSERT(child == kSrc_Child); |
| 304 | flags = fp->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 305 | : kNone_OptimizationFlags; |
| 306 | break; |
| 307 | |
| 308 | case SkBlendMode::kDst: |
| 309 | SkASSERT(child == kDst_Child); |
| 310 | flags = fp->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 311 | : kNone_OptimizationFlags; |
| 312 | break; |
| 313 | |
| 314 | // Produces opaque if both src and dst are opaque. These also will modulate the child's |
Brian Salomon | e026511 | 2017-02-21 14:19:07 -0500 | [diff] [blame] | 315 | // output by either the input color or alpha. However, if the child is not compatible |
| 316 | // with the coverage as alpha then it may produce a color that is not valid premul. |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 317 | case SkBlendMode::kSrcIn: |
| 318 | case SkBlendMode::kDstIn: |
| 319 | case SkBlendMode::kModulate: |
Brian Salomon | e026511 | 2017-02-21 14:19:07 -0500 | [diff] [blame] | 320 | if (fp->compatibleWithCoverageAsAlpha()) { |
| 321 | if (fp->preservesOpaqueInput()) { |
| 322 | flags = kPreservesOpaqueInput_OptimizationFlag | |
| 323 | kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 324 | } else { |
| 325 | flags = kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 326 | } |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 327 | } else { |
Brian Salomon | e026511 | 2017-02-21 14:19:07 -0500 | [diff] [blame] | 328 | flags = fp->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 329 | : kNone_OptimizationFlags; |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 330 | } |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 331 | break; |
| 332 | |
| 333 | // Produces zero when both are opaque, indeterminate if one is opaque. |
| 334 | case SkBlendMode::kSrcOut: |
| 335 | case SkBlendMode::kDstOut: |
| 336 | case SkBlendMode::kXor: |
| 337 | flags = kNone_OptimizationFlags; |
| 338 | break; |
| 339 | |
| 340 | // Is opaque if the dst is opaque. |
| 341 | case SkBlendMode::kSrcATop: |
| 342 | if (child == kDst_Child) { |
| 343 | flags = fp->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 344 | : kNone_OptimizationFlags; |
| 345 | } else { |
| 346 | flags = kPreservesOpaqueInput_OptimizationFlag; |
| 347 | } |
| 348 | break; |
| 349 | |
| 350 | // DstATop is the converse of kSrcATop. Screen is also opaque if the src is a opaque. |
| 351 | case SkBlendMode::kDstATop: |
| 352 | case SkBlendMode::kScreen: |
| 353 | if (child == kSrc_Child) { |
| 354 | flags = fp->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag |
| 355 | : kNone_OptimizationFlags; |
| 356 | } else { |
| 357 | flags = kPreservesOpaqueInput_OptimizationFlag; |
| 358 | } |
| 359 | break; |
| 360 | |
| 361 | // These modes are all opaque if either src or dst is opaque. All the advanced modes |
| 362 | // compute alpha as src-over. |
| 363 | case SkBlendMode::kSrcOver: |
| 364 | case SkBlendMode::kDstOver: |
| 365 | case SkBlendMode::kPlus: |
| 366 | case SkBlendMode::kOverlay: |
| 367 | case SkBlendMode::kDarken: |
| 368 | case SkBlendMode::kLighten: |
| 369 | case SkBlendMode::kColorDodge: |
| 370 | case SkBlendMode::kColorBurn: |
| 371 | case SkBlendMode::kHardLight: |
| 372 | case SkBlendMode::kSoftLight: |
| 373 | case SkBlendMode::kDifference: |
| 374 | case SkBlendMode::kExclusion: |
| 375 | case SkBlendMode::kMultiply: |
| 376 | case SkBlendMode::kHue: |
| 377 | case SkBlendMode::kSaturation: |
| 378 | case SkBlendMode::kColor: |
| 379 | case SkBlendMode::kLuminosity: |
| 380 | flags = kPreservesOpaqueInput_OptimizationFlag; |
| 381 | break; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 382 | } |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 383 | if (does_cpu_blend_impl_match_gpu(mode) && fp->hasConstantOutputForConstantInput()) { |
| 384 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 385 | } |
| 386 | return flags; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 387 | } |
| 388 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 389 | bool onIsEqual(const GrFragmentProcessor& that) const override { |
| 390 | return fMode == that.cast<ComposeOneFragmentProcessor>().fMode; |
| 391 | } |
| 392 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 393 | GrColor4f constantOutputForConstantInput(GrColor4f inputColor) const override { |
| 394 | GrColor4f childColor = |
| 395 | ConstantOutputForConstantInput(this->childProcessor(0), GrColor4f::OpaqueWhite()); |
| 396 | SkPM4f src, dst; |
| 397 | if (kSrc_Child == fChild) { |
| 398 | src = GrColor4fToSkPM4f(childColor); |
| 399 | dst = GrColor4fToSkPM4f(inputColor); |
| 400 | } else { |
| 401 | src = GrColor4fToSkPM4f(inputColor); |
| 402 | dst = GrColor4fToSkPM4f(childColor); |
| 403 | } |
Mike Reed | f066ac9 | 2017-06-09 14:36:59 -0400 | [diff] [blame] | 404 | SkPM4f res = SkBlendMode_Apply(fMode, src, dst); |
| 405 | return SkPM4fToGrColor4f(res); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 406 | } |
| 407 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 408 | private: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 409 | ComposeOneFragmentProcessor(sk_sp<GrFragmentProcessor> fp, SkBlendMode mode, Child child) |
| 410 | : INHERITED(OptFlags(fp.get(), mode, child)) |
| 411 | , fMode(mode) |
| 412 | , fChild(child) { |
| 413 | this->initClassID<ComposeOneFragmentProcessor>(); |
| 414 | SkDEBUGCODE(int dstIndex =) this->registerChildProcessor(std::move(fp)); |
| 415 | SkASSERT(0 == dstIndex); |
| 416 | } |
| 417 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 418 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 419 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 420 | SkBlendMode fMode; |
| 421 | Child fChild; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 422 | |
| 423 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 424 | |
| 425 | typedef GrFragmentProcessor INHERITED; |
| 426 | }; |
| 427 | |
| 428 | ////////////////////////////////////////////////////////////////////////////// |
| 429 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 430 | class GLComposeOneFragmentProcessor : public GrGLSLFragmentProcessor { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 431 | public: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 432 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 433 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 434 | SkBlendMode mode = args.fFp.cast<ComposeOneFragmentProcessor>().mode(); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 435 | ComposeOneFragmentProcessor::Child child = |
| 436 | args.fFp.cast<ComposeOneFragmentProcessor>().child(); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 437 | SkString childColor("child"); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 438 | this->emitChild(0, &childColor, args); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 439 | |
| 440 | const char* inputColor = args.fInputColor; |
| 441 | // We don't try to optimize for this case at all |
| 442 | if (!inputColor) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 443 | fragBuilder->codeAppendf("const vec4 ones = vec4(1);"); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 444 | inputColor = "ones"; |
| 445 | } |
| 446 | |
| 447 | // emit blend code |
Mike Reed | cde9031 | 2017-06-07 23:05:45 -0400 | [diff] [blame] | 448 | fragBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkBlendMode_Name(mode)); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 449 | const char* childStr = childColor.c_str(); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 450 | if (ComposeOneFragmentProcessor::kDst_Child == child) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 451 | GrGLSLBlend::AppendMode(fragBuilder, inputColor, childStr, args.fOutputColor, mode); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 452 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 453 | GrGLSLBlend::AppendMode(fragBuilder, childStr, inputColor, args.fOutputColor, mode); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 454 | } |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 458 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | ///////////////////////////////////////////////////////////////////// |
| 462 | |
| 463 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ComposeOneFragmentProcessor); |
| 464 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 465 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 466 | sk_sp<GrFragmentProcessor> ComposeOneFragmentProcessor::TestCreate(GrProcessorTestData* d) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 467 | // Create one random frag procs. |
| 468 | // For now, we'll prevent either children from being a shader with children to prevent the |
| 469 | // possibility of an arbitrarily large tree of procs. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 470 | sk_sp<GrFragmentProcessor> dst(GrProcessorUnitTest::MakeChildFP(d)); |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 471 | SkBlendMode mode; |
| 472 | ComposeOneFragmentProcessor::Child child; |
| 473 | do { |
| 474 | mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0, (int)SkBlendMode::kLastMode)); |
| 475 | child = d->fRandom->nextBool() ? kDst_Child : kSrc_Child; |
| 476 | } while (SkBlendMode::kClear == mode || (SkBlendMode::kDst == mode && child == kSrc_Child) || |
| 477 | (SkBlendMode::kSrc == mode && child == kDst_Child)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 478 | return sk_sp<GrFragmentProcessor>(new ComposeOneFragmentProcessor(std::move(dst), mode, child)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 479 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 480 | #endif |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 481 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 482 | GrGLSLFragmentProcessor* ComposeOneFragmentProcessor::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 483 | return new GLComposeOneFragmentProcessor; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | ////////////////////////////////////////////////////////////////////////////// |
| 487 | |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 488 | // It may seems as though when the input FP is the dst and the mode is kDst (or same for src/kSrc) |
| 489 | // that these factories could simply return the input FP. However, that doesn't have quite |
| 490 | // the same effect as the returned compose FP will replace the FP's input with solid white and |
| 491 | // ignore the original input. This could be implemented as: |
| 492 | // RunInSeries(ConstColor(GrColor_WHITE, kIgnoreInput), inputFP). |
| 493 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 494 | sk_sp<GrFragmentProcessor> GrXfermodeFragmentProcessor::MakeFromDstProcessor( |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 495 | sk_sp<GrFragmentProcessor> dst, SkBlendMode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 496 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 497 | case SkBlendMode::kClear: |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 498 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 499 | GrConstColorProcessor::kIgnore_InputMode); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 500 | case SkBlendMode::kSrc: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 501 | return nullptr; |
| 502 | default: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 503 | return ComposeOneFragmentProcessor::Make(std::move(dst), mode, |
| 504 | ComposeOneFragmentProcessor::kDst_Child); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 508 | sk_sp<GrFragmentProcessor> GrXfermodeFragmentProcessor::MakeFromSrcProcessor( |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 509 | sk_sp<GrFragmentProcessor> src, SkBlendMode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 510 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 511 | case SkBlendMode::kClear: |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 512 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
Brian Salomon | a12c153 | 2017-02-13 12:41:44 -0500 | [diff] [blame] | 513 | GrConstColorProcessor::kIgnore_InputMode); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 514 | case SkBlendMode::kDst: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 515 | return nullptr; |
| 516 | default: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame^] | 517 | return ComposeOneFragmentProcessor::Make(std::move(src), mode, |
| 518 | ComposeOneFragmentProcessor::kSrc_Child); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 519 | } |
| 520 | } |