blob: d887681f7dcf5e972524456fcde06e723324fa81 [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 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400184 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500185}
186
Brian Salomonaff329b2017-08-11 09:40:37 -0400187std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
188 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400189 class SwizzleFragmentProcessor : public GrFragmentProcessor {
190 public:
John Stileseed56f02020-06-04 13:30:51 -0400191 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
192 const GrSwizzle& swizzle) {
193 return std::unique_ptr<GrFragmentProcessor>(
194 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400195 }
196
197 const char* name() const override { return "Swizzle"; }
198 const GrSwizzle& swizzle() const { return fSwizzle; }
199
John Stileseed56f02020-06-04 13:30:51 -0400200 std::unique_ptr<GrFragmentProcessor> clone() const override {
201 return Make(this->childProcessor(0).clone(), fSwizzle);
202 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400203
Brian Osmance425512017-03-22 14:37:50 -0400204 private:
John Stileseed56f02020-06-04 13:30:51 -0400205 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
206 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
207 , fSwizzle(swizzle) {
208 this->registerChildProcessor(std::move(fp));
209 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400210
Brian Osmance425512017-03-22 14:37:50 -0400211 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
212 class GLFP : public GrGLSLFragmentProcessor {
213 public:
214 void emitCode(EmitArgs& args) override {
John Stiles0af87fe2020-06-05 13:33:09 -0400215 SkString childColor = this->invokeChild(0, args.fInputColor, args);
John Stileseed56f02020-06-04 13:30:51 -0400216
Brian Osmance425512017-03-22 14:37:50 -0400217 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
218 const GrSwizzle& swizzle = sfp.swizzle();
219 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
220
221 fragBuilder->codeAppendf("%s = %s.%s;",
John Stileseed56f02020-06-04 13:30:51 -0400222 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400223 }
224 };
225 return new GLFP;
226 }
227
228 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
229 b->add32(fSwizzle.asKey());
230 }
231
232 bool onIsEqual(const GrFragmentProcessor& other) const override {
233 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
234 return fSwizzle == sfp.fSwizzle;
235 }
236
Brian Osman1d5b5982018-10-01 13:41:39 -0400237 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400238 return fSwizzle.applyTo(input);
239 }
240
241 GrSwizzle fSwizzle;
242
243 typedef GrFragmentProcessor INHERITED;
244 };
245
246 if (!fp) {
247 return nullptr;
248 }
249 if (GrSwizzle::RGBA() == swizzle) {
250 return fp;
251 }
John Stileseed56f02020-06-04 13:30:51 -0400252 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400253}
254
Brian Salomonaff329b2017-08-11 09:40:37 -0400255std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
256 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700257 class PremulFragmentProcessor : public GrFragmentProcessor {
258 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400259 static std::unique_ptr<GrFragmentProcessor> Make(
260 std::unique_ptr<GrFragmentProcessor> processor) {
261 return std::unique_ptr<GrFragmentProcessor>(
262 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400263 }
264
265 const char* name() const override { return "Premultiply"; }
266
Brian Salomonaff329b2017-08-11 09:40:37 -0400267 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400268 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400269 }
270
Robert Phillips1c9686b2017-06-30 08:40:28 -0400271 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400272 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400273 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400274 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700275 }
276
egdaniel57d3b032015-11-13 11:57:27 -0800277 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800278 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700279 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700280 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800281 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500282 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500283 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800284 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500285 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800286 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700287 }
288 };
289 return new GLFP;
290 }
291
Brian Salomon94efbf52016-11-29 13:43:05 -0500292 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700293
294 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
295
Brian Salomon587e08f2017-01-27 10:59:27 -0500296 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
297 OptimizationFlags flags = kNone_OptimizationFlags;
298 if (inner->preservesOpaqueInput()) {
299 flags |= kPreservesOpaqueInput_OptimizationFlag;
300 }
301 if (inner->hasConstantOutputForConstantInput()) {
302 flags |= kConstantOutputForConstantInput_OptimizationFlag;
303 }
304 return flags;
305 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700306
Brian Osman1d5b5982018-10-01 13:41:39 -0400307 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
308 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400309 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400310 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
311 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500312 }
313
314 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700315 };
316 if (!fp) {
317 return nullptr;
318 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400319 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700320}
321
322//////////////////////////////////////////////////////////////////////////////
323
Brian Salomonaff329b2017-08-11 09:40:37 -0400324std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400325 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400326 if (!fp) {
327 return nullptr;
328 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400329 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700330}
bsalomone25eea42015-09-29 06:38:55 -0700331
Brian Salomonaff329b2017-08-11 09:40:37 -0400332std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500333 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700334 class SeriesFragmentProcessor : public GrFragmentProcessor {
335 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400336 static std::unique_ptr<GrFragmentProcessor> Make(
337 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
338 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700339 }
340
341 const char* name() const override { return "Series"; }
342
Brian Salomonaff329b2017-08-11 09:40:37 -0400343 std::unique_ptr<GrFragmentProcessor> clone() const override {
344 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400345 for (int i = 0; i < this->numChildProcessors(); ++i) {
346 if (!children.push_back(this->childProcessor(i).clone())) {
347 return nullptr;
348 }
349 }
350 return Make(children.begin(), this->numChildProcessors());
351 }
352
353 private:
egdaniel57d3b032015-11-13 11:57:27 -0800354 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800355 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700356 public:
bsalomone25eea42015-09-29 06:38:55 -0700357 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700358 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500359 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500360 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500361 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700362 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500363 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500364 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700365 }
366 };
367 return new GLFP;
368 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400369
Brian Salomonaff329b2017-08-11 09:40:37 -0400370 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400371 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400372 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400373 for (int i = 0; i < cnt; ++i) {
374 this->registerChildProcessor(std::move(children[i]));
375 }
376 }
377
Brian Salomonaff329b2017-08-11 09:40:37 -0400378 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500379 OptimizationFlags flags = kAll_OptimizationFlags;
380 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
381 flags &= children[i]->optimizationFlags();
382 }
383 return flags;
384 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500385 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700386
387 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
388
Brian Osman1d5b5982018-10-01 13:41:39 -0400389 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
390 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500391 int childCnt = this->numChildProcessors();
392 for (int i = 0; i < childCnt; ++i) {
393 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
394 }
395 return color;
396 }
397
398 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700399 };
400
401 if (!cnt) {
402 return nullptr;
403 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500404 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400405 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500406 }
bsalomone25eea42015-09-29 06:38:55 -0700407 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400408 GrProcessorAnalysisColor inputColor;
409 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500410 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400411 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400412 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500413 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
414 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400415 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500416 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500417 if (leadingFPsToEliminate == cnt) {
418 return colorFP;
419 }
420 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700421 replacementSeries.reserve(cnt);
422 replacementSeries.emplace_back(std::move(colorFP));
423 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500424 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700425 }
bungeman06ca8ec2016-06-09 08:01:03 -0700426 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700427 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400428 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700429}
bsalomona624bf32016-09-20 09:12:47 -0700430
431//////////////////////////////////////////////////////////////////////////////
432
Brian Salomon7eabfe82019-12-02 14:20:20 -0500433GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600434 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
435 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
436 }
437 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
438 fFPStack.push_back(paint.getColorFragmentProcessor(i));
439 }
440}
441
Brian Salomon7eabfe82019-12-02 14:20:20 -0500442GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500443 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
444 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700445 }
Brian Salomonc241b582019-11-27 08:57:17 -0500446 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
447 fFPStack.push_back(set.colorFragmentProcessor(i));
448 }
449}
450
Brian Salomon7eabfe82019-12-02 14:20:20 -0500451GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500452 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
453 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
454 }
455}
456
Brian Salomone782f842018-07-31 13:53:11 -0400457///////////////////////////////////////////////////////////////////////////////////////////////////
458
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000459GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500460 GrSamplerState samplerState)
461 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000462 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500463 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500464 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000465 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500466}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000467
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000468#if GR_TEST_UTILS
469void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500470 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000471 SkASSERT(view.proxy()->asTextureProxy());
472 fView = std::move(view);
473 fSamplerState = samplerState;
474
475 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500476 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000477 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
478}
479#endif