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 | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 13 | #include "GrPipelineInput.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 { |
| 35 | return fFragmentProcessors.count() - fColorFragmentProcessorCnt; |
| 36 | } |
| 37 | int numFragmentProcessors() const { return fFragmentProcessors.count(); } |
| 38 | |
| 39 | const GrFragmentProcessor* colorFragmentProcessor(int idx) const { |
| 40 | SkASSERT(idx < fColorFragmentProcessorCnt); |
| 41 | return fFragmentProcessors[idx]; |
| 42 | } |
| 43 | const GrFragmentProcessor* coverageFragmentProcessor(int idx) const { |
| 44 | return fFragmentProcessors[idx + fColorFragmentProcessorCnt]; |
| 45 | } |
| 46 | |
| 47 | const GrXPFactory* xpFactory() const { return fXPFactory; } |
| 48 | |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 49 | bool usesDistanceVectorField() const { return SkToBool(fFlags & kUseDistanceVectorField_Flag); } |
| 50 | bool disableOutputConversionToSRGB() const { |
| 51 | return SkToBool(fFlags & kDisableOutputConversionToSRGB_Flag); |
| 52 | } |
| 53 | bool allowSRGBInputs() const { return SkToBool(fFlags & kAllowSRGBInputs_Flag); } |
Brian Salomon | 189098e7 | 2017-01-19 09:55:19 -0500 | [diff] [blame] | 54 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame^] | 55 | bool operator==(const GrProcessorSet& that) const; |
| 56 | bool operator!=(const GrProcessorSet& that) const { return !(*this == that); } |
| 57 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 58 | /** |
| 59 | * This is used to track analysis of color and coverage values through the fragment processors. |
| 60 | */ |
| 61 | class FragmentProcessorAnalysis { |
| 62 | public: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 63 | /** |
| 64 | * This constructor allows an op to record its initial color in a FragmentProcessorAnalysis |
| 65 | * member and then run analysis later when the analysis inputs are available. If the |
| 66 | * analysis produces color fragment processor elimination then the input color is replaced |
| 67 | * by the expected input to the first non-eliminated processor. Otherwise, the original |
| 68 | * input color is preserved. The only reason to use this is to save space on the op by not |
| 69 | * separately storing the initial color. |
| 70 | */ |
| 71 | explicit FragmentProcessorAnalysis(GrColor initialColor) : FragmentProcessorAnalysis() { |
| 72 | fInputColor = initialColor; |
| 73 | fValidInputColor = true; |
| 74 | } |
| 75 | |
| 76 | FragmentProcessorAnalysis() |
| 77 | : fIsInitializedWithProcessorSet(false) |
| 78 | , fCompatibleWithCoverageAsAlpha(true) |
| 79 | , fValidInputColor(false) |
| 80 | , fOutputCoverageType(static_cast<unsigned>(CoverageType::kNone)) |
| 81 | , fOutputColorType(static_cast<unsigned>(ColorType::kUnknown)) |
| 82 | , fInitialColorProcessorsToEliminate(0) {} |
| 83 | |
| 84 | // This version is used by a unit test that assumes no clip, no processors, and no PLS. |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 85 | FragmentProcessorAnalysis(const GrPipelineInput& colorInput, |
| 86 | const GrPipelineInput coverageInput, const GrCaps&); |
| 87 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 88 | void init(const GrPipelineInput& colorInput, const GrPipelineInput coverageInput, |
| 89 | const GrProcessorSet&, const GrAppliedClip*, const GrCaps&); |
| 90 | |
| 91 | bool isInitializedWithProcessorSet() const { return fIsInitializedWithProcessorSet; } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 92 | |
| 93 | int initialColorProcessorsToEliminate(GrColor* newInputColor) const { |
| 94 | if (fInitialColorProcessorsToEliminate > 0) { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 95 | SkASSERT(fValidInputColor); |
| 96 | *newInputColor = fInputColor; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 97 | } |
| 98 | return fInitialColorProcessorsToEliminate; |
| 99 | } |
| 100 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 101 | /** |
| 102 | * Valid if initialProcessorsToEliminate returns true or this analysis was initialized with |
| 103 | * a known color via constructor or init(). If color fragment processors are eliminated then |
| 104 | * this returns the expected input to the first non-eliminated processors. Otherwise it is |
| 105 | * the color passed to the constructor or init(). |
| 106 | */ |
| 107 | GrColor inputColor() const { |
| 108 | SkASSERT(fValidInputColor); |
| 109 | return fInputColor; |
| 110 | } |
| 111 | |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 112 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 113 | bool isCompatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
| 114 | bool isOutputColorOpaque() const { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 115 | return ColorType::kOpaque == this->outputColorType() || |
| 116 | ColorType::kOpaqueConstant == this->outputColorType(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 117 | } |
| 118 | bool hasKnownOutputColor(GrColor* color = nullptr) const { |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 119 | bool constant = ColorType::kConstant == this->outputColorType() || |
| 120 | ColorType::kOpaqueConstant == this->outputColorType(); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 121 | if (constant && color) { |
| 122 | *color = fKnownOutputColor; |
| 123 | } |
| 124 | return constant; |
| 125 | } |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 126 | bool hasCoverage() const { return CoverageType::kNone != this->outputCoverageType(); } |
| 127 | bool hasLCDCoverage() const { return CoverageType::kLCD == this->outputCoverageType(); } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 128 | |
| 129 | private: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 130 | enum class ColorType : unsigned { kUnknown, kOpaqueConstant, kConstant, kOpaque }; |
| 131 | enum class CoverageType : unsigned { kNone, kSingleChannel, kLCD }; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 132 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 133 | CoverageType outputCoverageType() const { |
| 134 | return static_cast<CoverageType>(fOutputCoverageType); |
| 135 | } |
| 136 | ColorType outputColorType() const { return static_cast<ColorType>(fOutputColorType); } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 137 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 138 | void internalInit(const GrPipelineInput& colorInput, const GrPipelineInput coverageInput, |
| 139 | const GrProcessorSet&, const GrFragmentProcessor* clipFP, const GrCaps&); |
| 140 | |
| 141 | // MSVS 2015 won't pack a bool with an unsigned. |
| 142 | using PackedBool = unsigned; |
| 143 | |
| 144 | PackedBool fIsInitializedWithProcessorSet : 1; |
| 145 | PackedBool fUsesLocalCoords : 1; |
| 146 | PackedBool fCompatibleWithCoverageAsAlpha : 1; |
| 147 | PackedBool fValidInputColor : 1; |
| 148 | unsigned fOutputCoverageType : 2; |
| 149 | unsigned fOutputColorType : 2; |
| 150 | unsigned fInitialColorProcessorsToEliminate : 32 - 8; |
| 151 | |
| 152 | GrColor fInputColor; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 153 | GrColor fKnownOutputColor; |
| 154 | }; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 155 | GR_STATIC_ASSERT(sizeof(FragmentProcessorAnalysis) == 2 * sizeof(GrColor) + sizeof(uint32_t)); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 156 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 157 | private: |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 158 | // This absurdly large limit allows FragmentProcessorAnalysis and this to pack fields together. |
| 159 | static constexpr int kMaxColorProcessors = SK_MaxU16; |
| 160 | |
| 161 | enum Flags : uint16_t { |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 162 | kUseDistanceVectorField_Flag = 0x1, |
| 163 | kDisableOutputConversionToSRGB_Flag = 0x2, |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame^] | 164 | kAllowSRGBInputs_Flag = 0x4, |
| 165 | kPendingExecution_Flag = 0x8 |
Brian Salomon | f87e2b9 | 2017-01-19 11:31:50 -0500 | [diff] [blame] | 166 | }; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 167 | |
| 168 | const GrXPFactory* fXPFactory = nullptr; |
| 169 | SkAutoSTArray<4, const GrFragmentProcessor*> fFragmentProcessors; |
| 170 | uint16_t fColorFragmentProcessorCnt; |
| 171 | uint16_t fFlags; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | #endif |