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() { |
| 16 | static const GrProcessorSet gEmpty(GrProcessorSet::Empty::kEmpty); |
| 17 | return gEmpty; |
| 18 | } |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 19 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 20 | GrProcessorSet::GrProcessorSet(GrPaint&& paint) : fXP(paint.getXPFactory()) { |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 21 | fFlags = 0; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 22 | if (paint.numColorFragmentProcessors() <= kMaxColorProcessors) { |
| 23 | fColorFragmentProcessorCnt = paint.numColorFragmentProcessors(); |
| 24 | fFragmentProcessors.reset(paint.numTotalFragmentProcessors()); |
| 25 | int i = 0; |
| 26 | for (auto& fp : paint.fColorFragmentProcessors) { |
Brian Salomon | 5f970fe | 2017-06-16 17:30:59 -0400 | [diff] [blame] | 27 | SkASSERT(fp.get()); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 28 | fFragmentProcessors[i++] = fp.release(); |
| 29 | } |
| 30 | for (auto& fp : paint.fCoverageFragmentProcessors) { |
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 | } |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 34 | } else { |
| 35 | SkDebugf("Insane number of color fragment processors in paint. Dropping all processors."); |
| 36 | fColorFragmentProcessorCnt = 0; |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 37 | } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 38 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 39 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 40 | GrProcessorSet::GrProcessorSet(SkBlendMode mode) |
| 41 | : fXP(SkBlendMode_AsXPFactory(mode)) |
| 42 | , fColorFragmentProcessorCnt(0) |
| 43 | , fFragmentProcessorOffset(0) |
| 44 | , fFlags(0) {} |
| 45 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 46 | GrProcessorSet::GrProcessorSet(sk_sp<GrFragmentProcessor> colorFP) |
| 47 | : fFragmentProcessors(1) |
| 48 | , fXP((const GrXPFactory*)nullptr) |
| 49 | , fColorFragmentProcessorCnt(1) |
| 50 | , fFragmentProcessorOffset(0) |
| 51 | , fFlags(0) { |
| 52 | SkASSERT(colorFP); |
| 53 | fFragmentProcessors[0] = colorFP.release(); |
| 54 | } |
| 55 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 56 | GrProcessorSet::~GrProcessorSet() { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 57 | for (int i = fFragmentProcessorOffset; i < fFragmentProcessors.count(); ++i) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 58 | if (this->isFinalized()) { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 59 | fFragmentProcessors[i]->completedExecution(); |
| 60 | } else { |
| 61 | fFragmentProcessors[i]->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 62 | } |
| 63 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 64 | if (this->isFinalized() && this->xferProcessor()) { |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 65 | this->xferProcessor()->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 69 | SkString dump_fragment_processor_tree(const GrFragmentProcessor* fp, int indentCnt) { |
| 70 | SkString result; |
| 71 | SkString indentString; |
| 72 | for (int i = 0; i < indentCnt; ++i) { |
| 73 | indentString.append(" "); |
| 74 | } |
| 75 | result.appendf("%s%s %s \n", indentString.c_str(), fp->name(), fp->dumpInfo().c_str()); |
| 76 | if (fp->numChildProcessors()) { |
| 77 | for (int i = 0; i < fp->numChildProcessors(); ++i) { |
| 78 | result += dump_fragment_processor_tree(&fp->childProcessor(i), indentCnt + 1); |
| 79 | } |
| 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | SkString GrProcessorSet::dumpProcessors() const { |
| 85 | SkString result; |
| 86 | if (this->numFragmentProcessors()) { |
| 87 | if (this->numColorFragmentProcessors()) { |
| 88 | result.append("Color Fragment Processors:\n"); |
| 89 | for (int i = 0; i < this->numColorFragmentProcessors(); ++i) { |
| 90 | result += dump_fragment_processor_tree(this->colorFragmentProcessor(i), 1); |
| 91 | } |
| 92 | } else { |
| 93 | result.append("No color fragment processors.\n"); |
| 94 | } |
| 95 | if (this->numCoverageFragmentProcessors()) { |
| 96 | result.append("Coverage Fragment Processors:\n"); |
| 97 | for (int i = 0; i < this->numColorFragmentProcessors(); ++i) { |
| 98 | result += dump_fragment_processor_tree(this->coverageFragmentProcessor(i), 1); |
| 99 | } |
| 100 | } else { |
| 101 | result.append("No coverage fragment processors.\n"); |
| 102 | } |
| 103 | } else { |
| 104 | result.append("No color or coverage fragment processors.\n"); |
| 105 | } |
| 106 | if (this->isFinalized()) { |
| 107 | result.append("Xfer Processor: "); |
| 108 | if (this->xferProcessor()) { |
| 109 | result.appendf("%s\n", this->xferProcessor()->name()); |
| 110 | } else { |
| 111 | result.append("SrcOver\n"); |
| 112 | } |
| 113 | } else { |
| 114 | result.append("XP Factory dumping not implemented.\n"); |
| 115 | } |
| 116 | return result; |
| 117 | } |
| 118 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 119 | bool GrProcessorSet::operator==(const GrProcessorSet& that) const { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 120 | SkASSERT(this->isFinalized()); |
| 121 | SkASSERT(that.isFinalized()); |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 122 | int fpCount = this->numFragmentProcessors(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 123 | if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() || |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 124 | fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) { |
| 125 | return false; |
| 126 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 127 | |
| 128 | for (int i = 0; i < fpCount; ++i) { |
| 129 | int a = i + fFragmentProcessorOffset; |
| 130 | int b = i + that.fFragmentProcessorOffset; |
| 131 | if (!fFragmentProcessors[a]->isEqual(*that.fFragmentProcessors[b])) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 135 | // Most of the time both of these are null |
| 136 | if (!this->xferProcessor() && !that.xferProcessor()) { |
| 137 | return true; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 138 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 139 | const GrXferProcessor& thisXP = this->xferProcessor() |
| 140 | ? *this->xferProcessor() |
| 141 | : GrPorterDuffXPFactory::SimpleSrcOverXP(); |
| 142 | const GrXferProcessor& thatXP = that.xferProcessor() |
| 143 | ? *that.xferProcessor() |
| 144 | : GrPorterDuffXPFactory::SimpleSrcOverXP(); |
| 145 | return thisXP.isEqual(thatXP); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 148 | GrProcessorSet::Analysis GrProcessorSet::finalize(const GrProcessorAnalysisColor& colorInput, |
| 149 | const GrProcessorAnalysisCoverage coverageInput, |
| 150 | const GrAppliedClip* clip, bool isMixedSamples, |
| 151 | const GrCaps& caps, GrColor* overrideInputColor) { |
| 152 | SkASSERT(!this->isFinalized()); |
| 153 | SkASSERT(!fFragmentProcessorOffset); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 154 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 155 | GrProcessorSet::Analysis analysis; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 156 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 157 | const GrFragmentProcessor* clipFP = clip ? clip->clipCoverageFragmentProcessor() : nullptr; |
| 158 | GrColorFragmentProcessorAnalysis colorAnalysis(colorInput); |
| 159 | analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput; |
| 160 | |
| 161 | const GrFragmentProcessor* const* fps = fFragmentProcessors.get() + fFragmentProcessorOffset; |
| 162 | colorAnalysis.analyzeProcessors(fps, fColorFragmentProcessorCnt); |
| 163 | analysis.fCompatibleWithCoverageAsAlpha &= |
| 164 | colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha(); |
| 165 | fps += fColorFragmentProcessorCnt; |
| 166 | int n = this->numCoverageFragmentProcessors(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 167 | bool hasCoverageFP = n > 0; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 168 | bool coverageUsesLocalCoords = false; |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 169 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 170 | if (!fps[i]->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 171 | analysis.fCompatibleWithCoverageAsAlpha = false; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 172 | // Other than tests that exercise atypical behavior we expect all coverage FPs to be |
| 173 | // compatible with the coverage-as-alpha optimization. |
| 174 | GrCapsDebugf(&caps, "Coverage FP is not compatible with coverage as alpha.\n"); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 175 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 176 | coverageUsesLocalCoords |= fps[i]->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (clipFP) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 180 | analysis.fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha(); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 181 | coverageUsesLocalCoords |= clipFP->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 182 | hasCoverageFP = true; |
| 183 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 184 | int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor); |
| 185 | analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>( |
| 186 | colorFPsToEliminate ? Analysis::kOverridden_InputColorType |
| 187 | : Analysis::kOriginal_InputColorType); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 188 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 189 | GrProcessorAnalysisCoverage outputCoverage; |
| 190 | if (GrProcessorAnalysisCoverage::kLCD == coverageInput) { |
| 191 | outputCoverage = GrProcessorAnalysisCoverage::kLCD; |
| 192 | } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) { |
| 193 | outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 194 | } else { |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 195 | outputCoverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 196 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 197 | |
| 198 | GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties( |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 199 | this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps); |
| 200 | if (!this->numCoverageFragmentProcessors() && |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 201 | GrProcessorAnalysisCoverage::kNone == coverageInput) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 202 | analysis.fCanCombineOverlappedStencilAndCover = SkToBool( |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 203 | props & GrXPFactory::AnalysisProperties::kCanCombineOverlappedStencilAndCover); |
| 204 | } else { |
| 205 | // If we have non-clipping coverage processors we don't try to merge stencil steps as its |
| 206 | // 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] | 207 | analysis.fCanCombineOverlappedStencilAndCover = false; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 208 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 209 | analysis.fRequiresDstTexture = |
| 210 | SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture); |
| 211 | analysis.fCompatibleWithCoverageAsAlpha &= |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 212 | SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 213 | analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool( |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 214 | props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 215 | if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 216 | colorFPsToEliminate = this->numColorFragmentProcessors(); |
| 217 | analysis.fInputColorType = |
| 218 | static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType); |
| 219 | analysis.fUsesLocalCoords = coverageUsesLocalCoords; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 220 | } else { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 221 | analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 222 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 223 | for (int i = 0; i < colorFPsToEliminate; ++i) { |
| 224 | fFragmentProcessors[i]->unref(); |
| 225 | fFragmentProcessors[i] = nullptr; |
Brian Salomon | 5dac9b3 | 2017-04-08 02:53:30 +0000 | [diff] [blame] | 226 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 227 | for (int i = colorFPsToEliminate; i < fFragmentProcessors.count(); ++i) { |
| 228 | fFragmentProcessors[i]->addPendingExecution(); |
| 229 | fFragmentProcessors[i]->unref(); |
| 230 | } |
| 231 | fFragmentProcessorOffset = colorFPsToEliminate; |
| 232 | fColorFragmentProcessorCnt -= colorFPsToEliminate; |
| 233 | |
| 234 | auto xp = GrXPFactory::MakeXferProcessor(this->xpFactory(), colorAnalysis.outputColor(), |
| 235 | outputCoverage, isMixedSamples, caps); |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 236 | fXP.fProcessor = xp.release(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 237 | |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 238 | fFlags |= kFinalized_Flag; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 239 | analysis.fIsInitialized = true; |
| 240 | return analysis; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 241 | } |