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