blob: 999f9fd9cdc7bcfac2ee33a35f52e52575d419bd [file] [log] [blame]
bsalomonbf877302015-09-22 09:06:13 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Brian Osman6f5e9402020-01-22 10:39:31 -050013#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/effects/generated/GrConstColorProcessor.h"
15#include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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"
bsalomonbf877302015-09-22 09:06:13 -070020
bsalomon7312ff82016-09-12 08:55:38 -070021bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
Brian Salomone782f842018-07-31 13:53:11 -040022 if (this->classID() != that.classID()) {
bsalomonbf877302015-09-22 09:06:13 -070023 return false;
24 }
Brian Salomone782f842018-07-31 13:53:11 -040025 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 }
bsalomon7312ff82016-09-12 08:55:38 -070033 if (!this->hasSameTransforms(that)) {
bsalomonbf877302015-09-22 09:06:13 -070034 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) {
bsalomon7312ff82016-09-12 08:55:38 -070043 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070044 return false;
45 }
46 }
47 return true;
48}
49
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060050void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) {
Brian Salomonc241b582019-11-27 08:57:17 -050051 for (auto [sampler, fp] : FPTextureSamplerRange(*this)) {
52 bool mipped = (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter());
Robert Phillipsbd99c0c2019-12-12 13:26:58 +000053 func(sampler.view().proxy(), GrMipMapped(mipped));
Brian Salomone782f842018-07-31 13:53:11 -040054 }
55}
56
egdaniel57d3b032015-11-13 11:57:27 -080057GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
58 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070059 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
60 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080061 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070062 }
63 return glFragProc;
64}
65
Brian Salomone782f842018-07-31 13:53:11 -040066const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
67 SkASSERT(i >= 0 && i < fTextureSamplerCnt);
68 return this->onTextureSampler(i);
69}
70
Ethan Nicholasd4efe682019-08-29 16:10:13 -040071void GrFragmentProcessor::addCoordTransform(GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070072 fCoordTransforms.push_back(transform);
Ethan Nicholas7ef777e2020-02-12 13:06:27 -050073 fFlags |= kHasCoordTransforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -070074}
75
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040076void GrFragmentProcessor::setSampleMatrix(SkSL::SampleMatrix newMatrix) {
77 if (newMatrix == fMatrix) {
Ethan Nicholas58430122020-04-14 09:54:02 -040078 return;
79 }
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040080 SkASSERT(newMatrix.fKind != SkSL::SampleMatrix::Kind::kNone);
Ethan Nicholas58430122020-04-14 09:54:02 -040081 SkASSERT(fMatrix.fKind != SkSL::SampleMatrix::Kind::kVariable);
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040082 if (this->numCoordTransforms() == 0 &&
83 (newMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform ||
84 newMatrix.fKind == SkSL::SampleMatrix::Kind::kMixed)) {
85 // as things stand, matrices only work when there's a coord transform, so we need to add
86 // an identity transform to keep the downstream code happy
87 static GrCoordTransform identity;
88 this->addCoordTransform(&identity);
89 }
Ethan Nicholas58430122020-04-14 09:54:02 -040090 if (fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform) {
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040091 if (newMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform) {
92 // need to base this transform on the one that happened in our parent
Brian Salomon25776232020-06-10 12:47:25 -040093 // If we're already based on something, then we have to assume that parent is now
94 // based on yet another transform, so don't update our base pointer (or we'll skip
95 // the intermediate transform).
96 if (!fMatrix.fBase) {
97 fMatrix.fBase = newMatrix.fOwner;
98 }
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040099 } else {
100 SkASSERT(newMatrix.fKind == SkSL::SampleMatrix::Kind::kVariable);
101 fMatrix = SkSL::SampleMatrix(SkSL::SampleMatrix::Kind::kMixed, fMatrix.fOwner,
102 fMatrix.fExpression);
103 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400104 } else {
105 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone);
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400106 fMatrix = newMatrix;
Ethan Nicholas58430122020-04-14 09:54:02 -0400107 }
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400108 for (auto& child : fChildProcessors) {
109 child->setSampleMatrix(newMatrix);
Ethan Nicholas58430122020-04-14 09:54:02 -0400110 }
111}
112
Robert Phillips82774f82019-06-20 14:38:27 -0400113#ifdef SK_DEBUG
114bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -0400115 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400116 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -0400117 return false;
118 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400119 }
120
Robert Phillips9bee2e52017-05-29 12:37:20 -0400121 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400122 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -0400123 return false;
124 }
125 }
126
127 return true;
128}
Robert Phillips82774f82019-06-20 14:38:27 -0400129#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400130
Brian Salomonaff329b2017-08-11 09:40:37 -0400131int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
Ethan Nicholas7ef777e2020-02-12 13:06:27 -0500132 if (child->fFlags & kHasCoordTransforms_Flag) {
133 fFlags |= kHasCoordTransforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700134 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700135 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700136
bungeman06ca8ec2016-06-09 08:01:03 -0700137 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400138 fChildProcessors.push_back(std::move(child));
Ethan Nicholas58430122020-04-14 09:54:02 -0400139 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone ||
140 fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform);
bsalomonbf877302015-09-22 09:06:13 -0700141 return index;
142}
143
John Stiles3779f442020-06-15 10:48:49 -0400144int GrFragmentProcessor::cloneAndRegisterChildProcessor(const GrFragmentProcessor& fp) {
145 std::unique_ptr<GrFragmentProcessor> clone = fp.clone();
146 if (fp.isSampledWithExplicitCoords()) {
147 clone->setSampledWithExplicitCoords();
148 }
149 return this->registerChildProcessor(std::move(clone));
150}
151
John Stiles9ec6b052020-06-15 12:06:10 -0400152void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
153 for (int i = 0; i < src.numChildProcessors(); ++i) {
154 this->cloneAndRegisterChildProcessor(src.childProcessor(i));
155 }
156}
157
bsalomonbf877302015-09-22 09:06:13 -0700158bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700159 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700160 return false;
161 }
bsalomona624bf32016-09-20 09:12:47 -0700162 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700163 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500164 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700165 return false;
166 }
167 }
168 return true;
169}
170
Mike Reed28eaed22018-02-01 11:24:53 -0500171std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400172 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700173 if (!fp) {
174 return nullptr;
175 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400176 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700177}
178
Mike Reed28eaed22018-02-01 11:24:53 -0500179std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
180 std::unique_ptr<GrFragmentProcessor> fp) {
181 if (!fp) {
182 return nullptr;
183 }
184 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
185}
186
Brian Osman6f5e9402020-01-22 10:39:31 -0500187std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
188 std::unique_ptr<GrFragmentProcessor> fp) {
189 if (!fp) {
190 return nullptr;
191 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400192 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500193}
194
Brian Salomonaff329b2017-08-11 09:40:37 -0400195std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
196 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400197 class SwizzleFragmentProcessor : public GrFragmentProcessor {
198 public:
John Stileseed56f02020-06-04 13:30:51 -0400199 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
200 const GrSwizzle& swizzle) {
201 return std::unique_ptr<GrFragmentProcessor>(
202 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400203 }
204
205 const char* name() const override { return "Swizzle"; }
206 const GrSwizzle& swizzle() const { return fSwizzle; }
207
John Stileseed56f02020-06-04 13:30:51 -0400208 std::unique_ptr<GrFragmentProcessor> clone() const override {
209 return Make(this->childProcessor(0).clone(), fSwizzle);
210 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400211
Brian Osmance425512017-03-22 14:37:50 -0400212 private:
John Stileseed56f02020-06-04 13:30:51 -0400213 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
214 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
215 , fSwizzle(swizzle) {
216 this->registerChildProcessor(std::move(fp));
217 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400218
Brian Osmance425512017-03-22 14:37:50 -0400219 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
220 class GLFP : public GrGLSLFragmentProcessor {
221 public:
222 void emitCode(EmitArgs& args) override {
John Stiles0af87fe2020-06-05 13:33:09 -0400223 SkString childColor = this->invokeChild(0, args.fInputColor, args);
John Stileseed56f02020-06-04 13:30:51 -0400224
Brian Osmance425512017-03-22 14:37:50 -0400225 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
226 const GrSwizzle& swizzle = sfp.swizzle();
227 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
228
229 fragBuilder->codeAppendf("%s = %s.%s;",
John Stileseed56f02020-06-04 13:30:51 -0400230 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400231 }
232 };
233 return new GLFP;
234 }
235
236 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
237 b->add32(fSwizzle.asKey());
238 }
239
240 bool onIsEqual(const GrFragmentProcessor& other) const override {
241 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
242 return fSwizzle == sfp.fSwizzle;
243 }
244
Brian Osman1d5b5982018-10-01 13:41:39 -0400245 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400246 return fSwizzle.applyTo(input);
247 }
248
249 GrSwizzle fSwizzle;
250
251 typedef GrFragmentProcessor INHERITED;
252 };
253
254 if (!fp) {
255 return nullptr;
256 }
257 if (GrSwizzle::RGBA() == swizzle) {
258 return fp;
259 }
John Stileseed56f02020-06-04 13:30:51 -0400260 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400261}
262
Brian Salomonaff329b2017-08-11 09:40:37 -0400263std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
264 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700265 class PremulFragmentProcessor : public GrFragmentProcessor {
266 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400267 static std::unique_ptr<GrFragmentProcessor> Make(
268 std::unique_ptr<GrFragmentProcessor> processor) {
269 return std::unique_ptr<GrFragmentProcessor>(
270 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400271 }
272
273 const char* name() const override { return "Premultiply"; }
274
Brian Salomonaff329b2017-08-11 09:40:37 -0400275 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400276 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400277 }
278
Robert Phillips1c9686b2017-06-30 08:40:28 -0400279 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400280 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400281 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400282 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700283 }
284
egdaniel57d3b032015-11-13 11:57:27 -0800285 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800286 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700287 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700288 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800289 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500290 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500291 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800292 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500293 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800294 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700295 }
296 };
297 return new GLFP;
298 }
299
Brian Salomon94efbf52016-11-29 13:43:05 -0500300 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700301
302 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
303
Brian Salomon587e08f2017-01-27 10:59:27 -0500304 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
305 OptimizationFlags flags = kNone_OptimizationFlags;
306 if (inner->preservesOpaqueInput()) {
307 flags |= kPreservesOpaqueInput_OptimizationFlag;
308 }
309 if (inner->hasConstantOutputForConstantInput()) {
310 flags |= kConstantOutputForConstantInput_OptimizationFlag;
311 }
312 return flags;
313 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700314
Brian Osman1d5b5982018-10-01 13:41:39 -0400315 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
316 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400317 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400318 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
319 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500320 }
321
322 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700323 };
324 if (!fp) {
325 return nullptr;
326 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400327 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700328}
329
330//////////////////////////////////////////////////////////////////////////////
331
Brian Salomonaff329b2017-08-11 09:40:37 -0400332std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400333 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400334 if (!fp) {
335 return nullptr;
336 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400337 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700338}
bsalomone25eea42015-09-29 06:38:55 -0700339
Brian Salomonaff329b2017-08-11 09:40:37 -0400340std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500341 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700342 class SeriesFragmentProcessor : public GrFragmentProcessor {
343 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400344 static std::unique_ptr<GrFragmentProcessor> Make(
345 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
346 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700347 }
348
349 const char* name() const override { return "Series"; }
350
Brian Salomonaff329b2017-08-11 09:40:37 -0400351 std::unique_ptr<GrFragmentProcessor> clone() const override {
352 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400353 for (int i = 0; i < this->numChildProcessors(); ++i) {
354 if (!children.push_back(this->childProcessor(i).clone())) {
355 return nullptr;
356 }
357 }
358 return Make(children.begin(), this->numChildProcessors());
359 }
360
361 private:
egdaniel57d3b032015-11-13 11:57:27 -0800362 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800363 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700364 public:
bsalomone25eea42015-09-29 06:38:55 -0700365 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700366 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500367 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500368 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500369 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700370 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500371 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500372 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700373 }
374 };
375 return new GLFP;
376 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400377
Brian Salomonaff329b2017-08-11 09:40:37 -0400378 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400379 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400380 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400381 for (int i = 0; i < cnt; ++i) {
382 this->registerChildProcessor(std::move(children[i]));
383 }
384 }
385
Brian Salomonaff329b2017-08-11 09:40:37 -0400386 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500387 OptimizationFlags flags = kAll_OptimizationFlags;
388 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
389 flags &= children[i]->optimizationFlags();
390 }
391 return flags;
392 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500393 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700394
395 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
396
Brian Osman1d5b5982018-10-01 13:41:39 -0400397 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
398 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500399 int childCnt = this->numChildProcessors();
400 for (int i = 0; i < childCnt; ++i) {
401 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
402 }
403 return color;
404 }
405
406 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700407 };
408
409 if (!cnt) {
410 return nullptr;
411 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500412 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400413 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500414 }
bsalomone25eea42015-09-29 06:38:55 -0700415 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400416 GrProcessorAnalysisColor inputColor;
417 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500418 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400419 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400420 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500421 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
422 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400423 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500424 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500425 if (leadingFPsToEliminate == cnt) {
426 return colorFP;
427 }
428 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700429 replacementSeries.reserve(cnt);
430 replacementSeries.emplace_back(std::move(colorFP));
431 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500432 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700433 }
bungeman06ca8ec2016-06-09 08:01:03 -0700434 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700435 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400436 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700437}
bsalomona624bf32016-09-20 09:12:47 -0700438
439//////////////////////////////////////////////////////////////////////////////
440
Brian Salomon7eabfe82019-12-02 14:20:20 -0500441GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600442 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
443 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
444 }
445 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
446 fFPStack.push_back(paint.getColorFragmentProcessor(i));
447 }
448}
449
Brian Salomon7eabfe82019-12-02 14:20:20 -0500450GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500451 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
452 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700453 }
Brian Salomonc241b582019-11-27 08:57:17 -0500454 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
455 fFPStack.push_back(set.colorFragmentProcessor(i));
456 }
457}
458
Brian Salomon7eabfe82019-12-02 14:20:20 -0500459GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500460 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
461 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
462 }
463}
464
Brian Salomone782f842018-07-31 13:53:11 -0400465///////////////////////////////////////////////////////////////////////////////////////////////////
466
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000467GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500468 GrSamplerState samplerState)
469 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000470 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500471 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500472 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000473 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500474}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000475
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000476#if GR_TEST_UTILS
477void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500478 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000479 SkASSERT(view.proxy()->asTextureProxy());
480 fView = std::move(view);
481 fSamplerState = samplerState;
482
483 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500484 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000485 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
486}
487#endif