blob: 96a9ce918a1e08876f63e2f908fff3300243f583 [file] [log] [blame]
bsalomonbf877302015-09-22 09:06:13 -07001/*
2* Copyright 2015 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrCoordTransform.h"
9#include "src/gpu/GrFragmentProcessor.h"
10#include "src/gpu/GrPipeline.h"
11#include "src/gpu/GrProcessorAnalysis.h"
12#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050013#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/effects/generated/GrConstColorProcessor.h"
15#include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h"
16#include "src/gpu/effects/generated/GrPremulInputFragmentProcessor.h"
17#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
18#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
19#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
20#include "src/gpu/glsl/GrGLSLUniformHandler.h"
bsalomonbf877302015-09-22 09:06:13 -070021
bsalomon7312ff82016-09-12 08:55:38 -070022bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
Brian Salomone782f842018-07-31 13:53:11 -040023 if (this->classID() != that.classID()) {
bsalomonbf877302015-09-22 09:06:13 -070024 return false;
25 }
Brian Salomone782f842018-07-31 13:53:11 -040026 if (this->numTextureSamplers() != that.numTextureSamplers()) {
27 return false;
28 }
29 for (int i = 0; i < this->numTextureSamplers(); ++i) {
30 if (this->textureSampler(i) != that.textureSampler(i)) {
31 return false;
32 }
33 }
bsalomon7312ff82016-09-12 08:55:38 -070034 if (!this->hasSameTransforms(that)) {
bsalomonbf877302015-09-22 09:06:13 -070035 return false;
36 }
37 if (!this->onIsEqual(that)) {
38 return false;
39 }
40 if (this->numChildProcessors() != that.numChildProcessors()) {
41 return false;
42 }
43 for (int i = 0; i < this->numChildProcessors(); ++i) {
bsalomon7312ff82016-09-12 08:55:38 -070044 if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
bsalomonbf877302015-09-22 09:06:13 -070045 return false;
46 }
47 }
48 return true;
49}
50
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060051void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) {
Brian Salomonc241b582019-11-27 08:57:17 -050052 for (auto [sampler, fp] : FPTextureSamplerRange(*this)) {
53 bool mipped = (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter());
Robert Phillipsbd99c0c2019-12-12 13:26:58 +000054 func(sampler.view().proxy(), GrMipMapped(mipped));
Brian Salomone782f842018-07-31 13:53:11 -040055 }
56}
57
egdaniel57d3b032015-11-13 11:57:27 -080058GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
59 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070060 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
61 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080062 glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
bsalomonbf877302015-09-22 09:06:13 -070063 }
64 return glFragProc;
65}
66
Brian Salomone782f842018-07-31 13:53:11 -040067const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
68 SkASSERT(i >= 0 && i < fTextureSamplerCnt);
69 return this->onTextureSampler(i);
70}
71
Ethan Nicholasd4efe682019-08-29 16:10:13 -040072void GrFragmentProcessor::addCoordTransform(GrCoordTransform* transform) {
bsalomonbf877302015-09-22 09:06:13 -070073 fCoordTransforms.push_back(transform);
Brian Salomon7d8b3972019-11-26 22:34:44 -050074 fFlags |= kHasCoordTranforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -070075}
76
Robert Phillips82774f82019-06-20 14:38:27 -040077#ifdef SK_DEBUG
78bool GrFragmentProcessor::isInstantiated() const {
Brian Salomone782f842018-07-31 13:53:11 -040079 for (int i = 0; i < fTextureSamplerCnt; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040080 if (!this->textureSampler(i).isInstantiated()) {
Brian Salomone782f842018-07-31 13:53:11 -040081 return false;
82 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -040083 }
84
Robert Phillips9bee2e52017-05-29 12:37:20 -040085 for (int i = 0; i < this->numChildProcessors(); ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -040086 if (!this->childProcessor(i).isInstantiated()) {
Robert Phillips9bee2e52017-05-29 12:37:20 -040087 return false;
88 }
89 }
90
91 return true;
92}
Robert Phillips82774f82019-06-20 14:38:27 -040093#endif
Robert Phillips9bee2e52017-05-29 12:37:20 -040094
Brian Salomonaff329b2017-08-11 09:40:37 -040095int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
Brian Salomon7d8b3972019-11-26 22:34:44 -050096 if (child->fFlags & kHasCoordTranforms_Flag) {
97 fFlags |= kHasCoordTranforms_Flag;
bsalomonbf877302015-09-22 09:06:13 -070098 }
Chris Daltond7291ba2019-03-07 14:17:03 -070099 fRequestedFeatures |= child->fRequestedFeatures;
bsalomonbf877302015-09-22 09:06:13 -0700100
bungeman06ca8ec2016-06-09 08:01:03 -0700101 int index = fChildProcessors.count();
Brian Salomonaff329b2017-08-11 09:40:37 -0400102 fChildProcessors.push_back(std::move(child));
bungeman06ca8ec2016-06-09 08:01:03 -0700103
bsalomonbf877302015-09-22 09:06:13 -0700104 return index;
105}
106
bsalomonbf877302015-09-22 09:06:13 -0700107bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
bsalomona624bf32016-09-20 09:12:47 -0700108 if (this->numCoordTransforms() != that.numCoordTransforms()) {
bsalomonbf877302015-09-22 09:06:13 -0700109 return false;
110 }
bsalomona624bf32016-09-20 09:12:47 -0700111 int count = this->numCoordTransforms();
bsalomonbf877302015-09-22 09:06:13 -0700112 for (int i = 0; i < count; ++i) {
Brian Salomon7d8b3972019-11-26 22:34:44 -0500113 if (!this->coordTransform(i).hasSameEffectiveMatrix(that.coordTransform(i))) {
bsalomonbf877302015-09-22 09:06:13 -0700114 return false;
115 }
116 }
117 return true;
118}
119
Mike Reed28eaed22018-02-01 11:24:53 -0500120std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
Brian Salomonaff329b2017-08-11 09:40:37 -0400121 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700122 if (!fp) {
123 return nullptr;
124 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400125 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
bsalomonbf877302015-09-22 09:06:13 -0700126}
127
Mike Reed28eaed22018-02-01 11:24:53 -0500128std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
129 std::unique_ptr<GrFragmentProcessor> fp) {
130 if (!fp) {
131 return nullptr;
132 }
133 return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
134}
135
Brian Salomonaff329b2017-08-11 09:40:37 -0400136std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
137 std::unique_ptr<GrFragmentProcessor> fp) {
dvonbeckc526da92016-07-20 11:20:30 -0700138 if (!fp) {
139 return nullptr;
140 }
Ethan Nicholasbe0a0422017-11-17 13:44:05 -0500141 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
Brian Salomonaff329b2017-08-11 09:40:37 -0400142 std::move(fp) };
dvonbeckc526da92016-07-20 11:20:30 -0700143 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
144}
145
Brian Osman6f5e9402020-01-22 10:39:31 -0500146std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
147 std::unique_ptr<GrFragmentProcessor> fp) {
148 if (!fp) {
149 return nullptr;
150 }
151 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {
152 std::move(fp),
153 GrClampFragmentProcessor::Make(true)
154 };
155 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
156}
157
Brian Salomonaff329b2017-08-11 09:40:37 -0400158std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
159 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
Brian Osmance425512017-03-22 14:37:50 -0400160 class SwizzleFragmentProcessor : public GrFragmentProcessor {
161 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400162 static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
163 return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
Brian Osmance425512017-03-22 14:37:50 -0400164 }
165
166 const char* name() const override { return "Swizzle"; }
167 const GrSwizzle& swizzle() const { return fSwizzle; }
168
Brian Salomonaff329b2017-08-11 09:40:37 -0400169 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
Brian Salomon216f2e02017-07-25 15:52:51 -0400170
Brian Osmance425512017-03-22 14:37:50 -0400171 private:
Robert Phillips1c9686b2017-06-30 08:40:28 -0400172 SwizzleFragmentProcessor(const GrSwizzle& swizzle)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400173 : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
Brian Salomonf7dcd762018-07-30 14:48:15 -0400174 , fSwizzle(swizzle) {}
Robert Phillips1c9686b2017-06-30 08:40:28 -0400175
Brian Osmance425512017-03-22 14:37:50 -0400176 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
177 class GLFP : public GrGLSLFragmentProcessor {
178 public:
179 void emitCode(EmitArgs& args) override {
180 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
181 const GrSwizzle& swizzle = sfp.swizzle();
182 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
183
184 fragBuilder->codeAppendf("%s = %s.%s;",
Greg Daniel369ee6b2019-12-02 15:30:02 -0500185 args.fOutputColor, args.fInputColor, swizzle.asString().c_str());
Brian Osmance425512017-03-22 14:37:50 -0400186 }
187 };
188 return new GLFP;
189 }
190
191 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
192 b->add32(fSwizzle.asKey());
193 }
194
195 bool onIsEqual(const GrFragmentProcessor& other) const override {
196 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
197 return fSwizzle == sfp.fSwizzle;
198 }
199
Brian Osman1d5b5982018-10-01 13:41:39 -0400200 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
Brian Osmance425512017-03-22 14:37:50 -0400201 return fSwizzle.applyTo(input);
202 }
203
204 GrSwizzle fSwizzle;
205
206 typedef GrFragmentProcessor INHERITED;
207 };
208
209 if (!fp) {
210 return nullptr;
211 }
212 if (GrSwizzle::RGBA() == swizzle) {
213 return fp;
214 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400215 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
216 SwizzleFragmentProcessor::Make(swizzle) };
Brian Osmance425512017-03-22 14:37:50 -0400217 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
218}
219
Brian Salomonaff329b2017-08-11 09:40:37 -0400220std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
221 std::unique_ptr<GrFragmentProcessor> fp) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700222 class PremulFragmentProcessor : public GrFragmentProcessor {
223 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400224 static std::unique_ptr<GrFragmentProcessor> Make(
225 std::unique_ptr<GrFragmentProcessor> processor) {
226 return std::unique_ptr<GrFragmentProcessor>(
227 new PremulFragmentProcessor(std::move(processor)));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400228 }
229
230 const char* name() const override { return "Premultiply"; }
231
Brian Salomonaff329b2017-08-11 09:40:37 -0400232 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400233 return Make(this->childProcessor(0).clone());
Brian Salomon216f2e02017-07-25 15:52:51 -0400234 }
235
Robert Phillips1c9686b2017-06-30 08:40:28 -0400236 private:
Brian Salomonaff329b2017-08-11 09:40:37 -0400237 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400238 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400239 this->registerChildProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700240 }
241
egdaniel57d3b032015-11-13 11:57:27 -0800242 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800243 class GLFP : public GrGLSLFragmentProcessor {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700244 public:
bsalomonf1b7a1d2015-09-28 06:26:28 -0700245 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -0800246 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400247 this->invokeChild(0, args);
egdaniel4ca2e602015-11-18 08:01:26 -0800248 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700249 args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800250 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700251 }
252 };
253 return new GLFP;
254 }
255
Brian Salomon94efbf52016-11-29 13:43:05 -0500256 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomonf1b7a1d2015-09-28 06:26:28 -0700257
258 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
259
Brian Salomon587e08f2017-01-27 10:59:27 -0500260 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
261 OptimizationFlags flags = kNone_OptimizationFlags;
262 if (inner->preservesOpaqueInput()) {
263 flags |= kPreservesOpaqueInput_OptimizationFlag;
264 }
265 if (inner->hasConstantOutputForConstantInput()) {
266 flags |= kConstantOutputForConstantInput_OptimizationFlag;
267 }
268 return flags;
269 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700270
Brian Osman1d5b5982018-10-01 13:41:39 -0400271 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
272 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
Brian Osmanf28e55d2018-10-03 16:35:54 -0400273 SK_PMColor4fWHITE);
Brian Osman1d5b5982018-10-01 13:41:39 -0400274 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
275 return premulInput * childColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500276 }
277
278 typedef GrFragmentProcessor INHERITED;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700279 };
280 if (!fp) {
281 return nullptr;
282 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400283 return PremulFragmentProcessor::Make(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700284}
285
286//////////////////////////////////////////////////////////////////////////////
287
Brian Salomonaff329b2017-08-11 09:40:37 -0400288std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
Brian Salomonc0d79e52019-04-10 15:02:11 -0400289 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400290 if (!fp) {
291 return nullptr;
292 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400293 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700294}
bsalomone25eea42015-09-29 06:38:55 -0700295
Brian Salomonaff329b2017-08-11 09:40:37 -0400296std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
297 std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
bsalomone25eea42015-09-29 06:38:55 -0700298 class SeriesFragmentProcessor : public GrFragmentProcessor {
299 public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400300 static std::unique_ptr<GrFragmentProcessor> Make(
301 std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
302 return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
bsalomone25eea42015-09-29 06:38:55 -0700303 }
304
305 const char* name() const override { return "Series"; }
306
Brian Salomonaff329b2017-08-11 09:40:37 -0400307 std::unique_ptr<GrFragmentProcessor> clone() const override {
308 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
Brian Salomon216f2e02017-07-25 15:52:51 -0400309 for (int i = 0; i < this->numChildProcessors(); ++i) {
310 if (!children.push_back(this->childProcessor(i).clone())) {
311 return nullptr;
312 }
313 }
314 return Make(children.begin(), this->numChildProcessors());
315 }
316
317 private:
egdaniel57d3b032015-11-13 11:57:27 -0800318 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -0800319 class GLFP : public GrGLSLFragmentProcessor {
bsalomone25eea42015-09-29 06:38:55 -0700320 public:
bsalomone25eea42015-09-29 06:38:55 -0700321 void emitCode(EmitArgs& args) override {
dvonbeckca9eeab2016-07-06 12:00:06 -0700322 // First guy's input might be nil.
323 SkString temp("out0");
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400324 this->invokeChild(0, args.fInputColor, &temp, args);
dvonbeckca9eeab2016-07-06 12:00:06 -0700325 SkString input = temp;
326 for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
bsalomone25eea42015-09-29 06:38:55 -0700327 temp.printf("out%d", i);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400328 this->invokeChild(i, input.c_str(), &temp, args);
bsalomone25eea42015-09-29 06:38:55 -0700329 input = temp;
330 }
331 // Last guy writes to our output variable.
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400332 this->invokeChild(this->numChildProcessors() - 1, input.c_str(), args);
bsalomone25eea42015-09-29 06:38:55 -0700333 }
334 };
335 return new GLFP;
336 }
Brian Salomon216f2e02017-07-25 15:52:51 -0400337
Brian Salomonaff329b2017-08-11 09:40:37 -0400338 SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400339 : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400340 SkASSERT(cnt > 1);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400341 for (int i = 0; i < cnt; ++i) {
342 this->registerChildProcessor(std::move(children[i]));
343 }
344 }
345
Brian Salomonaff329b2017-08-11 09:40:37 -0400346 static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500347 OptimizationFlags flags = kAll_OptimizationFlags;
348 for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
349 flags &= children[i]->optimizationFlags();
350 }
351 return flags;
352 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500353 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bsalomone25eea42015-09-29 06:38:55 -0700354
355 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
356
Brian Osman1d5b5982018-10-01 13:41:39 -0400357 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
358 SkPMColor4f color = inColor;
Brian Salomon587e08f2017-01-27 10:59:27 -0500359 int childCnt = this->numChildProcessors();
360 for (int i = 0; i < childCnt; ++i) {
361 color = ConstantOutputForConstantInput(this->childProcessor(i), color);
362 }
363 return color;
364 }
365
366 typedef GrFragmentProcessor INHERITED;
bsalomone25eea42015-09-29 06:38:55 -0700367 };
368
369 if (!cnt) {
370 return nullptr;
371 }
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500372 if (1 == cnt) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400373 return std::move(series[0]);
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500374 }
bsalomone25eea42015-09-29 06:38:55 -0700375 // Run the through the series, do the invariant output processing, and look for eliminations.
Brian Salomon650ced02017-07-20 16:46:46 -0400376 GrProcessorAnalysisColor inputColor;
377 inputColor.setToUnknown();
Brian Salomonaff329b2017-08-11 09:40:37 -0400378 GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
Brian Salomon650ced02017-07-20 16:46:46 -0400379 cnt);
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400381 SkPMColor4f knownColor;
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500382 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
383 if (leadingFPsToEliminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400384 std::unique_ptr<GrFragmentProcessor> colorFP(
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500385 GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500386 if (leadingFPsToEliminate == cnt) {
387 return colorFP;
388 }
389 cnt = cnt - leadingFPsToEliminate + 1;
bungeman06ca8ec2016-06-09 08:01:03 -0700390 replacementSeries.reserve(cnt);
391 replacementSeries.emplace_back(std::move(colorFP));
392 for (int i = 0; i < cnt - 1; ++i) {
Brian Salomoneec6f7b2017-02-10 14:29:38 -0500393 replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
bsalomone25eea42015-09-29 06:38:55 -0700394 }
bungeman06ca8ec2016-06-09 08:01:03 -0700395 series = replacementSeries.begin();
bsalomone25eea42015-09-29 06:38:55 -0700396 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400397 return SeriesFragmentProcessor::Make(series, cnt);
bsalomone25eea42015-09-29 06:38:55 -0700398}
bsalomona624bf32016-09-20 09:12:47 -0700399
400//////////////////////////////////////////////////////////////////////////////
401
Brian Salomon7eabfe82019-12-02 14:20:20 -0500402GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
Chris Dalton1c548942018-05-22 13:09:48 -0600403 for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
404 fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
405 }
406 for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
407 fFPStack.push_back(paint.getColorFragmentProcessor(i));
408 }
409}
410
Brian Salomon7eabfe82019-12-02 14:20:20 -0500411GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) {
Brian Salomonc241b582019-11-27 08:57:17 -0500412 for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
413 fFPStack.push_back(set.coverageFragmentProcessor(i));
bsalomona624bf32016-09-20 09:12:47 -0700414 }
Brian Salomonc241b582019-11-27 08:57:17 -0500415 for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) {
416 fFPStack.push_back(set.colorFragmentProcessor(i));
417 }
418}
419
Brian Salomon7eabfe82019-12-02 14:20:20 -0500420GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
Brian Salomonc241b582019-11-27 08:57:17 -0500421 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
422 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
423 }
424}
425
Brian Salomone782f842018-07-31 13:53:11 -0400426///////////////////////////////////////////////////////////////////////////////////////////////////
427
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000428GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500429 GrSamplerState samplerState)
430 : fView(std::move(view)), fSamplerState(samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000431 GrSurfaceProxy* proxy = this->proxy();
Greg Danielacf59292019-12-11 13:45:17 -0500432 fSamplerState.setFilterMode(
433 SkTMin(samplerState.filter(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000434 GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType())));
Greg Danielacf59292019-12-11 13:45:17 -0500435}
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000436
437GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrSurfaceProxy> proxy,
Brian Salomonccb61422020-01-09 10:46:36 -0500438 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000439 SkASSERT(proxy->asTextureProxy());
440 GrSurfaceOrigin origin = proxy->origin();
441 GrSwizzle swizzle = proxy->textureSwizzle();
442 fView = GrSurfaceProxyView(std::move(proxy), origin, swizzle);
443
444 fSamplerState = samplerState;
445 GrSurfaceProxy* surfProxy = this->proxy();
446 fSamplerState.setFilterMode(
447 SkTMin(samplerState.filter(),
448 GrTextureProxy::HighestFilterMode(surfProxy->backendFormat().textureType())));
449}
450
451#if GR_TEST_UTILS
452void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view,
Brian Salomonccb61422020-01-09 10:46:36 -0500453 GrSamplerState samplerState) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000454 SkASSERT(view.proxy()->asTextureProxy());
455 fView = std::move(view);
456 fSamplerState = samplerState;
457
458 fSamplerState.setFilterMode(
459 SkTMin(samplerState.filter(),
460 GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType())));
461}
462#endif