1 /*
2  * Copyright 2014 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 GrProcOptInfo_DEFINED
9 #define GrProcOptInfo_DEFINED
10 
11 #include "GrColor.h"
12 #include "GrInvariantOutput.h"
13 
14 class GrBatch;
15 class GrFragmentStage;
16 class GrFragmentProcessor;
17 class GrPrimitiveProcessor;
18 class GrProcessor;
19 
20 /**
21  * GrProcOptInfo gathers invariant data from a set of processor stages.It is used to recognize
22  * optimizations related to eliminating stages and vertex attributes that aren't necessary for a
23  * draw.
24  */
25 class GrProcOptInfo {
26 public:
GrProcOptInfo()27     GrProcOptInfo()
28         : fInOut(0, static_cast<GrColorComponentFlags>(0), false)
29         , fFirstEffectStageIndex(0)
30         , fInputColorIsUsed(true)
31         , fInputColor(0)
32         , fReadsFragPosition(false) {}
33 
34     void calcWithInitialValues(const GrFragmentStage*, int stageCount, GrColor startColor,
35                                GrColorComponentFlags flags, bool areCoverageStages);
36 
37     void calcColorWithBatch(const GrBatch*, const GrFragmentStage*, int stagecount);
38     void calcCoverageWithBatch(const GrBatch*, const GrFragmentStage*, int stagecount);
39 
40     // TODO delete these when batch is everywhere
41     void calcColorWithPrimProc(const GrPrimitiveProcessor*, const GrFragmentStage*, int stagecount);
42     void calcCoverageWithPrimProc(const GrPrimitiveProcessor*, const GrFragmentStage*,
43                                   int stagecount);
44 
isSolidWhite()45     bool isSolidWhite() const { return fInOut.isSolidWhite(); }
isOpaque()46     bool isOpaque() const { return fInOut.isOpaque(); }
isSingleComponent()47     bool isSingleComponent() const { return fInOut.isSingleComponent(); }
allStagesMultiplyInput()48     bool allStagesMultiplyInput() const { return fInOut.allStagesMulInput(); }
49 
50     // TODO: Once texture pixel configs quaries are updated, we no longer need this function.
51     // For now this function will correctly tell us if we are using LCD text or not and should only
52     // be called when looking at the coverage output.
isFourChannelOutput()53     bool isFourChannelOutput() const { return !fInOut.isSingleComponent() &&
54                                                fInOut.isLCDCoverage(); }
55 
color()56     GrColor color() const { return fInOut.color(); }
validFlags()57     uint8_t validFlags() const { return fInOut.validFlags(); }
58 
59     /**
60      * Returns the index of the first effective color stage. If an intermediate stage doesn't read
61      * its input or has a known output, then we can ignore all earlier stages since they will not
62      * affect the final output. Thus the first effective stage index is the index to the first stage
63      * that will have an effect on the final output.
64      *
65      * If stages before the firstEffectiveStageIndex are removed, corresponding values from
66      * inputColorIsUsed(), inputColorToEffectiveStage(), removeVertexAttribs(), and readsDst() must
67      * be used when setting up the draw to ensure correct drawing.
68      */
firstEffectiveStageIndex()69     int firstEffectiveStageIndex() const { return fFirstEffectStageIndex; }
70 
71     /**
72      * True if the first effective stage reads its input, false otherwise.
73      */
inputColorIsUsed()74     bool inputColorIsUsed() const { return fInputColorIsUsed; }
75 
76     /**
77      * If input color is used and per-vertex colors are not used, this is the input color to the
78      * first effective stage.
79      */
inputColorToEffectiveStage()80     GrColor inputColorToEffectiveStage() const { return fInputColor; }
81 
82     /**
83      * Returns true if any of the stages preserved by GrProcOptInfo read the frag position.
84      */
readsFragPosition()85     bool readsFragPosition() const { return fReadsFragPosition; }
86 
87 private:
88     void internalCalc(const GrFragmentStage*, int stagecount, bool initWillReadFragPosition);
89 
90     GrInvariantOutput fInOut;
91     int fFirstEffectStageIndex;
92     bool fInputColorIsUsed;
93     GrColor fInputColor;
94     bool fReadsFragPosition;
95 };
96 
97 #endif
98