blob: fa79314551e00195a89c1e8cfb2ba20278650229 [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;
joshualitt56995b52014-12-11 15:44:02 -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
joshualitt56995b52014-12-11 15:44:02 -080032 GrOptDrawState(const GrDrawState& drawState, const GrGeometryProcessor*, const GrPathProcessor*,
bsalomon3e791242014-12-17 13:43:13 -080033 const GrDrawTargetCaps&, const GrScissorState&,
joshualitt56995b52014-12-11 15:44:02 -080034 const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType);
egdanielb109ac22014-10-07 06:45:44 -070035
joshualitt9b989322014-12-15 14:16:27 -080036 /*
37 * Returns true if it is possible to combine the two GrOptDrawStates and it will update 'this'
38 * to subsume 'that''s draw.
39 */
40 bool combineIfPossible(const GrOptDrawState& that);
egdaniel89af44a2014-09-26 06:15:04 -070041
42 /// @}
43
44 ///////////////////////////////////////////////////////////////////////////
45 /// @name Effect Stages
46 /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
47 /// fragment shader. Its inputs are the output from the previous stage as well as some variables
48 /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
49 /// the fragment position, local coordinates).
50 ///
51 /// The stages are divided into two sets, color-computing and coverage-computing. The final
52 /// color stage produces the final pixel color. The coverage-computing stages function exactly
53 /// as the color-computing but the output of the final coverage stage is treated as a fractional
54 /// pixel coverage rather than as input to the src/dst color blend step.
55 ///
56 /// The input color to the first color-stage is either the constant color or interpolated
57 /// per-vertex colors. The input to the first coverage stage is either a constant coverage
58 /// (usually full-coverage) or interpolated per-vertex coverage.
egdaniel89af44a2014-09-26 06:15:04 -070059 ////
60
egdanield9aa2182014-10-09 13:47:05 -070061 int numColorStages() const { return fNumColorStages; }
62 int numCoverageStages() const { return fFragmentStages.count() - fNumColorStages; }
63 int numFragmentStages() const { return fFragmentStages.count(); }
egdaniel89af44a2014-09-26 06:15:04 -070064
joshualitt9b989322014-12-15 14:16:27 -080065 // TODO remove the GP specific calls when the PathProc can provide the same interface
egdaniel89af44a2014-09-26 06:15:04 -070066 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
joshualitta5305a12014-10-10 17:47:00 -070067 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryProcessor.get(); }
joshualitt9b989322014-12-15 14:16:27 -080068 const GrPrimitiveProcessor* getPrimitiveProcessor() const { return fPrimitiveProcessor.get(); }
joshualitt87f48d92014-12-04 10:41:40 -080069 const GrBatchTracker& getBatchTracker() const { return fBatchTracker; }
egdaniel378092f2014-12-03 10:40:13 -080070
71 const GrXferProcessor* getXferProcessor() const { return fXferProcessor.get(); }
72
bsalomonae59b772014-11-19 08:23:49 -080073 const GrPendingFragmentStage& getColorStage(int idx) const {
egdanield9aa2182014-10-09 13:47:05 -070074 SkASSERT(idx < this->numColorStages());
75 return fFragmentStages[idx];
76 }
bsalomonae59b772014-11-19 08:23:49 -080077 const GrPendingFragmentStage& getCoverageStage(int idx) const {
egdanield9aa2182014-10-09 13:47:05 -070078 SkASSERT(idx < this->numCoverageStages());
79 return fFragmentStages[fNumColorStages + idx];
80 }
bsalomonae59b772014-11-19 08:23:49 -080081 const GrPendingFragmentStage& getFragmentStage(int idx) const {
82 return fFragmentStages[idx];
83 }
egdaniel89af44a2014-09-26 06:15:04 -070084
85 /// @}
86
87 ///////////////////////////////////////////////////////////////////////////
egdaniel89af44a2014-09-26 06:15:04 -070088 /// @name Render Target
89 ////
90
91 /**
92 * Retrieves the currently set render-target.
93 *
94 * @return The currently set render target.
95 */
bsalomon37dd3312014-11-03 08:47:23 -080096 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
egdaniel89af44a2014-09-26 06:15:04 -070097
98 /// @}
99
100 ///////////////////////////////////////////////////////////////////////////
101 /// @name Stencil
102 ////
103
104 const GrStencilSettings& getStencil() const { return fStencilSettings; }
105
106 /// @}
107
108 ///////////////////////////////////////////////////////////////////////////
joshualitt54e0c122014-11-19 09:38:51 -0800109 /// @name ScissorState
110 ////
111
bsalomon3e791242014-12-17 13:43:13 -0800112 const GrScissorState& getScissorState() const { return fScissorState; }
joshualitt54e0c122014-11-19 09:38:51 -0800113
114 /// @}
115
joshualitt54e0c122014-11-19 09:38:51 -0800116 ///////////////////////////////////////////////////////////////////////////
bsalomon04ddf892014-11-19 12:36:22 -0800117 /// @name Boolean Queries
egdaniel89af44a2014-09-26 06:15:04 -0700118 ////
119
bsalomon04ddf892014-11-19 12:36:22 -0800120 bool isDitherState() const { return SkToBool(fFlags & kDither_Flag); }
121 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAA_Flag); }
bsalomonb03c4a32014-11-20 09:56:11 -0800122 bool mustSkip() const { return NULL == this->getRenderTarget(); }
bsalomonae59b772014-11-19 08:23:49 -0800123
124 /// @}
125
egdaniel89af44a2014-09-26 06:15:04 -0700126 /**
bsalomonae59b772014-11-19 08:23:49 -0800127 * Gets whether the target is drawing clockwise, counterclockwise,
128 * or both faces.
129 * @return the current draw face(s).
egdaniel89af44a2014-09-26 06:15:04 -0700130 */
bsalomon04ddf892014-11-19 12:36:22 -0800131 GrDrawState::DrawFace getDrawFace() const { return fDrawFace; }
bsalomonae59b772014-11-19 08:23:49 -0800132
133 /// @}
134
135 ///////////////////////////////////////////////////////////////////////////
136
joshualittdafa4d02014-12-04 08:59:10 -0800137 GrGpu::DrawType drawType() const { return fDrawType; }
138
joshualitt9176e2c2014-11-20 07:28:52 -0800139 const GrDeviceCoordTexture* getDstCopy() const { return fDstCopy.texture() ? &fDstCopy : NULL; }
bsalomonae59b772014-11-19 08:23:49 -0800140
joshualittdafa4d02014-12-04 08:59:10 -0800141 // Finalize *MUST* be called before programDesc()
142 void finalize(GrGpu*);
143
144 const GrProgramDesc& programDesc() const { SkASSERT(fFinalized); return fDesc; }
bsalomonae59b772014-11-19 08:23:49 -0800145
146private:
egdaniel89af44a2014-09-26 06:15:04 -0700147 /**
bsalomon04ddf892014-11-19 12:36:22 -0800148 * Alter the program desc and inputs (attribs and processors) based on the blend optimization.
egdaniel170f90b2014-09-16 12:54:40 -0700149 */
egdaniel95131432014-12-09 11:15:43 -0800150 void adjustProgramFromOptimizations(const GrDrawState& ds,
151 GrXferProcessor::OptFlags,
152 const GrProcOptInfo& colorPOI,
153 const GrProcOptInfo& coveragePOI,
154 int* firstColorStageIdx,
155 int* firstCoverageStageIdx);
egdaniela7dc0a82014-09-17 08:25:05 -0700156
egdanielc0648242014-09-22 13:17:02 -0700157 /**
158 * Calculates the primary and secondary output types of the shader. For certain output types
159 * the function may adjust the blend coefficients. After this function is called the src and dst
160 * blend coeffs will represent those used by backend API.
161 */
joshualitt56995b52014-12-11 15:44:02 -0800162 void setOutputStateInfo(const GrDrawState& ds, GrXferProcessor::OptFlags,
egdaniel95131432014-12-09 11:15:43 -0800163 const GrDrawTargetCaps&);
egdanielc0648242014-09-22 13:17:02 -0700164
bsalomon04ddf892014-11-19 12:36:22 -0800165 enum Flags {
166 kDither_Flag = 0x1,
167 kHWAA_Flag = 0x2,
bsalomon04ddf892014-11-19 12:36:22 -0800168 };
169
bsalomonae59b772014-11-19 08:23:49 -0800170 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget;
171 typedef SkSTArray<8, GrPendingFragmentStage> FragmentStageArray;
172 typedef GrPendingProgramElement<const GrGeometryProcessor> ProgramGeometryProcessor;
joshualitt56995b52014-12-11 15:44:02 -0800173 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
egdaniel378092f2014-12-03 10:40:13 -0800174 typedef GrPendingProgramElement<const GrXferProcessor> ProgramXferProcessor;
bsalomonae59b772014-11-19 08:23:49 -0800175 RenderTarget fRenderTarget;
bsalomon3e791242014-12-17 13:43:13 -0800176 GrScissorState fScissorState;
egdaniel89af44a2014-09-26 06:15:04 -0700177 GrStencilSettings fStencilSettings;
bsalomon04ddf892014-11-19 12:36:22 -0800178 GrDrawState::DrawFace fDrawFace;
joshualitt9176e2c2014-11-20 07:28:52 -0800179 GrDeviceCoordTexture fDstCopy;
bsalomon04ddf892014-11-19 12:36:22 -0800180 uint32_t fFlags;
joshualitta5305a12014-10-10 17:47:00 -0700181 ProgramGeometryProcessor fGeometryProcessor;
joshualitt56995b52014-12-11 15:44:02 -0800182 ProgramPrimitiveProcessor fPrimitiveProcessor;
joshualitt87f48d92014-12-04 10:41:40 -0800183 GrBatchTracker fBatchTracker;
egdaniel378092f2014-12-03 10:40:13 -0800184 ProgramXferProcessor fXferProcessor;
joshualitta5305a12014-10-10 17:47:00 -0700185 FragmentStageArray fFragmentStages;
joshualittdafa4d02014-12-04 08:59:10 -0800186 GrGpu::DrawType fDrawType;
187 GrProgramDesc::DescInfo fDescInfo;
188 bool fFinalized;
egdanield9aa2182014-10-09 13:47:05 -0700189
190 // This function is equivalent to the offset into fFragmentStages where coverage stages begin.
joshualitta5305a12014-10-10 17:47:00 -0700191 int fNumColorStages;
egdaniel89af44a2014-09-26 06:15:04 -0700192
joshualitt79f8fae2014-10-28 17:59:26 -0700193 GrProgramDesc fDesc;
egdanielc0648242014-09-22 13:17:02 -0700194
egdaniel89af44a2014-09-26 06:15:04 -0700195 typedef SkRefCnt INHERITED;
egdaniel3658f382014-09-15 07:01:59 -0700196};
197
198#endif