blob: 1e7b5e5ad1a336390ece85e67278569942369b8f [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;
joshualittc6bc58e2014-12-10 13:48:57 -080022class GrPathProcessor;
egdaniel3658f382014-09-15 07:01:59 -070023
egdaniel3658f382014-09-15 07:01:59 -070024/**
egdaniel89af44a2014-09-26 06:15:04 -070025 * Class that holds an optimized version of a GrDrawState. It is meant to be an immutable class,
26 * and contains all data needed to set the state for a gpu draw.
egdaniel3658f382014-09-15 07:01:59 -070027 */
bsalomon932f8662014-11-24 06:47:48 -080028class GrOptDrawState {
egdaniel3658f382014-09-15 07:01:59 -070029public:
bsalomonae59b772014-11-19 08:23:49 -080030 SK_DECLARE_INST_COUNT(GrOptDrawState)
31
joshualitt54e0c122014-11-19 09:38:51 -080032 typedef GrClipMaskManager::ScissorState ScissorState;
33
joshualittc6bc58e2014-12-10 13:48:57 -080034 GrOptDrawState(const GrDrawState& drawState, const GrGeometryProcessor*, const GrPathProcessor*,
35 const GrDrawTargetCaps&, const ScissorState&,
36 const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType);
egdanielb109ac22014-10-07 06:45:44 -070037
egdaniel170f90b2014-09-16 12:54:40 -070038 bool operator== (const GrOptDrawState& that) const;
bsalomonae59b772014-11-19 08:23:49 -080039 bool operator!= (const GrOptDrawState& that) const { return !(*this == that); }
egdaniel170f90b2014-09-16 12:54:40 -070040
egdaniel89af44a2014-09-26 06:15:04 -070041 /// @}
42
43 ///////////////////////////////////////////////////////////////////////////
44 /// @name Color
45 ////
46
47 GrColor getColor() const { return fColor; }
48
49 /// @}
50
51 ///////////////////////////////////////////////////////////////////////////
52 /// @name Coverage
53 ////
54
55 uint8_t getCoverage() const { return fCoverage; }
56
57 GrColor getCoverageColor() const {
58 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
59 }
60
61 /// @}
62
63 ///////////////////////////////////////////////////////////////////////////
64 /// @name Effect Stages
65 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
66 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
67 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
68 /// the fragment position, local coordinates).
69 ///
70 /// The stages are divided into two sets, color-computing and coverage-computing. The final
71 /// color stage produces the final pixel color. The coverage-computing stages function exactly
72 /// as the color-computing but the output of the final coverage stage is treated as a fractional
73 /// pixel coverage rather than as input to the src/dst color blend step.
74 ///
75 /// The input color to the first color-stage is either the constant color or interpolated
76 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
77 /// (usually full-coverage) or interpolated per-vertex coverage.
egdaniel89af44a2014-09-26 06:15:04 -070078 ////
79
egdanield9aa2182014-10-09 13:47:05 -070080 int numColorStages() const { return fNumColorStages; }
81 int numCoverageStages() const { return fFragmentStages.count() - fNumColorStages; }
82 int numFragmentStages() const { return fFragmentStages.count(); }
egdaniel89af44a2014-09-26 06:15:04 -070083 int numTotalStages() const {
egdaniel4dffc942014-12-10 07:43:49 -080084 // the + 1 at the end is for the xferProcessor which will always be present
85 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 : 0) + 1;
egdaniel89af44a2014-09-26 06:15:04 -070086 }
87
88 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualitta5305a12014-10-10 17:47:00 -070089 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
joshualitt87f48d92014-12-04 10:41:40 -080090 const GrBatchTracker& getBatchTracker() const { return fBatchTracker; }
egdaniel378092f2014-12-03 10:40:13 -080091
92 const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); }
93
bsalomonae59b772014-11-19 08:23:49 -080094 const GrPendingFragmentStage& getColorStage(int idx) const {
egdanield9aa2182014-10-09 13:47:05 -070095 SkASSERT(idx < this->numColorStages());
96 return fFragmentStages[idx];
97 }
bsalomonae59b772014-11-19 08:23:49 -080098 const GrPendingFragmentStage& getCoverageStage(int idx) const {
egdanield9aa2182014-10-09 13:47:05 -070099 SkASSERT(idx < this->numCoverageStages());
100 return fFragmentStages[fNumColorStages + idx];
101 }
bsalomonae59b772014-11-19 08:23:49 -0800102 const GrPendingFragmentStage& getFragmentStage(int idx) const {
103 return fFragmentStages[idx];
104 }
egdaniel89af44a2014-09-26 06:15:04 -0700105
106 /// @}
107
108 ///////////////////////////////////////////////////////////////////////////
109 /// @name Blending
110 ////
111
112 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
113 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
114
115 /**
116 * Retrieves the last value set by setBlendConstant()
117 * @return the blending constant value
118 */
119 GrColor getBlendConstant() const { return fBlendConstant; }
120
121 /// @}
122
123 ///////////////////////////////////////////////////////////////////////////
124 /// @name View Matrix
125 ////
126
127 /**
128 * Retrieves the current view matrix
129 * @return the current view matrix.
130 */
131 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
132
egdaniel89af44a2014-09-26 06:15:04 -0700133 /// @}
134
135 ///////////////////////////////////////////////////////////////////////////
136 /// @name Render Target
137 ////
138
139 /**
140 * Retrieves the currently set render-target.
141 *
142 * @return The currently set render target.
143 */
bsalomon37dd3312014-11-03 08:47:23 -0800144 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -0700145
146 /// @}
147
148 ///////////////////////////////////////////////////////////////////////////
149 /// @name Stencil
150 ////
151
152 const GrStencilSettings& getStencil() const { return fStencilSettings; }
153
154 /// @}
155
156 ///////////////////////////////////////////////////////////////////////////
joshualitt54e0c122014-11-19 09:38:51 -0800157 /// @name ScissorState
158 ////
159
160 const ScissorState& getScissorState() const { return fScissorState; }
161
162 /// @}
163
164
165 ///////////////////////////////////////////////////////////////////////////
bsalomon04ddf892014-11-19 12:36:22 -0800166 /// @name Boolean Queries
egdaniel89af44a2014-09-26 06:15:04 -0700167 ////
168
bsalomon04ddf892014-11-19 12:36:22 -0800169 bool isDitherState() const { return SkToBool(fFlags & kDither_Flag); }
170 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); }
171 bool isColorWriteDisabled() const { return SkToBool(fFlags & kDisableColorWrite_Flag); }
bsalomonb03c4a32014-11-20 09:56:11 -0800172 bool mustSkip() const { return NULL == this->getRenderTarget(); }
bsalomonae59b772014-11-19 08:23:49 -0800173
174 /// @}
175
egdaniel89af44a2014-09-26 06:15:04 -0700176 /**
bsalomonae59b772014-11-19 08:23:49 -0800177 * Gets whether the target is drawing clockwise, counterclockwise,
178 * or both faces.
179 * @return the current draw face(s).
egdaniel89af44a2014-09-26 06:15:04 -0700180 */
bsalomon04ddf892014-11-19 12:36:22 -0800181 GrDrawState::DrawFace getDrawFace() const { return fDrawFace; }
bsalomonae59b772014-11-19 08:23:49 -0800182
183 /// @}
184
185 ///////////////////////////////////////////////////////////////////////////
186
joshualittdafa4d02014-12-04 08:59:10 -0800187 GrGpu::DrawType drawType() const { return fDrawType; }
188
joshualitt9176e2c2014-11-20 07:28:52 -0800189 const GrDeviceCoordTexture* getDstCopy() const { return fDstCopy.texture() ? &fDstCopy : NULL; }
bsalomonae59b772014-11-19 08:23:49 -0800190
joshualittdafa4d02014-12-04 08:59:10 -0800191 // Finalize *MUST* be called before programDesc()
192 void finalize(GrGpu*);
193
194 const GrProgramDesc& programDesc() const { SkASSERT(fFinalized); return fDesc; }
bsalomonae59b772014-11-19 08:23:49 -0800195
196private:
egdaniel89af44a2014-09-26 06:15:04 -0700197 /**
bsalomon04ddf892014-11-19 12:36:22 -0800198 * Alter the program desc and inputs (attribs and processors) based on the blend optimization.
egdaniel170f90b2014-09-16 12:54:40 -0700199 */
egdaniel95131432014-12-09 11:15:43 -0800200 void adjustProgramFromOptimizations(const GrDrawState& ds,
201 GrXferProcessor::OptFlags,
202 const GrProcOptInfo& colorPOI,
203 const GrProcOptInfo& coveragePOI,
204 int* firstColorStageIdx,
205 int* firstCoverageStageIdx);
egdaniela7dc0a82014-09-17 08:25:05 -0700206
egdanielc0648242014-09-22 13:17:02 -0700207 /**
208 * Calculates the primary and secondary output types of the shader. For certain output types
209 * the function may adjust the blend coefficients. After this function is called the src and dst
210 * blend coeffs will represent those used by backend API.
211 */
joshualittc6bc58e2014-12-10 13:48:57 -0800212 void setOutputStateInfo(const GrDrawState& ds, GrXferProcessor::OptFlags,
egdaniel95131432014-12-09 11:15:43 -0800213 const GrDrawTargetCaps&);
egdanielc0648242014-09-22 13:17:02 -0700214
bsalomon04ddf892014-11-19 12:36:22 -0800215 enum Flags {
216 kDither_Flag = 0x1,
217 kHWAA_Flag = 0x2,
218 kDisableColorWrite_Flag = 0x4,
219 };
220
bsalomonae59b772014-11-19 08:23:49 -0800221 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget;
222 typedef SkSTArray<8, GrPendingFragmentStage> FragmentStageArray;
223 typedef GrPendingProgramElement<const GrGeometryProcessor> ProgramGeometryProcessor;
joshualittc6bc58e2014-12-10 13:48:57 -0800224 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800225 typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor;
bsalomonae59b772014-11-19 08:23:49 -0800226 RenderTarget fRenderTarget;
joshualitt54e0c122014-11-19 09:38:51 -0800227 ScissorState fScissorState;
egdaniel89af44a2014-09-26 06:15:04 -0700228 GrColor fColor;
229 SkMatrix fViewMatrix;
230 GrColor fBlendConstant;
egdaniel89af44a2014-09-26 06:15:04 -0700231 GrStencilSettings fStencilSettings;
232 uint8_t fCoverage;
bsalomon04ddf892014-11-19 12:36:22 -0800233 GrDrawState::DrawFace fDrawFace;
joshualitt9176e2c2014-11-20 07:28:52 -0800234 GrDeviceCoordTexture fDstCopy;
egdaniel89af44a2014-09-26 06:15:04 -0700235 GrBlendCoeff fSrcBlend;
236 GrBlendCoeff fDstBlend;
bsalomon04ddf892014-11-19 12:36:22 -0800237 uint32_t fFlags;
joshualitta5305a12014-10-10 17:47:00 -0700238 ProgramGeometryProcessor fGeometryProcessor;
joshualittc6bc58e2014-12-10 13:48:57 -0800239 ProgramPrimitiveProcessor fPrimitiveProcessor;
joshualitt87f48d92014-12-04 10:41:40 -0800240 GrBatchTracker fBatchTracker;
egdaniel378092f2014-12-03 10:40:13 -0800241 ProgramXferProcessor fXferProcessor;
joshualitta5305a12014-10-10 17:47:00 -0700242 FragmentStageArray fFragmentStages;
joshualittdafa4d02014-12-04 08:59:10 -0800243 GrGpu::DrawType fDrawType;
244 GrProgramDesc::DescInfo fDescInfo;
245 bool fFinalized;
egdanield9aa2182014-10-09 13:47:05 -0700246
247 // This function is equivalent to the offset into fFragmentStages where coverage stages begin.
joshualitta5305a12014-10-10 17:47:00 -0700248 int fNumColorStages;
egdaniel89af44a2014-09-26 06:15:04 -0700249
joshualitt79f8fae2014-10-28 17:59:26 -0700250 GrProgramDesc fDesc;
egdanielc0648242014-09-22 13:17:02 -0700251
egdaniel89af44a2014-09-26 06:15:04 -0700252 typedef SkRefCnt INHERITED;
egdaniel3658f382014-09-15 07:01:59 -0700253};
254
255#endif