blob: 14761d197afa9dd320ce321e8f3213f9fa056a7e [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);
Brian Salomon7d8b3972019-11-26 22:34:44 -050074 fFlags |= kHasCoordTranforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -070075}
76
Robert Phillips82774f82019-06-20 14:38:27 -040077#ifdef SK_DEBUG
78bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -040079 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040080 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -040081 return false;
82 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040083 }
84
Robert Phillips9bee2e52017-05-29 12:37:20 -040085 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040086 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -040087 return false;
88 }
89 }
90
91 return true;
92}
Robert Phillips82774f82019-06-20 14:38:27 -040093#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -040094
Brian Salomonaff329b2017-08-11 09:40:37 -040095int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
Brian Salomon7d8b3972019-11-26 22:34:44 -050096 if (child->fFlags & kHasCoordTranforms_Flag) {
97 fFlags |= kHasCoordTranforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -070098 }
Chris Daltond7291ba2019-03-07 14:17:03 -070099 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700100
bungeman06ca8ec2016-06-09 08:01:03 -0700101 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400102 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700103
bsalomonbf877302015-09-22 09:06:13 -0700104 return index;
105}
106
bsalomonbf877302015-09-22 09:06:13 -0700107bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700108 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700109 return false;
110 }
bsalomona624bf32016-09-20 09:12:47 -0700111 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700112 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500113 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700114 return false;
115 }
116 }
117 return true;
118}
119
Mike Reed28eaed22018-02-01 11:24:53 -0500120std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400121 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700122 if (!fp) {
123 return nullptr;
124 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400125 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700126}
127
Mike Reed28eaed22018-02-01 11:24:53 -0500128std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
129 std::unique_ptr<GrFragmentProcessor> fp) {
130 if (!fp) {
131 return nullptr;
132 }
133 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
134}
135
Brian Salomonaff329b2017-08-11 09:40:37 -0400136std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
137 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700138 if (!fp) {
139 return nullptr;
140 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500141 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400142 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700143 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
144}
145
Brian Osman6f5e9402020-01-22 10:39:31 -0500146std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
147 std::unique_ptr<GrFragmentProcessor> fp) {
148 if (!fp) {
149 return nullptr;
150 }
151 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {
152 std::move(fp),
153 GrClampFragmentProcessor::Make(true)
154 };
155 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
156}
157
Brian Salomonaff329b2017-08-11 09:40:37 -0400158std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
159 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400160 class SwizzleFragmentProcessor : public GrFragmentProcessor {
161 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400162 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
163 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400164 }
165
166 const char* name() const override { return "Swizzle"; }
167 const GrSwizzle& swizzle() const { return fSwizzle; }
168
Brian Salomonaff329b2017-08-11 09:40:37 -0400169 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400170
Brian Osmance425512017-03-22 14:37:50 -0400171 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400172 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400173 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400174 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400175
Brian Osmance425512017-03-22 14:37:50 -0400176 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
177 class GLFP : public GrGLSLFragmentProcessor {
178 public:
179 void emitCode(EmitArgs& args) override {
180 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
181 const GrSwizzle& swizzle = sfp.swizzle();
182 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
183
184 fragBuilder->codeAppendf("%s = %s.%s;",
Greg Daniel369ee6b2019-12-02 15:30:02 -0500185 args.fOutputColor, args.fInputColor, swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400186 }
187 };
188 return new GLFP;
189 }
190
191 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
192 b->add32(fSwizzle.asKey());
193 }
194
195 bool onIsEqual(const GrFragmentProcessor& other) const override {
196 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
197 return fSwizzle == sfp.fSwizzle;
198 }
199
Brian Osman1d5b5982018-10-01 13:41:39 -0400200 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400201 return fSwizzle.applyTo(input);
202 }
203
204 GrSwizzle fSwizzle;
205
206 typedef GrFragmentProcessor INHERITED;
207 };
208
209 if (!fp) {
210 return nullptr;
211 }
212 if (GrSwizzle::RGBA() == swizzle) {
213 return fp;
214 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400215 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
216 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400217 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
218}
219
Brian Salomonaff329b2017-08-11 09:40:37 -0400220std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
221 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700222 class PremulFragmentProcessor : public GrFragmentProcessor {
223 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400224 static std::unique_ptr<GrFragmentProcessor> Make(
225 std::unique_ptr<GrFragmentProcessor> processor) {
226 return std::unique_ptr<GrFragmentProcessor>(
227 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400228 }
229
230 const char* name() const override { return "Premultiply"; }
231
Brian Salomonaff329b2017-08-11 09:40:37 -0400232 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400233 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400234 }
235
Robert Phillips1c9686b2017-06-30 08:40:28 -0400236 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400237 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400238 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400239 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700240 }
241
egdaniel57d3b032015-11-13 11:57:27 -0800242 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800243 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700244 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700245 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800246 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osmancddfc5e2020-01-24 10:24:27 -0500247 SkString temp("inColor");
248 this->invokeChild(0, &temp, args);
249 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800250 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500251 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800252 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700253 }
254 };
255 return new GLFP;
256 }
257
Brian Salomon94efbf52016-11-29 13:43:05 -0500258 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700259
260 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
261
Brian Salomon587e08f2017-01-27 10:59:27 -0500262 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
263 OptimizationFlags flags = kNone_OptimizationFlags;
264 if (inner->preservesOpaqueInput()) {
265 flags |= kPreservesOpaqueInput_OptimizationFlag;
266 }
267 if (inner->hasConstantOutputForConstantInput()) {
268 flags |= kConstantOutputForConstantInput_OptimizationFlag;
269 }
270 return flags;
271 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700272
Brian Osman1d5b5982018-10-01 13:41:39 -0400273 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
274 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400275 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400276 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
277 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500278 }
279
280 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700281 };
282 if (!fp) {
283 return nullptr;
284 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400285 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700286}
287
288//////////////////////////////////////////////////////////////////////////////
289
Brian Salomonaff329b2017-08-11 09:40:37 -0400290std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400291 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400292 if (!fp) {
293 return nullptr;
294 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400295 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700296}
bsalomone25eea42015-09-29 06:38:55 -0700297
Brian Salomonaff329b2017-08-11 09:40:37 -0400298std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
299 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700300 class SeriesFragmentProcessor : public GrFragmentProcessor {
301 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400302 static std::unique_ptr<GrFragmentProcessor> Make(
303 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
304 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700305 }
306
307 const char* name() const override { return "Series"; }
308
Brian Salomonaff329b2017-08-11 09:40:37 -0400309 std::unique_ptr<GrFragmentProcessor> clone() const override {
310 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400311 for (int i = 0; i < this->numChildProcessors(); ++i) {
312 if (!children.push_back(this->childProcessor(i).clone())) {
313 return nullptr;
314 }
315 }
316 return Make(children.begin(), this->numChildProcessors());
317 }
318
319 private:
egdaniel57d3b032015-11-13 11:57:27 -0800320 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800321 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700322 public:
bsalomone25eea42015-09-29 06:38:55 -0700323 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700324 // First guy's input might be nil.
325 SkString temp("out0");
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400326 this->invokeChild(0, args.fInputColor, &temp, args);
dvonbeckca9eeab2016-07-06 12:00:06 -0700327 SkString input = temp;
Brian Osmancddfc5e2020-01-24 10:24:27 -0500328 for (int i = 1; i < this->numChildProcessors(); ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700329 temp.printf("out%d", i);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400330 this->invokeChild(i, input.c_str(), &temp, args);
bsalomone25eea42015-09-29 06:38:55 -0700331 input = temp;
332 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500333 // Copy last output to our output variable
334 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, input.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700335 }
336 };
337 return new GLFP;
338 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400339
Brian Salomonaff329b2017-08-11 09:40:37 -0400340 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400341 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400342 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400343 for (int i = 0; i < cnt; ++i) {
344 this->registerChildProcessor(std::move(children[i]));
345 }
346 }
347
Brian Salomonaff329b2017-08-11 09:40:37 -0400348 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500349 OptimizationFlags flags = kAll_OptimizationFlags;
350 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
351 flags &= children[i]->optimizationFlags();
352 }
353 return flags;
354 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500355 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700356
357 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
358
Brian Osman1d5b5982018-10-01 13:41:39 -0400359 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
360 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500361 int childCnt = this->numChildProcessors();
362 for (int i = 0; i < childCnt; ++i) {
363 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
364 }
365 return color;
366 }
367
368 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700369 };
370
371 if (!cnt) {
372 return nullptr;
373 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500374 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400375 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500376 }
bsalomone25eea42015-09-29 06:38:55 -0700377 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400378 GrProcessorAnalysisColor inputColor;
379 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400381 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400382 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400383 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500384 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
385 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400386 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500387 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500388 if (leadingFPsToEliminate == cnt) {
389 return colorFP;
390 }
391 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700392 replacementSeries.reserve(cnt);
393 replacementSeries.emplace_back(std::move(colorFP));
394 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500395 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700396 }
bungeman06ca8ec2016-06-09 08:01:03 -0700397 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700398 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400399 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700400}
bsalomona624bf32016-09-20 09:12:47 -0700401
402//////////////////////////////////////////////////////////////////////////////
403
Brian Salomon7eabfe82019-12-02 14:20:20 -0500404GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600405 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
406 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
407 }
408 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
409 fFPStack.push_back(paint.getColorFragmentProcessor(i));
410 }
411}
412
Brian Salomon7eabfe82019-12-02 14:20:20 -0500413GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500414 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
415 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700416 }
Brian Salomonc241b582019-11-27 08:57:17 -0500417 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
418 fFPStack.push_back(set.colorFragmentProcessor(i));
419 }
420}
421
Brian Salomon7eabfe82019-12-02 14:20:20 -0500422GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500423 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
424 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
425 }
426}
427
Brian Salomone782f842018-07-31 13:53:11 -0400428///////////////////////////////////////////////////////////////////////////////////////////////////
429
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000430GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500431 GrSamplerState samplerState)
432 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000433 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500434 fSamplerState.setFilterMode(
435 SkTMin(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000436 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500437}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000438
439GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrSurfaceProxy> proxy,
Brian Salomonccb61422020-01-09 10:46:36 -0500440 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000441 SkASSERT(proxy->asTextureProxy());
442 GrSurfaceOrigin origin = proxy->origin();
443 GrSwizzle swizzle = proxy->textureSwizzle();
444 fView = GrSurfaceProxyView(std::move(proxy), origin, swizzle);
445
446 fSamplerState = samplerState;
447 GrSurfaceProxy* surfProxy = this->proxy();
448 fSamplerState.setFilterMode(
449 SkTMin(samplerState.filter(),
450 GrTextureProxy::HighestFilterMode(surfProxy->backendFormat().textureType())));
451}
452
453#if GR_TEST_UTILS
454void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500455 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000456 SkASSERT(view.proxy()->asTextureProxy());
457 fView = std::move(view);
458 fSamplerState = samplerState;
459
460 fSamplerState.setFilterMode(
461 SkTMin(samplerState.filter(),
462 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
463}
464#endif