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 | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 13 | #include "GrProcessorAnalysis.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 | /** |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 61 | * This is used to track analysis of color and coverage values through the processors. |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 62 | */ |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 63 | class Analysis { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 64 | public: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 65 | /** |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 66 | * This constructor allows an op to record its initial color in an Analysis member and then |
| 67 | * then run analysis later when the analysis inputs are available. If the analysis produces |
| 68 | * color fragment processor elimination then the input color is replaced by the expected |
| 69 | * input to the first non-eliminated processor. Otherwise, the original input color is |
| 70 | * preserved. The only reason to use this is to save space on the op by not separately |
| 71 | * storing the initial color. |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 72 | */ |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 73 | explicit Analysis(GrColor initialColor) : Analysis() { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 74 | fInputColor = initialColor; |
| 75 | fValidInputColor = true; |
| 76 | } |
| 77 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 78 | Analysis() |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 79 | : fIsInitializedWithProcessorSet(false) |
| 80 | , fCompatibleWithCoverageAsAlpha(true) |
| 81 | , fValidInputColor(false) |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 82 | , fRequiresDstTexture(false) |
| 83 | , fCanCombineOverlappedStencilAndCover(true) |
| 84 | , fIgnoresInputColor(false) |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 85 | , fRequiresBarrierBetweenOverlappingDraws(false) |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 86 | , fOutputCoverageType(static_cast<unsigned>(GrProcessorAnalysisCoverage::kNone)) |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 87 | , fOutputColorType(static_cast<unsigned>(ColorType::kUnknown)) |
| 88 | , fInitialColorProcessorsToEliminate(0) {} |
| 89 | |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 90 | // This version is used by a unit test that assumes no clip and no fragment processors. |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 91 | Analysis(const GrProcessorAnalysisColor&, GrProcessorAnalysisCoverage, const GrXPFactory*, |
| 92 | const GrCaps&); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 93 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 94 | void init(const GrProcessorAnalysisColor&, GrProcessorAnalysisCoverage, |
| 95 | const GrProcessorSet&, const GrAppliedClip*, const GrCaps&); |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 96 | |
| 97 | bool isInitializedWithProcessorSet() const { return fIsInitializedWithProcessorSet; } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 98 | |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 99 | /** |
| 100 | * If the return is greater than or equal to zero then 'newInputColor' should be used as the |
| 101 | * input color to the GrPipeline derived from this processor set, replacing the GrDrawOp's |
| 102 | * initial color. If the return is less than zero then newInputColor has not been |
| 103 | * modified and no modification need be made to the pipeline's input color by the op. |
| 104 | */ |
| 105 | int getInputColorOverrideAndColorProcessorEliminationCount(GrColor* newInputColor) const { |
| 106 | if (fValidInputColor) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 107 | *newInputColor = fInputColor; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 108 | return fInitialColorProcessorsToEliminate; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 109 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 110 | SkASSERT(!fInitialColorProcessorsToEliminate); |
| 111 | return -1; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Valid if initialProcessorsToEliminate returns true or this analysis was initialized with |
| 116 | * a known color via constructor or init(). If color fragment processors are eliminated then |
| 117 | * this returns the expected input to the first non-eliminated processors. Otherwise it is |
| 118 | * the color passed to the constructor or init(). |
| 119 | */ |
| 120 | GrColor inputColor() const { |
| 121 | SkASSERT(fValidInputColor); |
| 122 | return fInputColor; |
| 123 | } |
| 124 | |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 125 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 126 | bool requiresDstTexture() const { return fRequiresDstTexture; } |
| 127 | bool canCombineOverlappedStencilAndCover() const { |
| 128 | return fCanCombineOverlappedStencilAndCover; |
| 129 | } |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 130 | bool requiresBarrierBetweenOverlappingDraws() const { |
| 131 | return fRequiresBarrierBetweenOverlappingDraws; |
| 132 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 133 | bool isCompatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 134 | bool isInputColorIgnored() const { return fIgnoresInputColor; } |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 135 | GrProcessorAnalysisCoverage outputCoverage() const { |
| 136 | return static_cast<GrProcessorAnalysisCoverage>(fOutputCoverageType); |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 137 | } |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 138 | GrProcessorAnalysisColor outputColor() const { |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 139 | switch (this->outputColorType()) { |
| 140 | case ColorType::kConstant: |
| 141 | return fKnownOutputColor; |
| 142 | case ColorType::kOpaque: |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 143 | return GrProcessorAnalysisColor::Opaque::kYes; |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 144 | case ColorType::kUnknown: |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 145 | return GrProcessorAnalysisColor::Opaque::kNo; |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 146 | } |
| 147 | SkFAIL("Unexpected color type"); |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 148 | return GrProcessorAnalysisColor::Opaque::kNo; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 149 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 150 | |
| 151 | private: |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 152 | enum class ColorType : unsigned { kUnknown, kConstant, kOpaque }; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 153 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 154 | ColorType outputColorType() const { return static_cast<ColorType>(fOutputColorType); } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 155 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 156 | void internalInit(const GrProcessorAnalysisColor&, const GrProcessorAnalysisCoverage, |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 157 | const GrProcessorSet&, const GrFragmentProcessor* clipFP, const GrCaps&); |
| 158 | |
| 159 | // MSVS 2015 won't pack a bool with an unsigned. |
| 160 | using PackedBool = unsigned; |
| 161 | |
| 162 | PackedBool fIsInitializedWithProcessorSet : 1; |
| 163 | PackedBool fUsesLocalCoords : 1; |
| 164 | PackedBool fCompatibleWithCoverageAsAlpha : 1; |
| 165 | PackedBool fValidInputColor : 1; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 166 | PackedBool fRequiresDstTexture : 1; |
| 167 | PackedBool fCanCombineOverlappedStencilAndCover : 1; |
| 168 | // These could be removed if we created the XP from the XPFactory when doing analysis. |
| 169 | PackedBool fIgnoresInputColor : 1; |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 170 | PackedBool fRequiresBarrierBetweenOverlappingDraws : 1; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 171 | unsigned fOutputCoverageType : 2; |
| 172 | unsigned fOutputColorType : 2; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 173 | |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 174 | unsigned fInitialColorProcessorsToEliminate : 32 - 12; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 175 | |
| 176 | GrColor fInputColor; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 177 | // This could be removed if we created the XP from the XPFactory when doing analysis. |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 178 | GrColor fKnownOutputColor; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 179 | |
| 180 | friend class GrProcessorSet; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 181 | }; |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 182 | GR_STATIC_ASSERT(sizeof(Analysis) == 2 * sizeof(GrColor) + sizeof(uint32_t)); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 183 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 184 | void analyzeAndEliminateFragmentProcessors(Analysis*, |
| 185 | const GrProcessorAnalysisColor& colorInput, |
| 186 | const GrProcessorAnalysisCoverage coverageInput, |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 187 | const GrAppliedClip*, const GrCaps&); |
| 188 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 189 | private: |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 190 | // This absurdly large limit allows Analysis and this to pack fields together. |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 191 | static constexpr int kMaxColorProcessors = UINT8_MAX; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 192 | |
| 193 | enum Flags : uint16_t { |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 194 | kUseDistanceVectorField_Flag = 0x1, |
| 195 | kDisableOutputConversionToSRGB_Flag = 0x2, |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 196 | kAllowSRGBInputs_Flag = 0x4, |
| 197 | kPendingExecution_Flag = 0x8 |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 198 | }; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 199 | |
| 200 | const GrXPFactory* fXPFactory = nullptr; |
| 201 | SkAutoSTArray<4, const GrFragmentProcessor*> fFragmentProcessors; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 202 | uint8_t fColorFragmentProcessorCnt; |
| 203 | uint8_t fFragmentProcessorOffset = 0; |
| 204 | uint8_t fFlags; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | #endif |