blob: 49259481c21e1f82f5b60f78eac2f7e977b343e2 [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/GrFragmentProcessor.h"
9#include "src/gpu/GrPipeline.h"
10#include "src/gpu/GrProcessorAnalysis.h"
11#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050012#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/effects/generated/GrConstColorProcessor.h"
14#include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
18#include "src/gpu/glsl/GrGLSLUniformHandler.h"
bsalomonbf877302015-09-22 09:06:13 -070019
bsalomon7312ff82016-09-12 08:55:38 -070020bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
Brian Salomone782f842018-07-31 13:53:11 -040021 if (this->classID() != that.classID()) {
bsalomonbf877302015-09-22 09:06:13 -070022 return false;
23 }
Brian Osman9cf98dc2020-07-01 17:21:27 -040024 if (this->usesVaryingCoordsDirectly() != that.usesVaryingCoordsDirectly()) {
bsalomonbf877302015-09-22 09:06:13 -070025 return false;
26 }
27 if (!this->onIsEqual(that)) {
28 return false;
29 }
30 if (this->numChildProcessors() != that.numChildProcessors()) {
31 return false;
32 }
33 for (int i = 0; i < this->numChildProcessors(); ++i) {
bsalomon7312ff82016-09-12 08:55:38 -070034 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070035 return false;
36 }
37 }
38 return true;
39}
40
Brian Salomond90b3d32020-07-09 12:04:31 -040041void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) const {
42 this->visitTextureEffects([&func](const GrTextureEffect& te) {
43 bool mipped = (GrSamplerState::Filter::kMipMap == te.samplerState().filter());
44 func(te.view().proxy(), GrMipMapped(mipped));
45 });
46}
47
48void GrFragmentProcessor::visitTextureEffects(
49 const std::function<void(const GrTextureEffect&)>& func) const {
50 if (auto* te = this->asTextureEffect()) {
51 func(*te);
Brian Salomone782f842018-07-31 13:53:11 -040052 }
Brian Salomond90b3d32020-07-09 12:04:31 -040053 for (auto& child : fChildProcessors) {
54 child->visitTextureEffects(func);
55 }
56}
57
58GrTextureEffect* GrFragmentProcessor::asTextureEffect() {
59 if (this->classID() == kGrTextureEffect_ClassID) {
60 return static_cast<GrTextureEffect*>(this);
61 }
62 return nullptr;
63}
64
65const GrTextureEffect* GrFragmentProcessor::asTextureEffect() const {
66 if (this->classID() == kGrTextureEffect_ClassID) {
67 return static_cast<const GrTextureEffect*>(this);
68 }
69 return nullptr;
Brian Salomone782f842018-07-31 13:53:11 -040070}
71
egdaniel57d3b032015-11-13 11:57:27 -080072GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
73 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070074 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
75 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080076 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070077 }
78 return glFragProc;
79}
80
Michael Ludwige88320b2020-06-24 09:04:56 -040081void GrFragmentProcessor::addAndPushFlagToChildren(PrivateFlags flag) {
82 // This propagates down, so if we've already marked it, all our children should have it too
83 if (!(fFlags & flag)) {
84 fFlags |= flag;
85 for (auto& child : fChildProcessors) {
86 child->addAndPushFlagToChildren(flag);
87 }
Michael Ludwig9aba6252020-06-22 14:46:36 -040088 }
Michael Ludwige88320b2020-06-24 09:04:56 -040089#ifdef SK_DEBUG
90 for (auto& child : fChildProcessors) {
91 SkASSERT(child->fFlags & flag);
92 }
93#endif
Michael Ludwig9aba6252020-06-22 14:46:36 -040094}
95
Robert Phillips82774f82019-06-20 14:38:27 -040096#ifdef SK_DEBUG
97bool GrFragmentProcessor::isInstantiated() const {
Brian Salomond90b3d32020-07-09 12:04:31 -040098 bool result = true;
99 this->visitTextureEffects([&result](const GrTextureEffect& te) {
100 if (!te.texture()) {
101 result = false;
Brian Salomone782f842018-07-31 13:53:11 -0400102 }
Brian Salomond90b3d32020-07-09 12:04:31 -0400103 });
104 return result;
Robert Phillips9bee2e52017-05-29 12:37:20 -0400105}
Robert Phillips82774f82019-06-20 14:38:27 -0400106#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400107
Michael Ludwig9aba6252020-06-22 14:46:36 -0400108int GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child,
Brian Osman1298bc42020-06-30 13:39:35 -0400109 SkSL::SampleUsage sampleUsage) {
Brian Osman54867de2020-07-10 14:22:57 -0400110 if (!child) {
111 return -1;
112 }
113
Michael Ludwige88320b2020-06-24 09:04:56 -0400114 // The child should not have been attached to another FP already and not had any sampling
115 // strategy set on it.
Brian Osman54867de2020-07-10 14:22:57 -0400116 SkASSERT(!child->fParent && !child->sampleUsage().isSampled() &&
Michael Ludwige88320b2020-06-24 09:04:56 -0400117 !child->isSampledWithExplicitCoords() && !child->hasPerspectiveTransform());
118
Brian Osman1298bc42020-06-30 13:39:35 -0400119 // If a child is sampled directly (sample(child)), and with a single uniform matrix, we need to
120 // treat it as if it were sampled with multiple matrices (eg variable).
121 bool variableMatrix = sampleUsage.hasVariableMatrix() ||
122 (sampleUsage.fPassThrough && sampleUsage.hasUniformMatrix());
123
Michael Ludwig9aba6252020-06-22 14:46:36 -0400124 // Configure child's sampling state first
Brian Osman1298bc42020-06-30 13:39:35 -0400125 child->fUsage = sampleUsage;
126
127 // When an FP is sampled using variable matrix expressions, it is effectively being sampled
128 // explicitly, except that the call site will automatically evaluate the matrix expression to
129 // produce the float2 passed into this FP.
130 if (sampleUsage.fExplicitCoords || variableMatrix) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400131 child->addAndPushFlagToChildren(kSampledWithExplicitCoords_Flag);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400132 }
Brian Osman1298bc42020-06-30 13:39:35 -0400133
134 // Push perspective matrix type to children
135 if (sampleUsage.fHasPerspective) {
136 child->addAndPushFlagToChildren(kNetTransformHasPerspective_Flag);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400137 }
138
Brian Osman1298bc42020-06-30 13:39:35 -0400139 // If the child is sampled with a variable matrix expression, auto-generated code in
140 // invokeChildWithMatrix() for this FP will refer to the local coordinates.
141 if (variableMatrix) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400142 this->setUsesSampleCoordsDirectly();
143 }
144
145 // If the child is not sampled explicitly and not already accessing sample coords directly
146 // (through reference or variable matrix expansion), then mark that this FP tree relies on
147 // coordinates at a lower level. If the child is sampled with explicit coordinates and
148 // there isn't any other direct reference to the sample coords, we halt the upwards propagation
149 // because it means this FP is determining coordinates on its own.
150 if (!child->isSampledWithExplicitCoords()) {
151 if ((child->fFlags & kUsesSampleCoordsDirectly_Flag ||
152 child->fFlags & kUsesSampleCoordsIndirectly_Flag)) {
153 fFlags |= kUsesSampleCoordsIndirectly_Flag;
154 }
155 }
156
Chris Daltond7291ba2019-03-07 14:17:03 -0700157 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700158
bungeman06ca8ec2016-06-09 08:01:03 -0700159 int index = fChildProcessors.count();
Michael Ludwige88320b2020-06-24 09:04:56 -0400160 // Record that the child is attached to us; this FP is the source of any uniform data needed
161 // to evaluate the child sample matrix.
162 child->fParent = this;
Brian Salomonaff329b2017-08-11 09:40:37 -0400163 fChildProcessors.push_back(std::move(child));
Michael Ludwige88320b2020-06-24 09:04:56 -0400164
165 // Sanity check: our sample strategy comes from a parent we shouldn't have yet.
166 SkASSERT(!this->isSampledWithExplicitCoords() && !this->hasPerspectiveTransform() &&
Brian Osman1298bc42020-06-30 13:39:35 -0400167 !fUsage.isSampled() && !fParent);
bsalomonbf877302015-09-22 09:06:13 -0700168 return index;
169}
170
John Stiles3779f442020-06-15 10:48:49 -0400171int GrFragmentProcessor::cloneAndRegisterChildProcessor(const GrFragmentProcessor& fp) {
172 std::unique_ptr<GrFragmentProcessor> clone = fp.clone();
Brian Osman1298bc42020-06-30 13:39:35 -0400173 return this->registerChild(std::move(clone), fp.sampleUsage());
John Stiles3779f442020-06-15 10:48:49 -0400174}
175
John Stiles9ec6b052020-06-15 12:06:10 -0400176void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
177 for (int i = 0; i < src.numChildProcessors(); ++i) {
178 this->cloneAndRegisterChildProcessor(src.childProcessor(i));
179 }
180}
181
Mike Reed28eaed22018-02-01 11:24:53 -0500182std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400183 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700184 if (!fp) {
185 return nullptr;
186 }
John Stiles5c57e882020-06-30 14:05:03 -0400187 return GrXfermodeFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700188}
189
Mike Reed28eaed22018-02-01 11:24:53 -0500190std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
191 std::unique_ptr<GrFragmentProcessor> fp) {
192 if (!fp) {
193 return nullptr;
194 }
John Stiles5c57e882020-06-30 14:05:03 -0400195 return GrXfermodeFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn);
Mike Reed28eaed22018-02-01 11:24:53 -0500196}
197
Brian Osman6f5e9402020-01-22 10:39:31 -0500198std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
199 std::unique_ptr<GrFragmentProcessor> fp) {
200 if (!fp) {
201 return nullptr;
202 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400203 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500204}
205
Brian Salomonaff329b2017-08-11 09:40:37 -0400206std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
207 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400208 class SwizzleFragmentProcessor : public GrFragmentProcessor {
209 public:
John Stileseed56f02020-06-04 13:30:51 -0400210 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
211 const GrSwizzle& swizzle) {
212 return std::unique_ptr<GrFragmentProcessor>(
213 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400214 }
215
216 const char* name() const override { return "Swizzle"; }
217 const GrSwizzle& swizzle() const { return fSwizzle; }
218
John Stileseed56f02020-06-04 13:30:51 -0400219 std::unique_ptr<GrFragmentProcessor> clone() const override {
220 return Make(this->childProcessor(0).clone(), fSwizzle);
221 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400222
Brian Osmance425512017-03-22 14:37:50 -0400223 private:
John Stileseed56f02020-06-04 13:30:51 -0400224 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
225 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
226 , fSwizzle(swizzle) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400227 this->registerChild(std::move(fp));
John Stileseed56f02020-06-04 13:30:51 -0400228 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400229
Brian Osmance425512017-03-22 14:37:50 -0400230 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
231 class GLFP : public GrGLSLFragmentProcessor {
232 public:
233 void emitCode(EmitArgs& args) override {
John Stiles0af87fe2020-06-05 13:33:09 -0400234 SkString childColor = this->invokeChild(0, args.fInputColor, args);
John Stileseed56f02020-06-04 13:30:51 -0400235
Brian Osmance425512017-03-22 14:37:50 -0400236 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
237 const GrSwizzle& swizzle = sfp.swizzle();
238 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
239
240 fragBuilder->codeAppendf("%s = %s.%s;",
John Stileseed56f02020-06-04 13:30:51 -0400241 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400242 }
243 };
244 return new GLFP;
245 }
246
247 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
248 b->add32(fSwizzle.asKey());
249 }
250
251 bool onIsEqual(const GrFragmentProcessor& other) const override {
252 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
253 return fSwizzle == sfp.fSwizzle;
254 }
255
Brian Osman1d5b5982018-10-01 13:41:39 -0400256 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400257 return fSwizzle.applyTo(input);
258 }
259
260 GrSwizzle fSwizzle;
261
262 typedef GrFragmentProcessor INHERITED;
263 };
264
265 if (!fp) {
266 return nullptr;
267 }
268 if (GrSwizzle::RGBA() == swizzle) {
269 return fp;
270 }
John Stileseed56f02020-06-04 13:30:51 -0400271 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400272}
273
Brian Salomonaff329b2017-08-11 09:40:37 -0400274std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
275 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700276 class PremulFragmentProcessor : public GrFragmentProcessor {
277 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400278 static std::unique_ptr<GrFragmentProcessor> Make(
279 std::unique_ptr<GrFragmentProcessor> processor) {
280 return std::unique_ptr<GrFragmentProcessor>(
281 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400282 }
283
284 const char* name() const override { return "Premultiply"; }
285
Brian Salomonaff329b2017-08-11 09:40:37 -0400286 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400287 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400288 }
289
Robert Phillips1c9686b2017-06-30 08:40:28 -0400290 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400291 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400292 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400293 this->registerChild(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700294 }
295
egdaniel57d3b032015-11-13 11:57:27 -0800296 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800297 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700298 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700299 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800300 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500301 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500302 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800303 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500304 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800305 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700306 }
307 };
308 return new GLFP;
309 }
310
Brian Salomon94efbf52016-11-29 13:43:05 -0500311 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700312
313 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
314
Brian Salomon587e08f2017-01-27 10:59:27 -0500315 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
316 OptimizationFlags flags = kNone_OptimizationFlags;
317 if (inner->preservesOpaqueInput()) {
318 flags |= kPreservesOpaqueInput_OptimizationFlag;
319 }
320 if (inner->hasConstantOutputForConstantInput()) {
321 flags |= kConstantOutputForConstantInput_OptimizationFlag;
322 }
323 return flags;
324 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700325
Brian Osman1d5b5982018-10-01 13:41:39 -0400326 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
327 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400328 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400329 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
330 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500331 }
332
333 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700334 };
335 if (!fp) {
336 return nullptr;
337 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400338 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700339}
340
341//////////////////////////////////////////////////////////////////////////////
342
Brian Salomonaff329b2017-08-11 09:40:37 -0400343std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400344 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400345 if (!fp) {
346 return nullptr;
347 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400348 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700349}
bsalomone25eea42015-09-29 06:38:55 -0700350
Brian Salomonaff329b2017-08-11 09:40:37 -0400351std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500352 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700353 class SeriesFragmentProcessor : public GrFragmentProcessor {
354 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400355 static std::unique_ptr<GrFragmentProcessor> Make(
356 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
357 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700358 }
359
360 const char* name() const override { return "Series"; }
361
Brian Salomonaff329b2017-08-11 09:40:37 -0400362 std::unique_ptr<GrFragmentProcessor> clone() const override {
363 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400364 for (int i = 0; i < this->numChildProcessors(); ++i) {
365 if (!children.push_back(this->childProcessor(i).clone())) {
366 return nullptr;
367 }
368 }
369 return Make(children.begin(), this->numChildProcessors());
370 }
371
372 private:
egdaniel57d3b032015-11-13 11:57:27 -0800373 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800374 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700375 public:
bsalomone25eea42015-09-29 06:38:55 -0700376 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700377 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500378 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500379 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500380 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700381 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500382 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500383 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700384 }
385 };
386 return new GLFP;
387 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400388
Brian Salomonaff329b2017-08-11 09:40:37 -0400389 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400390 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400391 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400392 for (int i = 0; i < cnt; ++i) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400393 this->registerChild(std::move(children[i]));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400394 }
395 }
396
Brian Salomonaff329b2017-08-11 09:40:37 -0400397 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500398 OptimizationFlags flags = kAll_OptimizationFlags;
399 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
400 flags &= children[i]->optimizationFlags();
401 }
402 return flags;
403 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500404 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700405
406 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
407
Brian Osman1d5b5982018-10-01 13:41:39 -0400408 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
409 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500410 int childCnt = this->numChildProcessors();
411 for (int i = 0; i < childCnt; ++i) {
412 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
413 }
414 return color;
415 }
416
417 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700418 };
419
420 if (!cnt) {
421 return nullptr;
422 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500423 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400424 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500425 }
bsalomone25eea42015-09-29 06:38:55 -0700426 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400427 GrProcessorAnalysisColor inputColor;
428 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500429 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400430 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400431 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500432 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
433 if (leadingFPsToEliminate) {
John Stiles7c196772020-07-13 10:00:16 -0400434 std::unique_ptr<GrFragmentProcessor> colorFP = GrConstColorProcessor::Make(knownColor);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500435 if (leadingFPsToEliminate == cnt) {
436 return colorFP;
437 }
438 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700439 replacementSeries.reserve(cnt);
440 replacementSeries.emplace_back(std::move(colorFP));
441 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500442 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700443 }
bungeman06ca8ec2016-06-09 08:01:03 -0700444 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700445 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400446 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700447}
bsalomona624bf32016-09-20 09:12:47 -0700448
449//////////////////////////////////////////////////////////////////////////////
450
Brian Salomon7eabfe82019-12-02 14:20:20 -0500451GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600452 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
453 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
454 }
455 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
456 fFPStack.push_back(paint.getColorFragmentProcessor(i));
457 }
458}
459
Brian Salomon7eabfe82019-12-02 14:20:20 -0500460GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500461 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
462 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
463 }
464}
Brian Salomon87f4d292020-07-09 12:48:38 -0400465
466GrFragmentProcessor::CIter& GrFragmentProcessor::CIter::operator++() {
467 SkASSERT(!fFPStack.empty());
468 const GrFragmentProcessor* back = fFPStack.back();
469 fFPStack.pop_back();
470 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
471 fFPStack.push_back(&back->childProcessor(i));
472 }
473 return *this;
474}
475