blob: 7205f0813b250384f299042ed36e5c0752760e90 [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
Brian Salomone782f842018-07-31 13:53:11 -040050void GrFragmentProcessor::visitProxies(const std::function<void(GrSurfaceProxy*)>& func) {
51 GrFragmentProcessor::TextureAccessIter iter(this);
52 while (const TextureSampler* sampler = iter.next()) {
53 func(sampler->proxy());
54 }
55}
56
egdaniel57d3b032015-11-13 11:57:27 -080057GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
58 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070059 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
60 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080061 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070062 }
63 return glFragProc;
64}
65
Brian Salomone782f842018-07-31 13:53:11 -040066const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
67 SkASSERT(i >= 0 && i < fTextureSamplerCnt);
68 return this->onTextureSampler(i);
69}
70
bsalomonbf877302015-09-22 09:06:13 -070071void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070072 fCoordTransforms.push_back(transform);
Brian Salomon587e08f2017-01-27 10:59:27 -050073 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070074 SkDEBUGCODE(transform->setInProcessor();)
bsalomonbf877302015-09-22 09:06:13 -070075}
76
Robert Phillips9bee2e52017-05-29 12:37:20 -040077bool GrFragmentProcessor::instantiate(GrResourceProvider* resourceProvider) const {
Brian Salomone782f842018-07-31 13:53:11 -040078 for (int i = 0; i < fTextureSamplerCnt; ++i) {
79 if (!this->textureSampler(i).instantiate(resourceProvider)) {
80 return false;
81 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040082 }
83
Robert Phillips9bee2e52017-05-29 12:37:20 -040084 for (int i = 0; i < this->numChildProcessors(); ++i) {
85 if (!this->childProcessor(i).instantiate(resourceProvider)) {
86 return false;
87 }
88 }
89
90 return true;
91}
92
Brian Salomonaff329b2017-08-11 09:40:37 -040093void GrFragmentProcessor::markPendingExecution() const {
Brian Salomone782f842018-07-31 13:53:11 -040094 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Brian Salomonee783962018-08-01 09:55:10 -040095 auto* ref = this->textureSampler(i).proxyRef();
96 ref->markPendingIO();
97 ref->removeRef();
Brian Salomone782f842018-07-31 13:53:11 -040098 }
Brian Salomonaff329b2017-08-11 09:40:37 -040099 for (int i = 0; i < this->numChildProcessors(); ++i) {
100 this->childProcessor(i).markPendingExecution();
101 }
102}
103
104int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
bsalomonbf877302015-09-22 09:06:13 -0700105 if (child->usesLocalCoords()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500106 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700107 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700108 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700109
bungeman06ca8ec2016-06-09 08:01:03 -0700110 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400111 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700112
bsalomonbf877302015-09-22 09:06:13 -0700113 return index;
114}
115
bsalomonbf877302015-09-22 09:06:13 -0700116bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700117 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700118 return false;
119 }
bsalomona624bf32016-09-20 09:12:47 -0700120 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700121 for (int i = 0; i < count; ++i) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500122 if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700123 return false;
124 }
125 }
126 return true;
127}
128
Mike Reed28eaed22018-02-01 11:24:53 -0500129std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400130 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700131 if (!fp) {
132 return nullptr;
133 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400134 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700135}
136
Mike Reed28eaed22018-02-01 11:24:53 -0500137std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
138 std::unique_ptr<GrFragmentProcessor> fp) {
139 if (!fp) {
140 return nullptr;
141 }
142 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
143}
144
Brian Salomonaff329b2017-08-11 09:40:37 -0400145std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
146 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700147 if (!fp) {
148 return nullptr;
149 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500150 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400151 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700152 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
153}
154
Brian Salomonaff329b2017-08-11 09:40:37 -0400155std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
156 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400157 class SwizzleFragmentProcessor : public GrFragmentProcessor {
158 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400159 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
160 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400161 }
162
163 const char* name() const override { return "Swizzle"; }
164 const GrSwizzle& swizzle() const { return fSwizzle; }
165
Brian Salomonaff329b2017-08-11 09:40:37 -0400166 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400167
Brian Osmance425512017-03-22 14:37:50 -0400168 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400169 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400170 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400171 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400172
Brian Osmance425512017-03-22 14:37:50 -0400173 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
174 class GLFP : public GrGLSLFragmentProcessor {
175 public:
176 void emitCode(EmitArgs& args) override {
177 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
178 const GrSwizzle& swizzle = sfp.swizzle();
179 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
180
181 fragBuilder->codeAppendf("%s = %s.%s;",
182 args.fOutputColor, args.fInputColor, swizzle.c_str());
183 }
184 };
185 return new GLFP;
186 }
187
188 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
189 b->add32(fSwizzle.asKey());
190 }
191
192 bool onIsEqual(const GrFragmentProcessor& other) const override {
193 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
194 return fSwizzle == sfp.fSwizzle;
195 }
196
Brian Osman1d5b5982018-10-01 13:41:39 -0400197 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400198 return fSwizzle.applyTo(input);
199 }
200
201 GrSwizzle fSwizzle;
202
203 typedef GrFragmentProcessor INHERITED;
204 };
205
206 if (!fp) {
207 return nullptr;
208 }
209 if (GrSwizzle::RGBA() == swizzle) {
210 return fp;
211 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400212 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
213 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400214 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
215}
216
Brian Salomonaff329b2017-08-11 09:40:37 -0400217std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
218 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700219 class PremulFragmentProcessor : public GrFragmentProcessor {
220 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400221 static std::unique_ptr<GrFragmentProcessor> Make(
222 std::unique_ptr<GrFragmentProcessor> processor) {
223 return std::unique_ptr<GrFragmentProcessor>(
224 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400225 }
226
227 const char* name() const override { return "Premultiply"; }
228
Brian Salomonaff329b2017-08-11 09:40:37 -0400229 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400230 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400231 }
232
Robert Phillips1c9686b2017-06-30 08:40:28 -0400233 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400234 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400235 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400236 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700237 }
238
egdaniel57d3b032015-11-13 11:57:27 -0800239 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800240 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700241 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700242 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800243 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000244 this->emitChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800245 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700246 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800247 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700248 }
249 };
250 return new GLFP;
251 }
252
Brian Salomon94efbf52016-11-29 13:43:05 -0500253 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700254
255 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
256
Brian Salomon587e08f2017-01-27 10:59:27 -0500257 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
258 OptimizationFlags flags = kNone_OptimizationFlags;
259 if (inner->preservesOpaqueInput()) {
260 flags |= kPreservesOpaqueInput_OptimizationFlag;
261 }
262 if (inner->hasConstantOutputForConstantInput()) {
263 flags |= kConstantOutputForConstantInput_OptimizationFlag;
264 }
265 return flags;
266 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700267
Brian Osman1d5b5982018-10-01 13:41:39 -0400268 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
269 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400270 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400271 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
272 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500273 }
274
275 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700276 };
277 if (!fp) {
278 return nullptr;
279 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400280 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700281}
282
283//////////////////////////////////////////////////////////////////////////////
284
Brian Salomonaff329b2017-08-11 09:40:37 -0400285std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400286 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400287 if (!fp) {
288 return nullptr;
289 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400290 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700291}
bsalomone25eea42015-09-29 06:38:55 -0700292
Brian Salomonaff329b2017-08-11 09:40:37 -0400293std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
294 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700295 class SeriesFragmentProcessor : public GrFragmentProcessor {
296 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400297 static std::unique_ptr<GrFragmentProcessor> Make(
298 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
299 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700300 }
301
302 const char* name() const override { return "Series"; }
303
Brian Salomonaff329b2017-08-11 09:40:37 -0400304 std::unique_ptr<GrFragmentProcessor> clone() const override {
305 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400306 for (int i = 0; i < this->numChildProcessors(); ++i) {
307 if (!children.push_back(this->childProcessor(i).clone())) {
308 return nullptr;
309 }
310 }
311 return Make(children.begin(), this->numChildProcessors());
312 }
313
314 private:
egdaniel57d3b032015-11-13 11:57:27 -0800315 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800316 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700317 public:
bsalomone25eea42015-09-29 06:38:55 -0700318 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700319 // First guy's input might be nil.
320 SkString temp("out0");
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000321 this->emitChild(0, args.fInputColor, &temp, args);
dvonbeckca9eeab2016-07-06 12:00:06 -0700322 SkString input = temp;
323 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700324 temp.printf("out%d", i);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000325 this->emitChild(i, input.c_str(), &temp, args);
bsalomone25eea42015-09-29 06:38:55 -0700326 input = temp;
327 }
328 // Last guy writes to our output variable.
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000329 this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
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
399GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
400 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
401 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
402 }
403}
404
Chris Dalton1c548942018-05-22 13:09:48 -0600405GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
406 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
407 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
408 }
409 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
410 fFPStack.push_back(paint.getColorFragmentProcessor(i));
411 }
412}
413
bsalomona624bf32016-09-20 09:12:47 -0700414const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
415 if (fFPStack.empty()) {
416 return nullptr;
417 }
418 const GrFragmentProcessor* back = fFPStack.back();
419 fFPStack.pop_back();
420 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
421 fFPStack.push_back(&back->childProcessor(i));
422 }
423 return back;
424}
425
Brian Salomone782f842018-07-31 13:53:11 -0400426///////////////////////////////////////////////////////////////////////////////////////////////////
427
428GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
429 const GrSamplerState& samplerState) {
430 this->reset(std::move(proxy), samplerState);
431}
432
433GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
434 GrSamplerState::Filter filterMode,
435 GrSamplerState::WrapMode wrapXAndY) {
436 this->reset(std::move(proxy), filterMode, wrapXAndY);
437}
438
439void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
440 const GrSamplerState& samplerState) {
441 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
442 fSamplerState = samplerState;
443 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
444}
445
446void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
447 GrSamplerState::Filter filterMode,
448 GrSamplerState::WrapMode wrapXAndY) {
449 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
450 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
451 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
452}