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