Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 1 | /* |
| 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 Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 9 | #include "GrAppliedClip.h" |
| 10 | #include "GrCaps.h" |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 11 | #include "GrXferProcessor.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 12 | #include "SkBlendModePriv.h" |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 13 | #include "effects/GrPorterDuffXferProcessor.h" |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 14 | |
Brian Salomon | 292bf7a | 2017-05-17 09:43:55 -0400 | [diff] [blame] | 15 | const GrProcessorSet& GrProcessorSet::EmptySet() { |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 16 | static GrProcessorSet gEmpty(GrProcessorSet::Empty::kEmpty); |
Brian Salomon | 292bf7a | 2017-05-17 09:43:55 -0400 | [diff] [blame] | 17 | return gEmpty; |
| 18 | } |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 19 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 20 | GrProcessorSet GrProcessorSet::MakeEmptySet() { |
| 21 | return GrProcessorSet(GrProcessorSet::Empty::kEmpty); |
| 22 | } |
| 23 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 24 | GrProcessorSet::GrProcessorSet(GrPaint&& paint) : fXP(paint.getXPFactory()) { |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 25 | fFlags = 0; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 26 | if (paint.numColorFragmentProcessors() <= kMaxColorProcessors) { |
| 27 | fColorFragmentProcessorCnt = paint.numColorFragmentProcessors(); |
| 28 | fFragmentProcessors.reset(paint.numTotalFragmentProcessors()); |
| 29 | int i = 0; |
| 30 | for (auto& fp : paint.fColorFragmentProcessors) { |
Brian Salomon | 5f970fe | 2017-06-16 17:30:59 -0400 | [diff] [blame] | 31 | SkASSERT(fp.get()); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 32 | fFragmentProcessors[i++] = fp.release(); |
| 33 | } |
| 34 | for (auto& fp : paint.fCoverageFragmentProcessors) { |
Brian Salomon | 5f970fe | 2017-06-16 17:30:59 -0400 | [diff] [blame] | 35 | SkASSERT(fp.get()); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 36 | fFragmentProcessors[i++] = fp.release(); |
| 37 | } |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 38 | } else { |
| 39 | SkDebugf("Insane number of color fragment processors in paint. Dropping all processors."); |
| 40 | fColorFragmentProcessorCnt = 0; |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 41 | } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 42 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 43 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 44 | GrProcessorSet::GrProcessorSet(SkBlendMode mode) |
| 45 | : fXP(SkBlendMode_AsXPFactory(mode)) |
| 46 | , fColorFragmentProcessorCnt(0) |
| 47 | , fFragmentProcessorOffset(0) |
| 48 | , fFlags(0) {} |
| 49 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 50 | GrProcessorSet::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 Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 60 | GrProcessorSet::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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 73 | GrProcessorSet::~GrProcessorSet() { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 74 | for (int i = fFragmentProcessorOffset; i < fFragmentProcessors.count(); ++i) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 75 | if (this->isFinalized()) { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 76 | fFragmentProcessors[i]->completedExecution(); |
| 77 | } else { |
| 78 | fFragmentProcessors[i]->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 79 | } |
| 80 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 81 | if (this->isFinalized() && this->xferProcessor()) { |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 82 | this->xferProcessor()->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 86 | SkString 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 | |
| 101 | SkString 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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 136 | bool GrProcessorSet::operator==(const GrProcessorSet& that) const { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 137 | SkASSERT(this->isFinalized()); |
| 138 | SkASSERT(that.isFinalized()); |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 139 | int fpCount = this->numFragmentProcessors(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 140 | if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() || |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 141 | fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) { |
| 142 | return false; |
| 143 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 144 | |
| 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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 152 | // Most of the time both of these are null |
| 153 | if (!this->xferProcessor() && !that.xferProcessor()) { |
| 154 | return true; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 155 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 156 | 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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 165 | GrProcessorSet::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 Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 171 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 172 | GrProcessorSet::Analysis analysis; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 173 | analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput; |
| 174 | |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 175 | const GrFragmentProcessor* clipFP = clip ? clip->clipCoverageFragmentProcessor() : nullptr; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 176 | const GrFragmentProcessor* const* fps = fFragmentProcessors.get() + fFragmentProcessorOffset; |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 177 | GrColorFragmentProcessorAnalysis colorAnalysis(colorInput, fps, fColorFragmentProcessorCnt); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 178 | analysis.fCompatibleWithCoverageAsAlpha &= |
| 179 | colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha(); |
| 180 | fps += fColorFragmentProcessorCnt; |
| 181 | int n = this->numCoverageFragmentProcessors(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 182 | bool hasCoverageFP = n > 0; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 183 | bool coverageUsesLocalCoords = false; |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 184 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 185 | if (!fps[i]->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 186 | analysis.fCompatibleWithCoverageAsAlpha = false; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 187 | // 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 Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 190 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 191 | coverageUsesLocalCoords |= fps[i]->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (clipFP) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 195 | analysis.fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha(); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 196 | coverageUsesLocalCoords |= clipFP->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 197 | hasCoverageFP = true; |
| 198 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 199 | int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor); |
| 200 | analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>( |
| 201 | colorFPsToEliminate ? Analysis::kOverridden_InputColorType |
| 202 | : Analysis::kOriginal_InputColorType); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 203 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 204 | GrProcessorAnalysisCoverage outputCoverage; |
| 205 | if (GrProcessorAnalysisCoverage::kLCD == coverageInput) { |
| 206 | outputCoverage = GrProcessorAnalysisCoverage::kLCD; |
| 207 | } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) { |
| 208 | outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 209 | } else { |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 210 | outputCoverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 211 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 212 | |
| 213 | GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties( |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 214 | this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps); |
| 215 | if (!this->numCoverageFragmentProcessors() && |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 216 | GrProcessorAnalysisCoverage::kNone == coverageInput) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 217 | analysis.fCanCombineOverlappedStencilAndCover = SkToBool( |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 218 | 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 Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 222 | analysis.fCanCombineOverlappedStencilAndCover = false; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 223 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 224 | analysis.fRequiresDstTexture = |
| 225 | SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture); |
| 226 | analysis.fCompatibleWithCoverageAsAlpha &= |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 227 | SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 228 | analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool( |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 229 | props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 230 | if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 231 | colorFPsToEliminate = this->numColorFragmentProcessors(); |
| 232 | analysis.fInputColorType = |
| 233 | static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType); |
| 234 | analysis.fUsesLocalCoords = coverageUsesLocalCoords; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 235 | } else { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 236 | analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 237 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 238 | for (int i = 0; i < colorFPsToEliminate; ++i) { |
| 239 | fFragmentProcessors[i]->unref(); |
| 240 | fFragmentProcessors[i] = nullptr; |
Brian Salomon | 5dac9b3 | 2017-04-08 02:53:30 +0000 | [diff] [blame] | 241 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 242 | 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 Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 251 | fXP.fProcessor = xp.release(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 252 | |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 253 | fFlags |= kFinalized_Flag; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 254 | analysis.fIsInitialized = true; |
| 255 | return analysis; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 256 | } |