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 | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 14 | GrProcessorSet::GrProcessorSet(GrPaint&& paint) : fXP(paint.getXPFactory()) { |
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) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 43 | if (this->isFinalized()) { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 44 | fFragmentProcessors[i]->completedExecution(); |
| 45 | } else { |
| 46 | fFragmentProcessors[i]->unref(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 47 | } |
| 48 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 49 | if (this->isFinalized() && this->xferProcessor()) { |
| 50 | this->xferProcessor()->completedExecution(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | bool GrProcessorSet::operator==(const GrProcessorSet& that) const { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 55 | SkASSERT(this->isFinalized()); |
| 56 | SkASSERT(that.isFinalized()); |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 57 | int fpCount = this->numFragmentProcessors(); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 58 | if (((fFlags ^ that.fFlags) & ~kFinalized_Flag) || fpCount != that.numFragmentProcessors() || |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 59 | fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) { |
| 60 | return false; |
| 61 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 62 | |
| 63 | for (int i = 0; i < fpCount; ++i) { |
| 64 | int a = i + fFragmentProcessorOffset; |
| 65 | int b = i + that.fFragmentProcessorOffset; |
| 66 | if (!fFragmentProcessors[a]->isEqual(*that.fFragmentProcessors[b])) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 70 | // Most of the time both of these are null |
| 71 | if (!this->xferProcessor() && !that.xferProcessor()) { |
| 72 | return true; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 73 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 74 | const GrXferProcessor& thisXP = this->xferProcessor() |
| 75 | ? *this->xferProcessor() |
| 76 | : GrPorterDuffXPFactory::SimpleSrcOverXP(); |
| 77 | const GrXferProcessor& thatXP = that.xferProcessor() |
| 78 | ? *that.xferProcessor() |
| 79 | : GrPorterDuffXPFactory::SimpleSrcOverXP(); |
| 80 | return thisXP.isEqual(thatXP); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 83 | GrProcessorSet::Analysis GrProcessorSet::finalize(const GrProcessorAnalysisColor& colorInput, |
| 84 | const GrProcessorAnalysisCoverage coverageInput, |
| 85 | const GrAppliedClip* clip, bool isMixedSamples, |
| 86 | const GrCaps& caps, GrColor* overrideInputColor) { |
| 87 | SkASSERT(!this->isFinalized()); |
| 88 | SkASSERT(!fFragmentProcessorOffset); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 89 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 90 | GrProcessorSet::Analysis analysis; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 91 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 92 | const GrFragmentProcessor* clipFP = clip ? clip->clipCoverageFragmentProcessor() : nullptr; |
| 93 | GrColorFragmentProcessorAnalysis colorAnalysis(colorInput); |
| 94 | analysis.fCompatibleWithCoverageAsAlpha = GrProcessorAnalysisCoverage::kLCD != coverageInput; |
| 95 | |
| 96 | const GrFragmentProcessor* const* fps = fFragmentProcessors.get() + fFragmentProcessorOffset; |
| 97 | colorAnalysis.analyzeProcessors(fps, fColorFragmentProcessorCnt); |
| 98 | analysis.fCompatibleWithCoverageAsAlpha &= |
| 99 | colorAnalysis.allProcessorsCompatibleWithCoverageAsAlpha(); |
| 100 | fps += fColorFragmentProcessorCnt; |
| 101 | int n = this->numCoverageFragmentProcessors(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 102 | bool hasCoverageFP = n > 0; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 103 | bool coverageUsesLocalCoords = false; |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 104 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 105 | if (!fps[i]->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 106 | analysis.fCompatibleWithCoverageAsAlpha = false; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 107 | // Other than tests that exercise atypical behavior we expect all coverage FPs to be |
| 108 | // compatible with the coverage-as-alpha optimization. |
| 109 | GrCapsDebugf(&caps, "Coverage FP is not compatible with coverage as alpha.\n"); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 110 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 111 | coverageUsesLocalCoords |= fps[i]->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if (clipFP) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 115 | analysis.fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha(); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 116 | coverageUsesLocalCoords |= clipFP->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 117 | hasCoverageFP = true; |
| 118 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 119 | int colorFPsToEliminate = colorAnalysis.initialProcessorsToEliminate(overrideInputColor); |
| 120 | analysis.fInputColorType = static_cast<Analysis::PackedInputColorType>( |
| 121 | colorFPsToEliminate ? Analysis::kOverridden_InputColorType |
| 122 | : Analysis::kOriginal_InputColorType); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 123 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 124 | GrProcessorAnalysisCoverage outputCoverage; |
| 125 | if (GrProcessorAnalysisCoverage::kLCD == coverageInput) { |
| 126 | outputCoverage = GrProcessorAnalysisCoverage::kLCD; |
| 127 | } else if (hasCoverageFP || GrProcessorAnalysisCoverage::kSingleChannel == coverageInput) { |
| 128 | outputCoverage = GrProcessorAnalysisCoverage::kSingleChannel; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 129 | } else { |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 130 | outputCoverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 131 | } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 132 | |
| 133 | GrXPFactory::AnalysisProperties props = GrXPFactory::GetAnalysisProperties( |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 134 | this->xpFactory(), colorAnalysis.outputColor(), outputCoverage, caps); |
| 135 | if (!this->numCoverageFragmentProcessors() && |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 136 | GrProcessorAnalysisCoverage::kNone == coverageInput) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 137 | analysis.fCanCombineOverlappedStencilAndCover = SkToBool( |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 138 | props & GrXPFactory::AnalysisProperties::kCanCombineOverlappedStencilAndCover); |
| 139 | } else { |
| 140 | // If we have non-clipping coverage processors we don't try to merge stencil steps as its |
| 141 | // 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^] | 142 | analysis.fCanCombineOverlappedStencilAndCover = false; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 143 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 144 | analysis.fRequiresDstTexture = |
| 145 | SkToBool(props & GrXPFactory::AnalysisProperties::kRequiresDstTexture); |
| 146 | analysis.fCompatibleWithCoverageAsAlpha &= |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 147 | SkToBool(props & GrXPFactory::AnalysisProperties::kCompatibleWithAlphaAsCoverage); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 148 | analysis.fRequiresBarrierBetweenOverlappingDraws = SkToBool( |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 149 | props & GrXPFactory::AnalysisProperties::kRequiresBarrierBetweenOverlappingDraws); |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 150 | if (props & GrXPFactory::AnalysisProperties::kIgnoresInputColor) { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 151 | colorFPsToEliminate = this->numColorFragmentProcessors(); |
| 152 | analysis.fInputColorType = |
| 153 | static_cast<Analysis::PackedInputColorType>(Analysis::kIgnored_InputColorType); |
| 154 | analysis.fUsesLocalCoords = coverageUsesLocalCoords; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 155 | } else { |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 156 | analysis.fUsesLocalCoords = coverageUsesLocalCoords | colorAnalysis.usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 157 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 158 | for (int i = 0; i < colorFPsToEliminate; ++i) { |
| 159 | fFragmentProcessors[i]->unref(); |
| 160 | fFragmentProcessors[i] = nullptr; |
Brian Salomon | 5dac9b3 | 2017-04-08 02:53:30 +0000 | [diff] [blame] | 161 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame^] | 162 | for (int i = colorFPsToEliminate; i < fFragmentProcessors.count(); ++i) { |
| 163 | fFragmentProcessors[i]->addPendingExecution(); |
| 164 | fFragmentProcessors[i]->unref(); |
| 165 | } |
| 166 | fFragmentProcessorOffset = colorFPsToEliminate; |
| 167 | fColorFragmentProcessorCnt -= colorFPsToEliminate; |
| 168 | |
| 169 | auto xp = GrXPFactory::MakeXferProcessor(this->xpFactory(), colorAnalysis.outputColor(), |
| 170 | outputCoverage, isMixedSamples, caps); |
| 171 | fXP.fProcessor = xp.get(); |
| 172 | if (fXP.fProcessor) { |
| 173 | fXP.fProcessor->addPendingExecution(); |
| 174 | } |
| 175 | fFlags |= kFinalized_Flag; |
| 176 | |
| 177 | analysis.fIsInitialized = true; |
| 178 | return analysis; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 179 | } |