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