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" |
Hal Canary | ce78bad | 2017-05-04 14:15:40 -0400 | [diff] [blame] | 15 | #include "GrXferProcessor.h" |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 16 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 17 | class GrAppliedClip; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 18 | class GrXPFactory; |
| 19 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 20 | class GrProcessorSet { |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 21 | private: |
| 22 | // Arbitrary constructor arg for empty set and analysis |
| 23 | enum class Empty { kEmpty }; |
| 24 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 25 | public: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 26 | GrProcessorSet(GrPaint&&); |
| 27 | GrProcessorSet(SkBlendMode); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 28 | GrProcessorSet(std::unique_ptr<GrFragmentProcessor> colorFP); |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 29 | GrProcessorSet(GrProcessorSet&&); |
| 30 | GrProcessorSet(const GrProcessorSet&) = delete; |
| 31 | GrProcessorSet& operator=(const GrProcessorSet&) = delete; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 32 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 33 | ~GrProcessorSet(); |
| 34 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 35 | int numColorFragmentProcessors() const { return fColorFragmentProcessorCnt; } |
| 36 | int numCoverageFragmentProcessors() const { |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 37 | return this->numFragmentProcessors() - fColorFragmentProcessorCnt; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 38 | } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 39 | int numFragmentProcessors() const { |
| 40 | return fFragmentProcessors.count() - fFragmentProcessorOffset; |
| 41 | } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 42 | |
| 43 | const GrFragmentProcessor* colorFragmentProcessor(int idx) const { |
| 44 | SkASSERT(idx < fColorFragmentProcessorCnt); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 45 | return fFragmentProcessors[idx + fFragmentProcessorOffset].get(); |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 46 | } |
| 47 | const GrFragmentProcessor* coverageFragmentProcessor(int idx) const { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 48 | return fFragmentProcessors[idx + fColorFragmentProcessorCnt + |
| 49 | fFragmentProcessorOffset].get(); |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 52 | const GrXferProcessor* xferProcessor() const { |
| 53 | SkASSERT(this->isFinalized()); |
| 54 | return fXP.fProcessor; |
| 55 | } |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 56 | sk_sp<const GrXferProcessor> refXferProcessor() const { |
| 57 | SkASSERT(this->isFinalized()); |
| 58 | return sk_ref_sp(fXP.fProcessor); |
| 59 | } |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 60 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 61 | std::unique_ptr<const GrFragmentProcessor> detachColorFragmentProcessor(int idx) { |
| 62 | SkASSERT(idx < fColorFragmentProcessorCnt); |
| 63 | return std::move(fFragmentProcessors[idx + fFragmentProcessorOffset]); |
| 64 | } |
| 65 | |
| 66 | std::unique_ptr<const GrFragmentProcessor> detachCoverageFragmentProcessor(int idx) { |
| 67 | return std::move( |
| 68 | fFragmentProcessors[idx + fFragmentProcessorOffset + fColorFragmentProcessorCnt]); |
| 69 | } |
| 70 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 71 | /** Comparisons are only legal on finalized processor sets. */ |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 72 | bool operator==(const GrProcessorSet& that) const; |
| 73 | bool operator!=(const GrProcessorSet& that) const { return !(*this == that); } |
| 74 | |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 75 | /** |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 76 | * This is used to report results of processor analysis when a processor set is finalized (see |
| 77 | * below). |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 78 | */ |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 79 | class Analysis { |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 80 | public: |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 81 | Analysis(const Analysis&) = default; |
| 82 | Analysis() { *reinterpret_cast<uint32_t*>(this) = 0; } |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 83 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 84 | bool isInitialized() const { return fIsInitialized; } |
Brian Salomon | bfafcba | 2017-03-02 08:49:19 -0500 | [diff] [blame] | 85 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 86 | bool requiresDstTexture() const { return fRequiresDstTexture; } |
| 87 | bool canCombineOverlappedStencilAndCover() const { |
| 88 | return fCanCombineOverlappedStencilAndCover; |
| 89 | } |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 90 | bool requiresBarrierBetweenOverlappingDraws() const { |
| 91 | return fRequiresBarrierBetweenOverlappingDraws; |
| 92 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 93 | bool isCompatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 94 | |
| 95 | bool inputColorIsIgnored() const { return fInputColorType == kIgnored_InputColorType; } |
| 96 | bool inputColorIsOverridden() const { |
| 97 | return fInputColorType == kOverridden_InputColorType; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 98 | } |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 99 | |
| 100 | private: |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 101 | constexpr Analysis(Empty) |
| 102 | : fUsesLocalCoords(false) |
| 103 | , fCompatibleWithCoverageAsAlpha(true) |
| 104 | , fRequiresDstTexture(false) |
| 105 | , fCanCombineOverlappedStencilAndCover(true) |
| 106 | , fRequiresBarrierBetweenOverlappingDraws(false) |
| 107 | , fIsInitialized(true) |
| 108 | , fInputColorType(kOriginal_InputColorType) {} |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 109 | enum InputColorType : uint32_t { |
| 110 | kOriginal_InputColorType, |
| 111 | kOverridden_InputColorType, |
| 112 | kIgnored_InputColorType |
| 113 | }; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 114 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 115 | // MSVS 2015 won't pack different underlying types |
| 116 | using PackedBool = uint32_t; |
| 117 | using PackedInputColorType = uint32_t; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 118 | |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 119 | PackedBool fUsesLocalCoords : 1; |
| 120 | PackedBool fCompatibleWithCoverageAsAlpha : 1; |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 121 | PackedBool fRequiresDstTexture : 1; |
| 122 | PackedBool fCanCombineOverlappedStencilAndCover : 1; |
Brian Salomon | 4fc7740 | 2017-03-30 16:48:26 -0400 | [diff] [blame] | 123 | PackedBool fRequiresBarrierBetweenOverlappingDraws : 1; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 124 | PackedBool fIsInitialized : 1; |
| 125 | PackedInputColorType fInputColorType : 2; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 126 | |
| 127 | friend class GrProcessorSet; |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 128 | }; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 129 | GR_STATIC_ASSERT(sizeof(Analysis) <= sizeof(uint32_t)); |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 130 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 131 | /** |
| 132 | * This analyzes the processors given an op's input color and coverage as well as a clip. The |
| 133 | * state of the processor set may change to an equivalent but more optimal set of processors. |
| 134 | * This new state requires that the caller respect the returned 'inputColorOverride'. This is |
| 135 | * indicated by the returned Analysis's inputColorIsOverriden(). 'inputColorOverride' will not |
| 136 | * be written if the analysis does not override the input color. |
| 137 | * |
| 138 | * This must be called before the processor set is used to construct a GrPipeline and may only |
| 139 | * be called once. |
| 140 | * |
| 141 | * This also puts the processors in "pending execution" state and must be called when an op |
| 142 | * that owns a processor set is recorded to ensure pending and writes are propagated to |
| 143 | * resources referred to by the processors. Otherwise, data hazards may occur. |
| 144 | */ |
| 145 | Analysis finalize(const GrProcessorAnalysisColor& colorInput, |
| 146 | const GrProcessorAnalysisCoverage coverageInput, const GrAppliedClip*, |
| 147 | bool isMixedSamples, const GrCaps&, GrColor* inputColorOverride); |
| 148 | |
| 149 | bool isFinalized() const { return SkToBool(kFinalized_Flag & fFlags); } |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 150 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 151 | /** These are valid only for non-LCD coverage. */ |
Brian Salomon | 292bf7a | 2017-05-17 09:43:55 -0400 | [diff] [blame] | 152 | static const GrProcessorSet& EmptySet(); |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 153 | static GrProcessorSet MakeEmptySet(); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 154 | static constexpr const Analysis EmptySetAnalysis() { return Analysis(Empty::kEmpty); } |
| 155 | |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 156 | SkString dumpProcessors() const; |
| 157 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 158 | private: |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 159 | GrProcessorSet(Empty) : fXP((const GrXferProcessor*)nullptr), fFlags(kFinalized_Flag) {} |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 160 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 161 | // This absurdly large limit allows Analysis and this to pack fields together. |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 162 | static constexpr int kMaxColorProcessors = UINT8_MAX; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 163 | |
Brian Salomon | e23bffd | 2017-06-02 11:01:10 -0400 | [diff] [blame] | 164 | enum Flags : uint16_t { kFinalized_Flag = 0x1 }; |
Brian Salomon | 8d2f90b | 2017-03-13 09:11:58 -0400 | [diff] [blame] | 165 | |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 166 | union XP { |
| 167 | XP(const GrXPFactory* factory) : fFactory(factory) {} |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 168 | XP(const GrXferProcessor* processor) : fProcessor(processor) {} |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 169 | explicit XP(XP&& that) : fProcessor(that.fProcessor) { |
| 170 | SkASSERT(fProcessor == that.fProcessor); |
| 171 | that.fProcessor = nullptr; |
| 172 | } |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 173 | const GrXPFactory* fFactory; |
| 174 | const GrXferProcessor* fProcessor; |
| 175 | }; |
| 176 | |
| 177 | const GrXPFactory* xpFactory() const { |
| 178 | SkASSERT(!this->isFinalized()); |
| 179 | return fXP.fFactory; |
| 180 | } |
| 181 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 182 | SkAutoSTArray<4, std::unique_ptr<const GrFragmentProcessor>> fFragmentProcessors; |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 183 | XP fXP; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 184 | uint8_t fColorFragmentProcessorCnt = 0; |
Brian Salomon | 70288c0 | 2017-03-24 12:27:17 -0400 | [diff] [blame] | 185 | uint8_t fFragmentProcessorOffset = 0; |
| 186 | uint8_t fFlags; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | #endif |