blob: 8443e6127c5a44bb093a6da68e85d891d8887aa1 [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; }
270
John Stileseed56f02020-06-04 13:30:51 -0400271 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Osman12c5d292020-07-13 16:11:35 -0400272 return Make(this->childProcessor(0)->clone(), fSwizzle);
John Stileseed56f02020-06-04 13:30:51 -0400273 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400274
Brian Osmance425512017-03-22 14:37:50 -0400275 private:
John Stileseed56f02020-06-04 13:30:51 -0400276 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
277 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
278 , fSwizzle(swizzle) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400279 this->registerChild(std::move(fp));
John Stileseed56f02020-06-04 13:30:51 -0400280 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400281
Brian Osmance425512017-03-22 14:37:50 -0400282 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
283 class GLFP : public GrGLSLFragmentProcessor {
284 public:
285 void emitCode(EmitArgs& args) override {
Brian Osman6b5dbb42020-07-15 15:31:05 -0400286 SkString childColor = this->invokeChild(0, args);
John Stileseed56f02020-06-04 13:30:51 -0400287
Brian Osmance425512017-03-22 14:37:50 -0400288 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
289 const GrSwizzle& swizzle = sfp.swizzle();
290 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
291
John Stiles4bdc1212020-12-14 18:04:13 -0500292 fragBuilder->codeAppendf("return %s.%s;",
293 childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400294 }
295 };
296 return new GLFP;
297 }
298
299 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
300 b->add32(fSwizzle.asKey());
301 }
302
303 bool onIsEqual(const GrFragmentProcessor& other) const override {
304 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
305 return fSwizzle == sfp.fSwizzle;
306 }
307
Brian Osman1d5b5982018-10-01 13:41:39 -0400308 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400309 return fSwizzle.applyTo(input);
310 }
311
312 GrSwizzle fSwizzle;
313
John Stiles7571f9e2020-09-02 22:42:33 -0400314 using INHERITED = GrFragmentProcessor;
Brian Osmance425512017-03-22 14:37:50 -0400315 };
316
317 if (!fp) {
318 return nullptr;
319 }
320 if (GrSwizzle::RGBA() == swizzle) {
321 return fp;
322 }
John Stileseed56f02020-06-04 13:30:51 -0400323 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400324}
325
Brian Salomonaff329b2017-08-11 09:40:37 -0400326std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
327 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700328 class PremulFragmentProcessor : public GrFragmentProcessor {
329 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400330 static std::unique_ptr<GrFragmentProcessor> Make(
331 std::unique_ptr<GrFragmentProcessor> processor) {
332 return std::unique_ptr<GrFragmentProcessor>(
333 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400334 }
335
336 const char* name() const override { return "Premultiply"; }
337
Brian Salomonaff329b2017-08-11 09:40:37 -0400338 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Osman12c5d292020-07-13 16:11:35 -0400339 return Make(this->childProcessor(0)->clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400340 }
341
Robert Phillips1c9686b2017-06-30 08:40:28 -0400342 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400343 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400344 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400345 this->registerChild(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700346 }
347
egdaniel57d3b032015-11-13 11:57:27 -0800348 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800349 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700350 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700351 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800352 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
John Stiles4bdc1212020-12-14 18:04:13 -0500353 SkString temp = this->invokeChild(/*childIndex=*/0, "half4(1)", args);
354 fragBuilder->codeAppendf("half4 color = %s;", temp.c_str());
355 fragBuilder->codeAppendf("color.rgb *= %s.rgb;", args.fInputColor);
356 fragBuilder->codeAppendf("return color * %s.a;", args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700357 }
358 };
359 return new GLFP;
360 }
361
Brian Salomon94efbf52016-11-29 13:43:05 -0500362 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700363
364 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
365
Brian Salomon587e08f2017-01-27 10:59:27 -0500366 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
367 OptimizationFlags flags = kNone_OptimizationFlags;
368 if (inner->preservesOpaqueInput()) {
369 flags |= kPreservesOpaqueInput_OptimizationFlag;
370 }
371 if (inner->hasConstantOutputForConstantInput()) {
372 flags |= kConstantOutputForConstantInput_OptimizationFlag;
373 }
374 return flags;
375 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700376
Brian Osman1d5b5982018-10-01 13:41:39 -0400377 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
378 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400379 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400380 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
381 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500382 }
383
John Stiles7571f9e2020-09-02 22:42:33 -0400384 using INHERITED = GrFragmentProcessor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700385 };
386 if (!fp) {
387 return nullptr;
388 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400389 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700390}
391
392//////////////////////////////////////////////////////////////////////////////
393
Brian Salomonaff329b2017-08-11 09:40:37 -0400394std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400395 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400396 if (!fp) {
397 return nullptr;
398 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400399 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700400}
bsalomone25eea42015-09-29 06:38:55 -0700401
John Stiles024d7452020-07-22 19:07:15 -0400402//////////////////////////////////////////////////////////////////////////////
403
John Stiles024d7452020-07-22 19:07:15 -0400404std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Compose(
405 std::unique_ptr<GrFragmentProcessor> f, std::unique_ptr<GrFragmentProcessor> g) {
406 class ComposeProcessor : public GrFragmentProcessor {
407 public:
408 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> f,
409 std::unique_ptr<GrFragmentProcessor> g) {
410 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(std::move(f),
411 std::move(g)));
412 }
413
414 const char* name() const override { return "Compose"; }
415
416 std::unique_ptr<GrFragmentProcessor> clone() const override {
417 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(*this));
418 }
419
420 private:
421 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
422 class GLFP : public GrGLSLFragmentProcessor {
423 public:
424 void emitCode(EmitArgs& args) override {
425 SkString result = this->invokeChild(0, args);
426 result = this->invokeChild(1, result.c_str(), args);
John Stiles49643002020-10-19 18:52:40 -0400427 args.fFragBuilder->codeAppendf("return %s;", result.c_str());
John Stiles024d7452020-07-22 19:07:15 -0400428 }
429 };
430 return new GLFP;
431 }
432
433 ComposeProcessor(std::unique_ptr<GrFragmentProcessor> f,
434 std::unique_ptr<GrFragmentProcessor> g)
435 : INHERITED(kSeriesFragmentProcessor_ClassID,
436 f->optimizationFlags() & g->optimizationFlags()) {
437 this->registerChild(std::move(f));
438 this->registerChild(std::move(g));
439 }
440
441 ComposeProcessor(const ComposeProcessor& that)
442 : INHERITED(kSeriesFragmentProcessor_ClassID, that.optimizationFlags()) {
443 this->cloneAndRegisterAllChildProcessors(that);
444 }
445
446 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
447
448 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
449
450 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
451 SkPMColor4f color = inColor;
452 color = ConstantOutputForConstantInput(this->childProcessor(0), color);
453 color = ConstantOutputForConstantInput(this->childProcessor(1), color);
454 return color;
455 }
456
John Stiles7571f9e2020-09-02 22:42:33 -0400457 using INHERITED = GrFragmentProcessor;
John Stiles024d7452020-07-22 19:07:15 -0400458 };
459
460 // Allow either of the composed functions to be null.
461 if (f == nullptr) {
462 return g;
463 }
464 if (g == nullptr) {
465 return f;
466 }
467
468 // Run an optimization pass on this composition.
469 GrProcessorAnalysisColor inputColor;
470 inputColor.setToUnknown();
471
472 std::unique_ptr<GrFragmentProcessor> series[2] = {std::move(f), std::move(g)};
473 GrColorFragmentProcessorAnalysis info(inputColor, series, SK_ARRAY_COUNT(series));
474
475 SkPMColor4f knownColor;
476 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
477 switch (leadingFPsToEliminate) {
478 default:
479 // We shouldn't eliminate more than we started with.
480 SkASSERT(leadingFPsToEliminate <= 2);
481 [[fallthrough]];
482 case 0:
483 // Compose the two processors as requested.
484 return ComposeProcessor::Make(std::move(series[0]), std::move(series[1]));
485 case 1:
486 // Replace the first processor with a constant color.
487 return ComposeProcessor::Make(GrConstColorProcessor::Make(knownColor),
488 std::move(series[1]));
489 case 2:
490 // Replace the entire composition with a constant color.
491 return GrConstColorProcessor::Make(knownColor);
492 }
493}
494
495//////////////////////////////////////////////////////////////////////////////
496
Brian Salomon7eabfe82019-12-02 14:20:20 -0500497GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
John Stiles41d91b62020-07-21 14:39:40 -0400498 if (paint.hasCoverageFragmentProcessor()) {
499 fFPStack.push_back(paint.getCoverageFragmentProcessor());
Chris Dalton1c548942018-05-22 13:09:48 -0600500 }
John Stiles5933d7d2020-07-21 12:28:35 -0400501 if (paint.hasColorFragmentProcessor()) {
502 fFPStack.push_back(paint.getColorFragmentProcessor());
Chris Dalton1c548942018-05-22 13:09:48 -0600503 }
504}
505
Brian Salomon7eabfe82019-12-02 14:20:20 -0500506GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500507 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
508 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
509 }
510}
Brian Salomon87f4d292020-07-09 12:48:38 -0400511
512GrFragmentProcessor::CIter& GrFragmentProcessor::CIter::operator++() {
513 SkASSERT(!fFPStack.empty());
514 const GrFragmentProcessor* back = fFPStack.back();
515 fFPStack.pop_back();
516 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400517 if (auto child = back->childProcessor(i)) {
518 fFPStack.push_back(child);
519 }
Brian Salomon87f4d292020-07-09 12:48:38 -0400520 }
521 return *this;
522}
523