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" |
| 11 | #include "GrProcOptInfo.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() { |
| 42 | if (this->isPendingExecution()) { |
| 43 | for (auto fp : fFragmentProcessors) { |
| 44 | fp->completedExecution(); |
| 45 | } |
| 46 | } else { |
| 47 | for (auto fp : fFragmentProcessors) { |
| 48 | fp->unref(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void GrProcessorSet::makePendingExecution() { |
| 54 | SkASSERT(!(kPendingExecution_Flag & fFlags)); |
| 55 | fFlags |= kPendingExecution_Flag; |
| 56 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 57 | fFragmentProcessors[i]->addPendingExecution(); |
| 58 | fFragmentProcessors[i]->unref(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool GrProcessorSet::operator==(const GrProcessorSet& that) const { |
| 63 | if (((fFlags ^ that.fFlags) & ~kPendingExecution_Flag) || |
| 64 | fFragmentProcessors.count() != that.fFragmentProcessors.count() || |
| 65 | fColorFragmentProcessorCnt != that.fColorFragmentProcessorCnt) { |
| 66 | return false; |
| 67 | } |
| 68 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 69 | if (!fFragmentProcessors[i]->isEqual(*that.fFragmentProcessors[i])) { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | if (fXPFactory != that.fXPFactory) { |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 79 | ////////////////////////////////////////////////////////////////////////////// |
| 80 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 81 | void GrProcessorSet::FragmentProcessorAnalysis::internalInit(const GrPipelineInput& colorInput, |
| 82 | const GrPipelineInput coverageInput, |
| 83 | const GrProcessorSet& processors, |
| 84 | const GrFragmentProcessor* clipFP, |
| 85 | const GrCaps& caps) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 86 | GrProcOptInfo colorInfo(colorInput); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 87 | fCompatibleWithCoverageAsAlpha = !coverageInput.isLCDCoverage(); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 88 | fValidInputColor = colorInput.isConstant(&fInputColor); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 89 | |
| 90 | const GrFragmentProcessor* const* fps = processors.fFragmentProcessors.get(); |
| 91 | colorInfo.analyzeProcessors(fps, processors.fColorFragmentProcessorCnt); |
| 92 | fCompatibleWithCoverageAsAlpha &= colorInfo.allProcessorsCompatibleWithCoverageAsAlpha(); |
| 93 | fps += processors.fColorFragmentProcessorCnt; |
| 94 | int n = processors.numCoverageFragmentProcessors(); |
| 95 | bool hasCoverageFP = n > 0; |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 96 | fUsesLocalCoords = colorInfo.usesLocalCoords(); |
| 97 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 98 | if (!fps[i]->compatibleWithCoverageAsAlpha()) { |
| 99 | fCompatibleWithCoverageAsAlpha = false; |
| 100 | // Other than tests that exercise atypical behavior we expect all coverage FPs to be |
| 101 | // compatible with the coverage-as-alpha optimization. |
| 102 | GrCapsDebugf(&caps, "Coverage FP is not compatible with coverage as alpha.\n"); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 103 | } |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 104 | fUsesLocalCoords |= fps[i]->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (clipFP) { |
| 108 | fCompatibleWithCoverageAsAlpha &= clipFP->compatibleWithCoverageAsAlpha(); |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 109 | fUsesLocalCoords |= clipFP->usesLocalCoords(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 110 | hasCoverageFP = true; |
| 111 | } |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 112 | fInitialColorProcessorsToEliminate = colorInfo.initialProcessorsToEliminate(&fInputColor); |
| 113 | fValidInputColor |= SkToBool(fInitialColorProcessorsToEliminate); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 114 | |
| 115 | bool opaque = colorInfo.isOpaque(); |
| 116 | if (colorInfo.hasKnownOutputColor(&fKnownOutputColor)) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 117 | fOutputColorType = static_cast<unsigned>(opaque ? ColorType::kOpaqueConstant |
| 118 | : ColorType::kConstant); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 119 | } else if (opaque) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 120 | fOutputColorType = static_cast<unsigned>(ColorType::kOpaque); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 121 | } else { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 122 | fOutputColorType = static_cast<unsigned>(ColorType::kUnknown); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (coverageInput.isLCDCoverage()) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 126 | fOutputCoverageType = static_cast<unsigned>(CoverageType::kLCD); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 127 | } else { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 128 | fOutputCoverageType = hasCoverageFP || !coverageInput.isSolidWhite() |
| 129 | ? static_cast<unsigned>(CoverageType::kSingleChannel) |
| 130 | : static_cast<unsigned>(CoverageType::kNone); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 134 | void GrProcessorSet::FragmentProcessorAnalysis::init(const GrPipelineInput& colorInput, |
| 135 | const GrPipelineInput coverageInput, |
| 136 | const GrProcessorSet& processors, |
| 137 | const GrAppliedClip* appliedClip, |
| 138 | const GrCaps& caps) { |
| 139 | const GrFragmentProcessor* clipFP = |
| 140 | appliedClip ? appliedClip->clipCoverageFragmentProcessor() : nullptr; |
| 141 | this->internalInit(colorInput, coverageInput, processors, clipFP, caps); |
| 142 | fIsInitializedWithProcessorSet = true; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | GrProcessorSet::FragmentProcessorAnalysis::FragmentProcessorAnalysis( |
| 146 | const GrPipelineInput& colorInput, const GrPipelineInput coverageInput, const GrCaps& caps) |
| 147 | : FragmentProcessorAnalysis() { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 148 | this->internalInit(colorInput, coverageInput, GrProcessorSet(GrPaint()), nullptr, caps); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 149 | } |