blob: eff741906ed468215fa0f4a7d8fcd16ff1694628 [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(
Brian Osman080e77f2018-10-04 10:57:29 -0400284 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& 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,
Brian Osman080e77f2018-10-04 10:57:29 -0400288 const SkPMColor4f& color) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400289 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 {
Brian Osman080e77f2018-10-04 10:57:29 -0400315 SkPMColor4f color = fp.cast<ReplaceInputFragmentProcessor>().fColor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700316 if (!fHaveSetColor || color != fPreviousColor) {
Brian Osman080e77f2018-10-04 10:57:29 -0400317 pdman.set4fv(fColorUni, 1, color.vec());
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;
Brian Osman080e77f2018-10-04 10:57:29 -0400324 bool fHaveSetColor;
325 SkPMColor4f fPreviousColor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700326 };
327
328 return new GLFP;
329 }
330
Brian Osman080e77f2018-10-04 10:57:29 -0400331 ReplaceInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child,
332 const SkPMColor4f& color)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400333 : INHERITED(kReplaceInputFragmentProcessor_ClassID, OptFlags(child.get(), color))
334 , fColor(color) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400335 this->registerChildProcessor(std::move(child));
336 }
337
Brian Osman080e77f2018-10-04 10:57:29 -0400338 static OptimizationFlags OptFlags(const GrFragmentProcessor* child,
339 const SkPMColor4f& color) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500340 OptimizationFlags childFlags = child->optimizationFlags();
341 OptimizationFlags flags = kNone_OptimizationFlags;
342 if (childFlags & kConstantOutputForConstantInput_OptimizationFlag) {
343 flags |= kConstantOutputForConstantInput_OptimizationFlag;
344 }
345 if ((childFlags & kPreservesOpaqueInput_OptimizationFlag) && color.isOpaque()) {
346 flags |= kPreservesOpaqueInput_OptimizationFlag;
347 }
348 return flags;
349 }
350
Brian Salomon94efbf52016-11-29 13:43:05 -0500351 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override
egdaniel57d3b032015-11-13 11:57:27 -0800352 {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700353
354 bool onIsEqual(const GrFragmentProcessor& that) const override {
355 return fColor == that.cast<ReplaceInputFragmentProcessor>().fColor;
356 }
357
Brian Osman1d5b5982018-10-01 13:41:39 -0400358 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f&) const override {
Brian Osman080e77f2018-10-04 10:57:29 -0400359 return ConstantOutputForConstantInput(this->childProcessor(0), fColor);
Brian Salomon587e08f2017-01-27 10:59:27 -0500360 }
361
Brian Osman080e77f2018-10-04 10:57:29 -0400362 SkPMColor4f fColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500363
364 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700365 };
366
Robert Phillips1c9686b2017-06-30 08:40:28 -0400367 if (!fp) {
368 return nullptr;
369 }
370 return ReplaceInputFragmentProcessor::Make(std::move(fp), color);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700371}
bsalomone25eea42015-09-29 06:38:55 -0700372
Brian Salomonaff329b2017-08-11 09:40:37 -0400373std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
374 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700375 class SeriesFragmentProcessor : public GrFragmentProcessor {
376 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400377 static std::unique_ptr<GrFragmentProcessor> Make(
378 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
379 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700380 }
381
382 const char* name() const override { return "Series"; }
383
Brian Salomonaff329b2017-08-11 09:40:37 -0400384 std::unique_ptr<GrFragmentProcessor> clone() const override {
385 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400386 for (int i = 0; i < this->numChildProcessors(); ++i) {
387 if (!children.push_back(this->childProcessor(i).clone())) {
388 return nullptr;
389 }
390 }
391 return Make(children.begin(), this->numChildProcessors());
392 }
393
394 private:
egdaniel57d3b032015-11-13 11:57:27 -0800395 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800396 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700397 public:
bsalomone25eea42015-09-29 06:38:55 -0700398 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700399 // First guy's input might be nil.
400 SkString temp("out0");
401 this->emitChild(0, args.fInputColor, &temp, args);
402 SkString input = temp;
403 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700404 temp.printf("out%d", i);
405 this->emitChild(i, input.c_str(), &temp, args);
406 input = temp;
407 }
408 // Last guy writes to our output variable.
409 this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
410 }
411 };
412 return new GLFP;
413 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400414
Brian Salomonaff329b2017-08-11 09:40:37 -0400415 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400416 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400417 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400418 for (int i = 0; i < cnt; ++i) {
419 this->registerChildProcessor(std::move(children[i]));
420 }
421 }
422
Brian Salomonaff329b2017-08-11 09:40:37 -0400423 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500424 OptimizationFlags flags = kAll_OptimizationFlags;
425 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
426 flags &= children[i]->optimizationFlags();
427 }
428 return flags;
429 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500430 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700431
432 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
433
Brian Osman1d5b5982018-10-01 13:41:39 -0400434 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
435 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500436 int childCnt = this->numChildProcessors();
437 for (int i = 0; i < childCnt; ++i) {
438 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
439 }
440 return color;
441 }
442
443 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700444 };
445
446 if (!cnt) {
447 return nullptr;
448 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500449 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400450 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500451 }
bsalomone25eea42015-09-29 06:38:55 -0700452 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400453 GrProcessorAnalysisColor inputColor;
454 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400455 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400456 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400457 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400458 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500459 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
460 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400461 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500462 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500463 if (leadingFPsToEliminate == cnt) {
464 return colorFP;
465 }
466 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700467 replacementSeries.reserve(cnt);
468 replacementSeries.emplace_back(std::move(colorFP));
469 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500470 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700471 }
bungeman06ca8ec2016-06-09 08:01:03 -0700472 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700473 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400474 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700475}
bsalomona624bf32016-09-20 09:12:47 -0700476
477//////////////////////////////////////////////////////////////////////////////
478
479GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
480 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
481 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
482 }
483}
484
Chris Dalton1c548942018-05-22 13:09:48 -0600485GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
486 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
487 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
488 }
489 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
490 fFPStack.push_back(paint.getColorFragmentProcessor(i));
491 }
492}
493
bsalomona624bf32016-09-20 09:12:47 -0700494const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
495 if (fFPStack.empty()) {
496 return nullptr;
497 }
498 const GrFragmentProcessor* back = fFPStack.back();
499 fFPStack.pop_back();
500 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
501 fFPStack.push_back(&back->childProcessor(i));
502 }
503 return back;
504}
505
Brian Salomone782f842018-07-31 13:53:11 -0400506///////////////////////////////////////////////////////////////////////////////////////////////////
507
508GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
509 const GrSamplerState& samplerState) {
510 this->reset(std::move(proxy), samplerState);
511}
512
513GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
514 GrSamplerState::Filter filterMode,
515 GrSamplerState::WrapMode wrapXAndY) {
516 this->reset(std::move(proxy), filterMode, wrapXAndY);
517}
518
519void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
520 const GrSamplerState& samplerState) {
521 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
522 fSamplerState = samplerState;
523 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
524}
525
526void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
527 GrSamplerState::Filter filterMode,
528 GrSamplerState::WrapMode wrapXAndY) {
529 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
530 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
531 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
532}