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 | #ifndef GrProcessorSet_DEFINED |
| 9 | #define GrProcessorSet_DEFINED |
| 10 | |
| 11 | #include "GrFragmentProcessor.h" |
| 12 | #include "GrPaint.h" |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 13 | #include "GrPipelineAnalysis.h" |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 14 | #include "SkTemplates.h" |
| 15 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 16 | class GrAppliedClip; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 17 | class GrXPFactory; |
| 18 | |
| 19 | class GrProcessorSet : private SkNoncopyable { |
| 20 | public: |
| 21 | GrProcessorSet(GrPaint&& paint); |
| 22 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 23 | ~GrProcessorSet(); |
| 24 | |
| 25 | /** |
| 26 | * If an op is recorded with this processor set then this must be called to ensure pending |
| 27 | * reads and writes are propagated to resources referred to by the processors. Otherwise, |
| 28 | * data hazards may occur. |
| 29 | */ |
| 30 | void makePendingExecution(); |
| 31 | bool isPendingExecution() const { return SkToBool(kPendingExecution_Flag & fFlags); } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 32 | |
| 33 | int numColorFragmentProcessors() const { return fColorFragmentProcessorCnt; } |
| 34 | int numCoverageFragmentProcessors() const { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 35 | return this->numFragmentProcessors() - fColorFragmentProcessorCnt; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 36 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 37 | int numFragmentProcessors() const { |
| 38 | return fFragmentProcessors.count() - fFragmentProcessorOffset; |
| 39 | } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 40 | |
| 41 | const GrFragmentProcessor* colorFragmentProcessor(int idx) const { |
| 42 | SkASSERT(idx < fColorFragmentProcessorCnt); |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 43 | return fFragmentProcessors[idx + fFragmentProcessorOffset]; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 44 | } |
| 45 | const GrFragmentProcessor* coverageFragmentProcessor(int idx) const { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 46 | return fFragmentProcessors[idx + fColorFragmentProcessorCnt + fFragmentProcessorOffset]; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | const GrXPFactory* xpFactory() const { return fXPFactory; } |
| 50 | |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 51 | bool usesDistanceVectorField() const { return SkToBool(fFlags & kUseDistanceVectorField_Flag); } |
| 52 | bool disableOutputConversionToSRGB() const { |
| 53 | return SkToBool(fFlags & kDisableOutputConversionToSRGB_Flag); |
| 54 | } |
| 55 | bool allowSRGBInputs() const { return SkToBool(fFlags & kAllowSRGBInputs_Flag); } |
Brian Salomon | 189098e7 | 2017-01-19 09:55:19 -0500 | [diff] [blame] | 56 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 57 | bool operator==(const GrProcessorSet& that) const; |
| 58 | bool operator!=(const GrProcessorSet& that) const { return !(*this == that); } |
| 59 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 60 | /** |
| 61 | * This is used to track analysis of color and coverage values through the fragment processors. |
| 62 | */ |
| 63 | class FragmentProcessorAnalysis { |
| 64 | public: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 65 | /** |
| 66 | * This constructor allows an op to record its initial color in a FragmentProcessorAnalysis |
| 67 | * member and then run analysis later when the analysis inputs are available. If the |
| 68 | * analysis produces color fragment processor elimination then the input color is replaced |
| 69 | * by the expected input to the first non-eliminated processor. Otherwise, the original |
| 70 | * input color is preserved. The only reason to use this is to save space on the op by not |
| 71 | * separately storing the initial color. |
| 72 | */ |
| 73 | explicit FragmentProcessorAnalysis(GrColor initialColor) : FragmentProcessorAnalysis() { |
| 74 | fInputColor = initialColor; |
| 75 | fValidInputColor = true; |
| 76 | } |
| 77 | |
| 78 | FragmentProcessorAnalysis() |
| 79 | : fIsInitializedWithProcessorSet(false) |
| 80 | , fCompatibleWithCoverageAsAlpha(true) |
| 81 | , fValidInputColor(false) |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 82 | , fOutputCoverageType(static_cast<unsigned>(GrPipelineAnalysisCoverage::kNone)) |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 83 | , fOutputColorType(static_cast<unsigned>(ColorType::kUnknown)) |
| 84 | , fInitialColorProcessorsToEliminate(0) {} |
| 85 | |
| 86 | // This version is used by a unit test that assumes no clip, no processors, and no PLS. |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 87 | FragmentProcessorAnalysis(const GrPipelineAnalysisColor&, GrPipelineAnalysisCoverage, |
| 88 | const GrCaps&); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 89 | |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 90 | void init(const GrPipelineAnalysisColor&, GrPipelineAnalysisCoverage, const GrProcessorSet&, |
| 91 | const GrAppliedClip*, const GrCaps&); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 92 | |
| 93 | bool isInitializedWithProcessorSet() const { return fIsInitializedWithProcessorSet; } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 94 | |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 95 | /** |
| 96 | * If the return is greater than or equal to zero then 'newInputColor' should be used as the |
| 97 | * input color to the GrPipeline derived from this processor set, replacing the GrDrawOp's |
| 98 | * initial color. If the return is less than zero then newInputColor has not been |
| 99 | * modified and no modification need be made to the pipeline's input color by the op. |
| 100 | */ |
| 101 | int getInputColorOverrideAndColorProcessorEliminationCount(GrColor* newInputColor) const { |
| 102 | if (fValidInputColor) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 103 | *newInputColor = fInputColor; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 104 | return fInitialColorProcessorsToEliminate; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 105 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 106 | SkASSERT(!fInitialColorProcessorsToEliminate); |
| 107 | return -1; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 110 | /** |
| 111 | * Valid if initialProcessorsToEliminate returns true or this analysis was initialized with |
| 112 | * a known color via constructor or init(). If color fragment processors are eliminated then |
| 113 | * this returns the expected input to the first non-eliminated processors. Otherwise it is |
| 114 | * the color passed to the constructor or init(). |
| 115 | */ |
| 116 | GrColor inputColor() const { |
| 117 | SkASSERT(fValidInputColor); |
| 118 | return fInputColor; |
| 119 | } |
| 120 | |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 121 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 122 | bool isCompatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
| 123 | bool isOutputColorOpaque() const { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 124 | return ColorType::kOpaque == this->outputColorType() || |
| 125 | ColorType::kOpaqueConstant == this->outputColorType(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 126 | } |
| 127 | bool hasKnownOutputColor(GrColor* color = nullptr) const { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 128 | bool constant = ColorType::kConstant == this->outputColorType() || |
| 129 | ColorType::kOpaqueConstant == this->outputColorType(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 130 | if (constant && color) { |
| 131 | *color = fKnownOutputColor; |
| 132 | } |
| 133 | return constant; |
| 134 | } |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 135 | GrPipelineAnalysisCoverage outputCoverageType() const { |
| 136 | return static_cast<GrPipelineAnalysisCoverage>(fOutputCoverageType); |
| 137 | } |
| 138 | bool hasCoverage() const { |
| 139 | return this->outputCoverageType() != GrPipelineAnalysisCoverage::kNone; |
| 140 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 141 | |
| 142 | private: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 143 | enum class ColorType : unsigned { kUnknown, kOpaqueConstant, kConstant, kOpaque }; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 144 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 145 | ColorType outputColorType() const { return static_cast<ColorType>(fOutputColorType); } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 146 | |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 147 | void internalInit(const GrPipelineAnalysisColor&, const GrPipelineAnalysisCoverage, |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 148 | const GrProcessorSet&, const GrFragmentProcessor* clipFP, const GrCaps&); |
| 149 | |
| 150 | // MSVS 2015 won't pack a bool with an unsigned. |
| 151 | using PackedBool = unsigned; |
| 152 | |
| 153 | PackedBool fIsInitializedWithProcessorSet : 1; |
| 154 | PackedBool fUsesLocalCoords : 1; |
| 155 | PackedBool fCompatibleWithCoverageAsAlpha : 1; |
| 156 | PackedBool fValidInputColor : 1; |
| 157 | unsigned fOutputCoverageType : 2; |
| 158 | unsigned fOutputColorType : 2; |
| 159 | unsigned fInitialColorProcessorsToEliminate : 32 - 8; |
| 160 | |
| 161 | GrColor fInputColor; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 162 | GrColor fKnownOutputColor; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 163 | |
| 164 | friend class GrProcessorSet; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 165 | }; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 166 | GR_STATIC_ASSERT(sizeof(FragmentProcessorAnalysis) == 2 * sizeof(GrColor) + sizeof(uint32_t)); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 167 | |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 168 | void analyzeAndEliminateFragmentProcessors(FragmentProcessorAnalysis*, |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame^] | 169 | const GrPipelineAnalysisColor& colorInput, |
| 170 | const GrPipelineAnalysisCoverage coverageInput, |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 171 | const GrAppliedClip*, const GrCaps&); |
| 172 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 173 | private: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 174 | // This absurdly large limit allows FragmentProcessorAnalysis and this to pack fields together. |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 175 | static constexpr int kMaxColorProcessors = UINT8_MAX; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 176 | |
| 177 | enum Flags : uint16_t { |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 178 | kUseDistanceVectorField_Flag = 0x1, |
| 179 | kDisableOutputConversionToSRGB_Flag = 0x2, |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 180 | kAllowSRGBInputs_Flag = 0x4, |
| 181 | kPendingExecution_Flag = 0x8 |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 182 | }; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 183 | |
| 184 | const GrXPFactory* fXPFactory = nullptr; |
| 185 | SkAutoSTArray<4, const GrFragmentProcessor*> fFragmentProcessors; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 186 | uint8_t fColorFragmentProcessorCnt; |
| 187 | uint8_t fFragmentProcessorOffset = 0; |
| 188 | uint8_t fFlags; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | #endif |