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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrCoordTransform.h" |
| 9 | #include "src/gpu/GrFragmentProcessor.h" |
| 10 | #include "src/gpu/GrPipeline.h" |
| 11 | #include "src/gpu/GrProcessorAnalysis.h" |
| 12 | #include "src/gpu/effects/GrXfermodeFragmentProcessor.h" |
| 13 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
| 14 | #include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h" |
| 15 | #include "src/gpu/effects/generated/GrPremulInputFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 19 | #include "src/gpu/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 { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 22 | if (this->classID() != that.classID()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 23 | return false; |
| 24 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 25 | if (this->numTextureSamplers() != that.numTextureSamplers()) { |
| 26 | return false; |
| 27 | } |
| 28 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 29 | if (this->textureSampler(i) != that.textureSampler(i)) { |
| 30 | return false; |
| 31 | } |
| 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 | |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 50 | void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 51 | GrFragmentProcessor::TextureAccessIter iter(this); |
| 52 | while (const TextureSampler* sampler = iter.next()) { |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 53 | bool mipped = (GrSamplerState::Filter::kMipMap == sampler->samplerState().filter()); |
| 54 | func(sampler->proxy(), GrMipMapped(mipped)); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 58 | GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const { |
| 59 | GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 60 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 61 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 62 | glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 63 | } |
| 64 | return glFragProc; |
| 65 | } |
| 66 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 67 | const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const { |
| 68 | SkASSERT(i >= 0 && i < fTextureSamplerCnt); |
| 69 | return this->onTextureSampler(i); |
| 70 | } |
| 71 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 72 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 73 | fCoordTransforms.push_back(transform); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 74 | fFlags |= kUsesLocalCoords_Flag; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 75 | SkDEBUGCODE(transform->setInProcessor();) |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 78 | #ifdef SK_DEBUG |
| 79 | bool GrFragmentProcessor::isInstantiated() const { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 80 | for (int i = 0; i < fTextureSamplerCnt; ++i) { |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 81 | if (!this->textureSampler(i).isInstantiated()) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 82 | return false; |
| 83 | } |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 86 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 87 | if (!this->childProcessor(i).isInstantiated()) { |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return true; |
| 93 | } |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 94 | #endif |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 95 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 96 | int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 97 | if (child->usesLocalCoords()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 98 | fFlags |= kUsesLocalCoords_Flag; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 99 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 100 | fRequestedFeatures |= child->fRequestedFeatures; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 101 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 102 | int index = fChildProcessors.count(); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 103 | fChildProcessors.push_back(std::move(child)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 104 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 105 | return index; |
| 106 | } |
| 107 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 108 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 109 | if (this->numCoordTransforms() != that.numCoordTransforms()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 112 | int count = this->numCoordTransforms(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 113 | for (int i = 0; i < count; ++i) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 114 | if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 121 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha( |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 122 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 123 | if (!fp) { |
| 124 | return nullptr; |
| 125 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 126 | return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 129 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha( |
| 130 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 131 | if (!fp) { |
| 132 | return nullptr; |
| 133 | } |
| 134 | return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn); |
| 135 | } |
| 136 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 137 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput( |
| 138 | std::unique_ptr<GrFragmentProcessor> fp) { |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 139 | if (!fp) { |
| 140 | return nullptr; |
| 141 | } |
Ethan Nicholas | be0a042 | 2017-11-17 13:44:05 -0500 | [diff] [blame] | 142 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(), |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 143 | std::move(fp) }; |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 144 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 145 | } |
| 146 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 147 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput( |
| 148 | std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 149 | class SwizzleFragmentProcessor : public GrFragmentProcessor { |
| 150 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 151 | static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) { |
| 152 | return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle)); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | const char* name() const override { return "Swizzle"; } |
| 156 | const GrSwizzle& swizzle() const { return fSwizzle; } |
| 157 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 158 | std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 159 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 160 | private: |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 161 | SwizzleFragmentProcessor(const GrSwizzle& swizzle) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 162 | : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags) |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 163 | , fSwizzle(swizzle) {} |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 164 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 165 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 166 | class GLFP : public GrGLSLFragmentProcessor { |
| 167 | public: |
| 168 | void emitCode(EmitArgs& args) override { |
| 169 | const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>(); |
| 170 | const GrSwizzle& swizzle = sfp.swizzle(); |
| 171 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 172 | |
| 173 | fragBuilder->codeAppendf("%s = %s.%s;", |
| 174 | args.fOutputColor, args.fInputColor, swizzle.c_str()); |
| 175 | } |
| 176 | }; |
| 177 | return new GLFP; |
| 178 | } |
| 179 | |
| 180 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 181 | b->add32(fSwizzle.asKey()); |
| 182 | } |
| 183 | |
| 184 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
| 185 | const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>(); |
| 186 | return fSwizzle == sfp.fSwizzle; |
| 187 | } |
| 188 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 189 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 190 | return fSwizzle.applyTo(input); |
| 191 | } |
| 192 | |
| 193 | GrSwizzle fSwizzle; |
| 194 | |
| 195 | typedef GrFragmentProcessor INHERITED; |
| 196 | }; |
| 197 | |
| 198 | if (!fp) { |
| 199 | return nullptr; |
| 200 | } |
| 201 | if (GrSwizzle::RGBA() == swizzle) { |
| 202 | return fp; |
| 203 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 204 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), |
| 205 | SwizzleFragmentProcessor::Make(swizzle) }; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 206 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 207 | } |
| 208 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 209 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput( |
| 210 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 211 | class PremulFragmentProcessor : public GrFragmentProcessor { |
| 212 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 213 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 214 | std::unique_ptr<GrFragmentProcessor> processor) { |
| 215 | return std::unique_ptr<GrFragmentProcessor>( |
| 216 | new PremulFragmentProcessor(std::move(processor))); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | const char* name() const override { return "Premultiply"; } |
| 220 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 221 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 222 | return Make(this->childProcessor(0).clone()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 225 | private: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 226 | PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 227 | : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 228 | this->registerChildProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 229 | } |
| 230 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 231 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 232 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 233 | public: |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 234 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 235 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 236 | this->emitChild(0, args); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 237 | fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 238 | args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 239 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 240 | } |
| 241 | }; |
| 242 | return new GLFP; |
| 243 | } |
| 244 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 245 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 246 | |
| 247 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 248 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 249 | static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) { |
| 250 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 251 | if (inner->preservesOpaqueInput()) { |
| 252 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 253 | } |
| 254 | if (inner->hasConstantOutputForConstantInput()) { |
| 255 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 256 | } |
| 257 | return flags; |
| 258 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 259 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 260 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { |
| 261 | SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0), |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 262 | SK_PMColor4fWHITE); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 263 | SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul(); |
| 264 | return premulInput * childColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | typedef GrFragmentProcessor INHERITED; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 268 | }; |
| 269 | if (!fp) { |
| 270 | return nullptr; |
| 271 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 272 | return PremulFragmentProcessor::Make(std::move(fp)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | ////////////////////////////////////////////////////////////////////////////// |
| 276 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 277 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput( |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 278 | std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 279 | if (!fp) { |
| 280 | return nullptr; |
| 281 | } |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 282 | return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 283 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 284 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 285 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries( |
| 286 | std::unique_ptr<GrFragmentProcessor>* series, int cnt) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 287 | class SeriesFragmentProcessor : public GrFragmentProcessor { |
| 288 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 289 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 290 | std::unique_ptr<GrFragmentProcessor>* children, int cnt) { |
| 291 | return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | const char* name() const override { return "Series"; } |
| 295 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 296 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 297 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 298 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 299 | if (!children.push_back(this->childProcessor(i).clone())) { |
| 300 | return nullptr; |
| 301 | } |
| 302 | } |
| 303 | return Make(children.begin(), this->numChildProcessors()); |
| 304 | } |
| 305 | |
| 306 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 307 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 308 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 309 | public: |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 310 | void emitCode(EmitArgs& args) override { |
dvonbeck | ca9eeab | 2016-07-06 12:00:06 -0700 | [diff] [blame] | 311 | // First guy's input might be nil. |
| 312 | SkString temp("out0"); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 313 | this->emitChild(0, args.fInputColor, &temp, args); |
dvonbeck | ca9eeab | 2016-07-06 12:00:06 -0700 | [diff] [blame] | 314 | SkString input = temp; |
| 315 | for (int i = 1; i < this->numChildProcessors() - 1; ++i) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 316 | temp.printf("out%d", i); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 317 | this->emitChild(i, input.c_str(), &temp, args); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 318 | input = temp; |
| 319 | } |
| 320 | // Last guy writes to our output variable. |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 321 | this->emitChild(this->numChildProcessors() - 1, input.c_str(), args); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 322 | } |
| 323 | }; |
| 324 | return new GLFP; |
| 325 | } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 326 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 327 | SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 328 | : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 329 | SkASSERT(cnt > 1); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 330 | for (int i = 0; i < cnt; ++i) { |
| 331 | this->registerChildProcessor(std::move(children[i])); |
| 332 | } |
| 333 | } |
| 334 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 335 | static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 336 | OptimizationFlags flags = kAll_OptimizationFlags; |
| 337 | for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) { |
| 338 | flags &= children[i]->optimizationFlags(); |
| 339 | } |
| 340 | return flags; |
| 341 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 342 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 343 | |
| 344 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 345 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 346 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override { |
| 347 | SkPMColor4f color = inColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 348 | int childCnt = this->numChildProcessors(); |
| 349 | for (int i = 0; i < childCnt; ++i) { |
| 350 | color = ConstantOutputForConstantInput(this->childProcessor(i), color); |
| 351 | } |
| 352 | return color; |
| 353 | } |
| 354 | |
| 355 | typedef GrFragmentProcessor INHERITED; |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | if (!cnt) { |
| 359 | return nullptr; |
| 360 | } |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 361 | if (1 == cnt) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 362 | return std::move(series[0]); |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 363 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 364 | // 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] | 365 | GrProcessorAnalysisColor inputColor; |
| 366 | inputColor.setToUnknown(); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 367 | GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series), |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 368 | cnt); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 369 | SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries; |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 370 | SkPMColor4f knownColor; |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 371 | int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor); |
| 372 | if (leadingFPsToEliminate) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 373 | std::unique_ptr<GrFragmentProcessor> colorFP( |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 374 | GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore)); |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 375 | if (leadingFPsToEliminate == cnt) { |
| 376 | return colorFP; |
| 377 | } |
| 378 | cnt = cnt - leadingFPsToEliminate + 1; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 379 | replacementSeries.reserve(cnt); |
| 380 | replacementSeries.emplace_back(std::move(colorFP)); |
| 381 | for (int i = 0; i < cnt - 1; ++i) { |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 382 | replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 383 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 384 | series = replacementSeries.begin(); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 385 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 386 | return SeriesFragmentProcessor::Make(series, cnt); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 387 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 388 | |
| 389 | ////////////////////////////////////////////////////////////////////////////// |
| 390 | |
| 391 | GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) { |
| 392 | for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) { |
| 393 | fFPStack.push_back(&pipeline.getFragmentProcessor(i)); |
| 394 | } |
| 395 | } |
| 396 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 397 | GrFragmentProcessor::Iter::Iter(const GrPaint& paint) { |
| 398 | for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) { |
| 399 | fFPStack.push_back(paint.getCoverageFragmentProcessor(i)); |
| 400 | } |
| 401 | for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) { |
| 402 | fFPStack.push_back(paint.getColorFragmentProcessor(i)); |
| 403 | } |
| 404 | } |
| 405 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 406 | const GrFragmentProcessor* GrFragmentProcessor::Iter::next() { |
| 407 | if (fFPStack.empty()) { |
| 408 | return nullptr; |
| 409 | } |
| 410 | const GrFragmentProcessor* back = fFPStack.back(); |
| 411 | fFPStack.pop_back(); |
| 412 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 413 | fFPStack.push_back(&back->childProcessor(i)); |
| 414 | } |
| 415 | return back; |
| 416 | } |
| 417 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 418 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 419 | |
| 420 | GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy, |
| 421 | const GrSamplerState& samplerState) { |
| 422 | this->reset(std::move(proxy), samplerState); |
| 423 | } |
| 424 | |
| 425 | GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy, |
| 426 | GrSamplerState::Filter filterMode, |
| 427 | GrSamplerState::WrapMode wrapXAndY) { |
| 428 | this->reset(std::move(proxy), filterMode, wrapXAndY); |
| 429 | } |
| 430 | |
| 431 | void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy, |
| 432 | const GrSamplerState& samplerState) { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 433 | fProxy = std::move(proxy); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 434 | fSamplerState = samplerState; |
| 435 | fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode())); |
| 436 | } |
| 437 | |
| 438 | void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy, |
| 439 | GrSamplerState::Filter filterMode, |
| 440 | GrSamplerState::WrapMode wrapXAndY) { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 441 | fProxy = std::move(proxy); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 442 | filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode()); |
| 443 | fSamplerState = GrSamplerState(wrapXAndY, filterMode); |
| 444 | } |