blob: 20d6745cad401a97d887ae92761ac8faef4c3a63 [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 Salomon8d2f90b2017-03-13 09:11:58 -040032 fFragmentProcessors[i++] = fp.release();
33 }
34 for (auto& fp : paint.fCoverageFragmentProcessors) {
Brian Salomon5f970fe2017-06-16 17:30:59 -040035 SkASSERT(fp.get());
Brian Salomon8d2f90b2017-03-13 09:11:58 -040036 fFragmentProcessors[i++] = fp.release();
37 }
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 }
Brian Salomon92ce5942017-01-18 11:01:10 -050042}
Brian Salomon5298dc82017-02-22 11:52:03 -050043
Brian Salomon477d0ef2017-07-14 10:12:26 -040044GrProcessorSet::GrProcessorSet(SkBlendMode mode)
45 : fXP(SkBlendMode_AsXPFactory(mode))
46 , fColorFragmentProcessorCnt(0)
47 , fFragmentProcessorOffset(0)
48 , fFlags(0) {}
49
Brian Salomon82ddc942017-07-14 12:00:13 -040050GrProcessorSet::GrProcessorSet(sk_sp<GrFragmentProcessor> colorFP)
51 : fFragmentProcessors(1)
52 , fXP((const GrXPFactory*)nullptr)
53 , fColorFragmentProcessorCnt(1)
54 , fFragmentProcessorOffset(0)
55 , fFlags(0) {
56 SkASSERT(colorFP);
57 fFragmentProcessors[0] = colorFP.release();
58}
59
Brian Salomon91326c32017-08-09 16:02:19 -040060GrProcessorSet::GrProcessorSet(GrProcessorSet&& that)
61 : fXP(std::move(that.fXP))
62 , fColorFragmentProcessorCnt(that.fColorFragmentProcessorCnt)
63 , fFragmentProcessorOffset(0)
64 , fFlags(that.fFlags) {
65 fFragmentProcessors.reset(that.fFragmentProcessors.count() - that.fFragmentProcessorOffset);
66 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
67 fFragmentProcessors[i] = that.fFragmentProcessors[i + that.fFragmentProcessorOffset];
68 }
69 that.fColorFragmentProcessorCnt = 0;
70 that.fFragmentProcessors.reset(0);
71}
72
Brian Salomon54d212e2017-03-21 14:22:38 -040073GrProcessorSet::~GrProcessorSet() {
Brian Salomon70288c02017-03-24 12:27:17 -040074 for (int i = fFragmentProcessorOffset; i < fFragmentProcessors.count(); ++i) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040075 if (this->isFinalized()) {
Brian Salomon70288c02017-03-24 12:27:17 -040076 fFragmentProcessors[i]->completedExecution();
77 } else {
78 fFragmentProcessors[i]->unref();
Brian Salomon54d212e2017-03-21 14:22:38 -040079 }
80 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -040081 if (this->isFinalized() && this->xferProcessor()) {
Brian Salomond61c9d92017-04-10 10:54:25 -040082 this->xferProcessor()->unref();
Brian Salomon54d212e2017-03-21 14:22:38 -040083 }
84}
85
Brian Salomon82dfd3d2017-06-14 12:30:35 -040086SkString dump_fragment_processor_tree(const GrFragmentProcessor* fp, int indentCnt) {
87 SkString result;
88 SkString indentString;
89 for (int i = 0; i < indentCnt; ++i) {
90 indentString.append(" ");
91 }
92 result.appendf("%s%s %s \n", indentString.c_str(), fp->name(), fp->dumpInfo().c_str());
93 if (fp->numChildProcessors()) {
94 for (int i = 0; i < fp->numChildProcessors(); ++i) {
95 result += dump_fragment_processor_tree(&fp->childProcessor(i), indentCnt + 1);
96 }
97 }
98 return result;
99}
100
101SkString GrProcessorSet::dumpProcessors() const {
102 SkString result;
103 if (this->numFragmentProcessors()) {
104 if (this->numColorFragmentProcessors()) {
105 result.append("Color Fragment Processors:\n");
106 for (int i = 0; i < this->numColorFragmentProcessors(); ++i) {
107 result += dump_fragment_processor_tree(this->colorFragmentProcessor(i), 1);
108 }
109 } else {
110 result.append("No color fragment processors.\n");
111 }
112 if (this->numCoverageFragmentProcessors()) {
113 result.append("Coverage Fragment Processors:\n");
114 for (int i = 0; i < this->numColorFragmentProcessors(); ++i) {
115 result += dump_fragment_processor_tree(this->coverageFragmentProcessor(i), 1);
116 }
117 } else {
118 result.append("No coverage fragment processors.\n");
119 }
120 } else {
121 result.append("No color or coverage fragment processors.\n");
122 }
123 if (this->isFinalized()) {
124 result.append("Xfer Processor: ");
125 if (this->xferProcessor()) {
126 result.appendf("%s\n", this->xferProcessor()->name());
127 } else {
128 result.append("SrcOver\n");
129 }
130 } else {
131 result.append("XP Factory dumping not implemented.\n");
132 }
133 return result;
134}
135
Brian Salomon54d212e2017-03-21 14:22:38 -0400136bool GrProcessorSet::operator==(const GrProcessorSet& that) const {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400137 SkASSERT(this->isFinalized());
138 SkASSERT(that.isFinalized());
Brian Salomon70288c02017-03-24 12:27:17 -0400139 int fpCount = this->numFragmentProcessors();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400140 if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() ||
Brian Salomon54d212e2017-03-21 14:22:38 -0400141 fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) {
142 return false;
143 }
Brian Salomon70288c02017-03-24 12:27:17 -0400144
145 for (int i = 0; i < fpCount; ++i) {
146 int a = i + fFragmentProcessorOffset;
147 int b = i + that.fFragmentProcessorOffset;
148 if (!fFragmentProcessors[a]->isEqual(*that.fFragmentProcessors[b])) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400149 return false;
150 }
151 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400152 // Most of the time both of these are null
153 if (!this->xferProcessor() && !that.xferProcessor()) {
154 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400155 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400156 const GrXferProcessor& thisXP = this->xferProcessor()
157 ? *this->xferProcessor()
158 : GrPorterDuffXPFactory::SimpleSrcOverXP();
159 const GrXferProcessor& thatXP = that.xferProcessor()
160 ? *that.xferProcessor()
161 : GrPorterDuffXPFactory::SimpleSrcOverXP();
162 return thisXP.isEqual(thatXP);
Brian Salomon54d212e2017-03-21 14:22:38 -0400163}
164
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400165GrProcessorSet::Analysis GrProcessorSet::finalize(const GrProcessorAnalysisColor& colorInput,
166 const GrProcessorAnalysisCoverage coverageInput,
167 const GrAppliedClip* clip, bool isMixedSamples,
168 const GrCaps& caps, GrColor* overrideInputColor) {
169 SkASSERT(!this->isFinalized());
170 SkASSERT(!fFragmentProcessorOffset);
Brian Salomon5298dc82017-02-22 11:52:03 -0500171
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400172 GrProcessorSet::Analysis analysis;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400173 analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput;
174
Brian Salomon650ced02017-07-20 16:46:46 -0400175 const GrFragmentProcessor* clipFP = clip ? clip->clipCoverageFragmentProcessor() : nullptr;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400176 const GrFragmentProcessor* const* fps = fFragmentProcessors.get() + fFragmentProcessorOffset;
Brian Salomon650ced02017-07-20 16:46:46 -0400177 GrColorFragmentProcessorAnalysis colorAnalysis(colorInput, fps, fColorFragmentProcessorCnt);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400178 analysis.fCompatibleWithCoverageAsAlpha &=
179 colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha();
180 fps += fColorFragmentProcessorCnt;
181 int n = this->numCoverageFragmentProcessors();
Brian Salomon5298dc82017-02-22 11:52:03 -0500182 bool hasCoverageFP = n > 0;
Brian Salomon31853842017-03-28 16:32:05 -0400183 bool coverageUsesLocalCoords = false;
Brian Salomonbfafcba2017-03-02 08:49:19 -0500184 for (int i = 0; i < n; ++i) {
Brian Salomon5298dc82017-02-22 11:52:03 -0500185 if (!fps[i]->compatibleWithCoverageAsAlpha()) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400186 analysis.fCompatibleWithCoverageAsAlpha = false;
Brian Salomon5298dc82017-02-22 11:52:03 -0500187 // Other than tests that exercise atypical behavior we expect all coverage FPs to be
188 // compatible with the coverage-as-alpha optimization.
189 GrCapsDebugf(&caps, "Coverage FP is not compatible with coverage as alpha.\n");
Brian Salomon5298dc82017-02-22 11:52:03 -0500190 }
Brian Salomon31853842017-03-28 16:32:05 -0400191 coverageUsesLocalCoords |= fps[i]->usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500192 }
193
194 if (clipFP) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400195 analysis.fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha();
Brian Salomon31853842017-03-28 16:32:05 -0400196 coverageUsesLocalCoords |= clipFP->usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500197 hasCoverageFP = true;
198 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400199 int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor);
200 analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>(
201 colorFPsToEliminate ? Analysis::kOverridden_InputColorType
202 : Analysis::kOriginal_InputColorType);
Brian Salomon5298dc82017-02-22 11:52:03 -0500203
Brian Salomona811b122017-03-30 08:21:32 -0400204 GrProcessorAnalysisCoverage outputCoverage;
205 if (GrProcessorAnalysisCoverage::kLCD == coverageInput) {
206 outputCoverage = GrProcessorAnalysisCoverage::kLCD;
207 } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) {
208 outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel;
Brian Salomon5298dc82017-02-22 11:52:03 -0500209 } else {
Brian Salomona811b122017-03-30 08:21:32 -0400210 outputCoverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomon31853842017-03-28 16:32:05 -0400211 }
Brian Salomon31853842017-03-28 16:32:05 -0400212
213 GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties(
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400214 this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps);
215 if (!this->numCoverageFragmentProcessors() &&
Brian Salomona811b122017-03-30 08:21:32 -0400216 GrProcessorAnalysisCoverage::kNone == coverageInput) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400217 analysis.fCanCombineOverlappedStencilAndCover = SkToBool(
Brian Salomon31853842017-03-28 16:32:05 -0400218 props & GrXPFactory::AnalysisProperties::kCanCombineOverlappedStencilAndCover);
219 } else {
220 // If we have non-clipping coverage processors we don't try to merge stencil steps as its
221 // unclear whether it will be correct. We don't expect this to happen in practice.
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400222 analysis.fCanCombineOverlappedStencilAndCover = false;
Brian Salomon31853842017-03-28 16:32:05 -0400223 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400224 analysis.fRequiresDstTexture =
225 SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture);
226 analysis.fCompatibleWithCoverageAsAlpha &=
Brian Salomon31853842017-03-28 16:32:05 -0400227 SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400228 analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool(
Brian Salomon4fc77402017-03-30 16:48:26 -0400229 props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws);
Brian Salomon31853842017-03-28 16:32:05 -0400230 if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400231 colorFPsToEliminate = this->numColorFragmentProcessors();
232 analysis.fInputColorType =
233 static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType);
234 analysis.fUsesLocalCoords = coverageUsesLocalCoords;
Brian Salomon31853842017-03-28 16:32:05 -0400235 } else {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400236 analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500237 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400238 for (int i = 0; i < colorFPsToEliminate; ++i) {
239 fFragmentProcessors[i]->unref();
240 fFragmentProcessors[i] = nullptr;
Brian Salomon5dac9b32017-04-08 02:53:30 +0000241 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400242 for (int i = colorFPsToEliminate; i < fFragmentProcessors.count(); ++i) {
243 fFragmentProcessors[i]->addPendingExecution();
244 fFragmentProcessors[i]->unref();
245 }
246 fFragmentProcessorOffset = colorFPsToEliminate;
247 fColorFragmentProcessorCnt -= colorFPsToEliminate;
248
249 auto xp = GrXPFactory::MakeXferProcessor(this->xpFactory(), colorAnalysis.outputColor(),
250 outputCoverage, isMixedSamples, caps);
Brian Salomond61c9d92017-04-10 10:54:25 -0400251 fXP.fProcessor = xp.release();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400252
Brian Salomond61c9d92017-04-10 10:54:25 -0400253 fFlags |= kFinalized_Flag;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400254 analysis.fIsInitialized = true;
255 return analysis;
Brian Salomon70288c02017-03-24 12:27:17 -0400256}