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