blob: e4b18c89fcf78ffae39d9843ad9a4d28c936e126 [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 Nicholas58430122020-04-14 09:54:02 -040077void GrFragmentProcessor::setSampleMatrix(SkSL::SampleMatrix matrix) {
78 if (matrix == fMatrix) {
79 return;
80 }
81 SkASSERT(fMatrix.fKind != SkSL::SampleMatrix::Kind::kVariable);
82 if (fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform) {
83 SkASSERT(matrix.fKind == SkSL::SampleMatrix::Kind::kVariable ||
84 (matrix.fKind == SkSL::SampleMatrix::Kind::kMixed &&
85 matrix.fExpression == fMatrix.fExpression));
86 fMatrix = SkSL::SampleMatrix(SkSL::SampleMatrix::Kind::kMixed, fMatrix.fOwner,
87 fMatrix.fExpression);
88 } else {
89 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone);
90 fMatrix = matrix;
91 }
92 if (matrix.fKind == SkSL::SampleMatrix::Kind::kVariable) {
93 for (auto& child : fChildProcessors) {
94 child->setSampleMatrix(matrix);
95 }
96 }
97}
98
Robert Phillips82774f82019-06-20 14:38:27 -040099#ifdef SK_DEBUG
100bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -0400101 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400102 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -0400103 return false;
104 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400105 }
106
Robert Phillips9bee2e52017-05-29 12:37:20 -0400107 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400108 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -0400109 return false;
110 }
111 }
112
113 return true;
114}
Robert Phillips82774f82019-06-20 14:38:27 -0400115#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400116
Brian Salomonaff329b2017-08-11 09:40:37 -0400117int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
Ethan Nicholas7ef777e2020-02-12 13:06:27 -0500118 if (child->fFlags & kHasCoordTransforms_Flag) {
119 fFlags |= kHasCoordTransforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700120 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700121 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700122
bungeman06ca8ec2016-06-09 08:01:03 -0700123 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400124 fChildProcessors.push_back(std::move(child));
Ethan Nicholas58430122020-04-14 09:54:02 -0400125 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone ||
126 fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform);
bsalomonbf877302015-09-22 09:06:13 -0700127 return index;
128}
129
bsalomonbf877302015-09-22 09:06:13 -0700130bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700131 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700132 return false;
133 }
bsalomona624bf32016-09-20 09:12:47 -0700134 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700135 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500136 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700137 return false;
138 }
139 }
140 return true;
141}
142
Mike Reed28eaed22018-02-01 11:24:53 -0500143std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400144 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700145 if (!fp) {
146 return nullptr;
147 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400148 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700149}
150
Mike Reed28eaed22018-02-01 11:24:53 -0500151std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
152 std::unique_ptr<GrFragmentProcessor> fp) {
153 if (!fp) {
154 return nullptr;
155 }
156 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
157}
158
Brian Salomonaff329b2017-08-11 09:40:37 -0400159std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
160 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700161 if (!fp) {
162 return nullptr;
163 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500164 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400165 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700166 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
167}
168
Brian Osman6f5e9402020-01-22 10:39:31 -0500169std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
170 std::unique_ptr<GrFragmentProcessor> fp) {
171 if (!fp) {
172 return nullptr;
173 }
174 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {
175 std::move(fp),
176 GrClampFragmentProcessor::Make(true)
177 };
178 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
179}
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:
Brian Salomonaff329b2017-08-11 09:40:37 -0400185 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
186 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400187 }
188
189 const char* name() const override { return "Swizzle"; }
190 const GrSwizzle& swizzle() const { return fSwizzle; }
191
Brian Salomonaff329b2017-08-11 09:40:37 -0400192 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400193
Brian Osmance425512017-03-22 14:37:50 -0400194 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400195 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400196 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400197 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400198
Brian Osmance425512017-03-22 14:37:50 -0400199 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
200 class GLFP : public GrGLSLFragmentProcessor {
201 public:
202 void emitCode(EmitArgs& args) override {
203 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
204 const GrSwizzle& swizzle = sfp.swizzle();
205 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
206
207 fragBuilder->codeAppendf("%s = %s.%s;",
Greg Daniel369ee6b2019-12-02 15:30:02 -0500208 args.fOutputColor, args.fInputColor, swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400209 }
210 };
211 return new GLFP;
212 }
213
214 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
215 b->add32(fSwizzle.asKey());
216 }
217
218 bool onIsEqual(const GrFragmentProcessor& other) const override {
219 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
220 return fSwizzle == sfp.fSwizzle;
221 }
222
Brian Osman1d5b5982018-10-01 13:41:39 -0400223 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400224 return fSwizzle.applyTo(input);
225 }
226
227 GrSwizzle fSwizzle;
228
229 typedef GrFragmentProcessor INHERITED;
230 };
231
232 if (!fp) {
233 return nullptr;
234 }
235 if (GrSwizzle::RGBA() == swizzle) {
236 return fp;
237 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400238 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
239 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400240 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
241}
242
Brian Salomonaff329b2017-08-11 09:40:37 -0400243std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
244 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700245 class PremulFragmentProcessor : public GrFragmentProcessor {
246 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400247 static std::unique_ptr<GrFragmentProcessor> Make(
248 std::unique_ptr<GrFragmentProcessor> processor) {
249 return std::unique_ptr<GrFragmentProcessor>(
250 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400251 }
252
253 const char* name() const override { return "Premultiply"; }
254
Brian Salomonaff329b2017-08-11 09:40:37 -0400255 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400256 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400257 }
258
Robert Phillips1c9686b2017-06-30 08:40:28 -0400259 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400260 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400261 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400262 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700263 }
264
egdaniel57d3b032015-11-13 11:57:27 -0800265 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800266 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700267 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700268 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800269 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500270 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500271 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800272 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500273 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800274 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700275 }
276 };
277 return new GLFP;
278 }
279
Brian Salomon94efbf52016-11-29 13:43:05 -0500280 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700281
282 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
283
Brian Salomon587e08f2017-01-27 10:59:27 -0500284 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
285 OptimizationFlags flags = kNone_OptimizationFlags;
286 if (inner->preservesOpaqueInput()) {
287 flags |= kPreservesOpaqueInput_OptimizationFlag;
288 }
289 if (inner->hasConstantOutputForConstantInput()) {
290 flags |= kConstantOutputForConstantInput_OptimizationFlag;
291 }
292 return flags;
293 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700294
Brian Osman1d5b5982018-10-01 13:41:39 -0400295 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
296 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400297 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400298 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
299 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500300 }
301
302 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700303 };
304 if (!fp) {
305 return nullptr;
306 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400307 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700308}
309
310//////////////////////////////////////////////////////////////////////////////
311
Brian Salomonaff329b2017-08-11 09:40:37 -0400312std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400313 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400314 if (!fp) {
315 return nullptr;
316 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400317 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700318}
bsalomone25eea42015-09-29 06:38:55 -0700319
Brian Salomonaff329b2017-08-11 09:40:37 -0400320std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500321 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700322 class SeriesFragmentProcessor : public GrFragmentProcessor {
323 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400324 static std::unique_ptr<GrFragmentProcessor> Make(
325 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
326 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700327 }
328
329 const char* name() const override { return "Series"; }
330
Brian Salomonaff329b2017-08-11 09:40:37 -0400331 std::unique_ptr<GrFragmentProcessor> clone() const override {
332 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400333 for (int i = 0; i < this->numChildProcessors(); ++i) {
334 if (!children.push_back(this->childProcessor(i).clone())) {
335 return nullptr;
336 }
337 }
338 return Make(children.begin(), this->numChildProcessors());
339 }
340
341 private:
egdaniel57d3b032015-11-13 11:57:27 -0800342 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800343 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700344 public:
bsalomone25eea42015-09-29 06:38:55 -0700345 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700346 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500347 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500348 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500349 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700350 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500351 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500352 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700353 }
354 };
355 return new GLFP;
356 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400357
Brian Salomonaff329b2017-08-11 09:40:37 -0400358 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400359 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400360 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400361 for (int i = 0; i < cnt; ++i) {
362 this->registerChildProcessor(std::move(children[i]));
363 }
364 }
365
Brian Salomonaff329b2017-08-11 09:40:37 -0400366 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500367 OptimizationFlags flags = kAll_OptimizationFlags;
368 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
369 flags &= children[i]->optimizationFlags();
370 }
371 return flags;
372 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500373 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700374
375 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
376
Brian Osman1d5b5982018-10-01 13:41:39 -0400377 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
378 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500379 int childCnt = this->numChildProcessors();
380 for (int i = 0; i < childCnt; ++i) {
381 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
382 }
383 return color;
384 }
385
386 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700387 };
388
389 if (!cnt) {
390 return nullptr;
391 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500392 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400393 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500394 }
bsalomone25eea42015-09-29 06:38:55 -0700395 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400396 GrProcessorAnalysisColor inputColor;
397 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500398 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400399 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400400 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500401 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
402 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400403 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500404 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500405 if (leadingFPsToEliminate == cnt) {
406 return colorFP;
407 }
408 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700409 replacementSeries.reserve(cnt);
410 replacementSeries.emplace_back(std::move(colorFP));
411 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500412 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700413 }
bungeman06ca8ec2016-06-09 08:01:03 -0700414 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700415 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400416 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700417}
bsalomona624bf32016-09-20 09:12:47 -0700418
419//////////////////////////////////////////////////////////////////////////////
420
Brian Salomon7eabfe82019-12-02 14:20:20 -0500421GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600422 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
423 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
424 }
425 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
426 fFPStack.push_back(paint.getColorFragmentProcessor(i));
427 }
428}
429
Brian Salomon7eabfe82019-12-02 14:20:20 -0500430GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500431 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
432 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700433 }
Brian Salomonc241b582019-11-27 08:57:17 -0500434 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
435 fFPStack.push_back(set.colorFragmentProcessor(i));
436 }
437}
438
Brian Salomon7eabfe82019-12-02 14:20:20 -0500439GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500440 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
441 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
442 }
443}
444
Brian Salomone782f842018-07-31 13:53:11 -0400445///////////////////////////////////////////////////////////////////////////////////////////////////
446
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000447GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500448 GrSamplerState samplerState)
449 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000450 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500451 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500452 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000453 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500454}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000455
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000456#if GR_TEST_UTILS
457void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500458 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000459 SkASSERT(view.proxy()->asTextureProxy());
460 fView = std::move(view);
461 fSamplerState = samplerState;
462
463 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500464 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000465 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
466}
467#endif