blob: 4aef86a3ac61e887ee400092f2d85748f69b6506 [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
93 fMatrix.fBase = newMatrix.fOwner;
94 } else {
95 SkASSERT(newMatrix.fKind == SkSL::SampleMatrix::Kind::kVariable);
96 fMatrix = SkSL::SampleMatrix(SkSL::SampleMatrix::Kind::kMixed, fMatrix.fOwner,
97 fMatrix.fExpression);
98 }
Ethan Nicholas58430122020-04-14 09:54:02 -040099 } else {
100 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone);
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400101 fMatrix = newMatrix;
Ethan Nicholas58430122020-04-14 09:54:02 -0400102 }
Ethan Nicholasd3a95c22020-06-03 13:24:46 -0400103 for (auto& child : fChildProcessors) {
104 child->setSampleMatrix(newMatrix);
Ethan Nicholas58430122020-04-14 09:54:02 -0400105 }
106}
107
Robert Phillips82774f82019-06-20 14:38:27 -0400108#ifdef SK_DEBUG
109bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -0400110 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400111 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -0400112 return false;
113 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400114 }
115
Robert Phillips9bee2e52017-05-29 12:37:20 -0400116 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400117 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -0400118 return false;
119 }
120 }
121
122 return true;
123}
Robert Phillips82774f82019-06-20 14:38:27 -0400124#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400125
Brian Salomonaff329b2017-08-11 09:40:37 -0400126int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
Ethan Nicholas7ef777e2020-02-12 13:06:27 -0500127 if (child->fFlags & kHasCoordTransforms_Flag) {
128 fFlags |= kHasCoordTransforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700129 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700130 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700131
bungeman06ca8ec2016-06-09 08:01:03 -0700132 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400133 fChildProcessors.push_back(std::move(child));
Ethan Nicholas58430122020-04-14 09:54:02 -0400134 SkASSERT(fMatrix.fKind == SkSL::SampleMatrix::Kind::kNone ||
135 fMatrix.fKind == SkSL::SampleMatrix::Kind::kConstantOrUniform);
bsalomonbf877302015-09-22 09:06:13 -0700136 return index;
137}
138
bsalomonbf877302015-09-22 09:06:13 -0700139bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700140 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700141 return false;
142 }
bsalomona624bf32016-09-20 09:12:47 -0700143 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700144 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500145 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700146 return false;
147 }
148 }
149 return true;
150}
151
Mike Reed28eaed22018-02-01 11:24:53 -0500152std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400153 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700154 if (!fp) {
155 return nullptr;
156 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400157 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700158}
159
Mike Reed28eaed22018-02-01 11:24:53 -0500160std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
161 std::unique_ptr<GrFragmentProcessor> fp) {
162 if (!fp) {
163 return nullptr;
164 }
165 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
166}
167
Brian Osman6f5e9402020-01-22 10:39:31 -0500168std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
169 std::unique_ptr<GrFragmentProcessor> fp) {
170 if (!fp) {
171 return nullptr;
172 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400173 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500174}
175
Brian Salomonaff329b2017-08-11 09:40:37 -0400176std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
177 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400178 class SwizzleFragmentProcessor : public GrFragmentProcessor {
179 public:
John Stileseed56f02020-06-04 13:30:51 -0400180 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
181 const GrSwizzle& swizzle) {
182 return std::unique_ptr<GrFragmentProcessor>(
183 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400184 }
185
186 const char* name() const override { return "Swizzle"; }
187 const GrSwizzle& swizzle() const { return fSwizzle; }
188
John Stileseed56f02020-06-04 13:30:51 -0400189 std::unique_ptr<GrFragmentProcessor> clone() const override {
190 return Make(this->childProcessor(0).clone(), fSwizzle);
191 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400192
Brian Osmance425512017-03-22 14:37:50 -0400193 private:
John Stileseed56f02020-06-04 13:30:51 -0400194 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
195 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
196 , fSwizzle(swizzle) {
197 this->registerChildProcessor(std::move(fp));
198 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400199
Brian Osmance425512017-03-22 14:37:50 -0400200 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
201 class GLFP : public GrGLSLFragmentProcessor {
202 public:
203 void emitCode(EmitArgs& args) override {
John Stiles0af87fe2020-06-05 13:33:09 -0400204 SkString childColor = this->invokeChild(0, args.fInputColor, args);
John Stileseed56f02020-06-04 13:30:51 -0400205
Brian Osmance425512017-03-22 14:37:50 -0400206 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
207 const GrSwizzle& swizzle = sfp.swizzle();
208 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
209
210 fragBuilder->codeAppendf("%s = %s.%s;",
John Stileseed56f02020-06-04 13:30:51 -0400211 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400212 }
213 };
214 return new GLFP;
215 }
216
217 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
218 b->add32(fSwizzle.asKey());
219 }
220
221 bool onIsEqual(const GrFragmentProcessor& other) const override {
222 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
223 return fSwizzle == sfp.fSwizzle;
224 }
225
Brian Osman1d5b5982018-10-01 13:41:39 -0400226 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400227 return fSwizzle.applyTo(input);
228 }
229
230 GrSwizzle fSwizzle;
231
232 typedef GrFragmentProcessor INHERITED;
233 };
234
235 if (!fp) {
236 return nullptr;
237 }
238 if (GrSwizzle::RGBA() == swizzle) {
239 return fp;
240 }
John Stileseed56f02020-06-04 13:30:51 -0400241 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400242}
243
Brian Salomonaff329b2017-08-11 09:40:37 -0400244std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
245 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700246 class PremulFragmentProcessor : public GrFragmentProcessor {
247 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400248 static std::unique_ptr<GrFragmentProcessor> Make(
249 std::unique_ptr<GrFragmentProcessor> processor) {
250 return std::unique_ptr<GrFragmentProcessor>(
251 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400252 }
253
254 const char* name() const override { return "Premultiply"; }
255
Brian Salomonaff329b2017-08-11 09:40:37 -0400256 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400257 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400258 }
259
Robert Phillips1c9686b2017-06-30 08:40:28 -0400260 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400261 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400262 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400263 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700264 }
265
egdaniel57d3b032015-11-13 11:57:27 -0800266 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800267 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700268 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700269 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800270 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500271 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500272 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800273 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500274 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800275 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700276 }
277 };
278 return new GLFP;
279 }
280
Brian Salomon94efbf52016-11-29 13:43:05 -0500281 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700282
283 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
284
Brian Salomon587e08f2017-01-27 10:59:27 -0500285 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
286 OptimizationFlags flags = kNone_OptimizationFlags;
287 if (inner->preservesOpaqueInput()) {
288 flags |= kPreservesOpaqueInput_OptimizationFlag;
289 }
290 if (inner->hasConstantOutputForConstantInput()) {
291 flags |= kConstantOutputForConstantInput_OptimizationFlag;
292 }
293 return flags;
294 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700295
Brian Osman1d5b5982018-10-01 13:41:39 -0400296 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
297 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400298 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400299 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
300 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500301 }
302
303 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700304 };
305 if (!fp) {
306 return nullptr;
307 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400308 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700309}
310
311//////////////////////////////////////////////////////////////////////////////
312
Brian Salomonaff329b2017-08-11 09:40:37 -0400313std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400314 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400315 if (!fp) {
316 return nullptr;
317 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400318 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700319}
bsalomone25eea42015-09-29 06:38:55 -0700320
Brian Salomonaff329b2017-08-11 09:40:37 -0400321std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500322 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700323 class SeriesFragmentProcessor : public GrFragmentProcessor {
324 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400325 static std::unique_ptr<GrFragmentProcessor> Make(
326 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
327 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700328 }
329
330 const char* name() const override { return "Series"; }
331
Brian Salomonaff329b2017-08-11 09:40:37 -0400332 std::unique_ptr<GrFragmentProcessor> clone() const override {
333 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400334 for (int i = 0; i < this->numChildProcessors(); ++i) {
335 if (!children.push_back(this->childProcessor(i).clone())) {
336 return nullptr;
337 }
338 }
339 return Make(children.begin(), this->numChildProcessors());
340 }
341
342 private:
egdaniel57d3b032015-11-13 11:57:27 -0800343 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800344 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700345 public:
bsalomone25eea42015-09-29 06:38:55 -0700346 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700347 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500348 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500349 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500350 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700351 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500352 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500353 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700354 }
355 };
356 return new GLFP;
357 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400358
Brian Salomonaff329b2017-08-11 09:40:37 -0400359 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400360 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400361 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400362 for (int i = 0; i < cnt; ++i) {
363 this->registerChildProcessor(std::move(children[i]));
364 }
365 }
366
Brian Salomonaff329b2017-08-11 09:40:37 -0400367 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500368 OptimizationFlags flags = kAll_OptimizationFlags;
369 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
370 flags &= children[i]->optimizationFlags();
371 }
372 return flags;
373 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500374 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700375
376 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
377
Brian Osman1d5b5982018-10-01 13:41:39 -0400378 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
379 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500380 int childCnt = this->numChildProcessors();
381 for (int i = 0; i < childCnt; ++i) {
382 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
383 }
384 return color;
385 }
386
387 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700388 };
389
390 if (!cnt) {
391 return nullptr;
392 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500393 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400394 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500395 }
bsalomone25eea42015-09-29 06:38:55 -0700396 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400397 GrProcessorAnalysisColor inputColor;
398 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500399 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400400 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400401 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500402 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
403 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400404 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500405 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500406 if (leadingFPsToEliminate == cnt) {
407 return colorFP;
408 }
409 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700410 replacementSeries.reserve(cnt);
411 replacementSeries.emplace_back(std::move(colorFP));
412 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500413 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700414 }
bungeman06ca8ec2016-06-09 08:01:03 -0700415 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700416 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400417 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700418}
bsalomona624bf32016-09-20 09:12:47 -0700419
420//////////////////////////////////////////////////////////////////////////////
421
Brian Salomon7eabfe82019-12-02 14:20:20 -0500422GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600423 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
424 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
425 }
426 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
427 fFPStack.push_back(paint.getColorFragmentProcessor(i));
428 }
429}
430
Brian Salomon7eabfe82019-12-02 14:20:20 -0500431GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500432 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
433 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700434 }
Brian Salomonc241b582019-11-27 08:57:17 -0500435 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
436 fFPStack.push_back(set.colorFragmentProcessor(i));
437 }
438}
439
Brian Salomon7eabfe82019-12-02 14:20:20 -0500440GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500441 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
442 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
443 }
444}
445
Brian Salomone782f842018-07-31 13:53:11 -0400446///////////////////////////////////////////////////////////////////////////////////////////////////
447
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000448GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500449 GrSamplerState samplerState)
450 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000451 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500452 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500453 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000454 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500455}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000456
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000457#if GR_TEST_UTILS
458void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500459 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000460 SkASSERT(view.proxy()->asTextureProxy());
461 fView = std::move(view);
462 fSamplerState = samplerState;
463
464 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500465 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000466 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
467}
468#endif