blob: 33cde77c3f47c85978aa72192d0b0e57715ca9a3 [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 {
Brian Salomone782f842018-07-31 13:53:11 -040022 if (this->classID() != that.classID()) {
bsalomonbf877302015-09-22 09:06:13 -070023 return false;
24 }
Brian Salomone782f842018-07-31 13:53:11 -040025 if (this->numTextureSamplers() != that.numTextureSamplers()) {
26 return false;
27 }
28 for (int i = 0; i < this->numTextureSamplers(); ++i) {
29 if (this->textureSampler(i) != that.textureSampler(i)) {
30 return false;
31 }
32 }
bsalomon7312ff82016-09-12 08:55:38 -070033 if (!this->hasSameTransforms(that)) {
bsalomonbf877302015-09-22 09:06:13 -070034 return false;
35 }
36 if (!this->onIsEqual(that)) {
37 return false;
38 }
39 if (this->numChildProcessors() != that.numChildProcessors()) {
40 return false;
41 }
42 for (int i = 0; i < this->numChildProcessors(); ++i) {
bsalomon7312ff82016-09-12 08:55:38 -070043 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070044 return false;
45 }
46 }
47 return true;
48}
49
Brian Salomone782f842018-07-31 13:53:11 -040050void GrFragmentProcessor::visitProxies(const std::function<void(GrSurfaceProxy*)>& func) {
51 GrFragmentProcessor::TextureAccessIter iter(this);
52 while (const TextureSampler* sampler = iter.next()) {
53 func(sampler->proxy());
54 }
55}
56
egdaniel57d3b032015-11-13 11:57:27 -080057GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
58 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070059 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
60 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080061 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070062 }
63 return glFragProc;
64}
65
Brian Salomone782f842018-07-31 13:53:11 -040066const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
67 SkASSERT(i >= 0 && i < fTextureSamplerCnt);
68 return this->onTextureSampler(i);
69}
70
bsalomonbf877302015-09-22 09:06:13 -070071void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070072 fCoordTransforms.push_back(transform);
Brian Salomon587e08f2017-01-27 10:59:27 -050073 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -070074 SkDEBUGCODE(transform->setInProcessor();)
bsalomonbf877302015-09-22 09:06:13 -070075}
76
Robert Phillips9bee2e52017-05-29 12:37:20 -040077bool GrFragmentProcessor::instantiate(GrResourceProvider* resourceProvider) const {
Brian Salomone782f842018-07-31 13:53:11 -040078 for (int i = 0; i < fTextureSamplerCnt; ++i) {
79 if (!this->textureSampler(i).instantiate(resourceProvider)) {
80 return false;
81 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040082 }
83
Robert Phillips9bee2e52017-05-29 12:37:20 -040084 for (int i = 0; i < this->numChildProcessors(); ++i) {
85 if (!this->childProcessor(i).instantiate(resourceProvider)) {
86 return false;
87 }
88 }
89
90 return true;
91}
92
Brian Salomonaff329b2017-08-11 09:40:37 -040093void GrFragmentProcessor::markPendingExecution() const {
Brian Salomone782f842018-07-31 13:53:11 -040094 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Brian Salomonee783962018-08-01 09:55:10 -040095 auto* ref = this->textureSampler(i).proxyRef();
96 ref->markPendingIO();
97 ref->removeRef();
Brian Salomone782f842018-07-31 13:53:11 -040098 }
Brian Salomonaff329b2017-08-11 09:40:37 -040099 for (int i = 0; i < this->numChildProcessors(); ++i) {
100 this->childProcessor(i).markPendingExecution();
101 }
102}
103
104int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
bsalomonbf877302015-09-22 09:06:13 -0700105 if (child->usesLocalCoords()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500106 fFlags |= kUsesLocalCoords_Flag;
bsalomonbf877302015-09-22 09:06:13 -0700107 }
108
bungeman06ca8ec2016-06-09 08:01:03 -0700109 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700111
bsalomonbf877302015-09-22 09:06:13 -0700112 return index;
113}
114
bsalomonbf877302015-09-22 09:06:13 -0700115bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700116 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700117 return false;
118 }
bsalomona624bf32016-09-20 09:12:47 -0700119 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700120 for (int i = 0; i < count; ++i) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500121 if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700122 return false;
123 }
124 }
125 return true;
126}
127
Mike Reed28eaed22018-02-01 11:24:53 -0500128std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400129 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700130 if (!fp) {
131 return nullptr;
132 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400133 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700134}
135
Mike Reed28eaed22018-02-01 11:24:53 -0500136std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
137 std::unique_ptr<GrFragmentProcessor> fp) {
138 if (!fp) {
139 return nullptr;
140 }
141 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
142}
143
Brian Salomonaff329b2017-08-11 09:40:37 -0400144std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
145 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700146 if (!fp) {
147 return nullptr;
148 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500149 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400150 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700151 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
152}
153
Brian Salomonaff329b2017-08-11 09:40:37 -0400154std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulOutput(
155 std::unique_ptr<GrFragmentProcessor> fp) {
Brian Osmande1a6052017-03-22 10:57:00 -0400156 if (!fp) {
157 return nullptr;
158 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400159 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500160 GrPremulInputFragmentProcessor::Make() };
Brian Osmande1a6052017-03-22 10:57:00 -0400161 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
162}
163
Brian Salomonaff329b2017-08-11 09:40:37 -0400164std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::UnpremulOutput(
165 std::unique_ptr<GrFragmentProcessor> fp) {
Brian Osmande1a6052017-03-22 10:57:00 -0400166 if (!fp) {
167 return nullptr;
168 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400169 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500170 GrUnpremulInputFragmentProcessor::Make() };
Brian Osmande1a6052017-03-22 10:57:00 -0400171 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
172}
173
Brian Salomonaff329b2017-08-11 09:40:37 -0400174std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
175 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400176 class SwizzleFragmentProcessor : public GrFragmentProcessor {
177 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400178 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
179 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400180 }
181
182 const char* name() const override { return "Swizzle"; }
183 const GrSwizzle& swizzle() const { return fSwizzle; }
184
Brian Salomonaff329b2017-08-11 09:40:37 -0400185 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400186
Brian Osmance425512017-03-22 14:37:50 -0400187 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400188 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400189 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400190 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400191
Brian Osmance425512017-03-22 14:37:50 -0400192 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
193 class GLFP : public GrGLSLFragmentProcessor {
194 public:
195 void emitCode(EmitArgs& args) override {
196 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
197 const GrSwizzle& swizzle = sfp.swizzle();
198 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
199
200 fragBuilder->codeAppendf("%s = %s.%s;",
201 args.fOutputColor, args.fInputColor, swizzle.c_str());
202 }
203 };
204 return new GLFP;
205 }
206
207 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
208 b->add32(fSwizzle.asKey());
209 }
210
211 bool onIsEqual(const GrFragmentProcessor& other) const override {
212 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
213 return fSwizzle == sfp.fSwizzle;
214 }
215
216 GrColor4f constantOutputForConstantInput(GrColor4f input) const override {
217 return fSwizzle.applyTo(input);
218 }
219
220 GrSwizzle fSwizzle;
221
222 typedef GrFragmentProcessor INHERITED;
223 };
224
225 if (!fp) {
226 return nullptr;
227 }
228 if (GrSwizzle::RGBA() == swizzle) {
229 return fp;
230 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400231 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
232 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400233 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
234}
235
Brian Salomonaff329b2017-08-11 09:40:37 -0400236std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
237 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700238 class PremulFragmentProcessor : public GrFragmentProcessor {
239 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400240 static std::unique_ptr<GrFragmentProcessor> Make(
241 std::unique_ptr<GrFragmentProcessor> processor) {
242 return std::unique_ptr<GrFragmentProcessor>(
243 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400244 }
245
246 const char* name() const override { return "Premultiply"; }
247
Brian Salomonaff329b2017-08-11 09:40:37 -0400248 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400249 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400250 }
251
Robert Phillips1c9686b2017-06-30 08:40:28 -0400252 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400253 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400254 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400255 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700256 }
257
egdaniel57d3b032015-11-13 11:57:27 -0800258 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800259 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700260 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700261 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800262 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400263 this->emitChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800264 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700265 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800266 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700267 }
268 };
269 return new GLFP;
270 }
271
Brian Salomon94efbf52016-11-29 13:43:05 -0500272 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700273
274 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
275
Brian Salomon587e08f2017-01-27 10:59:27 -0500276 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
277 OptimizationFlags flags = kNone_OptimizationFlags;
278 if (inner->preservesOpaqueInput()) {
279 flags |= kPreservesOpaqueInput_OptimizationFlag;
280 }
281 if (inner->hasConstantOutputForConstantInput()) {
282 flags |= kConstantOutputForConstantInput_OptimizationFlag;
283 }
284 return flags;
285 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700286
Brian Salomon587e08f2017-01-27 10:59:27 -0500287 GrColor4f constantOutputForConstantInput(GrColor4f input) const override {
288 GrColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
289 GrColor4f::OpaqueWhite());
290 return GrColor4f(input.fRGBA[3] * input.fRGBA[0] * childColor.fRGBA[0],
291 input.fRGBA[3] * input.fRGBA[1] * childColor.fRGBA[1],
292 input.fRGBA[3] * input.fRGBA[2] * childColor.fRGBA[2],
293 input.fRGBA[3] * childColor.fRGBA[3]);
294 }
295
296 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700297 };
298 if (!fp) {
299 return nullptr;
300 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400301 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700302}
303
304//////////////////////////////////////////////////////////////////////////////
305
Brian Salomonaff329b2017-08-11 09:40:37 -0400306std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
307 std::unique_ptr<GrFragmentProcessor> fp, GrColor4f color) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700308 class ReplaceInputFragmentProcessor : public GrFragmentProcessor {
309 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400310 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child,
311 GrColor4f color) {
312 return std::unique_ptr<GrFragmentProcessor>(
313 new ReplaceInputFragmentProcessor(std::move(child), color));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700314 }
315
316 const char* name() const override { return "Replace Color"; }
317
Brian Salomonaff329b2017-08-11 09:40:37 -0400318 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400319 return Make(this->childProcessor(0).clone(), fColor);
Brian Salomon216f2e02017-07-25 15:52:51 -0400320 }
321
322 private:
egdaniel57d3b032015-11-13 11:57:27 -0800323 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800324 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700325 public:
326 GLFP() : fHaveSetColor(false) {}
327 void emitCode(EmitArgs& args) override {
328 const char* colorName;
cdalton5e58cee2016-02-11 12:49:47 -0800329 fColorUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400330 kHalf4_GrSLType,
cdalton5e58cee2016-02-11 12:49:47 -0800331 "Color", &colorName);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700332 this->emitChild(0, colorName, args);
333 }
334
335 private:
egdaniel018fb622015-10-28 07:26:40 -0700336 void onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400337 const GrFragmentProcessor& fp) override {
brianosman4cea3b92016-09-08 09:33:50 -0700338 GrColor4f color = fp.cast<ReplaceInputFragmentProcessor>().fColor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700339 if (!fHaveSetColor || color != fPreviousColor) {
brianosman4cea3b92016-09-08 09:33:50 -0700340 pdman.set4fv(fColorUni, 1, color.fRGBA);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700341 fPreviousColor = color;
342 fHaveSetColor = true;
343 }
344 }
345
egdaniel018fb622015-10-28 07:26:40 -0700346 GrGLSLProgramDataManager::UniformHandle fColorUni;
brianosman4cea3b92016-09-08 09:33:50 -0700347 bool fHaveSetColor;
348 GrColor4f fPreviousColor;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700349 };
350
351 return new GLFP;
352 }
353
Brian Salomonaff329b2017-08-11 09:40:37 -0400354 ReplaceInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child, GrColor4f color)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400355 : INHERITED(kReplaceInputFragmentProcessor_ClassID, OptFlags(child.get(), color))
356 , fColor(color) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400357 this->registerChildProcessor(std::move(child));
358 }
359
Brian Salomon587e08f2017-01-27 10:59:27 -0500360 static OptimizationFlags OptFlags(const GrFragmentProcessor* child, GrColor4f color) {
361 OptimizationFlags childFlags = child->optimizationFlags();
362 OptimizationFlags flags = kNone_OptimizationFlags;
363 if (childFlags & kConstantOutputForConstantInput_OptimizationFlag) {
364 flags |= kConstantOutputForConstantInput_OptimizationFlag;
365 }
366 if ((childFlags & kPreservesOpaqueInput_OptimizationFlag) && color.isOpaque()) {
367 flags |= kPreservesOpaqueInput_OptimizationFlag;
368 }
369 return flags;
370 }
371
Brian Salomon94efbf52016-11-29 13:43:05 -0500372 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override
egdaniel57d3b032015-11-13 11:57:27 -0800373 {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700374
375 bool onIsEqual(const GrFragmentProcessor& that) const override {
376 return fColor == that.cast<ReplaceInputFragmentProcessor>().fColor;
377 }
378
Brian Salomon587e08f2017-01-27 10:59:27 -0500379 GrColor4f constantOutputForConstantInput(GrColor4f) const override {
380 return ConstantOutputForConstantInput(this->childProcessor(0), fColor);
381 }
382
brianosman4cea3b92016-09-08 09:33:50 -0700383 GrColor4f fColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500384
385 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700386 };
387
Robert Phillips1c9686b2017-06-30 08:40:28 -0400388 if (!fp) {
389 return nullptr;
390 }
391 return ReplaceInputFragmentProcessor::Make(std::move(fp), color);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700392}
bsalomone25eea42015-09-29 06:38:55 -0700393
Brian Salomonaff329b2017-08-11 09:40:37 -0400394std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
395 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700396 class SeriesFragmentProcessor : public GrFragmentProcessor {
397 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400398 static std::unique_ptr<GrFragmentProcessor> Make(
399 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
400 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700401 }
402
403 const char* name() const override { return "Series"; }
404
Brian Salomonaff329b2017-08-11 09:40:37 -0400405 std::unique_ptr<GrFragmentProcessor> clone() const override {
406 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400407 for (int i = 0; i < this->numChildProcessors(); ++i) {
408 if (!children.push_back(this->childProcessor(i).clone())) {
409 return nullptr;
410 }
411 }
412 return Make(children.begin(), this->numChildProcessors());
413 }
414
415 private:
egdaniel57d3b032015-11-13 11:57:27 -0800416 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800417 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700418 public:
bsalomone25eea42015-09-29 06:38:55 -0700419 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700420 // First guy's input might be nil.
421 SkString temp("out0");
422 this->emitChild(0, args.fInputColor, &temp, args);
423 SkString input = temp;
424 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700425 temp.printf("out%d", i);
426 this->emitChild(i, input.c_str(), &temp, args);
427 input = temp;
428 }
429 // Last guy writes to our output variable.
430 this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
431 }
432 };
433 return new GLFP;
434 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400435
Brian Salomonaff329b2017-08-11 09:40:37 -0400436 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400437 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400438 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400439 for (int i = 0; i < cnt; ++i) {
440 this->registerChildProcessor(std::move(children[i]));
441 }
442 }
443
Brian Salomonaff329b2017-08-11 09:40:37 -0400444 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500445 OptimizationFlags flags = kAll_OptimizationFlags;
446 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
447 flags &= children[i]->optimizationFlags();
448 }
449 return flags;
450 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500451 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700452
453 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
454
Brian Salomon587e08f2017-01-27 10:59:27 -0500455 GrColor4f constantOutputForConstantInput(GrColor4f color) const override {
456 int childCnt = this->numChildProcessors();
457 for (int i = 0; i < childCnt; ++i) {
458 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
459 }
460 return color;
461 }
462
463 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700464 };
465
466 if (!cnt) {
467 return nullptr;
468 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500469 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400470 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500471 }
bsalomone25eea42015-09-29 06:38:55 -0700472 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400473 GrProcessorAnalysisColor inputColor;
474 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400475 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400476 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400477 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500478 GrColor4f knownColor;
479 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
480 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400481 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500482 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500483 if (leadingFPsToEliminate == cnt) {
484 return colorFP;
485 }
486 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700487 replacementSeries.reserve(cnt);
488 replacementSeries.emplace_back(std::move(colorFP));
489 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500490 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700491 }
bungeman06ca8ec2016-06-09 08:01:03 -0700492 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700493 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400494 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700495}
bsalomona624bf32016-09-20 09:12:47 -0700496
497//////////////////////////////////////////////////////////////////////////////
498
499GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
500 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
501 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
502 }
503}
504
Chris Dalton1c548942018-05-22 13:09:48 -0600505GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
506 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
507 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
508 }
509 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
510 fFPStack.push_back(paint.getColorFragmentProcessor(i));
511 }
512}
513
bsalomona624bf32016-09-20 09:12:47 -0700514const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
515 if (fFPStack.empty()) {
516 return nullptr;
517 }
518 const GrFragmentProcessor* back = fFPStack.back();
519 fFPStack.pop_back();
520 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
521 fFPStack.push_back(&back->childProcessor(i));
522 }
523 return back;
524}
525
Brian Salomone782f842018-07-31 13:53:11 -0400526///////////////////////////////////////////////////////////////////////////////////////////////////
527
528GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
529 const GrSamplerState& samplerState) {
530 this->reset(std::move(proxy), samplerState);
531}
532
533GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
534 GrSamplerState::Filter filterMode,
535 GrSamplerState::WrapMode wrapXAndY) {
536 this->reset(std::move(proxy), filterMode, wrapXAndY);
537}
538
539void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
540 const GrSamplerState& samplerState) {
541 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
542 fSamplerState = samplerState;
543 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
544}
545
546void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
547 GrSamplerState::Filter filterMode,
548 GrSamplerState::WrapMode wrapXAndY) {
549 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
550 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
551 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
552}