blob: 57c4aecd4c2bf991087fbeb1eeaa3f99953d04e7 [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
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) {
Ethan Nicholas7ef777e2020-02-12 13:06:27 -050096 if (child->fFlags & kHasCoordTransforms_Flag) {
97 fFlags |= kHasCoordTransforms_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 Osman978693c2020-01-24 14:52:10 -0500247 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500248 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800249 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500250 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800251 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700252 }
253 };
254 return new GLFP;
255 }
256
Brian Salomon94efbf52016-11-29 13:43:05 -0500257 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700258
259 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
260
Brian Salomon587e08f2017-01-27 10:59:27 -0500261 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
262 OptimizationFlags flags = kNone_OptimizationFlags;
263 if (inner->preservesOpaqueInput()) {
264 flags |= kPreservesOpaqueInput_OptimizationFlag;
265 }
266 if (inner->hasConstantOutputForConstantInput()) {
267 flags |= kConstantOutputForConstantInput_OptimizationFlag;
268 }
269 return flags;
270 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700271
Brian Osman1d5b5982018-10-01 13:41:39 -0400272 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
273 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400274 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400275 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
276 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500277 }
278
279 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700280 };
281 if (!fp) {
282 return nullptr;
283 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400284 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700285}
286
287//////////////////////////////////////////////////////////////////////////////
288
Brian Salomonaff329b2017-08-11 09:40:37 -0400289std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400290 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400291 if (!fp) {
292 return nullptr;
293 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400294 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700295}
bsalomone25eea42015-09-29 06:38:55 -0700296
Brian Salomonaff329b2017-08-11 09:40:37 -0400297std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
298 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700299 class SeriesFragmentProcessor : public GrFragmentProcessor {
300 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400301 static std::unique_ptr<GrFragmentProcessor> Make(
302 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
303 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700304 }
305
306 const char* name() const override { return "Series"; }
307
Brian Salomonaff329b2017-08-11 09:40:37 -0400308 std::unique_ptr<GrFragmentProcessor> clone() const override {
309 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400310 for (int i = 0; i < this->numChildProcessors(); ++i) {
311 if (!children.push_back(this->childProcessor(i).clone())) {
312 return nullptr;
313 }
314 }
315 return Make(children.begin(), this->numChildProcessors());
316 }
317
318 private:
egdaniel57d3b032015-11-13 11:57:27 -0800319 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800320 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700321 public:
bsalomone25eea42015-09-29 06:38:55 -0700322 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700323 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500324 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500325 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500326 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700327 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500328 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500329 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700330 }
331 };
332 return new GLFP;
333 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400334
Brian Salomonaff329b2017-08-11 09:40:37 -0400335 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400336 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400337 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400338 for (int i = 0; i < cnt; ++i) {
339 this->registerChildProcessor(std::move(children[i]));
340 }
341 }
342
Brian Salomonaff329b2017-08-11 09:40:37 -0400343 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500344 OptimizationFlags flags = kAll_OptimizationFlags;
345 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
346 flags &= children[i]->optimizationFlags();
347 }
348 return flags;
349 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500350 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700351
352 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
353
Brian Osman1d5b5982018-10-01 13:41:39 -0400354 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
355 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500356 int childCnt = this->numChildProcessors();
357 for (int i = 0; i < childCnt; ++i) {
358 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
359 }
360 return color;
361 }
362
363 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700364 };
365
366 if (!cnt) {
367 return nullptr;
368 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500369 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400370 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500371 }
bsalomone25eea42015-09-29 06:38:55 -0700372 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400373 GrProcessorAnalysisColor inputColor;
374 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400375 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400376 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400377 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400378 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500379 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
380 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400381 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500382 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500383 if (leadingFPsToEliminate == cnt) {
384 return colorFP;
385 }
386 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700387 replacementSeries.reserve(cnt);
388 replacementSeries.emplace_back(std::move(colorFP));
389 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500390 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700391 }
bungeman06ca8ec2016-06-09 08:01:03 -0700392 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700393 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400394 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700395}
bsalomona624bf32016-09-20 09:12:47 -0700396
397//////////////////////////////////////////////////////////////////////////////
398
Brian Salomon7eabfe82019-12-02 14:20:20 -0500399GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600400 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
401 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
402 }
403 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
404 fFPStack.push_back(paint.getColorFragmentProcessor(i));
405 }
406}
407
Brian Salomon7eabfe82019-12-02 14:20:20 -0500408GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500409 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
410 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700411 }
Brian Salomonc241b582019-11-27 08:57:17 -0500412 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
413 fFPStack.push_back(set.colorFragmentProcessor(i));
414 }
415}
416
Brian Salomon7eabfe82019-12-02 14:20:20 -0500417GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500418 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
419 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
420 }
421}
422
Brian Salomone782f842018-07-31 13:53:11 -0400423///////////////////////////////////////////////////////////////////////////////////////////////////
424
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000425GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500426 GrSamplerState samplerState)
427 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000428 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500429 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500430 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000431 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500432}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000433
434GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrSurfaceProxy> proxy,
Brian Salomonccb61422020-01-09 10:46:36 -0500435 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000436 SkASSERT(proxy->asTextureProxy());
437 GrSurfaceOrigin origin = proxy->origin();
438 GrSwizzle swizzle = proxy->textureSwizzle();
439 fView = GrSurfaceProxyView(std::move(proxy), origin, swizzle);
440
441 fSamplerState = samplerState;
442 GrSurfaceProxy* surfProxy = this->proxy();
443 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500444 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000445 GrTextureProxy::HighestFilterMode(surfProxy->backendFormat().textureType())));
446}
447
448#if GR_TEST_UTILS
449void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500450 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000451 SkASSERT(view.proxy()->asTextureProxy());
452 fView = std::move(view);
453 fSamplerState = samplerState;
454
455 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500456 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000457 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
458}
459#endif