blob: c16014c81c4dfae69dec184d5bf98dc281826ad2 [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 Salomone782f842018-07-31 13:53:11 -040024 if (this->numTextureSamplers() != that.numTextureSamplers()) {
25 return false;
26 }
27 for (int i = 0; i < this->numTextureSamplers(); ++i) {
28 if (this->textureSampler(i) != that.textureSampler(i)) {
29 return false;
30 }
31 }
Brian Osman9cf98dc2020-07-01 17:21:27 -040032 if (this->usesVaryingCoordsDirectly() != that.usesVaryingCoordsDirectly()) {
bsalomonbf877302015-09-22 09:06:13 -070033 return false;
34 }
35 if (!this->onIsEqual(that)) {
36 return false;
37 }
38 if (this->numChildProcessors() != that.numChildProcessors()) {
39 return false;
40 }
41 for (int i = 0; i < this->numChildProcessors(); ++i) {
bsalomon7312ff82016-09-12 08:55:38 -070042 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070043 return false;
44 }
45 }
46 return true;
47}
48
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060049void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) {
Brian Salomonc241b582019-11-27 08:57:17 -050050 for (auto [sampler, fp] : FPTextureSamplerRange(*this)) {
51 bool mipped = (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter());
Robert Phillipsbd99c0c2019-12-12 13:26:58 +000052 func(sampler.view().proxy(), GrMipMapped(mipped));
Brian Salomone782f842018-07-31 13:53:11 -040053 }
54}
55
egdaniel57d3b032015-11-13 11:57:27 -080056GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
57 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070058 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
59 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080060 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070061 }
62 return glFragProc;
63}
64
Brian Salomone782f842018-07-31 13:53:11 -040065const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
66 SkASSERT(i >= 0 && i < fTextureSamplerCnt);
67 return this->onTextureSampler(i);
68}
69
Michael Ludwige88320b2020-06-24 09:04:56 -040070void GrFragmentProcessor::addAndPushFlagToChildren(PrivateFlags flag) {
71 // This propagates down, so if we've already marked it, all our children should have it too
72 if (!(fFlags & flag)) {
73 fFlags |= flag;
74 for (auto& child : fChildProcessors) {
75 child->addAndPushFlagToChildren(flag);
76 }
Michael Ludwig9aba6252020-06-22 14:46:36 -040077 }
Michael Ludwige88320b2020-06-24 09:04:56 -040078#ifdef SK_DEBUG
79 for (auto& child : fChildProcessors) {
80 SkASSERT(child->fFlags & flag);
81 }
82#endif
Michael Ludwig9aba6252020-06-22 14:46:36 -040083}
84
Robert Phillips82774f82019-06-20 14:38:27 -040085#ifdef SK_DEBUG
86bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -040087 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040088 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -040089 return false;
90 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040091 }
92
Robert Phillips9bee2e52017-05-29 12:37:20 -040093 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040094 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -040095 return false;
96 }
97 }
98
99 return true;
100}
Robert Phillips82774f82019-06-20 14:38:27 -0400101#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -0400102
Michael Ludwig9aba6252020-06-22 14:46:36 -0400103int GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child,
Brian Osman1298bc42020-06-30 13:39:35 -0400104 SkSL::SampleUsage sampleUsage) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400105 // The child should not have been attached to another FP already and not had any sampling
106 // strategy set on it.
Brian Osman1298bc42020-06-30 13:39:35 -0400107 SkASSERT(child && !child->fParent && !child->sampleUsage().isSampled() &&
Michael Ludwige88320b2020-06-24 09:04:56 -0400108 !child->isSampledWithExplicitCoords() && !child->hasPerspectiveTransform());
109
Brian Osman1298bc42020-06-30 13:39:35 -0400110 // If a child is sampled directly (sample(child)), and with a single uniform matrix, we need to
111 // treat it as if it were sampled with multiple matrices (eg variable).
112 bool variableMatrix = sampleUsage.hasVariableMatrix() ||
113 (sampleUsage.fPassThrough && sampleUsage.hasUniformMatrix());
114
Michael Ludwig9aba6252020-06-22 14:46:36 -0400115 // Configure child's sampling state first
Brian Osman1298bc42020-06-30 13:39:35 -0400116 child->fUsage = sampleUsage;
117
118 // When an FP is sampled using variable matrix expressions, it is effectively being sampled
119 // explicitly, except that the call site will automatically evaluate the matrix expression to
120 // produce the float2 passed into this FP.
121 if (sampleUsage.fExplicitCoords || variableMatrix) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400122 child->addAndPushFlagToChildren(kSampledWithExplicitCoords_Flag);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400123 }
Brian Osman1298bc42020-06-30 13:39:35 -0400124
125 // Push perspective matrix type to children
126 if (sampleUsage.fHasPerspective) {
127 child->addAndPushFlagToChildren(kNetTransformHasPerspective_Flag);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400128 }
129
Brian Osman1298bc42020-06-30 13:39:35 -0400130 // If the child is sampled with a variable matrix expression, auto-generated code in
131 // invokeChildWithMatrix() for this FP will refer to the local coordinates.
132 if (variableMatrix) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400133 this->setUsesSampleCoordsDirectly();
134 }
135
136 // If the child is not sampled explicitly and not already accessing sample coords directly
137 // (through reference or variable matrix expansion), then mark that this FP tree relies on
138 // coordinates at a lower level. If the child is sampled with explicit coordinates and
139 // there isn't any other direct reference to the sample coords, we halt the upwards propagation
140 // because it means this FP is determining coordinates on its own.
141 if (!child->isSampledWithExplicitCoords()) {
142 if ((child->fFlags & kUsesSampleCoordsDirectly_Flag ||
143 child->fFlags & kUsesSampleCoordsIndirectly_Flag)) {
144 fFlags |= kUsesSampleCoordsIndirectly_Flag;
145 }
146 }
147
Chris Daltond7291ba2019-03-07 14:17:03 -0700148 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700149
bungeman06ca8ec2016-06-09 08:01:03 -0700150 int index = fChildProcessors.count();
Michael Ludwige88320b2020-06-24 09:04:56 -0400151 // Record that the child is attached to us; this FP is the source of any uniform data needed
152 // to evaluate the child sample matrix.
153 child->fParent = this;
Brian Salomonaff329b2017-08-11 09:40:37 -0400154 fChildProcessors.push_back(std::move(child));
Michael Ludwige88320b2020-06-24 09:04:56 -0400155
156 // Sanity check: our sample strategy comes from a parent we shouldn't have yet.
157 SkASSERT(!this->isSampledWithExplicitCoords() && !this->hasPerspectiveTransform() &&
Brian Osman1298bc42020-06-30 13:39:35 -0400158 !fUsage.isSampled() && !fParent);
bsalomonbf877302015-09-22 09:06:13 -0700159 return index;
160}
161
John Stiles3779f442020-06-15 10:48:49 -0400162int GrFragmentProcessor::cloneAndRegisterChildProcessor(const GrFragmentProcessor& fp) {
163 std::unique_ptr<GrFragmentProcessor> clone = fp.clone();
Brian Osman1298bc42020-06-30 13:39:35 -0400164 return this->registerChild(std::move(clone), fp.sampleUsage());
John Stiles3779f442020-06-15 10:48:49 -0400165}
166
John Stiles9ec6b052020-06-15 12:06:10 -0400167void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
168 for (int i = 0; i < src.numChildProcessors(); ++i) {
169 this->cloneAndRegisterChildProcessor(src.childProcessor(i));
170 }
171}
172
Mike Reed28eaed22018-02-01 11:24:53 -0500173std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400174 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700175 if (!fp) {
176 return nullptr;
177 }
John Stiles5c57e882020-06-30 14:05:03 -0400178 return GrXfermodeFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700179}
180
Mike Reed28eaed22018-02-01 11:24:53 -0500181std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
182 std::unique_ptr<GrFragmentProcessor> fp) {
183 if (!fp) {
184 return nullptr;
185 }
John Stiles5c57e882020-06-30 14:05:03 -0400186 return GrXfermodeFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn);
Mike Reed28eaed22018-02-01 11:24:53 -0500187}
188
Brian Osman6f5e9402020-01-22 10:39:31 -0500189std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
190 std::unique_ptr<GrFragmentProcessor> fp) {
191 if (!fp) {
192 return nullptr;
193 }
John Stiles5a2a7b32020-06-04 10:57:21 -0400194 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
Brian Osman6f5e9402020-01-22 10:39:31 -0500195}
196
Brian Salomonaff329b2017-08-11 09:40:37 -0400197std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
198 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400199 class SwizzleFragmentProcessor : public GrFragmentProcessor {
200 public:
John Stileseed56f02020-06-04 13:30:51 -0400201 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
202 const GrSwizzle& swizzle) {
203 return std::unique_ptr<GrFragmentProcessor>(
204 new SwizzleFragmentProcessor(std::move(fp), swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400205 }
206
207 const char* name() const override { return "Swizzle"; }
208 const GrSwizzle& swizzle() const { return fSwizzle; }
209
John Stileseed56f02020-06-04 13:30:51 -0400210 std::unique_ptr<GrFragmentProcessor> clone() const override {
211 return Make(this->childProcessor(0).clone(), fSwizzle);
212 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400213
Brian Osmance425512017-03-22 14:37:50 -0400214 private:
John Stileseed56f02020-06-04 13:30:51 -0400215 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
216 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
217 , fSwizzle(swizzle) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400218 this->registerChild(std::move(fp));
John Stileseed56f02020-06-04 13:30:51 -0400219 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400220
Brian Osmance425512017-03-22 14:37:50 -0400221 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
222 class GLFP : public GrGLSLFragmentProcessor {
223 public:
224 void emitCode(EmitArgs& args) override {
John Stiles0af87fe2020-06-05 13:33:09 -0400225 SkString childColor = this->invokeChild(0, args.fInputColor, args);
John Stileseed56f02020-06-04 13:30:51 -0400226
Brian Osmance425512017-03-22 14:37:50 -0400227 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
228 const GrSwizzle& swizzle = sfp.swizzle();
229 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
230
231 fragBuilder->codeAppendf("%s = %s.%s;",
John Stileseed56f02020-06-04 13:30:51 -0400232 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400233 }
234 };
235 return new GLFP;
236 }
237
238 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
239 b->add32(fSwizzle.asKey());
240 }
241
242 bool onIsEqual(const GrFragmentProcessor& other) const override {
243 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
244 return fSwizzle == sfp.fSwizzle;
245 }
246
Brian Osman1d5b5982018-10-01 13:41:39 -0400247 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400248 return fSwizzle.applyTo(input);
249 }
250
251 GrSwizzle fSwizzle;
252
253 typedef GrFragmentProcessor INHERITED;
254 };
255
256 if (!fp) {
257 return nullptr;
258 }
259 if (GrSwizzle::RGBA() == swizzle) {
260 return fp;
261 }
John Stileseed56f02020-06-04 13:30:51 -0400262 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
Brian Osmance425512017-03-22 14:37:50 -0400263}
264
Brian Salomonaff329b2017-08-11 09:40:37 -0400265std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
266 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700267 class PremulFragmentProcessor : public GrFragmentProcessor {
268 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400269 static std::unique_ptr<GrFragmentProcessor> Make(
270 std::unique_ptr<GrFragmentProcessor> processor) {
271 return std::unique_ptr<GrFragmentProcessor>(
272 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400273 }
274
275 const char* name() const override { return "Premultiply"; }
276
Brian Salomonaff329b2017-08-11 09:40:37 -0400277 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400278 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400279 }
280
Robert Phillips1c9686b2017-06-30 08:40:28 -0400281 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400282 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400283 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400284 this->registerChild(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700285 }
286
egdaniel57d3b032015-11-13 11:57:27 -0800287 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800288 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700289 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700290 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800291 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -0500292 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500293 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
egdaniel4ca2e602015-11-18 08:01:26 -0800294 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
Brian Osmancddfc5e2020-01-24 10:24:27 -0500295 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800296 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700297 }
298 };
299 return new GLFP;
300 }
301
Brian Salomon94efbf52016-11-29 13:43:05 -0500302 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700303
304 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
305
Brian Salomon587e08f2017-01-27 10:59:27 -0500306 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
307 OptimizationFlags flags = kNone_OptimizationFlags;
308 if (inner->preservesOpaqueInput()) {
309 flags |= kPreservesOpaqueInput_OptimizationFlag;
310 }
311 if (inner->hasConstantOutputForConstantInput()) {
312 flags |= kConstantOutputForConstantInput_OptimizationFlag;
313 }
314 return flags;
315 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700316
Brian Osman1d5b5982018-10-01 13:41:39 -0400317 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
318 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400319 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400320 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
321 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500322 }
323
324 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700325 };
326 if (!fp) {
327 return nullptr;
328 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400329 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700330}
331
332//////////////////////////////////////////////////////////////////////////////
333
Brian Salomonaff329b2017-08-11 09:40:37 -0400334std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400335 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400336 if (!fp) {
337 return nullptr;
338 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400339 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700340}
bsalomone25eea42015-09-29 06:38:55 -0700341
Brian Salomonaff329b2017-08-11 09:40:37 -0400342std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
Brian Salomon64f42062020-02-14 10:42:45 -0500343 std::unique_ptr<GrFragmentProcessor> series[], int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700344 class SeriesFragmentProcessor : public GrFragmentProcessor {
345 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400346 static std::unique_ptr<GrFragmentProcessor> Make(
347 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
348 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700349 }
350
351 const char* name() const override { return "Series"; }
352
Brian Salomonaff329b2017-08-11 09:40:37 -0400353 std::unique_ptr<GrFragmentProcessor> clone() const override {
354 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400355 for (int i = 0; i < this->numChildProcessors(); ++i) {
356 if (!children.push_back(this->childProcessor(i).clone())) {
357 return nullptr;
358 }
359 }
360 return Make(children.begin(), this->numChildProcessors());
361 }
362
363 private:
egdaniel57d3b032015-11-13 11:57:27 -0800364 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800365 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700366 public:
bsalomone25eea42015-09-29 06:38:55 -0700367 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700368 // First guy's input might be nil.
Brian Osman978693c2020-01-24 14:52:10 -0500369 SkString result = this->invokeChild(0, args.fInputColor, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500370 for (int i = 1; i < this->numChildProcessors(); ++i) {
Brian Osman978693c2020-01-24 14:52:10 -0500371 result = this->invokeChild(i, result.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700372 }
Brian Osmancddfc5e2020-01-24 10:24:27 -0500373 // Copy last output to our output variable
Brian Osman978693c2020-01-24 14:52:10 -0500374 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
bsalomone25eea42015-09-29 06:38:55 -0700375 }
376 };
377 return new GLFP;
378 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400379
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400381 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400382 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400383 for (int i = 0; i < cnt; ++i) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400384 this->registerChild(std::move(children[i]));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400385 }
386 }
387
Brian Salomonaff329b2017-08-11 09:40:37 -0400388 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500389 OptimizationFlags flags = kAll_OptimizationFlags;
390 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
391 flags &= children[i]->optimizationFlags();
392 }
393 return flags;
394 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500395 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700396
397 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
398
Brian Osman1d5b5982018-10-01 13:41:39 -0400399 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
400 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500401 int childCnt = this->numChildProcessors();
402 for (int i = 0; i < childCnt; ++i) {
403 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
404 }
405 return color;
406 }
407
408 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700409 };
410
411 if (!cnt) {
412 return nullptr;
413 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500414 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400415 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500416 }
bsalomone25eea42015-09-29 06:38:55 -0700417 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400418 GrProcessorAnalysisColor inputColor;
419 inputColor.setToUnknown();
Brian Salomon64f42062020-02-14 10:42:45 -0500420 GrColorFragmentProcessorAnalysis info(inputColor, series, cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400422 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500423 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
424 if (leadingFPsToEliminate) {
John Stilese3a39f72020-06-15 13:58:48 -0400425 std::unique_ptr<GrFragmentProcessor> colorFP = GrConstColorProcessor::Make(
426 /*inputFP=*/nullptr, knownColor, GrConstColorProcessor::InputMode::kIgnore);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500427 if (leadingFPsToEliminate == cnt) {
428 return colorFP;
429 }
430 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700431 replacementSeries.reserve(cnt);
432 replacementSeries.emplace_back(std::move(colorFP));
433 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500434 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700435 }
bungeman06ca8ec2016-06-09 08:01:03 -0700436 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700437 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400438 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700439}
bsalomona624bf32016-09-20 09:12:47 -0700440
441//////////////////////////////////////////////////////////////////////////////
442
Brian Salomon7eabfe82019-12-02 14:20:20 -0500443GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600444 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
445 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
446 }
447 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
448 fFPStack.push_back(paint.getColorFragmentProcessor(i));
449 }
450}
451
Brian Salomon7eabfe82019-12-02 14:20:20 -0500452GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500453 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
454 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700455 }
Brian Salomonc241b582019-11-27 08:57:17 -0500456 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
457 fFPStack.push_back(set.colorFragmentProcessor(i));
458 }
459}
460
Brian Salomon7eabfe82019-12-02 14:20:20 -0500461GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500462 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
463 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
464 }
465}
466
Brian Salomone782f842018-07-31 13:53:11 -0400467///////////////////////////////////////////////////////////////////////////////////////////////////
468
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000469GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500470 GrSamplerState samplerState)
471 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000472 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500473 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500474 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000475 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500476}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000477
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000478#if GR_TEST_UTILS
479void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500480 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000481 SkASSERT(view.proxy()->asTextureProxy());
482 fView = std::move(view);
483 fSamplerState = samplerState;
484
485 fSamplerState.setFilterMode(
Brian Osman788b9162020-02-07 10:36:46 -0500486 std::min(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000487 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
488}
489#endif