blob: 5b6633928a5544b2414e8efb1974c7a5fc65000d [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"
16#include "src/gpu/effects/generated/GrPremulInputFragmentProcessor.h"
17#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
18#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
19#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
20#include "src/gpu/glsl/GrGLSLUniformHandler.h"
bsalomonbf877302015-09-22 09:06:13 -070021
bsalomon7312ff82016-09-12 08:55:38 -070022bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
Brian Salomone782f842018-07-31 13:53:11 -040023 if (this->classID() != that.classID()) {
bsalomonbf877302015-09-22 09:06:13 -070024 return false;
25 }
Brian Salomone782f842018-07-31 13:53:11 -040026 if (this->numTextureSamplers() != that.numTextureSamplers()) {
27 return false;
28 }
29 for (int i = 0; i < this->numTextureSamplers(); ++i) {
30 if (this->textureSampler(i) != that.textureSampler(i)) {
31 return false;
32 }
33 }
bsalomon7312ff82016-09-12 08:55:38 -070034 if (!this->hasSameTransforms(that)) {
bsalomonbf877302015-09-22 09:06:13 -070035 return false;
36 }
37 if (!this->onIsEqual(that)) {
38 return false;
39 }
40 if (this->numChildProcessors() != that.numChildProcessors()) {
41 return false;
42 }
43 for (int i = 0; i < this->numChildProcessors(); ++i) {
bsalomon7312ff82016-09-12 08:55:38 -070044 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070045 return false;
46 }
47 }
48 return true;
49}
50
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060051void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) {
Brian Salomonc241b582019-11-27 08:57:17 -050052 for (auto [sampler, fp] : FPTextureSamplerRange(*this)) {
53 bool mipped = (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter());
Robert Phillipsbd99c0c2019-12-12 13:26:58 +000054 func(sampler.view().proxy(), GrMipMapped(mipped));
Brian Salomone782f842018-07-31 13:53:11 -040055 }
56}
57
egdaniel57d3b032015-11-13 11:57:27 -080058GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
59 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070060 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
61 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080062 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070063 }
64 return glFragProc;
65}
66
Brian Salomone782f842018-07-31 13:53:11 -040067const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
68 SkASSERT(i >= 0 && i < fTextureSamplerCnt);
69 return this->onTextureSampler(i);
70}
71
Ethan Nicholasd4efe682019-08-29 16:10:13 -040072void GrFragmentProcessor::addCoordTransform(GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070073 fCoordTransforms.push_back(transform);
Ethan Nicholas7ef777e2020-02-12 13:06:27 -050074 fFlags |= kHasCoordTransforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -070075}
76
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040077void GrFragmentProcessor::setSampleMatrix(SkSL::SampleMatrix newMatrix) {
78 if (newMatrix == fMatrix) {
Ethan Nicholas58430122020-04-14 09:54:02 -040079 return;
80 }
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040081 SkASSERT(newMatrix.fKind != SkSL::SampleMatrix::Kind::kNone);
Ethan Nicholas58430122020-04-14 09:54:02 -040082 SkASSERT(fMatrix.fKind != SkSL::SampleMatrix::Kind::kVariable);
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040083 if (this->numCoordTransforms() == 0 &&
84 (newMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform ||
85 newMatrix.fKind == SkSL::SampleMatrix::Kind::kMixed)) {
86 // as things stand, matrices only work when there's a coord transform, so we need to add
87 // an identity transform to keep the downstream code happy
88 static GrCoordTransform identity;
89 this->addCoordTransform(&identity);
90 }
Ethan Nicholas58430122020-04-14 09:54:02 -040091 if (fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform) {
Ethan Nicholasd3a95c22020-06-03 13:24:46 -040092 if (newMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform) {
93 // need to base this transform on the one that happened in our parent
94 fMatrix.fBase = newMatrix.fOwner;
95 } else {
96 SkASSERT(newMatrix.fKind == SkSL::SampleMatrix::Kind::kVariable);
97 fMatrix = SkSL::SampleMatrix(SkSL::SampleMatrix::Kind::kMixed, fMatrix.fOwner,
98 fMatrix.fExpression);
99 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400100 } else {
101 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone);
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400102 fMatrix = newMatrix;
Ethan Nicholas58430122020-04-14 09:54:02 -0400103 }
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400104 for (auto& child : fChildProcessors) {
105 child->setSampleMatrix(newMatrix);
Ethan Nicholas58430122020-04-14 09:54:02 -0400106 }
107}
108
Robert Phillips82774f82019-06-20 14:38:27 -0400109#ifdef SK_DEBUG
110bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -0400111 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400112 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -0400113 return false;
114 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400115 }
116
Robert Phillips9bee2e52017-05-29 12:37:20 -0400117 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400118 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -0400119 return false;
120 }
121 }
122
123 return true;
124}
Robert Phillips82774f82019-06-20 14:38:27 -0400125#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400126
Brian Salomonaff329b2017-08-11 09:40:37 -0400127int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
Ethan Nicholas7ef777e2020-02-12 13:06:27 -0500128 if (child->fFlags & kHasCoordTransforms_Flag) {
129 fFlags |= kHasCoordTransforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700130 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700131 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700132
bungeman06ca8ec2016-06-09 08:01:03 -0700133 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400134 fChildProcessors.push_back(std::move(child));
Ethan Nicholas58430122020-04-14 09:54:02 -0400135 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone ||
136 fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform);
bsalomonbf877302015-09-22 09:06:13 -0700137 return index;
138}
139
bsalomonbf877302015-09-22 09:06:13 -0700140bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700141 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700142 return false;
143 }
bsalomona624bf32016-09-20 09:12:47 -0700144 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700145 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500146 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700147 return false;
148 }
149 }
150 return true;
151}
152
Mike Reed28eaed22018-02-01 11:24:53 -0500153std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400154 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700155 if (!fp) {
156 return nullptr;
157 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400158 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700159}
160
Mike Reed28eaed22018-02-01 11:24:53 -0500161std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
162 std::unique_ptr<GrFragmentProcessor> fp) {
163 if (!fp) {
164 return nullptr;
165 }
166 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
167}
168
Brian Salomonaff329b2017-08-11 09:40:37 -0400169std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
170 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700171 if (!fp) {
172 return nullptr;
173 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500174 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400175 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700176 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
177}
178
Brian Osman6f5e9402020-01-22 10:39:31 -0500179std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
180 std::unique_ptr<GrFragmentProcessor> fp) {
181 if (!fp) {
182 return nullptr;
183 }
184 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {
185 std::move(fp),
186 GrClampFragmentProcessor::Make(true)
187 };
188 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
189}
190
Brian Salomonaff329b2017-08-11 09:40:37 -0400191std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
192 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400193 class SwizzleFragmentProcessor : public GrFragmentProcessor {
194 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400195 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
196 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400197 }
198
199 const char* name() const override { return "Swizzle"; }
200 const GrSwizzle& swizzle() const { return fSwizzle; }
201
Brian Salomonaff329b2017-08-11 09:40:37 -0400202 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400203
Brian Osmance425512017-03-22 14:37:50 -0400204 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400205 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400206 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400207 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400208
Brian Osmance425512017-03-22 14:37:50 -0400209 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
210 class GLFP : public GrGLSLFragmentProcessor {
211 public:
212 void emitCode(EmitArgs& args) override {
213 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
214 const GrSwizzle& swizzle = sfp.swizzle();
215 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
216
217 fragBuilder->codeAppendf("%s = %s.%s;",
Greg Daniel369ee6b2019-12-02 15:30:02 -0500218 args.fOutputColor, args.fInputColor, swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400219 }
220 };
221 return new GLFP;
222 }
223
224 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
225 b->add32(fSwizzle.asKey());
226 }
227
228 bool onIsEqual(const GrFragmentProcessor& other) const override {
229 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
230 return fSwizzle == sfp.fSwizzle;
231 }
232
Brian Osman1d5b5982018-10-01 13:41:39 -0400233 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400234 return fSwizzle.applyTo(input);
235 }
236
237 GrSwizzle fSwizzle;
238
239 typedef GrFragmentProcessor INHERITED;
240 };
241
242 if (!fp) {
243 return nullptr;
244 }
245 if (GrSwizzle::RGBA() == swizzle) {
246 return fp;
247 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400248 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
249 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400250 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
251}
252
Brian Salomonaff329b2017-08-11 09:40:37 -0400253std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
254 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700255 class PremulFragmentProcessor : public GrFragmentProcessor {
256 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400257 static std::unique_ptr<GrFragmentProcessor> Make(
258 std::unique_ptr<GrFragmentProcessor> processor) {
259 return std::unique_ptr<GrFragmentProcessor>(
260 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400261 }
262
263 const char* name() const override { return "Premultiply"; }
264
Brian Salomonaff329b2017-08-11 09:40:37 -0400265 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400266 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400267 }
268
Robert Phillips1c9686b2017-06-30 08:40:28 -0400269 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400270 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400271 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400272 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700273 }
274
egdaniel57d3b032015-11-13 11:57:27 -0800275 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800276 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700277 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700278 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800279 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500280 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500281 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800282 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500283 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800284 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700285 }
286 };
287 return new GLFP;
288 }
289
Brian Salomon94efbf52016-11-29 13:43:05 -0500290 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700291
292 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
293
Brian Salomon587e08f2017-01-27 10:59:27 -0500294 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
295 OptimizationFlags flags = kNone_OptimizationFlags;
296 if (inner->preservesOpaqueInput()) {
297 flags |= kPreservesOpaqueInput_OptimizationFlag;
298 }
299 if (inner->hasConstantOutputForConstantInput()) {
300 flags |= kConstantOutputForConstantInput_OptimizationFlag;
301 }
302 return flags;
303 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700304
Brian Osman1d5b5982018-10-01 13:41:39 -0400305 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
306 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400307 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400308 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
309 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500310 }
311
312 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700313 };
314 if (!fp) {
315 return nullptr;
316 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400317 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700318}
319
320//////////////////////////////////////////////////////////////////////////////
321
Brian Salomonaff329b2017-08-11 09:40:37 -0400322std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400323 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400324 if (!fp) {
325 return nullptr;
326 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400327 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700328}
bsalomone25eea42015-09-29 06:38:55 -0700329
Brian Salomonaff329b2017-08-11 09:40:37 -0400330std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500331 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700332 class SeriesFragmentProcessor : public GrFragmentProcessor {
333 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400334 static std::unique_ptr<GrFragmentProcessor> Make(
335 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
336 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700337 }
338
339 const char* name() const override { return "Series"; }
340
Brian Salomonaff329b2017-08-11 09:40:37 -0400341 std::unique_ptr<GrFragmentProcessor> clone() const override {
342 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400343 for (int i = 0; i < this->numChildProcessors(); ++i) {
344 if (!children.push_back(this->childProcessor(i).clone())) {
345 return nullptr;
346 }
347 }
348 return Make(children.begin(), this->numChildProcessors());
349 }
350
351 private:
egdaniel57d3b032015-11-13 11:57:27 -0800352 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800353 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700354 public:
bsalomone25eea42015-09-29 06:38:55 -0700355 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700356 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500357 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500358 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500359 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700360 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500361 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500362 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700363 }
364 };
365 return new GLFP;
366 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400367
Brian Salomonaff329b2017-08-11 09:40:37 -0400368 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400369 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400370 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400371 for (int i = 0; i < cnt; ++i) {
372 this->registerChildProcessor(std::move(children[i]));
373 }
374 }
375
Brian Salomonaff329b2017-08-11 09:40:37 -0400376 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500377 OptimizationFlags flags = kAll_OptimizationFlags;
378 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
379 flags &= children[i]->optimizationFlags();
380 }
381 return flags;
382 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500383 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700384
385 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
386
Brian Osman1d5b5982018-10-01 13:41:39 -0400387 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
388 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500389 int childCnt = this->numChildProcessors();
390 for (int i = 0; i < childCnt; ++i) {
391 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
392 }
393 return color;
394 }
395
396 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700397 };
398
399 if (!cnt) {
400 return nullptr;
401 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500402 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400403 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500404 }
bsalomone25eea42015-09-29 06:38:55 -0700405 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400406 GrProcessorAnalysisColor inputColor;
407 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500408 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400409 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400410 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500411 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
412 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400413 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500414 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500415 if (leadingFPsToEliminate == cnt) {
416 return colorFP;
417 }
418 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700419 replacementSeries.reserve(cnt);
420 replacementSeries.emplace_back(std::move(colorFP));
421 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500422 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700423 }
bungeman06ca8ec2016-06-09 08:01:03 -0700424 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700425 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400426 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700427}
bsalomona624bf32016-09-20 09:12:47 -0700428
429//////////////////////////////////////////////////////////////////////////////
430
Brian Salomon7eabfe82019-12-02 14:20:20 -0500431GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600432 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
433 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
434 }
435 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
436 fFPStack.push_back(paint.getColorFragmentProcessor(i));
437 }
438}
439
Brian Salomon7eabfe82019-12-02 14:20:20 -0500440GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500441 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
442 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700443 }
Brian Salomonc241b582019-11-27 08:57:17 -0500444 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
445 fFPStack.push_back(set.colorFragmentProcessor(i));
446 }
447}
448
Brian Salomon7eabfe82019-12-02 14:20:20 -0500449GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500450 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
451 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
452 }
453}
454
Brian Salomone782f842018-07-31 13:53:11 -0400455///////////////////////////////////////////////////////////////////////////////////////////////////
456
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000457GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500458 GrSamplerState samplerState)
459 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000460 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500461 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500462 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000463 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500464}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000465
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000466#if GR_TEST_UTILS
467void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500468 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000469 SkASSERT(view.proxy()->asTextureProxy());
470 fView = std::move(view);
471 fSamplerState = samplerState;
472
473 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500474 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000475 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
476}
477#endif