blob: e4da038e3b7a0c6f2fa039addee7f96da305df9f [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 }
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 Salomonaff329b2017-08-11 09:40:37 -040050GrProcessorSet::GrProcessorSet(std::unique_ptr<GrFragmentProcessor> colorFP)
Brian Salomon82ddc942017-07-14 12:00:13 -040051 : fFragmentProcessors(1)
52 , fXP((const GrXPFactory*)nullptr)
53 , fColorFragmentProcessorCnt(1)
54 , fFragmentProcessorOffset(0)
55 , fFlags(0) {
56 SkASSERT(colorFP);
Brian Salomonaff329b2017-08-11 09:40:37 -040057 fFragmentProcessors[0] = std::move(colorFP);
Brian Salomon82ddc942017-07-14 12:00:13 -040058}
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) {
Brian Salomonaff329b2017-08-11 09:40:37 -040067 fFragmentProcessors[i] =
68 std::move(that.fFragmentProcessors[i + that.fFragmentProcessorOffset]);
Brian Salomon91326c32017-08-09 16:02:19 -040069 }
70 that.fColorFragmentProcessorCnt = 0;
71 that.fFragmentProcessors.reset(0);
72}
73
Brian Salomon54d212e2017-03-21 14:22:38 -040074GrProcessorSet::~GrProcessorSet() {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040075 if (this->isFinalized() && this->xferProcessor()) {
Brian Salomond61c9d92017-04-10 10:54:25 -040076 this->xferProcessor()->unref();
Brian Salomon54d212e2017-03-21 14:22:38 -040077 }
78}
79
Brian Salomon82dfd3d2017-06-14 12:30:35 -040080SkString dump_fragment_processor_tree(const GrFragmentProcessor* fp, int indentCnt) {
81 SkString result;
82 SkString indentString;
83 for (int i = 0; i < indentCnt; ++i) {
84 indentString.append(" ");
85 }
86 result.appendf("%s%s %s \n", indentString.c_str(), fp->name(), fp->dumpInfo().c_str());
87 if (fp->numChildProcessors()) {
88 for (int i = 0; i < fp->numChildProcessors(); ++i) {
89 result += dump_fragment_processor_tree(&fp->childProcessor(i), indentCnt + 1);
90 }
91 }
92 return result;
93}
94
95SkString GrProcessorSet::dumpProcessors() const {
96 SkString result;
97 if (this->numFragmentProcessors()) {
98 if (this->numColorFragmentProcessors()) {
99 result.append("Color Fragment Processors:\n");
100 for (int i = 0; i < this->numColorFragmentProcessors(); ++i) {
101 result += dump_fragment_processor_tree(this->colorFragmentProcessor(i), 1);
102 }
103 } else {
104 result.append("No color fragment processors.\n");
105 }
106 if (this->numCoverageFragmentProcessors()) {
107 result.append("Coverage Fragment Processors:\n");
108 for (int i = 0; i < this->numColorFragmentProcessors(); ++i) {
109 result += dump_fragment_processor_tree(this->coverageFragmentProcessor(i), 1);
110 }
111 } else {
112 result.append("No coverage fragment processors.\n");
113 }
114 } else {
115 result.append("No color or coverage fragment processors.\n");
116 }
117 if (this->isFinalized()) {
118 result.append("Xfer Processor: ");
119 if (this->xferProcessor()) {
120 result.appendf("%s\n", this->xferProcessor()->name());
121 } else {
122 result.append("SrcOver\n");
123 }
124 } else {
125 result.append("XP Factory dumping not implemented.\n");
126 }
127 return result;
128}
129
Brian Salomon54d212e2017-03-21 14:22:38 -0400130bool GrProcessorSet::operator==(const GrProcessorSet& that) const {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400131 SkASSERT(this->isFinalized());
132 SkASSERT(that.isFinalized());
Brian Salomon70288c02017-03-24 12:27:17 -0400133 int fpCount = this->numFragmentProcessors();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400134 if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() ||
Brian Salomon54d212e2017-03-21 14:22:38 -0400135 fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) {
136 return false;
137 }
Brian Salomon70288c02017-03-24 12:27:17 -0400138
139 for (int i = 0; i < fpCount; ++i) {
140 int a = i + fFragmentProcessorOffset;
141 int b = i + that.fFragmentProcessorOffset;
142 if (!fFragmentProcessors[a]->isEqual(*that.fFragmentProcessors[b])) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400143 return false;
144 }
145 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400146 // Most of the time both of these are null
147 if (!this->xferProcessor() && !that.xferProcessor()) {
148 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400149 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400150 const GrXferProcessor& thisXP = this->xferProcessor()
151 ? *this->xferProcessor()
152 : GrPorterDuffXPFactory::SimpleSrcOverXP();
153 const GrXferProcessor& thatXP = that.xferProcessor()
154 ? *that.xferProcessor()
155 : GrPorterDuffXPFactory::SimpleSrcOverXP();
156 return thisXP.isEqual(thatXP);
Brian Salomon54d212e2017-03-21 14:22:38 -0400157}
158
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400159GrProcessorSet::Analysis GrProcessorSet::finalize(const GrProcessorAnalysisColor& colorInput,
160 const GrProcessorAnalysisCoverage coverageInput,
161 const GrAppliedClip* clip, bool isMixedSamples,
Brian Osman532b3f92018-07-11 10:02:07 -0400162 const GrCaps& caps, GrColor* overrideInputColor) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400163 SkASSERT(!this->isFinalized());
164 SkASSERT(!fFragmentProcessorOffset);
Brian Salomon5298dc82017-02-22 11:52:03 -0500165
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400166 GrProcessorSet::Analysis analysis;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400167 analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput;
168
Brian Salomonaff329b2017-08-11 09:40:37 -0400169 const std::unique_ptr<const GrFragmentProcessor>* fps =
170 fFragmentProcessors.get() + fFragmentProcessorOffset;
171 GrColorFragmentProcessorAnalysis colorAnalysis(
172 colorInput, unique_ptr_address_as_pointer_address(fps), fColorFragmentProcessorCnt);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400173 analysis.fCompatibleWithCoverageAsAlpha &=
174 colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha();
175 fps += fColorFragmentProcessorCnt;
176 int n = this->numCoverageFragmentProcessors();
Brian Salomon5298dc82017-02-22 11:52:03 -0500177 bool hasCoverageFP = n > 0;
Brian Salomon31853842017-03-28 16:32:05 -0400178 bool coverageUsesLocalCoords = false;
Brian Salomonbfafcba2017-03-02 08:49:19 -0500179 for (int i = 0; i < n; ++i) {
Brian Salomon5298dc82017-02-22 11:52:03 -0500180 if (!fps[i]->compatibleWithCoverageAsAlpha()) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400181 analysis.fCompatibleWithCoverageAsAlpha = false;
Brian Salomon5298dc82017-02-22 11:52:03 -0500182 }
Brian Salomon31853842017-03-28 16:32:05 -0400183 coverageUsesLocalCoords |= fps[i]->usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500184 }
Chris Dalton69824002017-10-31 00:37:52 -0600185 if (clip) {
186 hasCoverageFP = hasCoverageFP || clip->numClipCoverageFragmentProcessors();
187 for (int i = 0; i < clip->numClipCoverageFragmentProcessors(); ++i) {
188 const GrFragmentProcessor* clipFP = clip->clipCoverageFragmentProcessor(i);
189 analysis.fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha();
190 coverageUsesLocalCoords |= clipFP->usesLocalCoords();
191 }
Brian Salomon5298dc82017-02-22 11:52:03 -0500192 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400193 int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor);
194 analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>(
195 colorFPsToEliminate ? Analysis::kOverridden_InputColorType
196 : Analysis::kOriginal_InputColorType);
Brian Salomon5298dc82017-02-22 11:52:03 -0500197
Brian Salomona811b122017-03-30 08:21:32 -0400198 GrProcessorAnalysisCoverage outputCoverage;
199 if (GrProcessorAnalysisCoverage::kLCD == coverageInput) {
200 outputCoverage = GrProcessorAnalysisCoverage::kLCD;
201 } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) {
202 outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel;
Brian Salomon5298dc82017-02-22 11:52:03 -0500203 } else {
Brian Salomona811b122017-03-30 08:21:32 -0400204 outputCoverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomon31853842017-03-28 16:32:05 -0400205 }
Brian Salomon31853842017-03-28 16:32:05 -0400206
207 GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties(
Brian Osman532b3f92018-07-11 10:02:07 -0400208 this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400209 if (!this->numCoverageFragmentProcessors() &&
Brian Salomona811b122017-03-30 08:21:32 -0400210 GrProcessorAnalysisCoverage::kNone == coverageInput) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400211 analysis.fCanCombineOverlappedStencilAndCover = SkToBool(
Brian Salomon31853842017-03-28 16:32:05 -0400212 props & GrXPFactory::AnalysisProperties::kCanCombineOverlappedStencilAndCover);
213 } else {
214 // If we have non-clipping coverage processors we don't try to merge stencil steps as its
215 // unclear whether it will be correct. We don't expect this to happen in practice.
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400216 analysis.fCanCombineOverlappedStencilAndCover = false;
Brian Salomon31853842017-03-28 16:32:05 -0400217 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400218 analysis.fRequiresDstTexture =
219 SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture);
220 analysis.fCompatibleWithCoverageAsAlpha &=
Brian Salomon31853842017-03-28 16:32:05 -0400221 SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400222 analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool(
Brian Salomon4fc77402017-03-30 16:48:26 -0400223 props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws);
Brian Salomon31853842017-03-28 16:32:05 -0400224 if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400225 colorFPsToEliminate = this->numColorFragmentProcessors();
226 analysis.fInputColorType =
227 static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType);
228 analysis.fUsesLocalCoords = coverageUsesLocalCoords;
Brian Salomon31853842017-03-28 16:32:05 -0400229 } else {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400230 analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords();
Brian Salomon5298dc82017-02-22 11:52:03 -0500231 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400232 for (int i = 0; i < colorFPsToEliminate; ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400233 fFragmentProcessors[i].reset(nullptr);
Brian Salomon5dac9b32017-04-08 02:53:30 +0000234 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400235 for (int i = colorFPsToEliminate; i < fFragmentProcessors.count(); ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400236 fFragmentProcessors[i]->markPendingExecution();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400237 }
238 fFragmentProcessorOffset = colorFPsToEliminate;
239 fColorFragmentProcessorCnt -= colorFPsToEliminate;
240
241 auto xp = GrXPFactory::MakeXferProcessor(this->xpFactory(), colorAnalysis.outputColor(),
Brian Osman532b3f92018-07-11 10:02:07 -0400242 outputCoverage, isMixedSamples, caps);
Brian Salomond61c9d92017-04-10 10:54:25 -0400243 fXP.fProcessor = xp.release();
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400244
Brian Salomond61c9d92017-04-10 10:54:25 -0400245 fFlags |= kFinalized_Flag;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400246 analysis.fIsInitialized = true;
247 return analysis;
Brian Salomon70288c02017-03-24 12:27:17 -0400248}