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