blob: 2e6b1a1acff20f2bbe9f424472d5be22cc772089 [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"
13#include "src/gpu/effects/generated/GrConstColorProcessor.h"
14#include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h"
15#include "src/gpu/effects/generated/GrPremulInputFragmentProcessor.h"
16#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 Salomone782f842018-07-31 13:53:11 -040051 GrFragmentProcessor::TextureAccessIter iter(this);
52 while (const TextureSampler* sampler = iter.next()) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060053 bool mipped = (GrSamplerState::Filter::kMipMap == sampler->samplerState().filter());
54 func(sampler->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) {
73 transform->setComputeInVertexShader(this->computeLocalCoordsInVertexShader());
bsalomonbf877302015-09-22 09:06:13 -070074 fCoordTransforms.push_back(transform);
Brian Salomon587e08f2017-01-27 10:59:27 -050075 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070076 SkDEBUGCODE(transform->setInProcessor();)
bsalomonbf877302015-09-22 09:06:13 -070077}
78
Robert Phillips82774f82019-06-20 14:38:27 -040079#ifdef SK_DEBUG
80bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -040081 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040082 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -040083 return false;
84 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040085 }
86
Robert Phillips9bee2e52017-05-29 12:37:20 -040087 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040088 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -040089 return false;
90 }
91 }
92
93 return true;
94}
Robert Phillips82774f82019-06-20 14:38:27 -040095#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -040096
Brian Salomonaff329b2017-08-11 09:40:37 -040097int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
bsalomonbf877302015-09-22 09:06:13 -070098 if (child->usesLocalCoords()) {
Brian Salomon587e08f2017-01-27 10:59:27 -050099 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700100 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700101 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700102
bungeman06ca8ec2016-06-09 08:01:03 -0700103 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400104 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700105
bsalomonbf877302015-09-22 09:06:13 -0700106 return index;
107}
108
bsalomonbf877302015-09-22 09:06:13 -0700109bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700110 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700111 return false;
112 }
bsalomona624bf32016-09-20 09:12:47 -0700113 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700114 for (int i = 0; i < count; ++i) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500115 if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700116 return false;
117 }
118 }
119 return true;
120}
121
Mike Reed28eaed22018-02-01 11:24:53 -0500122std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400123 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700124 if (!fp) {
125 return nullptr;
126 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400127 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700128}
129
Mike Reed28eaed22018-02-01 11:24:53 -0500130std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
131 std::unique_ptr<GrFragmentProcessor> fp) {
132 if (!fp) {
133 return nullptr;
134 }
135 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
136}
137
Brian Salomonaff329b2017-08-11 09:40:37 -0400138std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
139 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700140 if (!fp) {
141 return nullptr;
142 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500143 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400144 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700145 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
146}
147
Brian Salomonaff329b2017-08-11 09:40:37 -0400148std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
149 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400150 class SwizzleFragmentProcessor : public GrFragmentProcessor {
151 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400152 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
153 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400154 }
155
156 const char* name() const override { return "Swizzle"; }
157 const GrSwizzle& swizzle() const { return fSwizzle; }
158
Brian Salomonaff329b2017-08-11 09:40:37 -0400159 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400160
Brian Osmance425512017-03-22 14:37:50 -0400161 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400162 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400163 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400164 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400165
Brian Osmance425512017-03-22 14:37:50 -0400166 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
167 class GLFP : public GrGLSLFragmentProcessor {
168 public:
169 void emitCode(EmitArgs& args) override {
170 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
171 const GrSwizzle& swizzle = sfp.swizzle();
172 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
173
174 fragBuilder->codeAppendf("%s = %s.%s;",
175 args.fOutputColor, args.fInputColor, swizzle.c_str());
176 }
177 };
178 return new GLFP;
179 }
180
181 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
182 b->add32(fSwizzle.asKey());
183 }
184
185 bool onIsEqual(const GrFragmentProcessor& other) const override {
186 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
187 return fSwizzle == sfp.fSwizzle;
188 }
189
Brian Osman1d5b5982018-10-01 13:41:39 -0400190 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400191 return fSwizzle.applyTo(input);
192 }
193
194 GrSwizzle fSwizzle;
195
196 typedef GrFragmentProcessor INHERITED;
197 };
198
199 if (!fp) {
200 return nullptr;
201 }
202 if (GrSwizzle::RGBA() == swizzle) {
203 return fp;
204 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400205 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
206 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400207 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
208}
209
Brian Salomonaff329b2017-08-11 09:40:37 -0400210std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
211 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700212 class PremulFragmentProcessor : public GrFragmentProcessor {
213 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400214 static std::unique_ptr<GrFragmentProcessor> Make(
215 std::unique_ptr<GrFragmentProcessor> processor) {
216 return std::unique_ptr<GrFragmentProcessor>(
217 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400218 }
219
220 const char* name() const override { return "Premultiply"; }
221
Brian Salomonaff329b2017-08-11 09:40:37 -0400222 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400223 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400224 }
225
Robert Phillips1c9686b2017-06-30 08:40:28 -0400226 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400227 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400228 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400229 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700230 }
231
egdaniel57d3b032015-11-13 11:57:27 -0800232 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800233 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700234 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700235 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800236 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400237 this->invokeChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800238 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700239 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800240 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700241 }
242 };
243 return new GLFP;
244 }
245
Brian Salomon94efbf52016-11-29 13:43:05 -0500246 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700247
248 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
249
Brian Salomon587e08f2017-01-27 10:59:27 -0500250 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
251 OptimizationFlags flags = kNone_OptimizationFlags;
252 if (inner->preservesOpaqueInput()) {
253 flags |= kPreservesOpaqueInput_OptimizationFlag;
254 }
255 if (inner->hasConstantOutputForConstantInput()) {
256 flags |= kConstantOutputForConstantInput_OptimizationFlag;
257 }
258 return flags;
259 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700260
Brian Osman1d5b5982018-10-01 13:41:39 -0400261 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
262 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400263 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400264 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
265 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500266 }
267
268 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700269 };
270 if (!fp) {
271 return nullptr;
272 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400273 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700274}
275
276//////////////////////////////////////////////////////////////////////////////
277
Brian Salomonaff329b2017-08-11 09:40:37 -0400278std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400279 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400280 if (!fp) {
281 return nullptr;
282 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400283 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700284}
bsalomone25eea42015-09-29 06:38:55 -0700285
Brian Salomonaff329b2017-08-11 09:40:37 -0400286std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
287 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700288 class SeriesFragmentProcessor : public GrFragmentProcessor {
289 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400290 static std::unique_ptr<GrFragmentProcessor> Make(
291 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
292 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700293 }
294
295 const char* name() const override { return "Series"; }
296
Brian Salomonaff329b2017-08-11 09:40:37 -0400297 std::unique_ptr<GrFragmentProcessor> clone() const override {
298 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400299 for (int i = 0; i < this->numChildProcessors(); ++i) {
300 if (!children.push_back(this->childProcessor(i).clone())) {
301 return nullptr;
302 }
303 }
304 return Make(children.begin(), this->numChildProcessors());
305 }
306
307 private:
egdaniel57d3b032015-11-13 11:57:27 -0800308 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800309 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700310 public:
bsalomone25eea42015-09-29 06:38:55 -0700311 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700312 // First guy's input might be nil.
313 SkString temp("out0");
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400314 this->invokeChild(0, args.fInputColor, &temp, args);
dvonbeckca9eeab2016-07-06 12:00:06 -0700315 SkString input = temp;
316 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700317 temp.printf("out%d", i);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400318 this->invokeChild(i, input.c_str(), &temp, args);
bsalomone25eea42015-09-29 06:38:55 -0700319 input = temp;
320 }
321 // Last guy writes to our output variable.
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400322 this->invokeChild(this->numChildProcessors() - 1, input.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700323 }
324 };
325 return new GLFP;
326 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400327
Brian Salomonaff329b2017-08-11 09:40:37 -0400328 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400329 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400330 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400331 for (int i = 0; i < cnt; ++i) {
332 this->registerChildProcessor(std::move(children[i]));
333 }
334 }
335
Brian Salomonaff329b2017-08-11 09:40:37 -0400336 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500337 OptimizationFlags flags = kAll_OptimizationFlags;
338 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
339 flags &= children[i]->optimizationFlags();
340 }
341 return flags;
342 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500343 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700344
345 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
346
Brian Osman1d5b5982018-10-01 13:41:39 -0400347 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
348 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500349 int childCnt = this->numChildProcessors();
350 for (int i = 0; i < childCnt; ++i) {
351 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
352 }
353 return color;
354 }
355
356 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700357 };
358
359 if (!cnt) {
360 return nullptr;
361 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500362 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400363 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500364 }
bsalomone25eea42015-09-29 06:38:55 -0700365 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400366 GrProcessorAnalysisColor inputColor;
367 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400368 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400369 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400370 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400371 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500372 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
373 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400374 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500375 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500376 if (leadingFPsToEliminate == cnt) {
377 return colorFP;
378 }
379 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700380 replacementSeries.reserve(cnt);
381 replacementSeries.emplace_back(std::move(colorFP));
382 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500383 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700384 }
bungeman06ca8ec2016-06-09 08:01:03 -0700385 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700386 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400387 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700388}
bsalomona624bf32016-09-20 09:12:47 -0700389
390//////////////////////////////////////////////////////////////////////////////
391
392GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
393 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
394 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
395 }
396}
397
Chris Dalton1c548942018-05-22 13:09:48 -0600398GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
399 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
400 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
401 }
402 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
403 fFPStack.push_back(paint.getColorFragmentProcessor(i));
404 }
405}
406
bsalomona624bf32016-09-20 09:12:47 -0700407const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
408 if (fFPStack.empty()) {
409 return nullptr;
410 }
411 const GrFragmentProcessor* back = fFPStack.back();
412 fFPStack.pop_back();
413 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
414 fFPStack.push_back(&back->childProcessor(i));
415 }
416 return back;
417}
418
Brian Salomone782f842018-07-31 13:53:11 -0400419///////////////////////////////////////////////////////////////////////////////////////////////////
420
421GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
422 const GrSamplerState& samplerState) {
423 this->reset(std::move(proxy), samplerState);
424}
425
426GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
427 GrSamplerState::Filter filterMode,
428 GrSamplerState::WrapMode wrapXAndY) {
429 this->reset(std::move(proxy), filterMode, wrapXAndY);
430}
431
432void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
433 const GrSamplerState& samplerState) {
Robert Phillipsb5204762019-06-19 14:12:13 -0400434 fProxy = std::move(proxy);
Brian Salomone782f842018-07-31 13:53:11 -0400435 fSamplerState = samplerState;
436 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
437}
438
439void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
440 GrSamplerState::Filter filterMode,
441 GrSamplerState::WrapMode wrapXAndY) {
Robert Phillipsb5204762019-06-19 14:12:13 -0400442 fProxy = std::move(proxy);
Brian Salomone782f842018-07-31 13:53:11 -0400443 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
444 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
445}