blob: fcae5e08c9c59d31c9a50d9b78c96605d70ec6f7 [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
bsalomonbf877302015-09-22 09:06:13 -0700144bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700145 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700146 return false;
147 }
bsalomona624bf32016-09-20 09:12:47 -0700148 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700149 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500150 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700151 return false;
152 }
153 }
154 return true;
155}
156
Mike Reed28eaed22018-02-01 11:24:53 -0500157std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400158 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700159 if (!fp) {
160 return nullptr;
161 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400162 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700163}
164
Mike Reed28eaed22018-02-01 11:24:53 -0500165std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
166 std::unique_ptr<GrFragmentProcessor> fp) {
167 if (!fp) {
168 return nullptr;
169 }
170 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
171}
172
Brian Osman6f5e9402020-01-22 10:39:31 -0500173std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
174 std::unique_ptr<GrFragmentProcessor> fp) {
175 if (!fp) {
176 return nullptr;
177 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400178 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500179}
180
Brian Salomonaff329b2017-08-11 09:40:37 -0400181std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
182 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400183 class SwizzleFragmentProcessor : public GrFragmentProcessor {
184 public:
John Stileseed56f02020-06-04 13:30:51 -0400185 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
186 const GrSwizzle& swizzle) {
187 return std::unique_ptr<GrFragmentProcessor>(
188 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400189 }
190
191 const char* name() const override { return "Swizzle"; }
192 const GrSwizzle& swizzle() const { return fSwizzle; }
193
John Stileseed56f02020-06-04 13:30:51 -0400194 std::unique_ptr<GrFragmentProcessor> clone() const override {
195 return Make(this->childProcessor(0).clone(), fSwizzle);
196 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400197
Brian Osmance425512017-03-22 14:37:50 -0400198 private:
John Stileseed56f02020-06-04 13:30:51 -0400199 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
200 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
201 , fSwizzle(swizzle) {
202 this->registerChildProcessor(std::move(fp));
203 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400204
Brian Osmance425512017-03-22 14:37:50 -0400205 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
206 class GLFP : public GrGLSLFragmentProcessor {
207 public:
208 void emitCode(EmitArgs& args) override {
John Stiles0af87fe2020-06-05 13:33:09 -0400209 SkString childColor = this->invokeChild(0, args.fInputColor, args);
John Stileseed56f02020-06-04 13:30:51 -0400210
Brian Osmance425512017-03-22 14:37:50 -0400211 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
212 const GrSwizzle& swizzle = sfp.swizzle();
213 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
214
215 fragBuilder->codeAppendf("%s = %s.%s;",
John Stileseed56f02020-06-04 13:30:51 -0400216 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400217 }
218 };
219 return new GLFP;
220 }
221
222 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
223 b->add32(fSwizzle.asKey());
224 }
225
226 bool onIsEqual(const GrFragmentProcessor& other) const override {
227 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
228 return fSwizzle == sfp.fSwizzle;
229 }
230
Brian Osman1d5b5982018-10-01 13:41:39 -0400231 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400232 return fSwizzle.applyTo(input);
233 }
234
235 GrSwizzle fSwizzle;
236
237 typedef GrFragmentProcessor INHERITED;
238 };
239
240 if (!fp) {
241 return nullptr;
242 }
243 if (GrSwizzle::RGBA() == swizzle) {
244 return fp;
245 }
John Stileseed56f02020-06-04 13:30:51 -0400246 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400247}
248
Brian Salomonaff329b2017-08-11 09:40:37 -0400249std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
250 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700251 class PremulFragmentProcessor : public GrFragmentProcessor {
252 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400253 static std::unique_ptr<GrFragmentProcessor> Make(
254 std::unique_ptr<GrFragmentProcessor> processor) {
255 return std::unique_ptr<GrFragmentProcessor>(
256 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400257 }
258
259 const char* name() const override { return "Premultiply"; }
260
Brian Salomonaff329b2017-08-11 09:40:37 -0400261 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400262 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400263 }
264
Robert Phillips1c9686b2017-06-30 08:40:28 -0400265 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400266 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400267 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400268 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700269 }
270
egdaniel57d3b032015-11-13 11:57:27 -0800271 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800272 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700273 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700274 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800275 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500276 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500277 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800278 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500279 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800280 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700281 }
282 };
283 return new GLFP;
284 }
285
Brian Salomon94efbf52016-11-29 13:43:05 -0500286 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700287
288 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
289
Brian Salomon587e08f2017-01-27 10:59:27 -0500290 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
291 OptimizationFlags flags = kNone_OptimizationFlags;
292 if (inner->preservesOpaqueInput()) {
293 flags |= kPreservesOpaqueInput_OptimizationFlag;
294 }
295 if (inner->hasConstantOutputForConstantInput()) {
296 flags |= kConstantOutputForConstantInput_OptimizationFlag;
297 }
298 return flags;
299 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700300
Brian Osman1d5b5982018-10-01 13:41:39 -0400301 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
302 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400303 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400304 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
305 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500306 }
307
308 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700309 };
310 if (!fp) {
311 return nullptr;
312 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400313 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700314}
315
316//////////////////////////////////////////////////////////////////////////////
317
Brian Salomonaff329b2017-08-11 09:40:37 -0400318std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400319 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400320 if (!fp) {
321 return nullptr;
322 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400323 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700324}
bsalomone25eea42015-09-29 06:38:55 -0700325
Brian Salomonaff329b2017-08-11 09:40:37 -0400326std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500327 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700328 class SeriesFragmentProcessor : public GrFragmentProcessor {
329 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400330 static std::unique_ptr<GrFragmentProcessor> Make(
331 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
332 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700333 }
334
335 const char* name() const override { return "Series"; }
336
Brian Salomonaff329b2017-08-11 09:40:37 -0400337 std::unique_ptr<GrFragmentProcessor> clone() const override {
338 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400339 for (int i = 0; i < this->numChildProcessors(); ++i) {
340 if (!children.push_back(this->childProcessor(i).clone())) {
341 return nullptr;
342 }
343 }
344 return Make(children.begin(), this->numChildProcessors());
345 }
346
347 private:
egdaniel57d3b032015-11-13 11:57:27 -0800348 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800349 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700350 public:
bsalomone25eea42015-09-29 06:38:55 -0700351 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700352 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500353 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500354 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500355 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700356 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500357 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500358 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700359 }
360 };
361 return new GLFP;
362 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400363
Brian Salomonaff329b2017-08-11 09:40:37 -0400364 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400365 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400366 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400367 for (int i = 0; i < cnt; ++i) {
368 this->registerChildProcessor(std::move(children[i]));
369 }
370 }
371
Brian Salomonaff329b2017-08-11 09:40:37 -0400372 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500373 OptimizationFlags flags = kAll_OptimizationFlags;
374 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
375 flags &= children[i]->optimizationFlags();
376 }
377 return flags;
378 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500379 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700380
381 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
382
Brian Osman1d5b5982018-10-01 13:41:39 -0400383 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
384 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500385 int childCnt = this->numChildProcessors();
386 for (int i = 0; i < childCnt; ++i) {
387 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
388 }
389 return color;
390 }
391
392 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700393 };
394
395 if (!cnt) {
396 return nullptr;
397 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500398 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400399 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500400 }
bsalomone25eea42015-09-29 06:38:55 -0700401 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400402 GrProcessorAnalysisColor inputColor;
403 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500404 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400405 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400406 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500407 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
408 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400409 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500410 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500411 if (leadingFPsToEliminate == cnt) {
412 return colorFP;
413 }
414 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700415 replacementSeries.reserve(cnt);
416 replacementSeries.emplace_back(std::move(colorFP));
417 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500418 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700419 }
bungeman06ca8ec2016-06-09 08:01:03 -0700420 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700421 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400422 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700423}
bsalomona624bf32016-09-20 09:12:47 -0700424
425//////////////////////////////////////////////////////////////////////////////
426
Brian Salomon7eabfe82019-12-02 14:20:20 -0500427GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600428 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
429 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
430 }
431 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
432 fFPStack.push_back(paint.getColorFragmentProcessor(i));
433 }
434}
435
Brian Salomon7eabfe82019-12-02 14:20:20 -0500436GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500437 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
438 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700439 }
Brian Salomonc241b582019-11-27 08:57:17 -0500440 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
441 fFPStack.push_back(set.colorFragmentProcessor(i));
442 }
443}
444
Brian Salomon7eabfe82019-12-02 14:20:20 -0500445GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500446 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
447 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
448 }
449}
450
Brian Salomone782f842018-07-31 13:53:11 -0400451///////////////////////////////////////////////////////////////////////////////////////////////////
452
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000453GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500454 GrSamplerState samplerState)
455 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000456 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500457 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500458 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000459 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500460}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000461
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000462#if GR_TEST_UTILS
463void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500464 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000465 SkASSERT(view.proxy()->asTextureProxy());
466 fView = std::move(view);
467 fSamplerState = samplerState;
468
469 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500470 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000471 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
472}
473#endif