blob: 55a5071d4179414af49eca990ec7166d934fe678 [file] [log] [blame]
egdaniel3658f382014-09-15 07:01:59 -07001/*
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 GrOptDrawState_DEFINED
9#define GrOptDrawState_DEFINED
10
egdanielb109ac22014-10-07 06:45:44 -070011#include "GrColor.h"
12#include "GrGpu.h"
bsalomonae59b772014-11-19 08:23:49 -080013#include "GrPendingFragmentStage.h"
joshualitt79f8fae2014-10-28 17:59:26 -070014#include "GrProgramDesc.h"
egdanielb109ac22014-10-07 06:45:44 -070015#include "GrStencil.h"
16#include "GrTypesPriv.h"
17#include "SkMatrix.h"
18#include "SkRefCnt.h"
19
joshualitt79f8fae2014-10-28 17:59:26 -070020class GrDeviceCoordTexture;
egdanielb109ac22014-10-07 06:45:44 -070021class GrDrawState;
egdaniel3658f382014-09-15 07:01:59 -070022
egdaniel3658f382014-09-15 07:01:59 -070023/**
egdaniel89af44a2014-09-26 06:15:04 -070024 * Class that holds an optimized version of a GrDrawState. It is meant to be an immutable class,
25 * and contains all data needed to set the state for a gpu draw.
egdaniel3658f382014-09-15 07:01:59 -070026 */
bsalomon932f8662014-11-24 06:47:48 -080027class GrOptDrawState {
egdaniel3658f382014-09-15 07:01:59 -070028public:
bsalomonae59b772014-11-19 08:23:49 -080029 SK_DECLARE_INST_COUNT(GrOptDrawState)
30
joshualitt54e0c122014-11-19 09:38:51 -080031 typedef GrClipMaskManager::ScissorState ScissorState;
32
joshualitt8c0f6152014-12-10 14:12:22 -080033 GrOptDrawState(const GrDrawState& drawState, GrColor, uint8_t coverage, const GrDrawTargetCaps&,
34 const ScissorState&, const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType);
egdanielb109ac22014-10-07 06:45:44 -070035
egdaniel170f90b2014-09-16 12:54:40 -070036 bool operator== (const GrOptDrawState& that) const;
bsalomonae59b772014-11-19 08:23:49 -080037 bool operator!= (const GrOptDrawState& that) const { return !(*this == that); }
egdaniel170f90b2014-09-16 12:54:40 -070038
egdaniel89af44a2014-09-26 06:15:04 -070039 /// @}
40
41 ///////////////////////////////////////////////////////////////////////////
42 /// @name Color
43 ////
44
45 GrColor getColor() const { return fColor; }
46
47 /// @}
48
49 ///////////////////////////////////////////////////////////////////////////
50 /// @name Coverage
51 ////
52
53 uint8_t getCoverage() const { return fCoverage; }
54
55 GrColor getCoverageColor() const {
56 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
57 }
58
59 /// @}
60
61 ///////////////////////////////////////////////////////////////////////////
62 /// @name Effect Stages
63 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
64 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
65 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
66 /// the fragment position, local coordinates).
67 ///
68 /// The stages are divided into two sets, color-computing and coverage-computing. The final
69 /// color stage produces the final pixel color. The coverage-computing stages function exactly
70 /// as the color-computing but the output of the final coverage stage is treated as a fractional
71 /// pixel coverage rather than as input to the src/dst color blend step.
72 ///
73 /// The input color to the first color-stage is either the constant color or interpolated
74 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
75 /// (usually full-coverage) or interpolated per-vertex coverage.
egdaniel89af44a2014-09-26 06:15:04 -070076 ////
77
egdanield9aa2182014-10-09 13:47:05 -070078 int numColorStages() const { return fNumColorStages; }
79 int numCoverageStages() const { return fFragmentStages.count() - fNumColorStages; }
80 int numFragmentStages() const { return fFragmentStages.count(); }
egdaniel89af44a2014-09-26 06:15:04 -070081 int numTotalStages() const {
egdaniel4dffc942014-12-10 07:43:49 -080082 // the + 1 at the end is for the xferProcessor which will always be present
83 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 : 0) + 1;
egdaniel89af44a2014-09-26 06:15:04 -070084 }
85
86 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualitta5305a12014-10-10 17:47:00 -070087 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
joshualitt87f48d92014-12-04 10:41:40 -080088 const GrBatchTracker& getBatchTracker() const { return fBatchTracker; }
egdaniel378092f2014-12-03 10:40:13 -080089
90 const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); }
91
bsalomonae59b772014-11-19 08:23:49 -080092 const GrPendingFragmentStage& getColorStage(int idx) const {
egdanield9aa2182014-10-09 13:47:05 -070093 SkASSERT(idx < this->numColorStages());
94 return fFragmentStages[idx];
95 }
bsalomonae59b772014-11-19 08:23:49 -080096 const GrPendingFragmentStage& getCoverageStage(int idx) const {
egdanield9aa2182014-10-09 13:47:05 -070097 SkASSERT(idx < this->numCoverageStages());
98 return fFragmentStages[fNumColorStages + idx];
99 }
bsalomonae59b772014-11-19 08:23:49 -0800100 const GrPendingFragmentStage& getFragmentStage(int idx) const {
101 return fFragmentStages[idx];
102 }
egdaniel89af44a2014-09-26 06:15:04 -0700103
104 /// @}
105
106 ///////////////////////////////////////////////////////////////////////////
107 /// @name Blending
108 ////
109
110 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
111 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
112
113 /**
114 * Retrieves the last value set by setBlendConstant()
115 * @return the blending constant value
116 */
117 GrColor getBlendConstant() const { return fBlendConstant; }
118
119 /// @}
120
121 ///////////////////////////////////////////////////////////////////////////
122 /// @name View Matrix
123 ////
124
125 /**
126 * Retrieves the current view matrix
127 * @return the current view matrix.
128 */
129 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
130
egdaniel89af44a2014-09-26 06:15:04 -0700131 /// @}
132
133 ///////////////////////////////////////////////////////////////////////////
134 /// @name Render Target
135 ////
136
137 /**
138 * Retrieves the currently set render-target.
139 *
140 * @return The currently set render target.
141 */
bsalomon37dd3312014-11-03 08:47:23 -0800142 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700143
144 /// @}
145
146 ///////////////////////////////////////////////////////////////////////////
147 /// @name Stencil
148 ////
149
150 const GrStencilSettings& getStencil() const { return fStencilSettings; }
151
152 /// @}
153
154 ///////////////////////////////////////////////////////////////////////////
joshualitt54e0c122014-11-19 09:38:51 -0800155 /// @name ScissorState
156 ////
157
158 const ScissorState& getScissorState() const { return fScissorState; }
159
160 /// @}
161
162
163 ///////////////////////////////////////////////////////////////////////////
bsalomon04ddf892014-11-19 12:36:22 -0800164 /// @name Boolean Queries
egdaniel89af44a2014-09-26 06:15:04 -0700165 ////
166
bsalomon04ddf892014-11-19 12:36:22 -0800167 bool isDitherState() const { return SkToBool(fFlags & kDither_Flag); }
168 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); }
169 bool isColorWriteDisabled() const { return SkToBool(fFlags & kDisableColorWrite_Flag); }
bsalomonb03c4a32014-11-20 09:56:11 -0800170 bool mustSkip() const { return NULL == this->getRenderTarget(); }
bsalomonae59b772014-11-19 08:23:49 -0800171
172 /// @}
173
egdaniel89af44a2014-09-26 06:15:04 -0700174 /**
bsalomonae59b772014-11-19 08:23:49 -0800175 * Gets whether the target is drawing clockwise, counterclockwise,
176 * or both faces.
177 * @return the current draw face(s).
egdaniel89af44a2014-09-26 06:15:04 -0700178 */
bsalomon04ddf892014-11-19 12:36:22 -0800179 GrDrawState::DrawFace getDrawFace() const { return fDrawFace; }
bsalomonae59b772014-11-19 08:23:49 -0800180
181 /// @}
182
183 ///////////////////////////////////////////////////////////////////////////
184
joshualittdafa4d02014-12-04 08:59:10 -0800185 GrGpu::DrawType drawType() const { return fDrawType; }
186
joshualitt9176e2c2014-11-20 07:28:52 -0800187 const GrDeviceCoordTexture* getDstCopy() const { return fDstCopy.texture() ? &fDstCopy : NULL; }
bsalomonae59b772014-11-19 08:23:49 -0800188
joshualittdafa4d02014-12-04 08:59:10 -0800189 // Finalize *MUST* be called before programDesc()
190 void finalize(GrGpu*);
191
192 const GrProgramDesc& programDesc() const { SkASSERT(fFinalized); return fDesc; }
bsalomonae59b772014-11-19 08:23:49 -0800193
194private:
egdaniel89af44a2014-09-26 06:15:04 -0700195 /**
bsalomon04ddf892014-11-19 12:36:22 -0800196 * Alter the program desc and inputs (attribs and processors) based on the blend optimization.
egdaniel170f90b2014-09-16 12:54:40 -0700197 */
egdaniel95131432014-12-09 11:15:43 -0800198 void adjustProgramFromOptimizations(const GrDrawState& ds,
199 GrXferProcessor::OptFlags,
200 const GrProcOptInfo& colorPOI,
201 const GrProcOptInfo& coveragePOI,
202 int* firstColorStageIdx,
203 int* firstCoverageStageIdx);
egdaniela7dc0a82014-09-17 08:25:05 -0700204
egdanielc0648242014-09-22 13:17:02 -0700205 /**
206 * Calculates the primary and secondary output types of the shader. For certain output types
207 * the function may adjust the blend coefficients. After this function is called the src and dst
208 * blend coeffs will represent those used by backend API.
209 */
joshualitt8c0f6152014-12-10 14:12:22 -0800210 void setOutputStateInfo(const GrDrawState& ds, GrColor coverage, GrXferProcessor::OptFlags,
egdaniel95131432014-12-09 11:15:43 -0800211 const GrDrawTargetCaps&);
egdanielc0648242014-09-22 13:17:02 -0700212
bsalomon04ddf892014-11-19 12:36:22 -0800213 enum Flags {
214 kDither_Flag = 0x1,
215 kHWAA_Flag = 0x2,
216 kDisableColorWrite_Flag = 0x4,
217 };
218
bsalomonae59b772014-11-19 08:23:49 -0800219 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget;
220 typedef SkSTArray<8, GrPendingFragmentStage> FragmentStageArray;
221 typedef GrPendingProgramElement<const GrGeometryProcessor> ProgramGeometryProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800222 typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor;
bsalomonae59b772014-11-19 08:23:49 -0800223 RenderTarget fRenderTarget;
joshualitt54e0c122014-11-19 09:38:51 -0800224 ScissorState fScissorState;
egdaniel89af44a2014-09-26 06:15:04 -0700225 GrColor fColor;
226 SkMatrix fViewMatrix;
227 GrColor fBlendConstant;
egdaniel89af44a2014-09-26 06:15:04 -0700228 GrStencilSettings fStencilSettings;
229 uint8_t fCoverage;
bsalomon04ddf892014-11-19 12:36:22 -0800230 GrDrawState::DrawFace fDrawFace;
joshualitt9176e2c2014-11-20 07:28:52 -0800231 GrDeviceCoordTexture fDstCopy;
egdaniel89af44a2014-09-26 06:15:04 -0700232 GrBlendCoeff fSrcBlend;
233 GrBlendCoeff fDstBlend;
bsalomon04ddf892014-11-19 12:36:22 -0800234 uint32_t fFlags;
joshualitta5305a12014-10-10 17:47:00 -0700235 ProgramGeometryProcessor fGeometryProcessor;
joshualitt87f48d92014-12-04 10:41:40 -0800236 GrBatchTracker fBatchTracker;
egdaniel378092f2014-12-03 10:40:13 -0800237 ProgramXferProcessor fXferProcessor;
joshualitta5305a12014-10-10 17:47:00 -0700238 FragmentStageArray fFragmentStages;
joshualittdafa4d02014-12-04 08:59:10 -0800239 GrGpu::DrawType fDrawType;
240 GrProgramDesc::DescInfo fDescInfo;
241 bool fFinalized;
egdanield9aa2182014-10-09 13:47:05 -0700242
243 // This function is equivalent to the offset into fFragmentStages where coverage stages begin.
joshualitta5305a12014-10-10 17:47:00 -0700244 int fNumColorStages;
egdaniel89af44a2014-09-26 06:15:04 -0700245
joshualitt79f8fae2014-10-28 17:59:26 -0700246 GrProgramDesc fDesc;
egdanielc0648242014-09-22 13:17:02 -0700247
egdaniel89af44a2014-09-26 06:15:04 -0700248 typedef SkRefCnt INHERITED;
egdaniel3658f382014-09-15 07:01:59 -0700249};
250
251#endif