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 | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 32 | fFragmentProcessors[i++] = std::move(fp); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 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 | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 36 | fFragmentProcessors[i++] = std::move(fp); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 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 | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 50 | GrProcessorSet::GrProcessorSet(std::unique_ptr<GrFragmentProcessor> colorFP) |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 51 | : fFragmentProcessors(1) |
| 52 | , fXP((const GrXPFactory*)nullptr) |
| 53 | , fColorFragmentProcessorCnt(1) |
| 54 | , fFragmentProcessorOffset(0) |
| 55 | , fFlags(0) { |
| 56 | SkASSERT(colorFP); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 57 | fFragmentProcessors[0] = std::move(colorFP); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 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) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 67 | fFragmentProcessors[i] = |
| 68 | std::move(that.fFragmentProcessors[i + that.fFragmentProcessorOffset]); |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 69 | } |
| 70 | that.fColorFragmentProcessorCnt = 0; |
| 71 | that.fFragmentProcessors.reset(0); |
| 72 | } |
| 73 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 74 | GrProcessorSet::~GrProcessorSet() { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 75 | if (this->isFinalized() && this->xferProcessor()) { |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 76 | this->xferProcessor()->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 80 | SkString 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 | |
| 95 | SkString 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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 130 | bool GrProcessorSet::operator==(const GrProcessorSet& that) const { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 131 | SkASSERT(this->isFinalized()); |
| 132 | SkASSERT(that.isFinalized()); |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 133 | int fpCount = this->numFragmentProcessors(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 134 | if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() || |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 135 | fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) { |
| 136 | return false; |
| 137 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 138 | |
| 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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 146 | // Most of the time both of these are null |
| 147 | if (!this->xferProcessor() && !that.xferProcessor()) { |
| 148 | return true; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 149 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 150 | 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 Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 159 | GrProcessorSet::Analysis GrProcessorSet::finalize(const GrProcessorAnalysisColor& colorInput, |
| 160 | const GrProcessorAnalysisCoverage coverageInput, |
| 161 | const GrAppliedClip* clip, bool isMixedSamples, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 162 | const GrCaps& caps, GrColor* overrideInputColor) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 163 | SkASSERT(!this->isFinalized()); |
| 164 | SkASSERT(!fFragmentProcessorOffset); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 165 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 166 | GrProcessorSet::Analysis analysis; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 167 | analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput; |
| 168 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 169 | 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 Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 173 | analysis.fCompatibleWithCoverageAsAlpha &= |
| 174 | colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha(); |
| 175 | fps += fColorFragmentProcessorCnt; |
| 176 | int n = this->numCoverageFragmentProcessors(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 177 | bool hasCoverageFP = n > 0; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 178 | bool coverageUsesLocalCoords = false; |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 179 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 180 | if (!fps[i]->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 181 | analysis.fCompatibleWithCoverageAsAlpha = false; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 182 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 183 | coverageUsesLocalCoords |= fps[i]->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 184 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 185 | 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 Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 192 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 193 | int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor); |
| 194 | analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>( |
| 195 | colorFPsToEliminate ? Analysis::kOverridden_InputColorType |
| 196 | : Analysis::kOriginal_InputColorType); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 197 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 198 | GrProcessorAnalysisCoverage outputCoverage; |
| 199 | if (GrProcessorAnalysisCoverage::kLCD == coverageInput) { |
| 200 | outputCoverage = GrProcessorAnalysisCoverage::kLCD; |
| 201 | } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) { |
| 202 | outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 203 | } else { |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 204 | outputCoverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 205 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 206 | |
| 207 | GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties( |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 208 | this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 209 | if (!this->numCoverageFragmentProcessors() && |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 210 | GrProcessorAnalysisCoverage::kNone == coverageInput) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 211 | analysis.fCanCombineOverlappedStencilAndCover = SkToBool( |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 212 | 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 Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 216 | analysis.fCanCombineOverlappedStencilAndCover = false; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 217 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 218 | analysis.fRequiresDstTexture = |
| 219 | SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture); |
| 220 | analysis.fCompatibleWithCoverageAsAlpha &= |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 221 | SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 222 | analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool( |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 223 | props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 224 | if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 225 | colorFPsToEliminate = this->numColorFragmentProcessors(); |
| 226 | analysis.fInputColorType = |
| 227 | static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType); |
| 228 | analysis.fUsesLocalCoords = coverageUsesLocalCoords; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 229 | } else { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 230 | analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 231 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 232 | for (int i = 0; i < colorFPsToEliminate; ++i) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 233 | fFragmentProcessors[i].reset(nullptr); |
Brian Salomon | 5dac9b3 | 2017-04-08 02:53:30 +0000 | [diff] [blame] | 234 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 235 | for (int i = colorFPsToEliminate; i < fFragmentProcessors.count(); ++i) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 236 | fFragmentProcessors[i]->markPendingExecution(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 237 | } |
| 238 | fFragmentProcessorOffset = colorFPsToEliminate; |
| 239 | fColorFragmentProcessorCnt -= colorFPsToEliminate; |
| 240 | |
| 241 | auto xp = GrXPFactory::MakeXferProcessor(this->xpFactory(), colorAnalysis.outputColor(), |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 242 | outputCoverage, isMixedSamples, caps); |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 243 | fXP.fProcessor = xp.release(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 244 | |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 245 | fFlags |= kFinalized_Flag; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 246 | analysis.fIsInitialized = true; |
| 247 | return analysis; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 248 | } |