blob: dc1633d361bd0e82397b1fd0110e777808608ed1 [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"
John Stilesf743d4e2020-07-23 11:35:08 -040011#include "src/gpu/effects/GrBlendFragmentProcessor.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) {
Brian Osman12c5d292020-07-13 16:11:35 -040034 auto thisChild = this->childProcessor(i),
35 thatChild = that .childProcessor(i);
36 if (SkToBool(thisChild) != SkToBool(thatChild)) {
37 return false;
38 }
39 if (thisChild && !thisChild->isEqual(*thatChild)) {
bsalomonbf877302015-09-22 09:06:13 -070040 return false;
41 }
42 }
43 return true;
44}
45
Brian Salomond90b3d32020-07-09 12:04:31 -040046void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) const {
47 this->visitTextureEffects([&func](const GrTextureEffect& te) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040048 func(te.view().proxy(), te.samplerState().mipmapped());
Brian Salomond90b3d32020-07-09 12:04:31 -040049 });
50}
51
52void GrFragmentProcessor::visitTextureEffects(
53 const std::function<void(const GrTextureEffect&)>& func) const {
54 if (auto* te = this->asTextureEffect()) {
55 func(*te);
Brian Salomone782f842018-07-31 13:53:11 -040056 }
Brian Salomond90b3d32020-07-09 12:04:31 -040057 for (auto& child : fChildProcessors) {
Brian Osman12c5d292020-07-13 16:11:35 -040058 if (child) {
59 child->visitTextureEffects(func);
60 }
Brian Salomond90b3d32020-07-09 12:04:31 -040061 }
62}
63
64GrTextureEffect* GrFragmentProcessor::asTextureEffect() {
65 if (this->classID() == kGrTextureEffect_ClassID) {
66 return static_cast<GrTextureEffect*>(this);
67 }
68 return nullptr;
69}
70
71const GrTextureEffect* GrFragmentProcessor::asTextureEffect() const {
72 if (this->classID() == kGrTextureEffect_ClassID) {
73 return static_cast<const GrTextureEffect*>(this);
74 }
75 return nullptr;
Brian Salomone782f842018-07-31 13:53:11 -040076}
77
John Stiles8d9bf642020-08-12 15:07:45 -040078#if GR_TEST_UTILS
John Stilesba1879d2020-08-11 13:58:32 -040079static void recursive_dump_tree_info(const GrFragmentProcessor& fp,
80 SkString indent,
81 SkString* text) {
82 for (int index = 0; index < fp.numChildProcessors(); ++index) {
83 text->appendf("\n%s(#%d) -> ", indent.c_str(), index);
84 if (const GrFragmentProcessor* childFP = fp.childProcessor(index)) {
85 text->append(childFP->dumpInfo());
86 indent.append("\t");
87 recursive_dump_tree_info(*childFP, indent, text);
88 } else {
89 text->append("null");
90 }
91 }
92}
93
94SkString GrFragmentProcessor::dumpTreeInfo() const {
95 SkString text = this->dumpInfo();
96 recursive_dump_tree_info(*this, SkString("\t"), &text);
97 text.append("\n");
98 return text;
99}
100#endif
101
egdaniel57d3b032015-11-13 11:57:27 -0800102GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
103 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -0700104 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
105 for (int i = 0; i < fChildProcessors.count(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400106 glFragProc->fChildProcessors[i] =
107 fChildProcessors[i] ? fChildProcessors[i]->createGLSLInstance() : nullptr;
bsalomonbf877302015-09-22 09:06:13 -0700108 }
109 return glFragProc;
110}
111
Michael Ludwige88320b2020-06-24 09:04:56 -0400112void GrFragmentProcessor::addAndPushFlagToChildren(PrivateFlags flag) {
113 // This propagates down, so if we've already marked it, all our children should have it too
114 if (!(fFlags & flag)) {
115 fFlags |= flag;
116 for (auto& child : fChildProcessors) {
Brian Osman12c5d292020-07-13 16:11:35 -0400117 if (child) {
118 child->addAndPushFlagToChildren(flag);
119 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400120 }
Michael Ludwig9aba6252020-06-22 14:46:36 -0400121 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400122#ifdef SK_DEBUG
123 for (auto& child : fChildProcessors) {
Brian Osman12c5d292020-07-13 16:11:35 -0400124 SkASSERT(!child || (child->fFlags & flag));
Michael Ludwige88320b2020-06-24 09:04:56 -0400125 }
126#endif
Michael Ludwig9aba6252020-06-22 14:46:36 -0400127}
128
Brian Osman12c5d292020-07-13 16:11:35 -0400129int GrFragmentProcessor::numNonNullChildProcessors() const {
130 return std::count_if(fChildProcessors.begin(), fChildProcessors.end(),
131 [](const auto& c) { return c != nullptr; });
132}
133
Robert Phillips82774f82019-06-20 14:38:27 -0400134#ifdef SK_DEBUG
135bool GrFragmentProcessor::isInstantiated() const {
Brian Salomond90b3d32020-07-09 12:04:31 -0400136 bool result = true;
137 this->visitTextureEffects([&result](const GrTextureEffect& te) {
138 if (!te.texture()) {
139 result = false;
Brian Salomone782f842018-07-31 13:53:11 -0400140 }
Brian Salomond90b3d32020-07-09 12:04:31 -0400141 });
142 return result;
Robert Phillips9bee2e52017-05-29 12:37:20 -0400143}
Robert Phillips82774f82019-06-20 14:38:27 -0400144#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400145
Brian Osman12c5d292020-07-13 16:11:35 -0400146void GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child,
147 SkSL::SampleUsage sampleUsage) {
Brian Osman54867de2020-07-10 14:22:57 -0400148 if (!child) {
Brian Osman12c5d292020-07-13 16:11:35 -0400149 fChildProcessors.push_back(nullptr);
150 return;
Brian Osman54867de2020-07-10 14:22:57 -0400151 }
152
Michael Ludwige88320b2020-06-24 09:04:56 -0400153 // The child should not have been attached to another FP already and not had any sampling
154 // strategy set on it.
Brian Osman54867de2020-07-10 14:22:57 -0400155 SkASSERT(!child->fParent && !child->sampleUsage().isSampled() &&
Michael Ludwige88320b2020-06-24 09:04:56 -0400156 !child->isSampledWithExplicitCoords() && !child->hasPerspectiveTransform());
157
Brian Osman1298bc42020-06-30 13:39:35 -0400158 // If a child is sampled directly (sample(child)), and with a single uniform matrix, we need to
159 // treat it as if it were sampled with multiple matrices (eg variable).
160 bool variableMatrix = sampleUsage.hasVariableMatrix() ||
161 (sampleUsage.fPassThrough && sampleUsage.hasUniformMatrix());
162
Michael Ludwig9aba6252020-06-22 14:46:36 -0400163 // Configure child's sampling state first
Brian Osman1298bc42020-06-30 13:39:35 -0400164 child->fUsage = sampleUsage;
165
166 // When an FP is sampled using variable matrix expressions, it is effectively being sampled
167 // explicitly, except that the call site will automatically evaluate the matrix expression to
168 // produce the float2 passed into this FP.
169 if (sampleUsage.fExplicitCoords || variableMatrix) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400170 child->addAndPushFlagToChildren(kSampledWithExplicitCoords_Flag);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400171 }
Brian Osman1298bc42020-06-30 13:39:35 -0400172
173 // Push perspective matrix type to children
174 if (sampleUsage.fHasPerspective) {
175 child->addAndPushFlagToChildren(kNetTransformHasPerspective_Flag);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400176 }
177
Brian Osman1298bc42020-06-30 13:39:35 -0400178 // If the child is sampled with a variable matrix expression, auto-generated code in
179 // invokeChildWithMatrix() for this FP will refer to the local coordinates.
180 if (variableMatrix) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400181 this->setUsesSampleCoordsDirectly();
182 }
183
184 // If the child is not sampled explicitly and not already accessing sample coords directly
185 // (through reference or variable matrix expansion), then mark that this FP tree relies on
186 // coordinates at a lower level. If the child is sampled with explicit coordinates and
187 // there isn't any other direct reference to the sample coords, we halt the upwards propagation
188 // because it means this FP is determining coordinates on its own.
189 if (!child->isSampledWithExplicitCoords()) {
190 if ((child->fFlags & kUsesSampleCoordsDirectly_Flag ||
191 child->fFlags & kUsesSampleCoordsIndirectly_Flag)) {
192 fFlags |= kUsesSampleCoordsIndirectly_Flag;
193 }
194 }
195
Chris Daltond7291ba2019-03-07 14:17:03 -0700196 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700197
Michael Ludwige88320b2020-06-24 09:04:56 -0400198 // Record that the child is attached to us; this FP is the source of any uniform data needed
199 // to evaluate the child sample matrix.
200 child->fParent = this;
Brian Salomonaff329b2017-08-11 09:40:37 -0400201 fChildProcessors.push_back(std::move(child));
Michael Ludwige88320b2020-06-24 09:04:56 -0400202
Leon Scroggins III982fff22020-07-31 14:09:06 -0400203 // Validate: our sample strategy comes from a parent we shouldn't have yet.
Michael Ludwige88320b2020-06-24 09:04:56 -0400204 SkASSERT(!this->isSampledWithExplicitCoords() && !this->hasPerspectiveTransform() &&
Brian Osman1298bc42020-06-30 13:39:35 -0400205 !fUsage.isSampled() && !fParent);
John Stiles3779f442020-06-15 10:48:49 -0400206}
207
John Stiles9ec6b052020-06-15 12:06:10 -0400208void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
209 for (int i = 0; i < src.numChildProcessors(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400210 if (auto fp = src.childProcessor(i)) {
211 this->registerChild(fp->clone(), fp->sampleUsage());
212 } else {
213 this->registerChild(nullptr);
214 }
John Stiles9ec6b052020-06-15 12:06:10 -0400215 }
216}
217
Mike Reed28eaed22018-02-01 11:24:53 -0500218std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400219 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700220 if (!fp) {
221 return nullptr;
222 }
John Stilesf743d4e2020-07-23 11:35:08 -0400223 return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700224}
225
Mike Reed28eaed22018-02-01 11:24:53 -0500226std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
227 std::unique_ptr<GrFragmentProcessor> fp) {
228 if (!fp) {
229 return nullptr;
230 }
John Stilesf743d4e2020-07-23 11:35:08 -0400231 return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn);
Mike Reed28eaed22018-02-01 11:24:53 -0500232}
233
John Stiles7bf26002020-07-13 11:30:12 -0400234std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateAlpha(
235 std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) {
236 auto colorFP = GrConstColorProcessor::Make(color);
John Stilesf743d4e2020-07-23 11:35:08 -0400237 return GrBlendFragmentProcessor::Make(
John Stiles7bf26002020-07-13 11:30:12 -0400238 std::move(colorFP), std::move(inputFP), SkBlendMode::kSrcIn,
John Stilesf743d4e2020-07-23 11:35:08 -0400239 GrBlendFragmentProcessor::BlendBehavior::kSkModeBehavior);
John Stiles7bf26002020-07-13 11:30:12 -0400240}
241
John Stiles85894302020-07-13 11:39:52 -0400242std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateRGBA(
243 std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) {
244 auto colorFP = GrConstColorProcessor::Make(color);
John Stilesf743d4e2020-07-23 11:35:08 -0400245 return GrBlendFragmentProcessor::Make(
John Stiles85894302020-07-13 11:39:52 -0400246 std::move(colorFP), std::move(inputFP), SkBlendMode::kModulate,
John Stilesf743d4e2020-07-23 11:35:08 -0400247 GrBlendFragmentProcessor::BlendBehavior::kSkModeBehavior);
John Stiles85894302020-07-13 11:39:52 -0400248}
249
Brian Osman6f5e9402020-01-22 10:39:31 -0500250std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
251 std::unique_ptr<GrFragmentProcessor> fp) {
252 if (!fp) {
253 return nullptr;
254 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400255 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500256}
257
Brian Salomonaff329b2017-08-11 09:40:37 -0400258std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
259 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400260 class SwizzleFragmentProcessor : public GrFragmentProcessor {
261 public:
John Stileseed56f02020-06-04 13:30:51 -0400262 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
263 const GrSwizzle& swizzle) {
264 return std::unique_ptr<GrFragmentProcessor>(
John Stiles4bdc1212020-12-14 18:04:13 -0500265 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400266 }
267
268 const char* name() const override { return "Swizzle"; }
269 const GrSwizzle& swizzle() const { return fSwizzle; }
John Stiles4bdc1212020-12-14 18:04:13 -0500270 bool usesExplicitReturn() const override { return true; }
Brian Osmance425512017-03-22 14:37:50 -0400271
John Stileseed56f02020-06-04 13:30:51 -0400272 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Osman12c5d292020-07-13 16:11:35 -0400273 return Make(this->childProcessor(0)->clone(), fSwizzle);
John Stileseed56f02020-06-04 13:30:51 -0400274 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400275
Brian Osmance425512017-03-22 14:37:50 -0400276 private:
John Stileseed56f02020-06-04 13:30:51 -0400277 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
278 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
279 , fSwizzle(swizzle) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400280 this->registerChild(std::move(fp));
John Stileseed56f02020-06-04 13:30:51 -0400281 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400282
Brian Osmance425512017-03-22 14:37:50 -0400283 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
284 class GLFP : public GrGLSLFragmentProcessor {
285 public:
286 void emitCode(EmitArgs& args) override {
Brian Osman6b5dbb42020-07-15 15:31:05 -0400287 SkString childColor = this->invokeChild(0, args);
John Stileseed56f02020-06-04 13:30:51 -0400288
Brian Osmance425512017-03-22 14:37:50 -0400289 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
290 const GrSwizzle& swizzle = sfp.swizzle();
291 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
292
John Stiles4bdc1212020-12-14 18:04:13 -0500293 fragBuilder->codeAppendf("return %s.%s;",
294 childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400295 }
296 };
297 return new GLFP;
298 }
299
300 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
301 b->add32(fSwizzle.asKey());
302 }
303
304 bool onIsEqual(const GrFragmentProcessor& other) const override {
305 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
306 return fSwizzle == sfp.fSwizzle;
307 }
308
Brian Osman1d5b5982018-10-01 13:41:39 -0400309 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400310 return fSwizzle.applyTo(input);
311 }
312
313 GrSwizzle fSwizzle;
314
John Stiles7571f9e2020-09-02 22:42:33 -0400315 using INHERITED = GrFragmentProcessor;
Brian Osmance425512017-03-22 14:37:50 -0400316 };
317
318 if (!fp) {
319 return nullptr;
320 }
321 if (GrSwizzle::RGBA() == swizzle) {
322 return fp;
323 }
John Stileseed56f02020-06-04 13:30:51 -0400324 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400325}
326
Brian Salomonaff329b2017-08-11 09:40:37 -0400327std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
328 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700329 class PremulFragmentProcessor : public GrFragmentProcessor {
330 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400331 static std::unique_ptr<GrFragmentProcessor> Make(
332 std::unique_ptr<GrFragmentProcessor> processor) {
333 return std::unique_ptr<GrFragmentProcessor>(
334 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400335 }
336
337 const char* name() const override { return "Premultiply"; }
John Stiles4bdc1212020-12-14 18:04:13 -0500338 bool usesExplicitReturn() const override { return true; }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400339
Brian Salomonaff329b2017-08-11 09:40:37 -0400340 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Osman12c5d292020-07-13 16:11:35 -0400341 return Make(this->childProcessor(0)->clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400342 }
343
Robert Phillips1c9686b2017-06-30 08:40:28 -0400344 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400345 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400346 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400347 this->registerChild(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700348 }
349
egdaniel57d3b032015-11-13 11:57:27 -0800350 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800351 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700352 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700353 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800354 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
John Stiles4bdc1212020-12-14 18:04:13 -0500355 SkString temp = this->invokeChild(/*childIndex=*/0, "half4(1)", args);
356 fragBuilder->codeAppendf("half4 color = %s;", temp.c_str());
357 fragBuilder->codeAppendf("color.rgb *= %s.rgb;", args.fInputColor);
358 fragBuilder->codeAppendf("return color * %s.a;", args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700359 }
360 };
361 return new GLFP;
362 }
363
Brian Salomon94efbf52016-11-29 13:43:05 -0500364 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700365
366 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
367
Brian Salomon587e08f2017-01-27 10:59:27 -0500368 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
369 OptimizationFlags flags = kNone_OptimizationFlags;
370 if (inner->preservesOpaqueInput()) {
371 flags |= kPreservesOpaqueInput_OptimizationFlag;
372 }
373 if (inner->hasConstantOutputForConstantInput()) {
374 flags |= kConstantOutputForConstantInput_OptimizationFlag;
375 }
376 return flags;
377 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700378
Brian Osman1d5b5982018-10-01 13:41:39 -0400379 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
380 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400381 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400382 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
383 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500384 }
385
John Stiles7571f9e2020-09-02 22:42:33 -0400386 using INHERITED = GrFragmentProcessor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700387 };
388 if (!fp) {
389 return nullptr;
390 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400391 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700392}
393
394//////////////////////////////////////////////////////////////////////////////
395
Brian Salomonaff329b2017-08-11 09:40:37 -0400396std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400397 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400398 if (!fp) {
399 return nullptr;
400 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400401 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700402}
bsalomone25eea42015-09-29 06:38:55 -0700403
John Stiles024d7452020-07-22 19:07:15 -0400404//////////////////////////////////////////////////////////////////////////////
405
John Stiles024d7452020-07-22 19:07:15 -0400406std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Compose(
407 std::unique_ptr<GrFragmentProcessor> f, std::unique_ptr<GrFragmentProcessor> g) {
408 class ComposeProcessor : public GrFragmentProcessor {
409 public:
410 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> f,
411 std::unique_ptr<GrFragmentProcessor> g) {
412 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(std::move(f),
413 std::move(g)));
414 }
415
416 const char* name() const override { return "Compose"; }
417
418 std::unique_ptr<GrFragmentProcessor> clone() const override {
419 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(*this));
420 }
421
422 private:
423 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
424 class GLFP : public GrGLSLFragmentProcessor {
425 public:
426 void emitCode(EmitArgs& args) override {
427 SkString result = this->invokeChild(0, args);
428 result = this->invokeChild(1, result.c_str(), args);
John Stiles49643002020-10-19 18:52:40 -0400429 args.fFragBuilder->codeAppendf("return %s;", result.c_str());
John Stiles024d7452020-07-22 19:07:15 -0400430 }
431 };
432 return new GLFP;
433 }
434
435 ComposeProcessor(std::unique_ptr<GrFragmentProcessor> f,
436 std::unique_ptr<GrFragmentProcessor> g)
437 : INHERITED(kSeriesFragmentProcessor_ClassID,
438 f->optimizationFlags() & g->optimizationFlags()) {
439 this->registerChild(std::move(f));
440 this->registerChild(std::move(g));
441 }
442
443 ComposeProcessor(const ComposeProcessor& that)
444 : INHERITED(kSeriesFragmentProcessor_ClassID, that.optimizationFlags()) {
445 this->cloneAndRegisterAllChildProcessors(that);
446 }
447
448 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
449
450 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
John Stiles49643002020-10-19 18:52:40 -0400451 bool usesExplicitReturn() const override { return true; }
John Stiles024d7452020-07-22 19:07:15 -0400452
453 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
454 SkPMColor4f color = inColor;
455 color = ConstantOutputForConstantInput(this->childProcessor(0), color);
456 color = ConstantOutputForConstantInput(this->childProcessor(1), color);
457 return color;
458 }
459
John Stiles7571f9e2020-09-02 22:42:33 -0400460 using INHERITED = GrFragmentProcessor;
John Stiles024d7452020-07-22 19:07:15 -0400461 };
462
463 // Allow either of the composed functions to be null.
464 if (f == nullptr) {
465 return g;
466 }
467 if (g == nullptr) {
468 return f;
469 }
470
471 // Run an optimization pass on this composition.
472 GrProcessorAnalysisColor inputColor;
473 inputColor.setToUnknown();
474
475 std::unique_ptr<GrFragmentProcessor> series[2] = {std::move(f), std::move(g)};
476 GrColorFragmentProcessorAnalysis info(inputColor, series, SK_ARRAY_COUNT(series));
477
478 SkPMColor4f knownColor;
479 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
480 switch (leadingFPsToEliminate) {
481 default:
482 // We shouldn't eliminate more than we started with.
483 SkASSERT(leadingFPsToEliminate <= 2);
484 [[fallthrough]];
485 case 0:
486 // Compose the two processors as requested.
487 return ComposeProcessor::Make(std::move(series[0]), std::move(series[1]));
488 case 1:
489 // Replace the first processor with a constant color.
490 return ComposeProcessor::Make(GrConstColorProcessor::Make(knownColor),
491 std::move(series[1]));
492 case 2:
493 // Replace the entire composition with a constant color.
494 return GrConstColorProcessor::Make(knownColor);
495 }
496}
497
498//////////////////////////////////////////////////////////////////////////////
499
Brian Salomon7eabfe82019-12-02 14:20:20 -0500500GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
John Stiles41d91b62020-07-21 14:39:40 -0400501 if (paint.hasCoverageFragmentProcessor()) {
502 fFPStack.push_back(paint.getCoverageFragmentProcessor());
Chris Dalton1c548942018-05-22 13:09:48 -0600503 }
John Stiles5933d7d2020-07-21 12:28:35 -0400504 if (paint.hasColorFragmentProcessor()) {
505 fFPStack.push_back(paint.getColorFragmentProcessor());
Chris Dalton1c548942018-05-22 13:09:48 -0600506 }
507}
508
Brian Salomon7eabfe82019-12-02 14:20:20 -0500509GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500510 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
511 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
512 }
513}
Brian Salomon87f4d292020-07-09 12:48:38 -0400514
515GrFragmentProcessor::CIter& GrFragmentProcessor::CIter::operator++() {
516 SkASSERT(!fFPStack.empty());
517 const GrFragmentProcessor* back = fFPStack.back();
518 fFPStack.pop_back();
519 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400520 if (auto child = back->childProcessor(i)) {
521 fFPStack.push_back(child);
522 }
Brian Salomon87f4d292020-07-09 12:48:38 -0400523 }
524 return *this;
525}
526