blob: 550134bd3d0c9348104f920de6ac50956041eac6 [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"
egdaniel64c47282015-11-13 06:54:19 -080015#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080016#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070017#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080018#include "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 }
bsalomon7312ff82016-09-12 08:55:38 -070032 if (!this->hasSameTransforms(that)) {
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
Brian Salomone782f842018-07-31 13:53:11 -040049void GrFragmentProcessor::visitProxies(const std::function<void(GrSurfaceProxy*)>& func) {
50 GrFragmentProcessor::TextureAccessIter iter(this);
51 while (const TextureSampler* sampler = iter.next()) {
52 func(sampler->proxy());
53 }
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
bsalomonbf877302015-09-22 09:06:13 -070070void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070071 fCoordTransforms.push_back(transform);
Brian Salomon587e08f2017-01-27 10:59:27 -050072 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070073 SkDEBUGCODE(transform->setInProcessor();)
bsalomonbf877302015-09-22 09:06:13 -070074}
75
Robert Phillips9bee2e52017-05-29 12:37:20 -040076bool GrFragmentProcessor::instantiate(GrResourceProvider* resourceProvider) const {
Brian Salomone782f842018-07-31 13:53:11 -040077 for (int i = 0; i < fTextureSamplerCnt; ++i) {
78 if (!this->textureSampler(i).instantiate(resourceProvider)) {
79 return false;
80 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040081 }
82
Robert Phillips9bee2e52017-05-29 12:37:20 -040083 for (int i = 0; i < this->numChildProcessors(); ++i) {
84 if (!this->childProcessor(i).instantiate(resourceProvider)) {
85 return false;
86 }
87 }
88
89 return true;
90}
91
Brian Salomonaff329b2017-08-11 09:40:37 -040092void GrFragmentProcessor::markPendingExecution() const {
Brian Salomone782f842018-07-31 13:53:11 -040093 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Brian Salomonee783962018-08-01 09:55:10 -040094 auto* ref = this->textureSampler(i).proxyRef();
95 ref->markPendingIO();
96 ref->removeRef();
Brian Salomone782f842018-07-31 13:53:11 -040097 }
Brian Salomonaff329b2017-08-11 09:40:37 -040098 for (int i = 0; i < this->numChildProcessors(); ++i) {
99 this->childProcessor(i).markPendingExecution();
100 }
101}
102
103int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
bsalomonbf877302015-09-22 09:06:13 -0700104 if (child->usesLocalCoords()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500105 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700106 }
107
bungeman06ca8ec2016-06-09 08:01:03 -0700108 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400109 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700110
bsalomonbf877302015-09-22 09:06:13 -0700111 return index;
112}
113
bsalomonbf877302015-09-22 09:06:13 -0700114bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700115 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700116 return false;
117 }
bsalomona624bf32016-09-20 09:12:47 -0700118 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700119 for (int i = 0; i < count; ++i) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500120 if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700121 return false;
122 }
123 }
124 return true;
125}
126
Mike Reed28eaed22018-02-01 11:24:53 -0500127std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400128 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700129 if (!fp) {
130 return nullptr;
131 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400132 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700133}
134
Mike Reed28eaed22018-02-01 11:24:53 -0500135std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
136 std::unique_ptr<GrFragmentProcessor> fp) {
137 if (!fp) {
138 return nullptr;
139 }
140 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
141}
142
Brian Salomonaff329b2017-08-11 09:40:37 -0400143std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
144 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700145 if (!fp) {
146 return nullptr;
147 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500148 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400149 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700150 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
151}
152
Brian Salomonaff329b2017-08-11 09:40:37 -0400153std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
154 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400155 class SwizzleFragmentProcessor : public GrFragmentProcessor {
156 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400157 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
158 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400159 }
160
161 const char* name() const override { return "Swizzle"; }
162 const GrSwizzle& swizzle() const { return fSwizzle; }
163
Brian Salomonaff329b2017-08-11 09:40:37 -0400164 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400165
Brian Osmance425512017-03-22 14:37:50 -0400166 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400167 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400168 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400169 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400170
Brian Osmance425512017-03-22 14:37:50 -0400171 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
172 class GLFP : public GrGLSLFragmentProcessor {
173 public:
174 void emitCode(EmitArgs& args) override {
175 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
176 const GrSwizzle& swizzle = sfp.swizzle();
177 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
178
179 fragBuilder->codeAppendf("%s = %s.%s;",
180 args.fOutputColor, args.fInputColor, swizzle.c_str());
181 }
182 };
183 return new GLFP;
184 }
185
186 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
187 b->add32(fSwizzle.asKey());
188 }
189
190 bool onIsEqual(const GrFragmentProcessor& other) const override {
191 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
192 return fSwizzle == sfp.fSwizzle;
193 }
194
Brian Osman1d5b5982018-10-01 13:41:39 -0400195 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400196 return fSwizzle.applyTo(input);
197 }
198
199 GrSwizzle fSwizzle;
200
201 typedef GrFragmentProcessor INHERITED;
202 };
203
204 if (!fp) {
205 return nullptr;
206 }
207 if (GrSwizzle::RGBA() == swizzle) {
208 return fp;
209 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400210 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
211 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400212 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
213}
214
Brian Salomonaff329b2017-08-11 09:40:37 -0400215std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
216 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700217 class PremulFragmentProcessor : public GrFragmentProcessor {
218 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400219 static std::unique_ptr<GrFragmentProcessor> Make(
220 std::unique_ptr<GrFragmentProcessor> processor) {
221 return std::unique_ptr<GrFragmentProcessor>(
222 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400223 }
224
225 const char* name() const override { return "Premultiply"; }
226
Brian Salomonaff329b2017-08-11 09:40:37 -0400227 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400228 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400229 }
230
Robert Phillips1c9686b2017-06-30 08:40:28 -0400231 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400232 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400233 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400234 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700235 }
236
egdaniel57d3b032015-11-13 11:57:27 -0800237 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800238 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700239 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700240 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800241 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400242 this->emitChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800243 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700244 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800245 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700246 }
247 };
248 return new GLFP;
249 }
250
Brian Salomon94efbf52016-11-29 13:43:05 -0500251 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700252
253 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
254
Brian Salomon587e08f2017-01-27 10:59:27 -0500255 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
256 OptimizationFlags flags = kNone_OptimizationFlags;
257 if (inner->preservesOpaqueInput()) {
258 flags |= kPreservesOpaqueInput_OptimizationFlag;
259 }
260 if (inner->hasConstantOutputForConstantInput()) {
261 flags |= kConstantOutputForConstantInput_OptimizationFlag;
262 }
263 return flags;
264 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700265
Brian Osman1d5b5982018-10-01 13:41:39 -0400266 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
267 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400268 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400269 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
270 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500271 }
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 Osman1d5b5982018-10-01 13:41:39 -0400356 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f&) const override {
357 return ConstantOutputForConstantInput(this->childProcessor(0),
358 fColor.asRGBA4f<kPremul_SkAlphaType>());
Brian Salomon587e08f2017-01-27 10:59:27 -0500359 }
360
brianosman4cea3b92016-09-08 09:33:50 -0700361 GrColor4f fColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500362
363 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700364 };
365
Robert Phillips1c9686b2017-06-30 08:40:28 -0400366 if (!fp) {
367 return nullptr;
368 }
369 return ReplaceInputFragmentProcessor::Make(std::move(fp), color);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700370}
bsalomone25eea42015-09-29 06:38:55 -0700371
Brian Salomonaff329b2017-08-11 09:40:37 -0400372std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
373 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700374 class SeriesFragmentProcessor : public GrFragmentProcessor {
375 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400376 static std::unique_ptr<GrFragmentProcessor> Make(
377 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
378 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700379 }
380
381 const char* name() const override { return "Series"; }
382
Brian Salomonaff329b2017-08-11 09:40:37 -0400383 std::unique_ptr<GrFragmentProcessor> clone() const override {
384 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400385 for (int i = 0; i < this->numChildProcessors(); ++i) {
386 if (!children.push_back(this->childProcessor(i).clone())) {
387 return nullptr;
388 }
389 }
390 return Make(children.begin(), this->numChildProcessors());
391 }
392
393 private:
egdaniel57d3b032015-11-13 11:57:27 -0800394 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800395 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700396 public:
bsalomone25eea42015-09-29 06:38:55 -0700397 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700398 // First guy's input might be nil.
399 SkString temp("out0");
400 this->emitChild(0, args.fInputColor, &temp, args);
401 SkString input = temp;
402 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700403 temp.printf("out%d", i);
404 this->emitChild(i, input.c_str(), &temp, args);
405 input = temp;
406 }
407 // Last guy writes to our output variable.
408 this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
409 }
410 };
411 return new GLFP;
412 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400413
Brian Salomonaff329b2017-08-11 09:40:37 -0400414 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400415 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400416 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400417 for (int i = 0; i < cnt; ++i) {
418 this->registerChildProcessor(std::move(children[i]));
419 }
420 }
421
Brian Salomonaff329b2017-08-11 09:40:37 -0400422 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500423 OptimizationFlags flags = kAll_OptimizationFlags;
424 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
425 flags &= children[i]->optimizationFlags();
426 }
427 return flags;
428 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500429 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700430
431 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
432
Brian Osman1d5b5982018-10-01 13:41:39 -0400433 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
434 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500435 int childCnt = this->numChildProcessors();
436 for (int i = 0; i < childCnt; ++i) {
437 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
438 }
439 return color;
440 }
441
442 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700443 };
444
445 if (!cnt) {
446 return nullptr;
447 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500448 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400449 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500450 }
bsalomone25eea42015-09-29 06:38:55 -0700451 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400452 GrProcessorAnalysisColor inputColor;
453 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400454 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400455 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400456 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400457 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500458 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
459 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400460 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500461 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500462 if (leadingFPsToEliminate == cnt) {
463 return colorFP;
464 }
465 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700466 replacementSeries.reserve(cnt);
467 replacementSeries.emplace_back(std::move(colorFP));
468 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500469 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700470 }
bungeman06ca8ec2016-06-09 08:01:03 -0700471 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700472 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400473 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700474}
bsalomona624bf32016-09-20 09:12:47 -0700475
476//////////////////////////////////////////////////////////////////////////////
477
478GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
479 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
480 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
481 }
482}
483
Chris Dalton1c548942018-05-22 13:09:48 -0600484GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
485 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
486 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
487 }
488 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
489 fFPStack.push_back(paint.getColorFragmentProcessor(i));
490 }
491}
492
bsalomona624bf32016-09-20 09:12:47 -0700493const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
494 if (fFPStack.empty()) {
495 return nullptr;
496 }
497 const GrFragmentProcessor* back = fFPStack.back();
498 fFPStack.pop_back();
499 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
500 fFPStack.push_back(&back->childProcessor(i));
501 }
502 return back;
503}
504
Brian Salomone782f842018-07-31 13:53:11 -0400505///////////////////////////////////////////////////////////////////////////////////////////////////
506
507GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
508 const GrSamplerState& samplerState) {
509 this->reset(std::move(proxy), samplerState);
510}
511
512GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
513 GrSamplerState::Filter filterMode,
514 GrSamplerState::WrapMode wrapXAndY) {
515 this->reset(std::move(proxy), filterMode, wrapXAndY);
516}
517
518void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
519 const GrSamplerState& samplerState) {
520 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
521 fSamplerState = samplerState;
522 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
523}
524
525void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
526 GrSamplerState::Filter filterMode,
527 GrSamplerState::WrapMode wrapXAndY) {
528 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
529 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
530 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
531}