wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [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/GrXfermodeFragmentProcessor.h" |
| 9 | |
| 10 | #include "GrFragmentProcessor.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 11 | #include "GrInvariantOutput.h" |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 12 | #include "effects/GrConstColorProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 14 | #include "glsl/GrGLSLBlend.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 16 | #include "SkGrPriv.h" |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 17 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 18 | class ComposeTwoFragmentProcessor : public GrFragmentProcessor { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 19 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 20 | ComposeTwoFragmentProcessor(sk_sp<GrFragmentProcessor> src, sk_sp<GrFragmentProcessor> dst, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 21 | SkBlendMode mode) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 22 | : INHERITED(OptFlags(src.get(), dst.get(), mode)), fMode(mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 23 | this->initClassID<ComposeTwoFragmentProcessor>(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 24 | SkDEBUGCODE(int shaderAChildIndex = )this->registerChildProcessor(std::move(src)); |
| 25 | SkDEBUGCODE(int shaderBChildIndex = )this->registerChildProcessor(std::move(dst)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 26 | SkASSERT(0 == shaderAChildIndex); |
| 27 | SkASSERT(1 == shaderBChildIndex); |
| 28 | } |
| 29 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 30 | const char* name() const override { return "ComposeTwo"; } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 31 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 32 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 33 | b->add32((int)fMode); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 36 | SkBlendMode getMode() const { return fMode; } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 37 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 38 | private: |
| 39 | static OptimizationFlags OptFlags(const GrFragmentProcessor* src, |
| 40 | const GrFragmentProcessor* dst, SkBlendMode mode) { |
| 41 | // We only attempt the constant output optimization. |
| 42 | // The CPU and GPU implementations differ significantly for the advanced modes. |
| 43 | if (mode <= SkBlendMode::kLastSeparableMode && src->hasConstantOutputForConstantInput() && |
| 44 | dst->hasConstantOutputForConstantInput()) { |
| 45 | return kConstantOutputForConstantInput_OptimizationFlag; |
| 46 | } |
| 47 | return kNone_OptimizationFlags; |
| 48 | } |
| 49 | |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 50 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 51 | const ComposeTwoFragmentProcessor& cs = other.cast<ComposeTwoFragmentProcessor>(); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 52 | return fMode == cs.fMode; |
| 53 | } |
| 54 | |
| 55 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
Brian Salomon | 5f13fba | 2017-01-23 14:35:25 -0500 | [diff] [blame] | 56 | inout->setToUnknown(); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 59 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 60 | float alpha = input.fRGBA[3]; |
| 61 | input = input.opaque(); |
| 62 | GrColor4f srcColor = ConstantOutputForConstantInput(this->childProcessor(0), input); |
| 63 | GrColor4f dstColor = ConstantOutputForConstantInput(this->childProcessor(1), input); |
| 64 | SkPM4f src = GrColor4fToSkPM4f(srcColor); |
| 65 | SkPM4f dst = GrColor4fToSkPM4f(dstColor); |
| 66 | auto proc = SkXfermode::GetProc4f(fMode); |
| 67 | return SkPM4fToGrColor4f(proc(src, dst)).mulByScalar(alpha); |
| 68 | } |
| 69 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 70 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 71 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 72 | SkBlendMode fMode; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 73 | |
| 74 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 75 | |
| 76 | typedef GrFragmentProcessor INHERITED; |
| 77 | }; |
| 78 | |
| 79 | ///////////////////////////////////////////////////////////////////// |
| 80 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 81 | class GLComposeTwoFragmentProcessor : public GrGLSLFragmentProcessor { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 82 | public: |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 83 | void emitCode(EmitArgs&) override; |
| 84 | |
| 85 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 86 | typedef GrGLSLFragmentProcessor INHERITED; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | ///////////////////////////////////////////////////////////////////// |
| 90 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 91 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ComposeTwoFragmentProcessor); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 92 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame^] | 93 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 94 | sk_sp<GrFragmentProcessor> ComposeTwoFragmentProcessor::TestCreate(GrProcessorTestData* d) { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 95 | // Create two random frag procs. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 96 | sk_sp<GrFragmentProcessor> fpA(GrProcessorUnitTest::MakeChildFP(d)); |
| 97 | sk_sp<GrFragmentProcessor> fpB(GrProcessorUnitTest::MakeChildFP(d)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 98 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 99 | SkBlendMode mode = static_cast<SkBlendMode>( |
| 100 | d->fRandom->nextRangeU(0, (int)SkBlendMode::kLastMode)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 101 | return sk_sp<GrFragmentProcessor>( |
| 102 | new ComposeTwoFragmentProcessor(std::move(fpA), std::move(fpB), mode)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 103 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame^] | 104 | #endif |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 105 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 106 | GrGLSLFragmentProcessor* ComposeTwoFragmentProcessor::onCreateGLSLInstance() const{ |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 107 | return new GLComposeTwoFragmentProcessor; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | ///////////////////////////////////////////////////////////////////// |
| 111 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 112 | void GLComposeTwoFragmentProcessor::emitCode(EmitArgs& args) { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 113 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 114 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 115 | const ComposeTwoFragmentProcessor& cs = args.fFp.cast<ComposeTwoFragmentProcessor>(); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 116 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 117 | const char* inputColor = nullptr; |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 118 | if (args.fInputColor) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 119 | inputColor = "inputColor"; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 120 | fragBuilder->codeAppendf("vec4 inputColor = vec4(%s.rgb, 1.0);", args.fInputColor); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 121 | } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 122 | |
| 123 | // declare outputColor and emit the code for each of the two children |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 124 | SkString srcColor("xfer_src"); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 125 | this->emitChild(0, inputColor, &srcColor, args); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 126 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 127 | SkString dstColor("xfer_dst"); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 128 | this->emitChild(1, inputColor, &dstColor, args); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 129 | |
| 130 | // emit blend code |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 131 | SkBlendMode mode = cs.getMode(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 132 | fragBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(mode)); |
| 133 | GrGLSLBlend::AppendMode(fragBuilder, |
| 134 | srcColor.c_str(), |
| 135 | dstColor.c_str(), |
| 136 | args.fOutputColor, |
| 137 | mode); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 138 | |
| 139 | // re-multiply the output color by the input color's alpha |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 140 | if (args.fInputColor) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 141 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 142 | } |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 143 | } |
| 144 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 145 | sk_sp<GrFragmentProcessor> GrXfermodeFragmentProcessor::MakeFromTwoProcessors( |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 146 | sk_sp<GrFragmentProcessor> src, sk_sp<GrFragmentProcessor> dst, SkBlendMode mode) { |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 147 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 148 | case SkBlendMode::kClear: |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 149 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 150 | GrConstColorProcessor::kIgnore_InputMode); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 151 | case SkBlendMode::kSrc: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 152 | return src; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 153 | case SkBlendMode::kDst: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 154 | return dst; |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 155 | default: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 156 | return sk_sp<GrFragmentProcessor>( |
| 157 | new ComposeTwoFragmentProcessor(std::move(src), std::move(dst), mode)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | ////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | class ComposeOneFragmentProcessor : public GrFragmentProcessor { |
| 164 | public: |
| 165 | enum Child { |
| 166 | kDst_Child, |
| 167 | kSrc_Child, |
| 168 | }; |
| 169 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 170 | ComposeOneFragmentProcessor(sk_sp<GrFragmentProcessor> dst, SkBlendMode mode, Child child) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 171 | : INHERITED(OptFlags(dst.get(), mode)), fMode(mode), fChild(child) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 172 | this->initClassID<ComposeOneFragmentProcessor>(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 173 | SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(std::move(dst)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 174 | SkASSERT(0 == dstIndex); |
| 175 | } |
| 176 | |
| 177 | const char* name() const override { return "ComposeOne"; } |
| 178 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 179 | SkString dumpInfo() const override { |
| 180 | SkString str; |
| 181 | |
| 182 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 183 | str.append(this->childProcessor(i).dumpInfo()); |
| 184 | } |
| 185 | return str; |
| 186 | } |
| 187 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 188 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 189 | GR_STATIC_ASSERT(((int)SkBlendMode::kLastMode & SK_MaxU16) == (int)SkBlendMode::kLastMode); |
| 190 | b->add32((int)fMode | (fChild << 16)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 193 | SkBlendMode mode() const { return fMode; } |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 194 | |
| 195 | Child child() const { return fChild; } |
| 196 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 197 | private: |
| 198 | OptimizationFlags OptFlags(const GrFragmentProcessor* child, SkBlendMode mode) { |
| 199 | // We only attempt the constant output optimization. |
| 200 | // The CPU and GPU implementations differ significantly for the advanced modes. |
| 201 | if (mode <= SkBlendMode::kLastSeparableMode && child->hasConstantOutputForConstantInput()) { |
| 202 | return kConstantOutputForConstantInput_OptimizationFlag; |
| 203 | } |
| 204 | return kNone_OptimizationFlags; |
| 205 | } |
| 206 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 207 | bool onIsEqual(const GrFragmentProcessor& that) const override { |
| 208 | return fMode == that.cast<ComposeOneFragmentProcessor>().fMode; |
| 209 | } |
| 210 | |
| 211 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 212 | SkXfermode::Coeff skSrcCoeff, skDstCoeff; |
| 213 | if (SkXfermode::ModeAsCoeff(fMode, &skSrcCoeff, &skDstCoeff)) { |
| 214 | GrBlendCoeff srcCoeff = SkXfermodeCoeffToGrBlendCoeff(skSrcCoeff); |
| 215 | GrBlendCoeff dstCoeff = SkXfermodeCoeffToGrBlendCoeff(skDstCoeff); |
Brian Salomon | aab259e | 2017-01-17 10:44:34 -0500 | [diff] [blame] | 216 | GrInvariantOutput childOutput(0xFFFFFFFF, kRGBA_GrColorComponentFlags); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 217 | this->childProcessor(0).computeInvariantOutput(&childOutput); |
| 218 | GrColor blendColor; |
| 219 | GrColorComponentFlags blendFlags; |
| 220 | if (kDst_Child == fChild) { |
| 221 | GrGetCoeffBlendKnownComponents(srcCoeff, dstCoeff, |
| 222 | inout->color(), inout->validFlags(), |
| 223 | childOutput.color(), childOutput.validFlags(), |
| 224 | &blendColor, &blendFlags); |
| 225 | } else { |
| 226 | GrGetCoeffBlendKnownComponents(srcCoeff, dstCoeff, |
| 227 | childOutput.color(), childOutput.validFlags(), |
| 228 | inout->color(), inout->validFlags(), |
| 229 | &blendColor, &blendFlags); |
| 230 | } |
Brian Salomon | 5f13fba | 2017-01-23 14:35:25 -0500 | [diff] [blame] | 231 | inout->setToOther(blendFlags, blendColor); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 232 | } else { |
Brian Salomon | 5f13fba | 2017-01-23 14:35:25 -0500 | [diff] [blame] | 233 | inout->setToUnknown(); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 237 | GrColor4f constantOutputForConstantInput(GrColor4f inputColor) const override { |
| 238 | GrColor4f childColor = |
| 239 | ConstantOutputForConstantInput(this->childProcessor(0), GrColor4f::OpaqueWhite()); |
| 240 | SkPM4f src, dst; |
| 241 | if (kSrc_Child == fChild) { |
| 242 | src = GrColor4fToSkPM4f(childColor); |
| 243 | dst = GrColor4fToSkPM4f(inputColor); |
| 244 | } else { |
| 245 | src = GrColor4fToSkPM4f(inputColor); |
| 246 | dst = GrColor4fToSkPM4f(childColor); |
| 247 | } |
| 248 | auto proc = SkXfermode::GetProc4f(fMode); |
| 249 | return SkPM4fToGrColor4f(proc(src, dst)); |
| 250 | } |
| 251 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 252 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 253 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 254 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 255 | SkBlendMode fMode; |
| 256 | Child fChild; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 257 | |
| 258 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 259 | |
| 260 | typedef GrFragmentProcessor INHERITED; |
| 261 | }; |
| 262 | |
| 263 | ////////////////////////////////////////////////////////////////////////////// |
| 264 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 265 | class GLComposeOneFragmentProcessor : public GrGLSLFragmentProcessor { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 266 | public: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 267 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 268 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 269 | SkBlendMode mode = args.fFp.cast<ComposeOneFragmentProcessor>().mode(); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 270 | ComposeOneFragmentProcessor::Child child = |
| 271 | args.fFp.cast<ComposeOneFragmentProcessor>().child(); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 272 | SkString childColor("child"); |
| 273 | this->emitChild(0, nullptr, &childColor, args); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 274 | |
| 275 | const char* inputColor = args.fInputColor; |
| 276 | // We don't try to optimize for this case at all |
| 277 | if (!inputColor) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 278 | fragBuilder->codeAppendf("const vec4 ones = vec4(1);"); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 279 | inputColor = "ones"; |
| 280 | } |
| 281 | |
| 282 | // emit blend code |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 283 | fragBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(mode)); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 284 | const char* childStr = childColor.c_str(); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 285 | if (ComposeOneFragmentProcessor::kDst_Child == child) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 286 | GrGLSLBlend::AppendMode(fragBuilder, inputColor, childStr, args.fOutputColor, mode); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 287 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 288 | GrGLSLBlend::AppendMode(fragBuilder, childStr, inputColor, args.fOutputColor, mode); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 289 | } |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 293 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | ///////////////////////////////////////////////////////////////////// |
| 297 | |
| 298 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ComposeOneFragmentProcessor); |
| 299 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame^] | 300 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 301 | sk_sp<GrFragmentProcessor> ComposeOneFragmentProcessor::TestCreate(GrProcessorTestData* d) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 302 | // Create one random frag procs. |
| 303 | // For now, we'll prevent either children from being a shader with children to prevent the |
| 304 | // possibility of an arbitrarily large tree of procs. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 305 | sk_sp<GrFragmentProcessor> dst(GrProcessorUnitTest::MakeChildFP(d)); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 306 | SkBlendMode mode = static_cast<SkBlendMode>( |
| 307 | d->fRandom->nextRangeU(0, (int)SkBlendMode::kLastMode)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 308 | ComposeOneFragmentProcessor::Child child = d->fRandom->nextBool() ? |
| 309 | ComposeOneFragmentProcessor::kDst_Child : |
| 310 | ComposeOneFragmentProcessor::kSrc_Child; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 311 | return sk_sp<GrFragmentProcessor>(new ComposeOneFragmentProcessor(std::move(dst), mode, child)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 312 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame^] | 313 | #endif |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 314 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 315 | GrGLSLFragmentProcessor* ComposeOneFragmentProcessor::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 316 | return new GLComposeOneFragmentProcessor; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | ////////////////////////////////////////////////////////////////////////////// |
| 320 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 321 | sk_sp<GrFragmentProcessor> GrXfermodeFragmentProcessor::MakeFromDstProcessor( |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 322 | sk_sp<GrFragmentProcessor> dst, SkBlendMode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 323 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 324 | case SkBlendMode::kClear: |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 325 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 326 | GrConstColorProcessor::kIgnore_InputMode); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 327 | case SkBlendMode::kSrc: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 328 | return nullptr; |
| 329 | default: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 330 | return sk_sp<GrFragmentProcessor>( |
| 331 | new ComposeOneFragmentProcessor(std::move(dst), mode, |
| 332 | ComposeOneFragmentProcessor::kDst_Child)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 336 | sk_sp<GrFragmentProcessor> GrXfermodeFragmentProcessor::MakeFromSrcProcessor( |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 337 | sk_sp<GrFragmentProcessor> src, SkBlendMode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 338 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 339 | case SkBlendMode::kClear: |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 340 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 341 | GrConstColorProcessor::kIgnore_InputMode); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 342 | case SkBlendMode::kDst: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 343 | return nullptr; |
| 344 | default: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 345 | return sk_sp<GrFragmentProcessor>( |
| 346 | new ComposeOneFragmentProcessor(src, mode, |
| 347 | ComposeOneFragmentProcessor::kSrc_Child)); |
wangyix | 809e5af | 2015-09-09 12:58:32 -0700 | [diff] [blame] | 348 | } |
| 349 | } |