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" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 10 | #include "GrInvariantOutput.h" |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 11 | #include "GrPipeline.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 12 | #include "GrProcOptInfo.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 17 | #include "effects/GrConstColorProcessor.h" |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 18 | #include "effects/GrXfermodeFragmentProcessor.h" |
| 19 | |
| 20 | GrFragmentProcessor::~GrFragmentProcessor() { |
| 21 | // If we got here then our ref count must have reached zero, so we will have converted refs |
| 22 | // to pending executions for all children. |
| 23 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| 24 | fChildProcessors[i]->completedExecution(); |
| 25 | } |
| 26 | } |
| 27 | |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 28 | bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 29 | if (this->classID() != that.classID() || |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 30 | !this->hasSameSamplersAndAccesses(that)) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 31 | return false; |
| 32 | } |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 33 | if (!this->hasSameTransforms(that)) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | if (!this->onIsEqual(that)) { |
| 37 | return false; |
| 38 | } |
| 39 | if (this->numChildProcessors() != that.numChildProcessors()) { |
| 40 | return false; |
| 41 | } |
| 42 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 43 | if (!this->childProcessor(i).isEqual(that.childProcessor(i))) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 50 | GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const { |
| 51 | GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 52 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 53 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 54 | glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 55 | } |
| 56 | return glFragProc; |
| 57 | } |
| 58 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 59 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 60 | fCoordTransforms.push_back(transform); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 61 | fUsesLocalCoords = true; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 62 | SkDEBUGCODE(transform->setInProcessor();) |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 63 | } |
| 64 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 65 | int GrFragmentProcessor::registerChildProcessor(sk_sp<GrFragmentProcessor> child) { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 66 | this->combineRequiredFeatures(*child); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 67 | |
| 68 | if (child->usesLocalCoords()) { |
| 69 | fUsesLocalCoords = true; |
| 70 | } |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 71 | if (child->usesDistanceVectorField()) { |
| 72 | fUsesDistanceVectorField = true; |
| 73 | } |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 74 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 75 | int index = fChildProcessors.count(); |
| 76 | fChildProcessors.push_back(child.release()); |
| 77 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 78 | return index; |
| 79 | } |
| 80 | |
| 81 | void GrFragmentProcessor::notifyRefCntIsZero() const { |
| 82 | // See comment above GrProgramElement for a detailed explanation of why we do this. |
| 83 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| 84 | fChildProcessors[i]->addPendingExecution(); |
| 85 | fChildProcessors[i]->unref(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 90 | if (this->numCoordTransforms() != that.numCoordTransforms()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 93 | int count = this->numCoordTransforms(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 94 | for (int i = 0; i < count; ++i) { |
| 95 | if (this->coordTransform(i) != that.coordTransform(i)) { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 102 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::MulOutputByInputAlpha( |
| 103 | sk_sp<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 104 | if (!fp) { |
| 105 | return nullptr; |
| 106 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 107 | return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 108 | } |
| 109 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 110 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::PremulInput(sk_sp<GrFragmentProcessor> fp) { |
| 111 | |
| 112 | class PremulInputFragmentProcessor : public GrFragmentProcessor { |
| 113 | public: |
| 114 | PremulInputFragmentProcessor() { |
| 115 | this->initClassID<PremulInputFragmentProcessor>(); |
| 116 | } |
| 117 | |
| 118 | const char* name() const override { return "PremultiplyInput"; } |
| 119 | |
| 120 | private: |
| 121 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 122 | class GLFP : public GrGLSLFragmentProcessor { |
| 123 | public: |
| 124 | void emitCode(EmitArgs& args) override { |
| 125 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 126 | |
| 127 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 128 | fragBuilder->codeAppendf("%s.rgb *= %s.a;", |
| 129 | args.fOutputColor, args.fInputColor); |
| 130 | } |
| 131 | }; |
| 132 | return new GLFP; |
| 133 | } |
| 134 | |
| 135 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override {} |
| 136 | |
| 137 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 138 | |
| 139 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 140 | inout->premulFourChannelColor(); |
| 141 | } |
| 142 | }; |
| 143 | if (!fp) { |
| 144 | return nullptr; |
| 145 | } |
| 146 | sk_sp<GrFragmentProcessor> fpPipeline[] = { sk_make_sp<PremulInputFragmentProcessor>(), fp}; |
| 147 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 148 | } |
| 149 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 150 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::MulOutputByInputUnpremulColor( |
| 151 | sk_sp<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 152 | |
| 153 | class PremulFragmentProcessor : public GrFragmentProcessor { |
| 154 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 155 | PremulFragmentProcessor(sk_sp<GrFragmentProcessor> processor) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 156 | this->initClassID<PremulFragmentProcessor>(); |
| 157 | this->registerChildProcessor(processor); |
| 158 | } |
| 159 | |
| 160 | const char* name() const override { return "Premultiply"; } |
| 161 | |
| 162 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 163 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 164 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 165 | public: |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 166 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 167 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 168 | this->emitChild(0, nullptr, args); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 169 | fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 170 | args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 171 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 172 | } |
| 173 | }; |
| 174 | return new GLFP; |
| 175 | } |
| 176 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 177 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 178 | |
| 179 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 180 | |
| 181 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 182 | // TODO: Add a helper to GrInvariantOutput that handles multiplying by color with flags? |
| 183 | if (!(inout->validFlags() & kA_GrColorComponentFlag)) { |
| 184 | inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | GrInvariantOutput childOutput(GrColor_WHITE, kRGBA_GrColorComponentFlags, false); |
| 189 | this->childProcessor(0).computeInvariantOutput(&childOutput); |
| 190 | |
| 191 | if (0 == GrColorUnpackA(inout->color()) || 0 == GrColorUnpackA(childOutput.color())) { |
| 192 | inout->mulByKnownFourComponents(0x0); |
| 193 | return; |
| 194 | } |
| 195 | GrColorComponentFlags commonFlags = childOutput.validFlags() & inout->validFlags(); |
| 196 | GrColor c0 = GrPremulColor(inout->color()); |
| 197 | GrColor c1 = childOutput.color(); |
| 198 | GrColor color = 0x0; |
| 199 | if (commonFlags & kR_GrColorComponentFlag) { |
| 200 | color |= SkMulDiv255Round(GrColorUnpackR(c0), GrColorUnpackR(c1)) << |
| 201 | GrColor_SHIFT_R; |
| 202 | } |
| 203 | if (commonFlags & kG_GrColorComponentFlag) { |
| 204 | color |= SkMulDiv255Round(GrColorUnpackG(c0), GrColorUnpackG(c1)) << |
| 205 | GrColor_SHIFT_G; |
| 206 | } |
| 207 | if (commonFlags & kB_GrColorComponentFlag) { |
| 208 | color |= SkMulDiv255Round(GrColorUnpackB(c0), GrColorUnpackB(c1)) << |
| 209 | GrColor_SHIFT_B; |
| 210 | } |
| 211 | inout->setToOther(commonFlags, color, GrInvariantOutput::kWill_ReadInput); |
| 212 | } |
| 213 | }; |
| 214 | if (!fp) { |
| 215 | return nullptr; |
| 216 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 217 | return sk_sp<GrFragmentProcessor>(new PremulFragmentProcessor(std::move(fp))); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | ////////////////////////////////////////////////////////////////////////////// |
| 221 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 222 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(sk_sp<GrFragmentProcessor> fp, |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 223 | GrColor4f color) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 224 | class ReplaceInputFragmentProcessor : public GrFragmentProcessor { |
| 225 | public: |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 226 | ReplaceInputFragmentProcessor(sk_sp<GrFragmentProcessor> child, GrColor4f color) |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 227 | : fColor(color) { |
| 228 | this->initClassID<ReplaceInputFragmentProcessor>(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 229 | this->registerChildProcessor(std::move(child)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | const char* name() const override { return "Replace Color"; } |
| 233 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 234 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 235 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 236 | public: |
| 237 | GLFP() : fHaveSetColor(false) {} |
| 238 | void emitCode(EmitArgs& args) override { |
| 239 | const char* colorName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 240 | fColorUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
| 241 | kVec4f_GrSLType, |
| 242 | kDefault_GrSLPrecision, |
| 243 | "Color", &colorName); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 244 | this->emitChild(0, colorName, args); |
| 245 | } |
| 246 | |
| 247 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 248 | void onSetData(const GrGLSLProgramDataManager& pdman, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 249 | const GrProcessor& fp) override { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 250 | GrColor4f color = fp.cast<ReplaceInputFragmentProcessor>().fColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 251 | if (!fHaveSetColor || color != fPreviousColor) { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 252 | pdman.set4fv(fColorUni, 1, color.fRGBA); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 253 | fPreviousColor = color; |
| 254 | fHaveSetColor = true; |
| 255 | } |
| 256 | } |
| 257 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 258 | GrGLSLProgramDataManager::UniformHandle fColorUni; |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 259 | bool fHaveSetColor; |
| 260 | GrColor4f fPreviousColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | return new GLFP; |
| 264 | } |
| 265 | |
| 266 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 267 | void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override |
| 268 | {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 269 | |
| 270 | bool onIsEqual(const GrFragmentProcessor& that) const override { |
| 271 | return fColor == that.cast<ReplaceInputFragmentProcessor>().fColor; |
| 272 | } |
| 273 | |
| 274 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 275 | inout->setToOther(kRGBA_GrColorComponentFlags, fColor.toGrColor(), |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 276 | GrInvariantOutput::kWillNot_ReadInput); |
| 277 | this->childProcessor(0).computeInvariantOutput(inout); |
| 278 | } |
| 279 | |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 280 | GrColor4f fColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | GrInvariantOutput childOut(0x0, kNone_GrColorComponentFlags, false); |
| 284 | fp->computeInvariantOutput(&childOut); |
| 285 | if (childOut.willUseInputColor()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 286 | return sk_sp<GrFragmentProcessor>(new ReplaceInputFragmentProcessor(std::move(fp), color)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 287 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 288 | return fp; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 291 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 292 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(sk_sp<GrFragmentProcessor>* series, |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 293 | int cnt) { |
| 294 | class SeriesFragmentProcessor : public GrFragmentProcessor { |
| 295 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 296 | SeriesFragmentProcessor(sk_sp<GrFragmentProcessor>* children, int cnt){ |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 297 | SkASSERT(cnt > 1); |
| 298 | this->initClassID<SeriesFragmentProcessor>(); |
| 299 | for (int i = 0; i < cnt; ++i) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 300 | this->registerChildProcessor(std::move(children[i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | const char* name() const override { return "Series"; } |
| 305 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 306 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 307 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 308 | public: |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 309 | void emitCode(EmitArgs& args) override { |
dvonbeck | ca9eeab | 2016-07-06 12:00:06 -0700 | [diff] [blame] | 310 | // First guy's input might be nil. |
| 311 | SkString temp("out0"); |
| 312 | this->emitChild(0, args.fInputColor, &temp, args); |
| 313 | SkString input = temp; |
| 314 | for (int i = 1; i < this->numChildProcessors() - 1; ++i) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 315 | temp.printf("out%d", i); |
| 316 | this->emitChild(i, input.c_str(), &temp, args); |
| 317 | input = temp; |
| 318 | } |
| 319 | // Last guy writes to our output variable. |
| 320 | this->emitChild(this->numChildProcessors() - 1, input.c_str(), args); |
| 321 | } |
| 322 | }; |
| 323 | return new GLFP; |
| 324 | } |
| 325 | |
| 326 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 327 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 328 | |
| 329 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 330 | |
| 331 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 332 | GrProcOptInfo info; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 333 | info.calcWithInitialValues(fChildProcessors.begin(), fChildProcessors.count(), |
| 334 | inout->color(), inout->validFlags(), false, false); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 335 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 336 | this->childProcessor(i).computeInvariantOutput(inout); |
| 337 | } |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | if (!cnt) { |
| 342 | return nullptr; |
| 343 | } |
| 344 | |
| 345 | // Run the through the series, do the invariant output processing, and look for eliminations. |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 346 | GrProcOptInfo info; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 347 | info.calcWithInitialValues(sk_sp_address_as_pointer_address(series), cnt, |
| 348 | 0x0, kNone_GrColorComponentFlags, false, false); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 349 | if (kRGBA_GrColorComponentFlags == info.validFlags()) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 350 | // TODO: We need to preserve 4f and color spaces during invariant processing. This color |
| 351 | // has definitely lost precision, and could easily be in the wrong gamut (or have been |
| 352 | // built from colors in multiple spaces). |
| 353 | return GrConstColorProcessor::Make(GrColor4f::FromGrColor(info.color()), |
| 354 | GrConstColorProcessor::kIgnore_InputMode); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | SkTArray<sk_sp<GrFragmentProcessor>> replacementSeries; |
| 358 | |
| 359 | int firstIdx = info.firstEffectiveProcessorIndex(); |
| 360 | cnt -= firstIdx; |
| 361 | if (firstIdx > 0 && info.inputColorIsUsed()) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 362 | // See comment above - need to preserve 4f and color spaces during invariant processing. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 363 | sk_sp<GrFragmentProcessor> colorFP(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 364 | GrColor4f::FromGrColor(info.inputColorToFirstEffectiveProccesor()), |
| 365 | GrConstColorProcessor::kIgnore_InputMode)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 366 | cnt += 1; |
| 367 | replacementSeries.reserve(cnt); |
| 368 | replacementSeries.emplace_back(std::move(colorFP)); |
| 369 | for (int i = 0; i < cnt - 1; ++i) { |
| 370 | replacementSeries.emplace_back(std::move(series[firstIdx + i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 371 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 372 | series = replacementSeries.begin(); |
| 373 | } else { |
| 374 | series += firstIdx; |
| 375 | cnt -= firstIdx; |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | if (1 == cnt) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 379 | return series[0]; |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 380 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 381 | return sk_sp<GrFragmentProcessor>(new SeriesFragmentProcessor(series, cnt)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 382 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 383 | |
| 384 | ////////////////////////////////////////////////////////////////////////////// |
| 385 | |
| 386 | GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) { |
| 387 | for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) { |
| 388 | fFPStack.push_back(&pipeline.getFragmentProcessor(i)); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | const GrFragmentProcessor* GrFragmentProcessor::Iter::next() { |
| 393 | if (fFPStack.empty()) { |
| 394 | return nullptr; |
| 395 | } |
| 396 | const GrFragmentProcessor* back = fFPStack.back(); |
| 397 | fFPStack.pop_back(); |
| 398 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 399 | fFPStack.push_back(&back->childProcessor(i)); |
| 400 | } |
| 401 | return back; |
| 402 | } |
| 403 | |