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