blob: 6ebb00bee2500a1fe089d853cb35be0d063ceb68 [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"
15
Brian Salomon5298dc82017-02-22 11:52:03 -050016class GrAppliedClip;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040017class GrXferProcessor;
Brian Salomon92ce5942017-01-18 11:01:10 -050018class GrXPFactory;
19
20class GrProcessorSet : private SkNoncopyable {
21public:
22 GrProcessorSet(GrPaint&& paint);
23
Brian Salomon54d212e2017-03-21 14:22:38 -040024 ~GrProcessorSet();
25
Brian Salomon92ce5942017-01-18 11:01:10 -050026 int numColorFragmentProcessors() const { return fColorFragmentProcessorCnt; }
27 int numCoverageFragmentProcessors() const {
Brian Salomon70288c02017-03-24 12:27:17 -040028 return this->numFragmentProcessors() - fColorFragmentProcessorCnt;
Brian Salomon92ce5942017-01-18 11:01:10 -050029 }
Brian Salomon70288c02017-03-24 12:27:17 -040030 int numFragmentProcessors() const {
31 return fFragmentProcessors.count() - fFragmentProcessorOffset;
32 }
Brian Salomon92ce5942017-01-18 11:01:10 -050033
34 const GrFragmentProcessor* colorFragmentProcessor(int idx) const {
35 SkASSERT(idx < fColorFragmentProcessorCnt);
Brian Salomon70288c02017-03-24 12:27:17 -040036 return fFragmentProcessors[idx + fFragmentProcessorOffset];
Brian Salomon92ce5942017-01-18 11:01:10 -050037 }
38 const GrFragmentProcessor* coverageFragmentProcessor(int idx) const {
Brian Salomon70288c02017-03-24 12:27:17 -040039 return fFragmentProcessors[idx + fColorFragmentProcessorCnt + fFragmentProcessorOffset];
Brian Salomon92ce5942017-01-18 11:01:10 -050040 }
41
Brian Salomon48d1b4c2017-04-08 07:38:53 -040042 const GrXferProcessor* xferProcessor() const {
43 SkASSERT(this->isFinalized());
44 return fXP.fProcessor;
45 }
Brian Salomond61c9d92017-04-10 10:54:25 -040046 sk_sp<const GrXferProcessor> refXferProcessor() const {
47 SkASSERT(this->isFinalized());
48 return sk_ref_sp(fXP.fProcessor);
49 }
Brian Salomon92ce5942017-01-18 11:01:10 -050050
Brian Salomonf87e2b92017-01-19 11:31:50 -050051 bool usesDistanceVectorField() const { return SkToBool(fFlags & kUseDistanceVectorField_Flag); }
Brian Salomon189098e72017-01-19 09:55:19 -050052
Brian Salomon48d1b4c2017-04-08 07:38:53 -040053 /** Comparisons are only legal on finalized processor sets. */
Brian Salomon54d212e2017-03-21 14:22:38 -040054 bool operator==(const GrProcessorSet& that) const;
55 bool operator!=(const GrProcessorSet& that) const { return !(*this == that); }
56
Brian Salomon5298dc82017-02-22 11:52:03 -050057 /**
Brian Salomon48d1b4c2017-04-08 07:38:53 -040058 * This is used to report results of processor analysis when a processor set is finalized (see
59 * below).
Brian Salomon5298dc82017-02-22 11:52:03 -050060 */
Brian Salomona811b122017-03-30 08:21:32 -040061 class Analysis {
Brian Salomon5298dc82017-02-22 11:52:03 -050062 public:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040063 Analysis(const Analysis&) = default;
64 Analysis() { *reinterpret_cast<uint32_t*>(this) = 0; }
Brian Salomon8d2f90b2017-03-13 09:11:58 -040065
Brian Salomon48d1b4c2017-04-08 07:38:53 -040066 bool isInitialized() const { return fIsInitialized; }
Brian Salomonbfafcba2017-03-02 08:49:19 -050067 bool usesLocalCoords() const { return fUsesLocalCoords; }
Brian Salomon31853842017-03-28 16:32:05 -040068 bool requiresDstTexture() const { return fRequiresDstTexture; }
69 bool canCombineOverlappedStencilAndCover() const {
70 return fCanCombineOverlappedStencilAndCover;
71 }
Brian Salomon4fc77402017-03-30 16:48:26 -040072 bool requiresBarrierBetweenOverlappingDraws() const {
73 return fRequiresBarrierBetweenOverlappingDraws;
74 }
Brian Salomon5298dc82017-02-22 11:52:03 -050075 bool isCompatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Brian Salomon48d1b4c2017-04-08 07:38:53 -040076
77 bool inputColorIsIgnored() const { return fInputColorType == kIgnored_InputColorType; }
78 bool inputColorIsOverridden() const {
79 return fInputColorType == kOverridden_InputColorType;
Brian Salomonc0b642c2017-03-27 13:09:36 -040080 }
Brian Salomon5298dc82017-02-22 11:52:03 -050081
82 private:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040083 enum InputColorType : uint32_t {
84 kOriginal_InputColorType,
85 kOverridden_InputColorType,
86 kIgnored_InputColorType
87 };
Brian Salomon5298dc82017-02-22 11:52:03 -050088
Brian Salomon48d1b4c2017-04-08 07:38:53 -040089 // MSVS 2015 won't pack different underlying types
90 using PackedBool = uint32_t;
91 using PackedInputColorType = uint32_t;
Brian Salomon5298dc82017-02-22 11:52:03 -050092
Brian Salomon8d2f90b2017-03-13 09:11:58 -040093 PackedBool fUsesLocalCoords : 1;
94 PackedBool fCompatibleWithCoverageAsAlpha : 1;
Brian Salomon31853842017-03-28 16:32:05 -040095 PackedBool fRequiresDstTexture : 1;
96 PackedBool fCanCombineOverlappedStencilAndCover : 1;
Brian Salomon4fc77402017-03-30 16:48:26 -040097 PackedBool fRequiresBarrierBetweenOverlappingDraws : 1;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040098 PackedBool fIsInitialized : 1;
99 PackedInputColorType fInputColorType : 2;
Brian Salomon70288c02017-03-24 12:27:17 -0400100
101 friend class GrProcessorSet;
Brian Salomon5298dc82017-02-22 11:52:03 -0500102 };
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400103 GR_STATIC_ASSERT(sizeof(Analysis) <= sizeof(uint32_t));
Brian Salomon5298dc82017-02-22 11:52:03 -0500104
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400105 /**
106 * This analyzes the processors given an op's input color and coverage as well as a clip. The
107 * state of the processor set may change to an equivalent but more optimal set of processors.
108 * This new state requires that the caller respect the returned 'inputColorOverride'. This is
109 * indicated by the returned Analysis's inputColorIsOverriden(). 'inputColorOverride' will not
110 * be written if the analysis does not override the input color.
111 *
112 * This must be called before the processor set is used to construct a GrPipeline and may only
113 * be called once.
114 *
115 * This also puts the processors in "pending execution" state and must be called when an op
116 * that owns a processor set is recorded to ensure pending and writes are propagated to
117 * resources referred to by the processors. Otherwise, data hazards may occur.
118 */
119 Analysis finalize(const GrProcessorAnalysisColor& colorInput,
120 const GrProcessorAnalysisCoverage coverageInput, const GrAppliedClip*,
121 bool isMixedSamples, const GrCaps&, GrColor* inputColorOverride);
122
123 bool isFinalized() const { return SkToBool(kFinalized_Flag & fFlags); }
Brian Salomon70288c02017-03-24 12:27:17 -0400124
Brian Salomon92ce5942017-01-18 11:01:10 -0500125private:
Brian Salomona811b122017-03-30 08:21:32 -0400126 // This absurdly large limit allows Analysis and this to pack fields together.
Brian Salomon70288c02017-03-24 12:27:17 -0400127 static constexpr int kMaxColorProcessors = UINT8_MAX;
Brian Salomon8d2f90b2017-03-13 09:11:58 -0400128
Brian Salomon611572c2017-04-28 08:57:12 -0400129 enum Flags : uint16_t { kUseDistanceVectorField_Flag = 0x1, kFinalized_Flag = 0x2 };
Brian Salomon8d2f90b2017-03-13 09:11:58 -0400130
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400131 union XP {
132 XP(const GrXPFactory* factory) : fFactory(factory) {}
133 const GrXPFactory* fFactory;
134 const GrXferProcessor* fProcessor;
135 };
136
137 const GrXPFactory* xpFactory() const {
138 SkASSERT(!this->isFinalized());
139 return fXP.fFactory;
140 }
141
Brian Salomon8d2f90b2017-03-13 09:11:58 -0400142 SkAutoSTArray<4, const GrFragmentProcessor*> fFragmentProcessors;
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400143 XP fXP;
Brian Salomon0f353322017-05-03 20:58:59 +0000144 uint8_t fColorFragmentProcessorCnt;
Brian Salomon70288c02017-03-24 12:27:17 -0400145 uint8_t fFragmentProcessorOffset = 0;
146 uint8_t fFlags;
Brian Salomon92ce5942017-01-18 11:01:10 -0500147};
148
149#endif