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