blob: a0bf114b377adea4755324d7749ebaf091718927 [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
bsalomonbf877302015-09-22 09:06:13 -070072void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070073 fCoordTransforms.push_back(transform);
Brian Salomon587e08f2017-01-27 10:59:27 -050074 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070075 SkDEBUGCODE(transform->setInProcessor();)
bsalomonbf877302015-09-22 09:06:13 -070076}
77
Robert Phillips9bee2e52017-05-29 12:37:20 -040078bool GrFragmentProcessor::instantiate(GrResourceProvider* resourceProvider) const {
Brian Salomone782f842018-07-31 13:53:11 -040079 for (int i = 0; i < fTextureSamplerCnt; ++i) {
80 if (!this->textureSampler(i).instantiate(resourceProvider)) {
81 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) {
86 if (!this->childProcessor(i).instantiate(resourceProvider)) {
87 return false;
88 }
89 }
90
91 return true;
92}
93
Brian Salomonaff329b2017-08-11 09:40:37 -040094void GrFragmentProcessor::markPendingExecution() const {
Brian Salomone782f842018-07-31 13:53:11 -040095 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Brian Salomonee783962018-08-01 09:55:10 -040096 auto* ref = this->textureSampler(i).proxyRef();
97 ref->markPendingIO();
98 ref->removeRef();
Brian Salomone782f842018-07-31 13:53:11 -040099 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400100 for (int i = 0; i < this->numChildProcessors(); ++i) {
101 this->childProcessor(i).markPendingExecution();
102 }
103}
104
105int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
bsalomonbf877302015-09-22 09:06:13 -0700106 if (child->usesLocalCoords()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500107 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700108 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700109 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700110
bungeman06ca8ec2016-06-09 08:01:03 -0700111 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400112 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700113
bsalomonbf877302015-09-22 09:06:13 -0700114 return index;
115}
116
bsalomonbf877302015-09-22 09:06:13 -0700117bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700118 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700119 return false;
120 }
bsalomona624bf32016-09-20 09:12:47 -0700121 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700122 for (int i = 0; i < count; ++i) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500123 if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700124 return false;
125 }
126 }
127 return true;
128}
129
Mike Reed28eaed22018-02-01 11:24:53 -0500130std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400131 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700132 if (!fp) {
133 return nullptr;
134 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400135 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700136}
137
Mike Reed28eaed22018-02-01 11:24:53 -0500138std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
139 std::unique_ptr<GrFragmentProcessor> fp) {
140 if (!fp) {
141 return nullptr;
142 }
143 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
144}
145
Brian Salomonaff329b2017-08-11 09:40:37 -0400146std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
147 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700148 if (!fp) {
149 return nullptr;
150 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500151 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400152 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700153 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
154}
155
Brian Salomonaff329b2017-08-11 09:40:37 -0400156std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
157 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400158 class SwizzleFragmentProcessor : public GrFragmentProcessor {
159 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400160 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
161 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400162 }
163
164 const char* name() const override { return "Swizzle"; }
165 const GrSwizzle& swizzle() const { return fSwizzle; }
166
Brian Salomonaff329b2017-08-11 09:40:37 -0400167 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400168
Brian Osmance425512017-03-22 14:37:50 -0400169 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400170 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400171 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400172 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400173
Brian Osmance425512017-03-22 14:37:50 -0400174 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
175 class GLFP : public GrGLSLFragmentProcessor {
176 public:
177 void emitCode(EmitArgs& args) override {
178 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
179 const GrSwizzle& swizzle = sfp.swizzle();
180 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
181
182 fragBuilder->codeAppendf("%s = %s.%s;",
183 args.fOutputColor, args.fInputColor, swizzle.c_str());
184 }
185 };
186 return new GLFP;
187 }
188
189 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
190 b->add32(fSwizzle.asKey());
191 }
192
193 bool onIsEqual(const GrFragmentProcessor& other) const override {
194 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
195 return fSwizzle == sfp.fSwizzle;
196 }
197
Brian Osman1d5b5982018-10-01 13:41:39 -0400198 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400199 return fSwizzle.applyTo(input);
200 }
201
202 GrSwizzle fSwizzle;
203
204 typedef GrFragmentProcessor INHERITED;
205 };
206
207 if (!fp) {
208 return nullptr;
209 }
210 if (GrSwizzle::RGBA() == swizzle) {
211 return fp;
212 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400213 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
214 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400215 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
216}
217
Brian Salomonaff329b2017-08-11 09:40:37 -0400218std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
219 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700220 class PremulFragmentProcessor : public GrFragmentProcessor {
221 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400222 static std::unique_ptr<GrFragmentProcessor> Make(
223 std::unique_ptr<GrFragmentProcessor> processor) {
224 return std::unique_ptr<GrFragmentProcessor>(
225 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400226 }
227
228 const char* name() const override { return "Premultiply"; }
229
Brian Salomonaff329b2017-08-11 09:40:37 -0400230 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400231 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400232 }
233
Robert Phillips1c9686b2017-06-30 08:40:28 -0400234 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400235 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400236 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400237 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700238 }
239
egdaniel57d3b032015-11-13 11:57:27 -0800240 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800241 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700242 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700243 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800244 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000245 this->emitChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800246 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700247 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800248 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700249 }
250 };
251 return new GLFP;
252 }
253
Brian Salomon94efbf52016-11-29 13:43:05 -0500254 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700255
256 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
257
Brian Salomon587e08f2017-01-27 10:59:27 -0500258 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
259 OptimizationFlags flags = kNone_OptimizationFlags;
260 if (inner->preservesOpaqueInput()) {
261 flags |= kPreservesOpaqueInput_OptimizationFlag;
262 }
263 if (inner->hasConstantOutputForConstantInput()) {
264 flags |= kConstantOutputForConstantInput_OptimizationFlag;
265 }
266 return flags;
267 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700268
Brian Osman1d5b5982018-10-01 13:41:39 -0400269 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
270 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400271 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400272 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
273 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500274 }
275
276 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700277 };
278 if (!fp) {
279 return nullptr;
280 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400281 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700282}
283
284//////////////////////////////////////////////////////////////////////////////
285
Brian Salomonaff329b2017-08-11 09:40:37 -0400286std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400287 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400288 if (!fp) {
289 return nullptr;
290 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400291 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700292}
bsalomone25eea42015-09-29 06:38:55 -0700293
Brian Salomonaff329b2017-08-11 09:40:37 -0400294std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
295 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700296 class SeriesFragmentProcessor : public GrFragmentProcessor {
297 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400298 static std::unique_ptr<GrFragmentProcessor> Make(
299 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
300 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700301 }
302
303 const char* name() const override { return "Series"; }
304
Brian Salomonaff329b2017-08-11 09:40:37 -0400305 std::unique_ptr<GrFragmentProcessor> clone() const override {
306 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400307 for (int i = 0; i < this->numChildProcessors(); ++i) {
308 if (!children.push_back(this->childProcessor(i).clone())) {
309 return nullptr;
310 }
311 }
312 return Make(children.begin(), this->numChildProcessors());
313 }
314
315 private:
egdaniel57d3b032015-11-13 11:57:27 -0800316 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800317 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700318 public:
bsalomone25eea42015-09-29 06:38:55 -0700319 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700320 // First guy's input might be nil.
321 SkString temp("out0");
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000322 this->emitChild(0, args.fInputColor, &temp, args);
dvonbeckca9eeab2016-07-06 12:00:06 -0700323 SkString input = temp;
324 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700325 temp.printf("out%d", i);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000326 this->emitChild(i, input.c_str(), &temp, args);
bsalomone25eea42015-09-29 06:38:55 -0700327 input = temp;
328 }
329 // Last guy writes to our output variable.
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000330 this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700331 }
332 };
333 return new GLFP;
334 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400335
Brian Salomonaff329b2017-08-11 09:40:37 -0400336 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400337 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400338 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400339 for (int i = 0; i < cnt; ++i) {
340 this->registerChildProcessor(std::move(children[i]));
341 }
342 }
343
Brian Salomonaff329b2017-08-11 09:40:37 -0400344 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500345 OptimizationFlags flags = kAll_OptimizationFlags;
346 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
347 flags &= children[i]->optimizationFlags();
348 }
349 return flags;
350 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500351 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700352
353 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
354
Brian Osman1d5b5982018-10-01 13:41:39 -0400355 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
356 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500357 int childCnt = this->numChildProcessors();
358 for (int i = 0; i < childCnt; ++i) {
359 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
360 }
361 return color;
362 }
363
364 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700365 };
366
367 if (!cnt) {
368 return nullptr;
369 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500370 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400371 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500372 }
bsalomone25eea42015-09-29 06:38:55 -0700373 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400374 GrProcessorAnalysisColor inputColor;
375 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400376 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400377 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400378 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400379 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500380 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
381 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400382 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500383 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500384 if (leadingFPsToEliminate == cnt) {
385 return colorFP;
386 }
387 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700388 replacementSeries.reserve(cnt);
389 replacementSeries.emplace_back(std::move(colorFP));
390 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500391 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700392 }
bungeman06ca8ec2016-06-09 08:01:03 -0700393 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700394 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400395 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700396}
bsalomona624bf32016-09-20 09:12:47 -0700397
398//////////////////////////////////////////////////////////////////////////////
399
400GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
401 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
402 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
403 }
404}
405
Chris Dalton1c548942018-05-22 13:09:48 -0600406GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
407 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
408 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
409 }
410 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
411 fFPStack.push_back(paint.getColorFragmentProcessor(i));
412 }
413}
414
bsalomona624bf32016-09-20 09:12:47 -0700415const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
416 if (fFPStack.empty()) {
417 return nullptr;
418 }
419 const GrFragmentProcessor* back = fFPStack.back();
420 fFPStack.pop_back();
421 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
422 fFPStack.push_back(&back->childProcessor(i));
423 }
424 return back;
425}
426
Brian Salomone782f842018-07-31 13:53:11 -0400427///////////////////////////////////////////////////////////////////////////////////////////////////
428
429GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
430 const GrSamplerState& samplerState) {
431 this->reset(std::move(proxy), samplerState);
432}
433
434GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
435 GrSamplerState::Filter filterMode,
436 GrSamplerState::WrapMode wrapXAndY) {
437 this->reset(std::move(proxy), filterMode, wrapXAndY);
438}
439
440void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
441 const GrSamplerState& samplerState) {
442 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
443 fSamplerState = samplerState;
444 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
445}
446
447void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
448 GrSamplerState::Filter filterMode,
449 GrSamplerState::WrapMode wrapXAndY) {
450 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
451 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
452 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
453}