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