blob: b605695a2284b0fc5f324b7e405d9f323d26f995 [file] [log] [blame]
Brian Salomon92ce5942017-01-18 11:01:10 -05001/*
2 * Copyright 2017 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 "GrProcessorSet.h"
Brian Salomon5298dc82017-02-22 11:52:03 -05009#include "GrAppliedClip.h"
10#include "GrCaps.h"
Brian Salomon31853842017-03-28 16:32:05 -040011#include "GrXferProcessor.h"
Brian Salomon477d0ef2017-07-14 10:12:26 -040012#include "SkBlendModePriv.h"
Brian Salomon48d1b4c2017-04-08 07:38:53 -040013#include "effects/GrPorterDuffXferProcessor.h"
Brian Salomon92ce5942017-01-18 11:01:10 -050014
Brian Salomon292bf7a2017-05-17 09:43:55 -040015const GrProcessorSet& GrProcessorSet::EmptySet() {
Brian Salomon91326c32017-08-09 16:02:19 -040016 static GrProcessorSet gEmpty(GrProcessorSet::Empty::kEmpty);
Brian Salomon292bf7a2017-05-17 09:43:55 -040017 return gEmpty;
18}
Brian Salomon6d4b65e2017-05-03 17:06:09 -040019
Brian Salomon91326c32017-08-09 16:02:19 -040020GrProcessorSet GrProcessorSet::MakeEmptySet() {
21 return GrProcessorSet(GrProcessorSet::Empty::kEmpty);
22}
23
Brian Salomon48d1b4c2017-04-08 07:38:53 -040024GrProcessorSet::GrProcessorSet(GrPaint&& paint) : fXP(paint.getXPFactory()) {
Brian Salomonf87e2b92017-01-19 11:31:50 -050025 fFlags = 0;
Brian Salomon8d2f90b2017-03-13 09:11:58 -040026 if (paint.numColorFragmentProcessors() <= kMaxColorProcessors) {
27 fColorFragmentProcessorCnt = paint.numColorFragmentProcessors();
28 fFragmentProcessors.reset(paint.numTotalFragmentProcessors());
29 int i = 0;
30 for (auto& fp : paint.fColorFragmentProcessors) {
Brian Salomon5f970fe2017-06-16 17:30:59 -040031 SkASSERT(fp.get());
Brian Salomonaff329b2017-08-11 09:40:37 -040032 fFragmentProcessors[i++] = std::move(fp);
Brian Salomon8d2f90b2017-03-13 09:11:58 -040033 }
34 for (auto& fp : paint.fCoverageFragmentProcessors) {
Brian Salomon5f970fe2017-06-16 17:30:59 -040035 SkASSERT(fp.get());
Brian Salomonaff329b2017-08-11 09:40:37 -040036 fFragmentProcessors[i++] = std::move(fp);
Brian Salomon8d2f90b2017-03-13 09:11:58 -040037 }
Brian Salomon8d2f90b2017-03-13 09:11:58 -040038 } else {
39 SkDebugf("Insane number of color fragment processors in paint. Dropping all processors.");
40 fColorFragmentProcessorCnt = 0;
Brian Salomonf87e2b92017-01-19 11:31:50 -050041 }
Mike Klein16885072018-12-11 09:54:31 -050042 SkDEBUGCODE(paint.fAlive = false;)
Brian Salomon92ce5942017-01-18 11:01:10 -050043}
Brian Salomon5298dc82017-02-22 11:52:03 -050044
Brian Salomon477d0ef2017-07-14 10:12:26 -040045GrProcessorSet::GrProcessorSet(SkBlendMode mode)
46 : fXP(SkBlendMode_AsXPFactory(mode))
47 , fColorFragmentProcessorCnt(0)
48 , fFragmentProcessorOffset(0)
49 , fFlags(0) {}
50
Brian Salomonaff329b2017-08-11 09:40:37 -040051GrProcessorSet::GrProcessorSet(std::unique_ptr<GrFragmentProcessor> colorFP)
Brian Salomon82ddc942017-07-14 12:00:13 -040052 : fFragmentProcessors(1)
53 , fXP((const GrXPFactory*)nullptr)
54 , fColorFragmentProcessorCnt(1)
55 , fFragmentProcessorOffset(0)
56 , fFlags(0) {
57 SkASSERT(colorFP);
Brian Salomonaff329b2017-08-11 09:40:37 -040058 fFragmentProcessors[0] = std::move(colorFP);
Brian Salomon82ddc942017-07-14 12:00:13 -040059}
60
Brian Salomon91326c32017-08-09 16:02:19 -040061GrProcessorSet::GrProcessorSet(GrProcessorSet&& that)
62 : fXP(std::move(that.fXP))
63 , fColorFragmentProcessorCnt(that.fColorFragmentProcessorCnt)
64 , fFragmentProcessorOffset(0)
65 , fFlags(that.fFlags) {
66 fFragmentProcessors.reset(that.fFragmentProcessors.count() - that.fFragmentProcessorOffset);
67 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -040068 fFragmentProcessors[i] =
69 std::move(that.fFragmentProcessors[i + that.fFragmentProcessorOffset]);
Brian Salomon91326c32017-08-09 16:02:19 -040070 }
71 that.fColorFragmentProcessorCnt = 0;
72 that.fFragmentProcessors.reset(0);
73}
74
Brian Salomon54d212e2017-03-21 14:22:38 -040075GrProcessorSet::~GrProcessorSet() {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040076 if (this->isFinalized() && this->xferProcessor()) {
Brian Salomond61c9d92017-04-10 10:54:25 -040077 this->xferProcessor()->unref();
Brian Salomon54d212e2017-03-21 14:22:38 -040078 }
79}
80
Brian Osman9a390ac2018-11-12 09:47:48 -050081#ifdef SK_DEBUG
Brian Salomon82dfd3d2017-06-14 12:30:35 -040082SkString dump_fragment_processor_tree(const GrFragmentProcessor* fp, int indentCnt) {
83 SkString result;
84 SkString indentString;
85 for (int i = 0; i < indentCnt; ++i) {
86 indentString.append(" ");
87 }
88 result.appendf("%s%s %s \n", indentString.c_str(), fp->name(), fp->dumpInfo().c_str());
89 if (fp->numChildProcessors()) {
90 for (int i = 0; i < fp->numChildProcessors(); ++i) {
91 result += dump_fragment_processor_tree(&fp->childProcessor(i), indentCnt + 1);
92 }
93 }
94 return result;
95}
96
97SkString GrProcessorSet::dumpProcessors() const {
98 SkString result;
99 if (this->numFragmentProcessors()) {
100 if (this->numColorFragmentProcessors()) {
101 result.append("Color Fragment Processors:\n");
102 for (int i = 0; i < this->numColorFragmentProcessors(); ++i) {
103 result += dump_fragment_processor_tree(this->colorFragmentProcessor(i), 1);
104 }
105 } else {
106 result.append("No color fragment processors.\n");
107 }
108 if (this->numCoverageFragmentProcessors()) {
109 result.append("Coverage Fragment Processors:\n");
110 for (int i = 0; i < this->numColorFragmentProcessors(); ++i) {
111 result += dump_fragment_processor_tree(this->coverageFragmentProcessor(i), 1);
112 }
113 } else {
114 result.append("No coverage fragment processors.\n");
115 }
116 } else {
117 result.append("No color or coverage fragment processors.\n");
118 }
119 if (this->isFinalized()) {
120 result.append("Xfer Processor: ");
121 if (this->xferProcessor()) {
122 result.appendf("%s\n", this->xferProcessor()->name());
123 } else {
124 result.append("SrcOver\n");
125 }
126 } else {
127 result.append("XP Factory dumping not implemented.\n");
128 }
129 return result;
130}
Brian Osman9a390ac2018-11-12 09:47:48 -0500131#endif
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400132
Brian Salomon54d212e2017-03-21 14:22:38 -0400133bool GrProcessorSet::operator==(const GrProcessorSet& that) const {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400134 SkASSERT(this->isFinalized());
135 SkASSERT(that.isFinalized());
Brian Salomon70288c02017-03-24 12:27:17 -0400136 int fpCount = this->numFragmentProcessors();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400137 if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() ||
Brian Salomon54d212e2017-03-21 14:22:38 -0400138 fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) {
139 return false;
140 }
Brian Salomon70288c02017-03-24 12:27:17 -0400141
142 for (int i = 0; i < fpCount; ++i) {
143 int a = i + fFragmentProcessorOffset;
144 int b = i + that.fFragmentProcessorOffset;
145 if (!fFragmentProcessors[a]->isEqual(*that.fFragmentProcessors[b])) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400146 return false;
147 }
148 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400149 // Most of the time both of these are null
150 if (!this->xferProcessor() && !that.xferProcessor()) {
151 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400152 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400153 const GrXferProcessor& thisXP = this->xferProcessor()
154 ? *this->xferProcessor()
155 : GrPorterDuffXPFactory::SimpleSrcOverXP();
156 const GrXferProcessor& thatXP = that.xferProcessor()
157 ? *that.xferProcessor()
158 : GrPorterDuffXPFactory::SimpleSrcOverXP();
159 return thisXP.isEqual(thatXP);
Brian Salomon54d212e2017-03-21 14:22:38 -0400160}
161
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400162GrProcessorSet::Analysis GrProcessorSet::finalize(const GrProcessorAnalysisColor& colorInput,
163 const GrProcessorAnalysisCoverage coverageInput,
164 const GrAppliedClip* clip, bool isMixedSamples,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400165 const GrCaps& caps,
Brian Osmancf860852018-10-31 14:04:39 -0400166 SkPMColor4f* overrideInputColor) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400167 SkASSERT(!this->isFinalized());
168 SkASSERT(!fFragmentProcessorOffset);
Brian Salomon5298dc82017-02-22 11:52:03 -0500169
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400170 GrProcessorSet::Analysis analysis;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400171 analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput;
172
Brian Salomonaff329b2017-08-11 09:40:37 -0400173 const std::unique_ptr<const GrFragmentProcessor>* fps =
174 fFragmentProcessors.get() + fFragmentProcessorOffset;
175 GrColorFragmentProcessorAnalysis colorAnalysis(
176 colorInput, unique_ptr_address_as_pointer_address(fps), fColorFragmentProcessorCnt);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400177 analysis.fCompatibleWithCoverageAsAlpha &=
178 colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha();
179 fps += fColorFragmentProcessorCnt;
180 int n = this->numCoverageFragmentProcessors();
Brian Salomon5298dc82017-02-22 11:52:03 -0500181 bool hasCoverageFP = n > 0;
Brian Salomon31853842017-03-28 16:32:05 -0400182 bool coverageUsesLocalCoords = false;
Brian Salomonbfafcba2017-03-02 08:49:19 -0500183 for (int i = 0; i < n; ++i) {
Brian Salomon5298dc82017-02-22 11:52:03 -0500184 if (!fps[i]->compatibleWithCoverageAsAlpha()) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400185 analysis.fCompatibleWithCoverageAsAlpha = false;
Brian Salomon5298dc82017-02-22 11:52:03 -0500186 }
Brian Salomon31853842017-03-28 16:32:05 -0400187 coverageUsesLocalCoords |= fps[i]->usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500188 }
Chris Dalton69824002017-10-31 00:37:52 -0600189 if (clip) {
190 hasCoverageFP = hasCoverageFP || clip->numClipCoverageFragmentProcessors();
191 for (int i = 0; i < clip->numClipCoverageFragmentProcessors(); ++i) {
192 const GrFragmentProcessor* clipFP = clip->clipCoverageFragmentProcessor(i);
193 analysis.fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha();
194 coverageUsesLocalCoords |= clipFP->usesLocalCoords();
195 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500196 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400197 int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor);
198 analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>(
199 colorFPsToEliminate ? Analysis::kOverridden_InputColorType
200 : Analysis::kOriginal_InputColorType);
Brian Salomon5298dc82017-02-22 11:52:03 -0500201
Brian Salomona811b122017-03-30 08:21:32 -0400202 GrProcessorAnalysisCoverage outputCoverage;
203 if (GrProcessorAnalysisCoverage::kLCD == coverageInput) {
204 outputCoverage = GrProcessorAnalysisCoverage::kLCD;
205 } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) {
206 outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel;
Brian Salomon5298dc82017-02-22 11:52:03 -0500207 } else {
Brian Salomona811b122017-03-30 08:21:32 -0400208 outputCoverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomon31853842017-03-28 16:32:05 -0400209 }
Brian Salomon31853842017-03-28 16:32:05 -0400210
211 GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties(
Brian Osman532b3f92018-07-11 10:02:07 -0400212 this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400213 if (!this->numCoverageFragmentProcessors() &&
Brian Salomona811b122017-03-30 08:21:32 -0400214 GrProcessorAnalysisCoverage::kNone == coverageInput) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400215 analysis.fCanCombineOverlappedStencilAndCover = SkToBool(
Brian Salomon31853842017-03-28 16:32:05 -0400216 props & GrXPFactory::AnalysisProperties::kCanCombineOverlappedStencilAndCover);
217 } else {
218 // If we have non-clipping coverage processors we don't try to merge stencil steps as its
219 // unclear whether it will be correct. We don't expect this to happen in practice.
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400220 analysis.fCanCombineOverlappedStencilAndCover = false;
Brian Salomon31853842017-03-28 16:32:05 -0400221 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400222 analysis.fRequiresDstTexture =
223 SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture);
224 analysis.fCompatibleWithCoverageAsAlpha &=
Brian Salomon31853842017-03-28 16:32:05 -0400225 SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400226 analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool(
Brian Salomon4fc77402017-03-30 16:48:26 -0400227 props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws);
Brian Salomon31853842017-03-28 16:32:05 -0400228 if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400229 colorFPsToEliminate = this->numColorFragmentProcessors();
230 analysis.fInputColorType =
231 static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType);
232 analysis.fUsesLocalCoords = coverageUsesLocalCoords;
Brian Salomon31853842017-03-28 16:32:05 -0400233 } else {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400234 analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500235 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400236 for (int i = 0; i < colorFPsToEliminate; ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400237 fFragmentProcessors[i].reset(nullptr);
Brian Salomon5dac9b32017-04-08 02:53:30 +0000238 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400239 for (int i = colorFPsToEliminate; i < fFragmentProcessors.count(); ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400240 fFragmentProcessors[i]->markPendingExecution();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400241 }
242 fFragmentProcessorOffset = colorFPsToEliminate;
243 fColorFragmentProcessorCnt -= colorFPsToEliminate;
244
245 auto xp = GrXPFactory::MakeXferProcessor(this->xpFactory(), colorAnalysis.outputColor(),
Brian Osman532b3f92018-07-11 10:02:07 -0400246 outputCoverage, isMixedSamples, caps);
Brian Salomond61c9d92017-04-10 10:54:25 -0400247 fXP.fProcessor = xp.release();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400248
Brian Salomond61c9d92017-04-10 10:54:25 -0400249 fFlags |= kFinalized_Flag;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400250 analysis.fIsInitialized = true;
251 return analysis;
Brian Salomon70288c02017-03-24 12:27:17 -0400252}