blob: 93e1c7fa8b400e18033e14b615802633c349127c [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
8#include "GrFragmentProcessor.h"
9#include "GrCoordTransform.h"
bsalomona624bf32016-09-20 09:12:47 -070010#include "GrPipeline.h"
Brian Salomona811b122017-03-30 08:21:32 -040011#include "GrProcessorAnalysis.h"
Brian Salomonc0b642c2017-03-27 13:09:36 -040012#include "effects/GrConstColorProcessor.h"
Ethan Nicholasbe0a0422017-11-17 13:44:05 -050013#include "effects/GrPremulInputFragmentProcessor.h"
Brian Salomonc0b642c2017-03-27 13:09:36 -040014#include "effects/GrXfermodeFragmentProcessor.h"
Ethan Nicholasbe0a0422017-11-17 13:44:05 -050015#include "effects/GrUnpremulInputFragmentProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080016#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080017#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070018#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080019#include "glsl/GrGLSLUniformHandler.h"
bsalomonbf877302015-09-22 09:06:13 -070020
bsalomon7312ff82016-09-12 08:55:38 -070021bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
bsalomonbf877302015-09-22 09:06:13 -070022 if (this->classID() != that.classID() ||
Brian Salomonf9f45122016-11-29 11:59:17 -050023 !this->hasSameSamplersAndAccesses(that)) {
bsalomonbf877302015-09-22 09:06:13 -070024 return false;
25 }
bsalomon7312ff82016-09-12 08:55:38 -070026 if (!this->hasSameTransforms(that)) {
bsalomonbf877302015-09-22 09:06:13 -070027 return false;
28 }
29 if (!this->onIsEqual(that)) {
30 return false;
31 }
32 if (this->numChildProcessors() != that.numChildProcessors()) {
33 return false;
34 }
35 for (int i = 0; i < this->numChildProcessors(); ++i) {
bsalomon7312ff82016-09-12 08:55:38 -070036 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070037 return false;
38 }
39 }
40 return true;
41}
42
egdaniel57d3b032015-11-13 11:57:27 -080043GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
44 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070045 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
46 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080047 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070048 }
49 return glFragProc;
50}
51
bsalomonbf877302015-09-22 09:06:13 -070052void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070053 fCoordTransforms.push_back(transform);
Brian Salomon587e08f2017-01-27 10:59:27 -050054 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070055 SkDEBUGCODE(transform->setInProcessor();)
bsalomonbf877302015-09-22 09:06:13 -070056}
57
Robert Phillips9bee2e52017-05-29 12:37:20 -040058bool GrFragmentProcessor::instantiate(GrResourceProvider* resourceProvider) const {
59 if (!INHERITED::instantiate(resourceProvider)) {
60 return false;
Robert Phillipsa91e0b72017-05-01 13:12:20 -040061 }
62
Robert Phillips9bee2e52017-05-29 12:37:20 -040063 for (int i = 0; i < this->numChildProcessors(); ++i) {
64 if (!this->childProcessor(i).instantiate(resourceProvider)) {
65 return false;
66 }
67 }
68
69 return true;
70}
71
Brian Salomonaff329b2017-08-11 09:40:37 -040072void GrFragmentProcessor::markPendingExecution() const {
73 INHERITED::addPendingIOs();
74 INHERITED::removeRefs();
75 for (int i = 0; i < this->numChildProcessors(); ++i) {
76 this->childProcessor(i).markPendingExecution();
77 }
78}
79
80int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
bsalomonbf877302015-09-22 09:06:13 -070081 if (child->usesLocalCoords()) {
Brian Salomon587e08f2017-01-27 10:59:27 -050082 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070083 }
84
bungeman06ca8ec2016-06-09 08:01:03 -070085 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -040086 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -070087
bsalomonbf877302015-09-22 09:06:13 -070088 return index;
89}
90
bsalomonbf877302015-09-22 09:06:13 -070091bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -070092 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -070093 return false;
94 }
bsalomona624bf32016-09-20 09:12:47 -070095 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -070096 for (int i = 0; i < count; ++i) {
Robert Phillips67c18d62017-01-20 12:44:06 -050097 if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -070098 return false;
99 }
100 }
101 return true;
102}
103
Mike Reed28eaed22018-02-01 11:24:53 -0500104std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700106 if (!fp) {
107 return nullptr;
108 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400109 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700110}
111
Mike Reed28eaed22018-02-01 11:24:53 -0500112std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
113 std::unique_ptr<GrFragmentProcessor> fp) {
114 if (!fp) {
115 return nullptr;
116 }
117 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
118}
119
Brian Salomonaff329b2017-08-11 09:40:37 -0400120std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
121 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700122 if (!fp) {
123 return nullptr;
124 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500125 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400126 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700127 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
128}
129
Brian Salomonaff329b2017-08-11 09:40:37 -0400130std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulOutput(
131 std::unique_ptr<GrFragmentProcessor> fp) {
Brian Osmande1a6052017-03-22 10:57:00 -0400132 if (!fp) {
133 return nullptr;
134 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400135 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500136 GrPremulInputFragmentProcessor::Make() };
Brian Osmande1a6052017-03-22 10:57:00 -0400137 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
138}
139
Brian Salomonaff329b2017-08-11 09:40:37 -0400140std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::UnpremulOutput(
141 std::unique_ptr<GrFragmentProcessor> fp) {
Brian Osmande1a6052017-03-22 10:57:00 -0400142 if (!fp) {
143 return nullptr;
144 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400145 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500146 GrUnpremulInputFragmentProcessor::Make() };
Brian Osmande1a6052017-03-22 10:57:00 -0400147 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
148}
149
Brian Salomonaff329b2017-08-11 09:40:37 -0400150std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
151 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400152 class SwizzleFragmentProcessor : public GrFragmentProcessor {
153 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400154 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
155 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400156 }
157
158 const char* name() const override { return "Swizzle"; }
159 const GrSwizzle& swizzle() const { return fSwizzle; }
160
Brian Salomonaff329b2017-08-11 09:40:37 -0400161 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400162
Brian Osmance425512017-03-22 14:37:50 -0400163 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400164 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400165 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Robert Phillips1c9686b2017-06-30 08:40:28 -0400166 , fSwizzle(swizzle) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400167 }
168
Brian Osmance425512017-03-22 14:37:50 -0400169 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
170 class GLFP : public GrGLSLFragmentProcessor {
171 public:
172 void emitCode(EmitArgs& args) override {
173 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
174 const GrSwizzle& swizzle = sfp.swizzle();
175 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
176
177 fragBuilder->codeAppendf("%s = %s.%s;",
178 args.fOutputColor, args.fInputColor, swizzle.c_str());
179 }
180 };
181 return new GLFP;
182 }
183
184 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
185 b->add32(fSwizzle.asKey());
186 }
187
188 bool onIsEqual(const GrFragmentProcessor& other) const override {
189 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
190 return fSwizzle == sfp.fSwizzle;
191 }
192
193 GrColor4f constantOutputForConstantInput(GrColor4f input) const override {
194 return fSwizzle.applyTo(input);
195 }
196
197 GrSwizzle fSwizzle;
198
199 typedef GrFragmentProcessor INHERITED;
200 };
201
202 if (!fp) {
203 return nullptr;
204 }
205 if (GrSwizzle::RGBA() == swizzle) {
206 return fp;
207 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400208 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
209 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400210 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
211}
212
Brian Salomonaff329b2017-08-11 09:40:37 -0400213std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
214 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700215 class PremulFragmentProcessor : public GrFragmentProcessor {
216 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400217 static std::unique_ptr<GrFragmentProcessor> Make(
218 std::unique_ptr<GrFragmentProcessor> processor) {
219 return std::unique_ptr<GrFragmentProcessor>(
220 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400221 }
222
223 const char* name() const override { return "Premultiply"; }
224
Brian Salomonaff329b2017-08-11 09:40:37 -0400225 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400226 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400227 }
228
Robert Phillips1c9686b2017-06-30 08:40:28 -0400229 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400230 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400231 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400232 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700233 }
234
egdaniel57d3b032015-11-13 11:57:27 -0800235 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800236 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700237 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700238 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800239 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400240 this->emitChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800241 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700242 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800243 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700244 }
245 };
246 return new GLFP;
247 }
248
Brian Salomon94efbf52016-11-29 13:43:05 -0500249 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700250
251 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
252
Brian Salomon587e08f2017-01-27 10:59:27 -0500253 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
254 OptimizationFlags flags = kNone_OptimizationFlags;
255 if (inner->preservesOpaqueInput()) {
256 flags |= kPreservesOpaqueInput_OptimizationFlag;
257 }
258 if (inner->hasConstantOutputForConstantInput()) {
259 flags |= kConstantOutputForConstantInput_OptimizationFlag;
260 }
261 return flags;
262 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700263
Brian Salomon587e08f2017-01-27 10:59:27 -0500264 GrColor4f constantOutputForConstantInput(GrColor4f input) const override {
265 GrColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
266 GrColor4f::OpaqueWhite());
267 return GrColor4f(input.fRGBA[3] * input.fRGBA[0] * childColor.fRGBA[0],
268 input.fRGBA[3] * input.fRGBA[1] * childColor.fRGBA[1],
269 input.fRGBA[3] * input.fRGBA[2] * childColor.fRGBA[2],
270 input.fRGBA[3] * childColor.fRGBA[3]);
271 }
272
273 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700274 };
275 if (!fp) {
276 return nullptr;
277 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400278 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700279}
280
281//////////////////////////////////////////////////////////////////////////////
282
Brian Salomonaff329b2017-08-11 09:40:37 -0400283std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
284 std::unique_ptr<GrFragmentProcessor> fp, GrColor4f color) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700285 class ReplaceInputFragmentProcessor : public GrFragmentProcessor {
286 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400287 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child,
288 GrColor4f color) {
289 return std::unique_ptr<GrFragmentProcessor>(
290 new ReplaceInputFragmentProcessor(std::move(child), color));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700291 }
292
293 const char* name() const override { return "Replace Color"; }
294
Brian Salomonaff329b2017-08-11 09:40:37 -0400295 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400296 return Make(this->childProcessor(0).clone(), fColor);
Brian Salomon216f2e02017-07-25 15:52:51 -0400297 }
298
299 private:
egdaniel57d3b032015-11-13 11:57:27 -0800300 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800301 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700302 public:
303 GLFP() : fHaveSetColor(false) {}
304 void emitCode(EmitArgs& args) override {
305 const char* colorName;
cdalton5e58cee2016-02-11 12:49:47 -0800306 fColorUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400307 kHalf4_GrSLType,
cdalton5e58cee2016-02-11 12:49:47 -0800308 "Color", &colorName);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700309 this->emitChild(0, colorName, args);
310 }
311
312 private:
egdaniel018fb622015-10-28 07:26:40 -0700313 void onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400314 const GrFragmentProcessor& fp) override {
brianosman4cea3b92016-09-08 09:33:50 -0700315 GrColor4f color = fp.cast<ReplaceInputFragmentProcessor>().fColor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700316 if (!fHaveSetColor || color != fPreviousColor) {
brianosman4cea3b92016-09-08 09:33:50 -0700317 pdman.set4fv(fColorUni, 1, color.fRGBA);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700318 fPreviousColor = color;
319 fHaveSetColor = true;
320 }
321 }
322
egdaniel018fb622015-10-28 07:26:40 -0700323 GrGLSLProgramDataManager::UniformHandle fColorUni;
brianosman4cea3b92016-09-08 09:33:50 -0700324 bool fHaveSetColor;
325 GrColor4f fPreviousColor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700326 };
327
328 return new GLFP;
329 }
330
Brian Salomonaff329b2017-08-11 09:40:37 -0400331 ReplaceInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child, GrColor4f color)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400332 : INHERITED(kReplaceInputFragmentProcessor_ClassID, OptFlags(child.get(), color))
333 , fColor(color) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400334 this->registerChildProcessor(std::move(child));
335 }
336
Brian Salomon587e08f2017-01-27 10:59:27 -0500337 static OptimizationFlags OptFlags(const GrFragmentProcessor* child, GrColor4f color) {
338 OptimizationFlags childFlags = child->optimizationFlags();
339 OptimizationFlags flags = kNone_OptimizationFlags;
340 if (childFlags & kConstantOutputForConstantInput_OptimizationFlag) {
341 flags |= kConstantOutputForConstantInput_OptimizationFlag;
342 }
343 if ((childFlags & kPreservesOpaqueInput_OptimizationFlag) && color.isOpaque()) {
344 flags |= kPreservesOpaqueInput_OptimizationFlag;
345 }
346 return flags;
347 }
348
Brian Salomon94efbf52016-11-29 13:43:05 -0500349 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override
egdaniel57d3b032015-11-13 11:57:27 -0800350 {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700351
352 bool onIsEqual(const GrFragmentProcessor& that) const override {
353 return fColor == that.cast<ReplaceInputFragmentProcessor>().fColor;
354 }
355
Brian Salomon587e08f2017-01-27 10:59:27 -0500356 GrColor4f constantOutputForConstantInput(GrColor4f) const override {
357 return ConstantOutputForConstantInput(this->childProcessor(0), fColor);
358 }
359
brianosman4cea3b92016-09-08 09:33:50 -0700360 GrColor4f fColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500361
362 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700363 };
364
Robert Phillips1c9686b2017-06-30 08:40:28 -0400365 if (!fp) {
366 return nullptr;
367 }
368 return ReplaceInputFragmentProcessor::Make(std::move(fp), color);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700369}
bsalomone25eea42015-09-29 06:38:55 -0700370
Brian Salomonaff329b2017-08-11 09:40:37 -0400371std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
372 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700373 class SeriesFragmentProcessor : public GrFragmentProcessor {
374 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400375 static std::unique_ptr<GrFragmentProcessor> Make(
376 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
377 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700378 }
379
380 const char* name() const override { return "Series"; }
381
Brian Salomonaff329b2017-08-11 09:40:37 -0400382 std::unique_ptr<GrFragmentProcessor> clone() const override {
383 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400384 for (int i = 0; i < this->numChildProcessors(); ++i) {
385 if (!children.push_back(this->childProcessor(i).clone())) {
386 return nullptr;
387 }
388 }
389 return Make(children.begin(), this->numChildProcessors());
390 }
391
392 private:
egdaniel57d3b032015-11-13 11:57:27 -0800393 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800394 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700395 public:
bsalomone25eea42015-09-29 06:38:55 -0700396 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700397 // First guy's input might be nil.
398 SkString temp("out0");
399 this->emitChild(0, args.fInputColor, &temp, args);
400 SkString input = temp;
401 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700402 temp.printf("out%d", i);
403 this->emitChild(i, input.c_str(), &temp, args);
404 input = temp;
405 }
406 // Last guy writes to our output variable.
407 this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
408 }
409 };
410 return new GLFP;
411 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400412
Brian Salomonaff329b2017-08-11 09:40:37 -0400413 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400414 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400415 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400416 for (int i = 0; i < cnt; ++i) {
417 this->registerChildProcessor(std::move(children[i]));
418 }
419 }
420
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500422 OptimizationFlags flags = kAll_OptimizationFlags;
423 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
424 flags &= children[i]->optimizationFlags();
425 }
426 return flags;
427 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500428 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700429
430 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
431
Brian Salomon587e08f2017-01-27 10:59:27 -0500432 GrColor4f constantOutputForConstantInput(GrColor4f color) const override {
433 int childCnt = this->numChildProcessors();
434 for (int i = 0; i < childCnt; ++i) {
435 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
436 }
437 return color;
438 }
439
440 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700441 };
442
443 if (!cnt) {
444 return nullptr;
445 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500446 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400447 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500448 }
bsalomone25eea42015-09-29 06:38:55 -0700449 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400450 GrProcessorAnalysisColor inputColor;
451 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400452 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400453 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400454 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500455 GrColor4f knownColor;
456 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
457 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400458 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500459 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500460 if (leadingFPsToEliminate == cnt) {
461 return colorFP;
462 }
463 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700464 replacementSeries.reserve(cnt);
465 replacementSeries.emplace_back(std::move(colorFP));
466 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500467 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700468 }
bungeman06ca8ec2016-06-09 08:01:03 -0700469 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700470 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400471 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700472}
bsalomona624bf32016-09-20 09:12:47 -0700473
474//////////////////////////////////////////////////////////////////////////////
475
476GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
477 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
478 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
479 }
480}
481
Chris Dalton1c548942018-05-22 13:09:48 -0600482GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
483 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
484 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
485 }
486 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
487 fFPStack.push_back(paint.getColorFragmentProcessor(i));
488 }
489}
490
bsalomona624bf32016-09-20 09:12:47 -0700491const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
492 if (fFPStack.empty()) {
493 return nullptr;
494 }
495 const GrFragmentProcessor* back = fFPStack.back();
496 fFPStack.pop_back();
497 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
498 fFPStack.push_back(&back->childProcessor(i));
499 }
500 return back;
501}
502