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