bsalomon | bf87730 | 2015-09-22 09:06:13 -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 "GrFragmentProcessor.h" |
| 9 | #include "GrCoordTransform.h" |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 10 | #include "GrPipeline.h" |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 11 | #include "GrProcessorAnalysis.h" |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 12 | #include "effects/GrConstColorProcessor.h" |
Ethan Nicholas | be0a042 | 2017-11-17 13:44:05 -0500 | [diff] [blame] | 13 | #include "effects/GrPremulInputFragmentProcessor.h" |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 14 | #include "effects/GrXfermodeFragmentProcessor.h" |
Ethan Nicholas | be0a042 | 2017-11-17 13:44:05 -0500 | [diff] [blame] | 15 | #include "effects/GrUnpremulInputFragmentProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 18 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 20 | |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 21 | bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 22 | if (this->classID() != that.classID() || |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 23 | !this->hasSameSamplersAndAccesses(that)) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 24 | return false; |
| 25 | } |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 26 | if (!this->hasSameTransforms(that)) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | if (!this->onIsEqual(that)) { |
| 30 | return false; |
| 31 | } |
| 32 | if (this->numChildProcessors() != that.numChildProcessors()) { |
| 33 | return false; |
| 34 | } |
| 35 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 36 | if (!this->childProcessor(i).isEqual(that.childProcessor(i))) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | return true; |
| 41 | } |
| 42 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 43 | GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const { |
| 44 | GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 45 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 46 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 47 | glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 48 | } |
| 49 | return glFragProc; |
| 50 | } |
| 51 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 52 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 53 | fCoordTransforms.push_back(transform); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 54 | fFlags |= kUsesLocalCoords_Flag; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 55 | SkDEBUGCODE(transform->setInProcessor();) |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 58 | bool GrFragmentProcessor::instantiate(GrResourceProvider* resourceProvider) const { |
| 59 | if (!INHERITED::instantiate(resourceProvider)) { |
| 60 | return false; |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 63 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 64 | if (!this->childProcessor(i).instantiate(resourceProvider)) { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 72 | void GrFragmentProcessor::markPendingExecution() const { |
| 73 | INHERITED::addPendingIOs(); |
| 74 | INHERITED::removeRefs(); |
| 75 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 76 | this->childProcessor(i).markPendingExecution(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 81 | if (child->usesLocalCoords()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 82 | fFlags |= kUsesLocalCoords_Flag; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 83 | } |
| 84 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 85 | int index = fChildProcessors.count(); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 86 | fChildProcessors.push_back(std::move(child)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 87 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 88 | return index; |
| 89 | } |
| 90 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 91 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 92 | if (this->numCoordTransforms() != that.numCoordTransforms()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 95 | int count = this->numCoordTransforms(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 96 | for (int i = 0; i < count; ++i) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 97 | if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 104 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha( |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 105 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 106 | if (!fp) { |
| 107 | return nullptr; |
| 108 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 109 | return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 112 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha( |
| 113 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 114 | if (!fp) { |
| 115 | return nullptr; |
| 116 | } |
| 117 | return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn); |
| 118 | } |
| 119 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 120 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput( |
| 121 | std::unique_ptr<GrFragmentProcessor> fp) { |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 122 | if (!fp) { |
| 123 | return nullptr; |
| 124 | } |
Ethan Nicholas | be0a042 | 2017-11-17 13:44:05 -0500 | [diff] [blame] | 125 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(), |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 126 | std::move(fp) }; |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 127 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 128 | } |
| 129 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 130 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulOutput( |
| 131 | std::unique_ptr<GrFragmentProcessor> fp) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 132 | if (!fp) { |
| 133 | return nullptr; |
| 134 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 135 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), |
Ethan Nicholas | be0a042 | 2017-11-17 13:44:05 -0500 | [diff] [blame] | 136 | GrPremulInputFragmentProcessor::Make() }; |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 137 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 138 | } |
| 139 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 140 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::UnpremulOutput( |
| 141 | std::unique_ptr<GrFragmentProcessor> fp) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 142 | if (!fp) { |
| 143 | return nullptr; |
| 144 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 145 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), |
Ethan Nicholas | be0a042 | 2017-11-17 13:44:05 -0500 | [diff] [blame] | 146 | GrUnpremulInputFragmentProcessor::Make() }; |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 147 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 148 | } |
| 149 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 150 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput( |
| 151 | std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 152 | class SwizzleFragmentProcessor : public GrFragmentProcessor { |
| 153 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 154 | static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) { |
| 155 | return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle)); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | const char* name() const override { return "Swizzle"; } |
| 159 | const GrSwizzle& swizzle() const { return fSwizzle; } |
| 160 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 161 | std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 162 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 163 | private: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 164 | SwizzleFragmentProcessor(const GrSwizzle& swizzle) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 165 | : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags) |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 166 | , fSwizzle(swizzle) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 169 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 170 | class GLFP : public GrGLSLFragmentProcessor { |
| 171 | public: |
| 172 | void emitCode(EmitArgs& args) override { |
| 173 | const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>(); |
| 174 | const GrSwizzle& swizzle = sfp.swizzle(); |
| 175 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 176 | |
| 177 | fragBuilder->codeAppendf("%s = %s.%s;", |
| 178 | args.fOutputColor, args.fInputColor, swizzle.c_str()); |
| 179 | } |
| 180 | }; |
| 181 | return new GLFP; |
| 182 | } |
| 183 | |
| 184 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 185 | b->add32(fSwizzle.asKey()); |
| 186 | } |
| 187 | |
| 188 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
| 189 | const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>(); |
| 190 | return fSwizzle == sfp.fSwizzle; |
| 191 | } |
| 192 | |
| 193 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 194 | return fSwizzle.applyTo(input); |
| 195 | } |
| 196 | |
| 197 | GrSwizzle fSwizzle; |
| 198 | |
| 199 | typedef GrFragmentProcessor INHERITED; |
| 200 | }; |
| 201 | |
| 202 | if (!fp) { |
| 203 | return nullptr; |
| 204 | } |
| 205 | if (GrSwizzle::RGBA() == swizzle) { |
| 206 | return fp; |
| 207 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 208 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), |
| 209 | SwizzleFragmentProcessor::Make(swizzle) }; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 210 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 211 | } |
| 212 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 213 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput( |
| 214 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 215 | class PremulFragmentProcessor : public GrFragmentProcessor { |
| 216 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 217 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 218 | std::unique_ptr<GrFragmentProcessor> processor) { |
| 219 | return std::unique_ptr<GrFragmentProcessor>( |
| 220 | new PremulFragmentProcessor(std::move(processor))); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | const char* name() const override { return "Premultiply"; } |
| 224 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 225 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 226 | return Make(this->childProcessor(0).clone()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 229 | private: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 230 | PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 231 | : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 232 | this->registerChildProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 233 | } |
| 234 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 235 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 236 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 237 | public: |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 238 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 239 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 240 | this->emitChild(0, args); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 241 | fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 242 | args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 243 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 244 | } |
| 245 | }; |
| 246 | return new GLFP; |
| 247 | } |
| 248 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 249 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 250 | |
| 251 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 252 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 253 | static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) { |
| 254 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 255 | if (inner->preservesOpaqueInput()) { |
| 256 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 257 | } |
| 258 | if (inner->hasConstantOutputForConstantInput()) { |
| 259 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 260 | } |
| 261 | return flags; |
| 262 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 263 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 264 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 265 | GrColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0), |
| 266 | GrColor4f::OpaqueWhite()); |
| 267 | return GrColor4f(input.fRGBA[3] * input.fRGBA[0] * childColor.fRGBA[0], |
| 268 | input.fRGBA[3] * input.fRGBA[1] * childColor.fRGBA[1], |
| 269 | input.fRGBA[3] * input.fRGBA[2] * childColor.fRGBA[2], |
| 270 | input.fRGBA[3] * childColor.fRGBA[3]); |
| 271 | } |
| 272 | |
| 273 | typedef GrFragmentProcessor INHERITED; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 274 | }; |
| 275 | if (!fp) { |
| 276 | return nullptr; |
| 277 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 278 | return PremulFragmentProcessor::Make(std::move(fp)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | ////////////////////////////////////////////////////////////////////////////// |
| 282 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 283 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput( |
| 284 | std::unique_ptr<GrFragmentProcessor> fp, GrColor4f color) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 285 | class ReplaceInputFragmentProcessor : public GrFragmentProcessor { |
| 286 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 287 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child, |
| 288 | GrColor4f color) { |
| 289 | return std::unique_ptr<GrFragmentProcessor>( |
| 290 | new ReplaceInputFragmentProcessor(std::move(child), color)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | const char* name() const override { return "Replace Color"; } |
| 294 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 295 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 296 | return Make(this->childProcessor(0).clone(), fColor); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 300 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 301 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 302 | public: |
| 303 | GLFP() : fHaveSetColor(false) {} |
| 304 | void emitCode(EmitArgs& args) override { |
| 305 | const char* colorName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 306 | fColorUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 307 | kHalf4_GrSLType, |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 308 | "Color", &colorName); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 309 | this->emitChild(0, colorName, args); |
| 310 | } |
| 311 | |
| 312 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 313 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 314 | const GrFragmentProcessor& fp) override { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 315 | GrColor4f color = fp.cast<ReplaceInputFragmentProcessor>().fColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 316 | if (!fHaveSetColor || color != fPreviousColor) { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 317 | pdman.set4fv(fColorUni, 1, color.fRGBA); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 318 | fPreviousColor = color; |
| 319 | fHaveSetColor = true; |
| 320 | } |
| 321 | } |
| 322 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 323 | GrGLSLProgramDataManager::UniformHandle fColorUni; |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 324 | bool fHaveSetColor; |
| 325 | GrColor4f fPreviousColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | return new GLFP; |
| 329 | } |
| 330 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 331 | ReplaceInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child, GrColor4f color) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 332 | : INHERITED(kReplaceInputFragmentProcessor_ClassID, OptFlags(child.get(), color)) |
| 333 | , fColor(color) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 334 | this->registerChildProcessor(std::move(child)); |
| 335 | } |
| 336 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 337 | static OptimizationFlags OptFlags(const GrFragmentProcessor* child, GrColor4f color) { |
| 338 | OptimizationFlags childFlags = child->optimizationFlags(); |
| 339 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 340 | if (childFlags & kConstantOutputForConstantInput_OptimizationFlag) { |
| 341 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 342 | } |
| 343 | if ((childFlags & kPreservesOpaqueInput_OptimizationFlag) && color.isOpaque()) { |
| 344 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 345 | } |
| 346 | return flags; |
| 347 | } |
| 348 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 349 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 350 | {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 351 | |
| 352 | bool onIsEqual(const GrFragmentProcessor& that) const override { |
| 353 | return fColor == that.cast<ReplaceInputFragmentProcessor>().fColor; |
| 354 | } |
| 355 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 356 | GrColor4f constantOutputForConstantInput(GrColor4f) const override { |
| 357 | return ConstantOutputForConstantInput(this->childProcessor(0), fColor); |
| 358 | } |
| 359 | |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 360 | GrColor4f fColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 361 | |
| 362 | typedef GrFragmentProcessor INHERITED; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 365 | if (!fp) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | return ReplaceInputFragmentProcessor::Make(std::move(fp), color); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 369 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 370 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 371 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries( |
| 372 | std::unique_ptr<GrFragmentProcessor>* series, int cnt) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 373 | class SeriesFragmentProcessor : public GrFragmentProcessor { |
| 374 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 375 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 376 | std::unique_ptr<GrFragmentProcessor>* children, int cnt) { |
| 377 | return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | const char* name() const override { return "Series"; } |
| 381 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 382 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 383 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 384 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 385 | if (!children.push_back(this->childProcessor(i).clone())) { |
| 386 | return nullptr; |
| 387 | } |
| 388 | } |
| 389 | return Make(children.begin(), this->numChildProcessors()); |
| 390 | } |
| 391 | |
| 392 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 393 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 394 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 395 | public: |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 396 | void emitCode(EmitArgs& args) override { |
dvonbeck | ca9eeab | 2016-07-06 12:00:06 -0700 | [diff] [blame] | 397 | // First guy's input might be nil. |
| 398 | SkString temp("out0"); |
| 399 | this->emitChild(0, args.fInputColor, &temp, args); |
| 400 | SkString input = temp; |
| 401 | for (int i = 1; i < this->numChildProcessors() - 1; ++i) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 402 | temp.printf("out%d", i); |
| 403 | this->emitChild(i, input.c_str(), &temp, args); |
| 404 | input = temp; |
| 405 | } |
| 406 | // Last guy writes to our output variable. |
| 407 | this->emitChild(this->numChildProcessors() - 1, input.c_str(), args); |
| 408 | } |
| 409 | }; |
| 410 | return new GLFP; |
| 411 | } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 412 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 413 | SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 414 | : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 415 | SkASSERT(cnt > 1); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 416 | for (int i = 0; i < cnt; ++i) { |
| 417 | this->registerChildProcessor(std::move(children[i])); |
| 418 | } |
| 419 | } |
| 420 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 421 | static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 422 | OptimizationFlags flags = kAll_OptimizationFlags; |
| 423 | for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) { |
| 424 | flags &= children[i]->optimizationFlags(); |
| 425 | } |
| 426 | return flags; |
| 427 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 428 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 429 | |
| 430 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 431 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 432 | GrColor4f constantOutputForConstantInput(GrColor4f color) const override { |
| 433 | int childCnt = this->numChildProcessors(); |
| 434 | for (int i = 0; i < childCnt; ++i) { |
| 435 | color = ConstantOutputForConstantInput(this->childProcessor(i), color); |
| 436 | } |
| 437 | return color; |
| 438 | } |
| 439 | |
| 440 | typedef GrFragmentProcessor INHERITED; |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | if (!cnt) { |
| 444 | return nullptr; |
| 445 | } |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 446 | if (1 == cnt) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 447 | return std::move(series[0]); |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 448 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 449 | // Run the through the series, do the invariant output processing, and look for eliminations. |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 450 | GrProcessorAnalysisColor inputColor; |
| 451 | inputColor.setToUnknown(); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 452 | GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series), |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 453 | cnt); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 454 | SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries; |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 455 | GrColor4f knownColor; |
| 456 | int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor); |
| 457 | if (leadingFPsToEliminate) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 458 | std::unique_ptr<GrFragmentProcessor> colorFP( |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 459 | GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore)); |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 460 | if (leadingFPsToEliminate == cnt) { |
| 461 | return colorFP; |
| 462 | } |
| 463 | cnt = cnt - leadingFPsToEliminate + 1; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 464 | replacementSeries.reserve(cnt); |
| 465 | replacementSeries.emplace_back(std::move(colorFP)); |
| 466 | for (int i = 0; i < cnt - 1; ++i) { |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 467 | replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 468 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 469 | series = replacementSeries.begin(); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 470 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 471 | return SeriesFragmentProcessor::Make(series, cnt); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 472 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 473 | |
| 474 | ////////////////////////////////////////////////////////////////////////////// |
| 475 | |
| 476 | GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) { |
| 477 | for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) { |
| 478 | fFPStack.push_back(&pipeline.getFragmentProcessor(i)); |
| 479 | } |
| 480 | } |
| 481 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 482 | GrFragmentProcessor::Iter::Iter(const GrPaint& paint) { |
| 483 | for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) { |
| 484 | fFPStack.push_back(paint.getCoverageFragmentProcessor(i)); |
| 485 | } |
| 486 | for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) { |
| 487 | fFPStack.push_back(paint.getColorFragmentProcessor(i)); |
| 488 | } |
| 489 | } |
| 490 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 491 | const GrFragmentProcessor* GrFragmentProcessor::Iter::next() { |
| 492 | if (fFPStack.empty()) { |
| 493 | return nullptr; |
| 494 | } |
| 495 | const GrFragmentProcessor* back = fFPStack.back(); |
| 496 | fFPStack.pop_back(); |
| 497 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 498 | fFPStack.push_back(&back->childProcessor(i)); |
| 499 | } |
| 500 | return back; |
| 501 | } |
| 502 | |