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 | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 12 | |
| 13 | GrProcessorSet::GrProcessorSet(GrPaint&& paint) { |
| 14 | fXPFactory = paint.fXPFactory; |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 15 | fFlags = 0; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 16 | if (paint.numColorFragmentProcessors() <= kMaxColorProcessors) { |
| 17 | fColorFragmentProcessorCnt = paint.numColorFragmentProcessors(); |
| 18 | fFragmentProcessors.reset(paint.numTotalFragmentProcessors()); |
| 19 | int i = 0; |
| 20 | for (auto& fp : paint.fColorFragmentProcessors) { |
| 21 | fFragmentProcessors[i++] = fp.release(); |
| 22 | } |
| 23 | for (auto& fp : paint.fCoverageFragmentProcessors) { |
| 24 | fFragmentProcessors[i++] = fp.release(); |
| 25 | } |
| 26 | if (paint.usesDistanceVectorField()) { |
| 27 | fFlags |= kUseDistanceVectorField_Flag; |
| 28 | } |
| 29 | } else { |
| 30 | SkDebugf("Insane number of color fragment processors in paint. Dropping all processors."); |
| 31 | fColorFragmentProcessorCnt = 0; |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 32 | } |
| 33 | if (paint.getDisableOutputConversionToSRGB()) { |
| 34 | fFlags |= kDisableOutputConversionToSRGB_Flag; |
| 35 | } |
| 36 | if (paint.getAllowSRGBInputs()) { |
| 37 | fFlags |= kAllowSRGBInputs_Flag; |
| 38 | } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 39 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 40 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 41 | GrProcessorSet::~GrProcessorSet() { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 42 | for (int i = fFragmentProcessorOffset; i < fFragmentProcessors.count(); ++i) { |
| 43 | if (this->isPendingExecution()) { |
| 44 | fFragmentProcessors[i]->completedExecution(); |
| 45 | } else { |
| 46 | fFragmentProcessors[i]->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void GrProcessorSet::makePendingExecution() { |
| 52 | SkASSERT(!(kPendingExecution_Flag & fFlags)); |
| 53 | fFlags |= kPendingExecution_Flag; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 54 | for (int i = fFragmentProcessorOffset; i < fFragmentProcessors.count(); ++i) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 55 | fFragmentProcessors[i]->addPendingExecution(); |
| 56 | fFragmentProcessors[i]->unref(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool GrProcessorSet::operator==(const GrProcessorSet& that) const { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 61 | int fpCount = this->numFragmentProcessors(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 62 | if (((fFlags ^ that.fFlags) & ~kPendingExecution_Flag) || |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 63 | fpCount != that.numFragmentProcessors() || |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 64 | fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) { |
| 65 | return false; |
| 66 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 67 | |
| 68 | for (int i = 0; i < fpCount; ++i) { |
| 69 | int a = i + fFragmentProcessorOffset; |
| 70 | int b = i + that.fFragmentProcessorOffset; |
| 71 | if (!fFragmentProcessors[a]->isEqual(*that.fFragmentProcessors[b])) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | if (fXPFactory != that.fXPFactory) { |
| 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 83 | void GrProcessorSet::Analysis::internalInit(const GrProcessorAnalysisColor& colorInput, |
| 84 | const GrProcessorAnalysisCoverage coverageInput, |
| 85 | const GrProcessorSet& processors, |
| 86 | const GrFragmentProcessor* clipFP, |
| 87 | const GrCaps& caps) { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 88 | GrColorFragmentProcessorAnalysis colorInfo(colorInput); |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 89 | fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 90 | fValidInputColor = colorInput.isConstant(&fInputColor); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 91 | |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 92 | const GrFragmentProcessor* const* fps = |
| 93 | processors.fFragmentProcessors.get() + processors.fFragmentProcessorOffset; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 94 | colorInfo.analyzeProcessors(fps, processors.fColorFragmentProcessorCnt); |
| 95 | fCompatibleWithCoverageAsAlpha &= colorInfo.allProcessorsCompatibleWithCoverageAsAlpha(); |
| 96 | fps += processors.fColorFragmentProcessorCnt; |
| 97 | int n = processors.numCoverageFragmentProcessors(); |
| 98 | bool hasCoverageFP = n > 0; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 99 | bool coverageUsesLocalCoords = false; |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 100 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 101 | if (!fps[i]->compatibleWithCoverageAsAlpha()) { |
| 102 | fCompatibleWithCoverageAsAlpha = false; |
| 103 | // Other than tests that exercise atypical behavior we expect all coverage FPs to be |
| 104 | // compatible with the coverage-as-alpha optimization. |
| 105 | GrCapsDebugf(&caps, "Coverage FP is not compatible with coverage as alpha.\n"); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 106 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 107 | coverageUsesLocalCoords |= fps[i]->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (clipFP) { |
| 111 | fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha(); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 112 | coverageUsesLocalCoords |= clipFP->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 113 | hasCoverageFP = true; |
| 114 | } |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 115 | fInitialColorProcessorsToEliminate = colorInfo.initialProcessorsToEliminate(&fInputColor); |
| 116 | fValidInputColor |= SkToBool(fInitialColorProcessorsToEliminate); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 117 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 118 | GrProcessorAnalysisColor outputColor = colorInfo.outputColor(); |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 119 | if (outputColor.isConstant(&fKnownOutputColor)) { |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 120 | fOutputColorType = static_cast<unsigned>(ColorType::kConstant); |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 121 | } else if (outputColor.isOpaque()) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 122 | fOutputColorType = static_cast<unsigned>(ColorType::kOpaque); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 123 | } else { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 124 | fOutputColorType = static_cast<unsigned>(ColorType::kUnknown); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 127 | GrProcessorAnalysisCoverage outputCoverage; |
| 128 | if (GrProcessorAnalysisCoverage::kLCD == coverageInput) { |
| 129 | outputCoverage = GrProcessorAnalysisCoverage::kLCD; |
| 130 | } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) { |
| 131 | outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 132 | } else { |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 133 | outputCoverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 134 | } |
| 135 | fOutputCoverageType = static_cast<unsigned>(outputCoverage); |
| 136 | |
| 137 | GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties( |
| 138 | processors.fXPFactory, colorInfo.outputColor(), outputCoverage, caps); |
| 139 | if (!processors.numCoverageFragmentProcessors() && |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 140 | GrProcessorAnalysisCoverage::kNone == coverageInput) { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 141 | fCanCombineOverlappedStencilAndCover = SkToBool( |
| 142 | props & GrXPFactory::AnalysisProperties::kCanCombineOverlappedStencilAndCover); |
| 143 | } else { |
| 144 | // If we have non-clipping coverage processors we don't try to merge stencil steps as its |
| 145 | // unclear whether it will be correct. We don't expect this to happen in practice. |
| 146 | fCanCombineOverlappedStencilAndCover = false; |
| 147 | } |
| 148 | fRequiresDstTexture = SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture); |
| 149 | fIgnoresInputColor = SkToBool(props & GrXPFactory::AnalysisProperties::kIgnoresInputColor); |
| 150 | fCompatibleWithCoverageAsAlpha &= |
| 151 | SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage); |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame^] | 152 | fRequiresBarrierBetweenOverlappingDraws = SkToBool( |
| 153 | props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 154 | if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) { |
| 155 | fInitialColorProcessorsToEliminate = processors.numColorFragmentProcessors(); |
| 156 | // If the output of the last color stage is known then the kIgnoresInputColor optimization |
| 157 | // may depend upon it being the input to the xp. |
| 158 | if (!outputColor.isConstant(&fInputColor)) { |
| 159 | // Otherwise, the only property the XP factory could have relied upon to compute |
| 160 | // kIgnoresInputColor is opaqueness. |
| 161 | fInputColor = GrColor_WHITE; |
| 162 | } |
| 163 | fValidInputColor = true; |
| 164 | fUsesLocalCoords = coverageUsesLocalCoords; |
| 165 | } else { |
| 166 | fUsesLocalCoords = coverageUsesLocalCoords | colorInfo.usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 170 | void GrProcessorSet::Analysis::init(const GrProcessorAnalysisColor& colorInput, |
| 171 | const GrProcessorAnalysisCoverage coverageInput, |
| 172 | const GrProcessorSet& processors, |
| 173 | const GrAppliedClip* appliedClip, |
| 174 | const GrCaps& caps) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 175 | const GrFragmentProcessor* clipFP = |
| 176 | appliedClip ? appliedClip->clipCoverageFragmentProcessor() : nullptr; |
| 177 | this->internalInit(colorInput, coverageInput, processors, clipFP, caps); |
| 178 | fIsInitializedWithProcessorSet = true; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 181 | GrProcessorSet::Analysis::Analysis(const GrProcessorAnalysisColor& colorInput, |
| 182 | const GrProcessorAnalysisCoverage coverageInput, |
| 183 | const GrXPFactory* factory, |
| 184 | const GrCaps& caps) |
| 185 | : Analysis() { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 186 | GrPaint paint; |
| 187 | paint.setXPFactory(factory); |
| 188 | this->internalInit(colorInput, coverageInput, GrProcessorSet(std::move(paint)), nullptr, caps); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 189 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 190 | |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 191 | void GrProcessorSet::analyzeAndEliminateFragmentProcessors( |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 192 | Analysis* analysis, |
| 193 | const GrProcessorAnalysisColor& colorInput, |
| 194 | const GrProcessorAnalysisCoverage coverageInput, |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 195 | const GrAppliedClip* clip, |
| 196 | const GrCaps& caps) { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 197 | analysis->init(colorInput, coverageInput, *this, clip, caps); |
| 198 | if (analysis->fInitialColorProcessorsToEliminate > 0) { |
| 199 | for (unsigned i = 0; i < analysis->fInitialColorProcessorsToEliminate; ++i) { |
| 200 | if (this->isPendingExecution()) { |
| 201 | fFragmentProcessors[i + fFragmentProcessorOffset]->completedExecution(); |
| 202 | } else { |
| 203 | fFragmentProcessors[i + fFragmentProcessorOffset]->unref(); |
| 204 | } |
| 205 | fFragmentProcessors[i + fFragmentProcessorOffset] = nullptr; |
| 206 | } |
| 207 | fFragmentProcessorOffset += analysis->fInitialColorProcessorsToEliminate; |
| 208 | fColorFragmentProcessorCnt -= analysis->fInitialColorProcessorsToEliminate; |
| 209 | SkASSERT(fFragmentProcessorOffset + fColorFragmentProcessorCnt <= |
| 210 | fFragmentProcessors.count()); |
| 211 | analysis->fInitialColorProcessorsToEliminate = 0; |
| 212 | } |
| 213 | } |