blob: 5dd1c4876e83532ce5a07d61d5d01fbce90cd76c [file] [log] [blame]
Brian Salomon92ce5942017-01-18 11:01:10 -05001/*
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 Salomona811b122017-03-30 08:21:32 -040013#include "GrProcessorAnalysis.h"
Brian Salomon92ce5942017-01-18 11:01:10 -050014#include "SkTemplates.h"
Hal Canaryce78bad2017-05-04 14:15:40 -040015#include "GrXferProcessor.h"
Brian Salomon92ce5942017-01-18 11:01:10 -050016
Brian Salomon5298dc82017-02-22 11:52:03 -050017class GrAppliedClip;
Brian Salomon92ce5942017-01-18 11:01:10 -050018class GrXPFactory;
19
Brian Salomon91326c32017-08-09 16:02:19 -040020class GrProcessorSet {
Brian Salomon6d4b65e2017-05-03 17:06:09 -040021private:
22 // Arbitrary constructor arg for empty set and analysis
23 enum class Empty { kEmpty };
24
Brian Salomon92ce5942017-01-18 11:01:10 -050025public:
Brian Salomon91326c32017-08-09 16:02:19 -040026 GrProcessorSet(GrPaint&&);
27 GrProcessorSet(SkBlendMode);
Brian Salomonaff329b2017-08-11 09:40:37 -040028 GrProcessorSet(std::unique_ptr<GrFragmentProcessor> colorFP);
Brian Salomon91326c32017-08-09 16:02:19 -040029 GrProcessorSet(GrProcessorSet&&);
30 GrProcessorSet(const GrProcessorSet&) = delete;
31 GrProcessorSet& operator=(const GrProcessorSet&) = delete;
Brian Salomon92ce5942017-01-18 11:01:10 -050032
Brian Salomon54d212e2017-03-21 14:22:38 -040033 ~GrProcessorSet();
34
Brian Salomon92ce5942017-01-18 11:01:10 -050035 int numColorFragmentProcessors() const { return fColorFragmentProcessorCnt; }
36 int numCoverageFragmentProcessors() const {
Brian Salomon70288c02017-03-24 12:27:17 -040037 return this->numFragmentProcessors() - fColorFragmentProcessorCnt;
Brian Salomon92ce5942017-01-18 11:01:10 -050038 }
Brian Salomon92ce5942017-01-18 11:01:10 -050039
40 const GrFragmentProcessor* colorFragmentProcessor(int idx) const {
41 SkASSERT(idx < fColorFragmentProcessorCnt);
Brian Salomonaff329b2017-08-11 09:40:37 -040042 return fFragmentProcessors[idx + fFragmentProcessorOffset].get();
Brian Salomon92ce5942017-01-18 11:01:10 -050043 }
44 const GrFragmentProcessor* coverageFragmentProcessor(int idx) const {
Brian Salomonaff329b2017-08-11 09:40:37 -040045 return fFragmentProcessors[idx + fColorFragmentProcessorCnt +
46 fFragmentProcessorOffset].get();
Brian Salomon92ce5942017-01-18 11:01:10 -050047 }
48
Brian Salomon48d1b4c2017-04-08 07:38:53 -040049 const GrXferProcessor* xferProcessor() const {
50 SkASSERT(this->isFinalized());
51 return fXP.fProcessor;
52 }
Brian Salomond61c9d92017-04-10 10:54:25 -040053 sk_sp<const GrXferProcessor> refXferProcessor() const {
54 SkASSERT(this->isFinalized());
55 return sk_ref_sp(fXP.fProcessor);
56 }
Brian Salomon92ce5942017-01-18 11:01:10 -050057
Brian Salomonaff329b2017-08-11 09:40:37 -040058 std::unique_ptr<const GrFragmentProcessor> detachColorFragmentProcessor(int idx) {
59 SkASSERT(idx < fColorFragmentProcessorCnt);
60 return std::move(fFragmentProcessors[idx + fFragmentProcessorOffset]);
61 }
62
63 std::unique_ptr<const GrFragmentProcessor> detachCoverageFragmentProcessor(int idx) {
64 return std::move(
65 fFragmentProcessors[idx + fFragmentProcessorOffset + fColorFragmentProcessorCnt]);
66 }
67
Brian Salomon48d1b4c2017-04-08 07:38:53 -040068 /** Comparisons are only legal on finalized processor sets. */
Brian Salomon54d212e2017-03-21 14:22:38 -040069 bool operator==(const GrProcessorSet& that) const;
70 bool operator!=(const GrProcessorSet& that) const { return !(*this == that); }
71
Brian Salomon5298dc82017-02-22 11:52:03 -050072 /**
Brian Salomon48d1b4c2017-04-08 07:38:53 -040073 * This is used to report results of processor analysis when a processor set is finalized (see
74 * below).
Brian Salomon5298dc82017-02-22 11:52:03 -050075 */
Brian Salomona811b122017-03-30 08:21:32 -040076 class Analysis {
Brian Salomon5298dc82017-02-22 11:52:03 -050077 public:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040078 Analysis(const Analysis&) = default;
79 Analysis() { *reinterpret_cast<uint32_t*>(this) = 0; }
Brian Salomon8d2f90b2017-03-13 09:11:58 -040080
Brian Salomon48d1b4c2017-04-08 07:38:53 -040081 bool isInitialized() const { return fIsInitialized; }
Brian Salomonbfafcba2017-03-02 08:49:19 -050082 bool usesLocalCoords() const { return fUsesLocalCoords; }
Brian Salomon31853842017-03-28 16:32:05 -040083 bool requiresDstTexture() const { return fRequiresDstTexture; }
84 bool canCombineOverlappedStencilAndCover() const {
85 return fCanCombineOverlappedStencilAndCover;
86 }
Brian Salomon4fc77402017-03-30 16:48:26 -040087 bool requiresBarrierBetweenOverlappingDraws() const {
88 return fRequiresBarrierBetweenOverlappingDraws;
89 }
Brian Salomon5298dc82017-02-22 11:52:03 -050090 bool isCompatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Brian Salomon48d1b4c2017-04-08 07:38:53 -040091
92 bool inputColorIsIgnored() const { return fInputColorType == kIgnored_InputColorType; }
93 bool inputColorIsOverridden() const {
94 return fInputColorType == kOverridden_InputColorType;
Brian Salomonc0b642c2017-03-27 13:09:36 -040095 }
Brian Salomon5298dc82017-02-22 11:52:03 -050096
97 private:
Brian Salomon6d4b65e2017-05-03 17:06:09 -040098 constexpr Analysis(Empty)
99 : fUsesLocalCoords(false)
100 , fCompatibleWithCoverageAsAlpha(true)
101 , fRequiresDstTexture(false)
102 , fCanCombineOverlappedStencilAndCover(true)
103 , fRequiresBarrierBetweenOverlappingDraws(false)
104 , fIsInitialized(true)
105 , fInputColorType(kOriginal_InputColorType) {}
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400106 enum InputColorType : uint32_t {
107 kOriginal_InputColorType,
108 kOverridden_InputColorType,
109 kIgnored_InputColorType
110 };
Brian Salomon5298dc82017-02-22 11:52:03 -0500111
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400112 // MSVS 2015 won't pack different underlying types
113 using PackedBool = uint32_t;
114 using PackedInputColorType = uint32_t;
Brian Salomon5298dc82017-02-22 11:52:03 -0500115
Brian Salomon8d2f90b2017-03-13 09:11:58 -0400116 PackedBool fUsesLocalCoords : 1;
117 PackedBool fCompatibleWithCoverageAsAlpha : 1;
Brian Salomon31853842017-03-28 16:32:05 -0400118 PackedBool fRequiresDstTexture : 1;
119 PackedBool fCanCombineOverlappedStencilAndCover : 1;
Brian Salomon4fc77402017-03-30 16:48:26 -0400120 PackedBool fRequiresBarrierBetweenOverlappingDraws : 1;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400121 PackedBool fIsInitialized : 1;
122 PackedInputColorType fInputColorType : 2;
Brian Salomon70288c02017-03-24 12:27:17 -0400123
124 friend class GrProcessorSet;
Brian Salomon5298dc82017-02-22 11:52:03 -0500125 };
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400126 GR_STATIC_ASSERT(sizeof(Analysis) <= sizeof(uint32_t));
Brian Salomon5298dc82017-02-22 11:52:03 -0500127
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400128 /**
129 * This analyzes the processors given an op's input color and coverage as well as a clip. The
130 * state of the processor set may change to an equivalent but more optimal set of processors.
131 * This new state requires that the caller respect the returned 'inputColorOverride'. This is
132 * indicated by the returned Analysis's inputColorIsOverriden(). 'inputColorOverride' will not
133 * be written if the analysis does not override the input color.
134 *
135 * This must be called before the processor set is used to construct a GrPipeline and may only
136 * be called once.
137 *
138 * This also puts the processors in "pending execution" state and must be called when an op
139 * that owns a processor set is recorded to ensure pending and writes are propagated to
140 * resources referred to by the processors. Otherwise, data hazards may occur.
141 */
142 Analysis finalize(const GrProcessorAnalysisColor& colorInput,
143 const GrProcessorAnalysisCoverage coverageInput, const GrAppliedClip*,
Brian Osman532b3f92018-07-11 10:02:07 -0400144 bool isMixedSamples, const GrCaps&, GrColor* inputColorOverride);
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400145
146 bool isFinalized() const { return SkToBool(kFinalized_Flag & fFlags); }
Brian Salomon70288c02017-03-24 12:27:17 -0400147
Brian Salomon44acb5b2017-07-18 19:59:24 -0400148 /** These are valid only for non-LCD coverage. */
Brian Salomon292bf7a2017-05-17 09:43:55 -0400149 static const GrProcessorSet& EmptySet();
Brian Salomon91326c32017-08-09 16:02:19 -0400150 static GrProcessorSet MakeEmptySet();
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400151 static constexpr const Analysis EmptySetAnalysis() { return Analysis(Empty::kEmpty); }
152
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400153 SkString dumpProcessors() const;
154
Robert Phillipsf1748f52017-09-14 14:11:24 -0400155 void visitProxies(const std::function<void(GrSurfaceProxy*)>& func) const {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400156 for (int i = 0; i < this->numFragmentProcessors(); ++i) {
157 GrFragmentProcessor::TextureAccessIter iter(this->fragmentProcessor(i));
Brian Salomone782f842018-07-31 13:53:11 -0400158 while (const GrFragmentProcessor::TextureSampler* sampler = iter.next()) {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400159 func(sampler->proxy());
160 }
161 }
162 }
163
Brian Salomon92ce5942017-01-18 11:01:10 -0500164private:
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400165 GrProcessorSet(Empty) : fXP((const GrXferProcessor*)nullptr), fFlags(kFinalized_Flag) {}
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400166
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400167 int numFragmentProcessors() const {
168 return fFragmentProcessors.count() - fFragmentProcessorOffset;
169 }
170
171 const GrFragmentProcessor* fragmentProcessor(int idx) const {
172 return fFragmentProcessors[idx + fFragmentProcessorOffset].get();
173 }
174
Brian Salomona811b122017-03-30 08:21:32 -0400175 // This absurdly large limit allows Analysis and this to pack fields together.
Brian Salomon70288c02017-03-24 12:27:17 -0400176 static constexpr int kMaxColorProcessors = UINT8_MAX;
Brian Salomon8d2f90b2017-03-13 09:11:58 -0400177
Brian Salomone23bffd2017-06-02 11:01:10 -0400178 enum Flags : uint16_t { kFinalized_Flag = 0x1 };
Brian Salomon8d2f90b2017-03-13 09:11:58 -0400179
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400180 union XP {
181 XP(const GrXPFactory* factory) : fFactory(factory) {}
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400182 XP(const GrXferProcessor* processor) : fProcessor(processor) {}
Brian Salomon91326c32017-08-09 16:02:19 -0400183 explicit XP(XP&& that) : fProcessor(that.fProcessor) {
184 SkASSERT(fProcessor == that.fProcessor);
185 that.fProcessor = nullptr;
186 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400187 const GrXPFactory* fFactory;
188 const GrXferProcessor* fProcessor;
189 };
190
191 const GrXPFactory* xpFactory() const {
192 SkASSERT(!this->isFinalized());
193 return fXP.fFactory;
194 }
195
Brian Salomonaff329b2017-08-11 09:40:37 -0400196 SkAutoSTArray<4, std::unique_ptr<const GrFragmentProcessor>> fFragmentProcessors;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400197 XP fXP;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400198 uint8_t fColorFragmentProcessorCnt = 0;
Brian Salomon70288c02017-03-24 12:27:17 -0400199 uint8_t fFragmentProcessorOffset = 0;
200 uint8_t fFlags;
Brian Salomon92ce5942017-01-18 11:01:10 -0500201};
202
203#endif