OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrOptDrawState.h" | 8 #include "GrOptDrawState.h" |
9 | 9 |
10 #include "GrDrawState.h" | 10 #include "GrDrawState.h" |
11 #include "GrDrawTargetCaps.h" | 11 #include "GrDrawTargetCaps.h" |
12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
13 #include "GrProcOptInfo.h" | 13 #include "GrProcOptInfo.h" |
| 14 #include "GrXferProcessor.h" |
14 | 15 |
15 GrOptDrawState::GrOptDrawState(const GrDrawState& drawState, | 16 GrOptDrawState::GrOptDrawState(const GrDrawState& drawState, |
16 const GrDrawTargetCaps& caps, | 17 const GrDrawTargetCaps& caps, |
17 const ScissorState& scissorState, | 18 const ScissorState& scissorState, |
18 const GrDeviceCoordTexture* dstCopy, | 19 const GrDeviceCoordTexture* dstCopy, |
19 GrGpu::DrawType drawType) | 20 GrGpu::DrawType drawType) |
20 : fFinalized(false) { | 21 : fFinalized(false) { |
21 fDrawType = drawType; | 22 fDrawType = drawType; |
22 GrBlendCoeff optSrcCoeff; | 23 |
23 GrBlendCoeff optDstCoeff; | 24 const GrProcOptInfo& colorPOI = drawState.colorProcInfo(); |
24 GrDrawState::BlendOpt blendOpt = drawState.getBlendOpt(false, &optSrcCoeff,
&optDstCoeff); | 25 const GrProcOptInfo& coveragePOI = drawState.coverageProcInfo(); |
| 26 |
| 27 fColor = colorPOI.inputColorToEffectiveStage(); |
| 28 fCoverage = drawState.getCoverage(); |
| 29 |
| 30 // Create XferProcessor from DS's XPFactory |
| 31 SkAutoTUnref<GrXferProcessor> xferProcessor( |
| 32 drawState.getXPFactory()->createXferProcessor(colorPOI, coveragePOI)); |
| 33 |
| 34 GrXferProcessor::OptFlags optFlags; |
| 35 if (xferProcessor) { |
| 36 fXferProcessor.reset(xferProcessor.get()); |
| 37 |
| 38 optFlags = xferProcessor->getOptimizations(colorPOI, |
| 39 coveragePOI, |
| 40 drawState.isCoverageDrawing()
, |
| 41 drawState.isColorWriteDisable
d(), |
| 42 drawState.getStencil().doesWr
ite(), |
| 43 &fColor, |
| 44 &fCoverage); |
| 45 } |
25 | 46 |
26 // When path rendering the stencil settings are not always set on the draw s
tate | 47 // When path rendering the stencil settings are not always set on the draw s
tate |
27 // so we must check the draw type. In cases where we will skip drawing we si
mply return a | 48 // so we must check the draw type. In cases where we will skip drawing we si
mply return a |
28 // null GrOptDrawState. | 49 // null GrOptDrawState. |
29 if (GrDrawState::kSkipDraw_BlendOpt == blendOpt && GrGpu::kStencilPath_DrawT
ype != drawType) { | 50 if (!xferProcessor || ((GrXferProcessor::kSkipDraw_OptFlag & optFlags) && |
| 51 GrGpu::kStencilPath_DrawType != drawType)) { |
30 // Set the fields that don't default init and return. The lack of a rend
er target will | 52 // Set the fields that don't default init and return. The lack of a rend
er target will |
31 // indicate that this can be skipped. | 53 // indicate that this can be skipped. |
32 fFlags = 0; | 54 fFlags = 0; |
33 fDrawFace = GrDrawState::kInvalid_DrawFace; | 55 fDrawFace = GrDrawState::kInvalid_DrawFace; |
34 fSrcBlend = kZero_GrBlendCoeff; | 56 fSrcBlend = kZero_GrBlendCoeff; |
35 fDstBlend = kZero_GrBlendCoeff; | 57 fDstBlend = kZero_GrBlendCoeff; |
36 fBlendConstant = 0x0; | 58 fBlendConstant = 0x0; |
37 fViewMatrix.reset(); | 59 fViewMatrix.reset(); |
38 return; | 60 return; |
39 } | 61 } |
40 | 62 |
41 fRenderTarget.reset(drawState.fRenderTarget.get()); | 63 fRenderTarget.reset(drawState.fRenderTarget.get()); |
42 SkASSERT(fRenderTarget); | 64 SkASSERT(fRenderTarget); |
43 fScissorState = scissorState; | 65 fScissorState = scissorState; |
44 fViewMatrix = drawState.getViewMatrix(); | 66 fViewMatrix = drawState.getViewMatrix(); |
45 fBlendConstant = drawState.getBlendConstant(); | |
46 fStencilSettings = drawState.getStencil(); | 67 fStencilSettings = drawState.getStencil(); |
47 fDrawFace = drawState.getDrawFace(); | 68 fDrawFace = drawState.getDrawFace(); |
48 fSrcBlend = optSrcCoeff; | |
49 fDstBlend = optDstCoeff; | |
50 | |
51 // TODO move this out of optDrawState | 69 // TODO move this out of optDrawState |
52 if (dstCopy) { | 70 if (dstCopy) { |
53 fDstCopy = *dstCopy; | 71 fDstCopy = *dstCopy; |
54 } | 72 } |
55 | 73 |
56 fFlags = 0; | 74 fFlags = 0; |
57 if (drawState.isHWAntialias()) { | 75 if (drawState.isHWAntialias()) { |
58 fFlags |= kHWAA_Flag; | 76 fFlags |= kHWAA_Flag; |
59 } | 77 } |
60 if (drawState.isColorWriteDisabled()) { | 78 if (drawState.isColorWriteDisabled()) { |
61 fFlags |= kDisableColorWrite_Flag; | 79 fFlags |= kDisableColorWrite_Flag; |
62 } | 80 } |
63 if (drawState.isDither()) { | 81 if (drawState.isDither()) { |
64 fFlags |= kDither_Flag; | 82 fFlags |= kDither_Flag; |
65 } | 83 } |
66 | 84 |
67 fDescInfo.fHasVertexColor = drawState.hasGeometryProcessor() && | 85 fDescInfo.fHasVertexColor = drawState.hasGeometryProcessor() && |
68 drawState.getGeometryProcessor()->hasVertexColor
(); | 86 drawState.getGeometryProcessor()->hasVertexColor
(); |
69 | 87 |
70 fDescInfo.fHasVertexCoverage = drawState.hasGeometryProcessor() && | 88 fDescInfo.fHasVertexCoverage = drawState.hasGeometryProcessor() && |
71 drawState.getGeometryProcessor()->hasVertexCo
verage(); | 89 drawState.getGeometryProcessor()->hasVertexCo
verage(); |
72 | 90 |
73 bool hasLocalCoords = drawState.hasGeometryProcessor() && | 91 bool hasLocalCoords = drawState.hasGeometryProcessor() && |
74 drawState.getGeometryProcessor()->hasLocalCoords(); | 92 drawState.getGeometryProcessor()->hasLocalCoords(); |
75 | 93 |
76 const GrProcOptInfo& colorPOI = drawState.colorProcInfo(); | |
77 int firstColorStageIdx = colorPOI.firstEffectiveStageIndex(); | 94 int firstColorStageIdx = colorPOI.firstEffectiveStageIndex(); |
78 fDescInfo.fInputColorIsUsed = colorPOI.inputColorIsUsed(); | 95 fDescInfo.fInputColorIsUsed = colorPOI.inputColorIsUsed(); |
79 fColor = colorPOI.inputColorToEffectiveStage(); | |
80 if (colorPOI.removeVertexAttrib()) { | 96 if (colorPOI.removeVertexAttrib()) { |
81 fDescInfo.fHasVertexColor = false; | 97 fDescInfo.fHasVertexColor = false; |
82 } | 98 } |
83 | 99 |
84 // TODO: Once we can handle single or four channel input into coverage stage
s then we can use | 100 // TODO: Once we can handle single or four channel input into coverage stage
s then we can use |
85 // drawState's coverageProcInfo (like color above) to set this initial infor
mation. | 101 // drawState's coverageProcInfo (like color above) to set this initial infor
mation. |
86 int firstCoverageStageIdx = 0; | 102 int firstCoverageStageIdx = 0; |
87 fDescInfo.fInputCoverageIsUsed = true; | 103 fDescInfo.fInputCoverageIsUsed = true; |
88 fCoverage = drawState.getCoverage(); | |
89 | 104 |
90 this->adjustProgramForBlendOpt(drawState, blendOpt, &firstColorStageIdx, | |
91 &firstCoverageStageIdx); | |
92 | 105 |
93 this->getStageStats(drawState, firstColorStageIdx, firstCoverageStageIdx, ha
sLocalCoords); | 106 GrXferProcessor::BlendInfo blendInfo; |
| 107 fXferProcessor->getBlendInfo(&blendInfo); |
| 108 fSrcBlend = blendInfo.fSrcBlend; |
| 109 fDstBlend = blendInfo.fDstBlend; |
| 110 fBlendConstant = blendInfo.fBlendConstant; |
| 111 |
| 112 this->adjustProgramFromOptimizations(drawState, optFlags, colorPOI, coverage
POI, |
| 113 &firstColorStageIdx, &firstCoverageStag
eIdx); |
| 114 |
| 115 fDescInfo.fRequiresLocalCoordAttrib = hasLocalCoords; |
94 | 116 |
95 // Copy GeometryProcesssor from DS or ODS | 117 // Copy GeometryProcesssor from DS or ODS |
96 SkASSERT(GrGpu::IsPathRenderingDrawType(drawType) || | 118 SkASSERT(GrGpu::IsPathRenderingDrawType(drawType) || |
97 GrGpu::kStencilPath_DrawType || | 119 GrGpu::kStencilPath_DrawType || |
98 drawState.hasGeometryProcessor()); | 120 drawState.hasGeometryProcessor()); |
99 fGeometryProcessor.reset(drawState.getGeometryProcessor()); | 121 fGeometryProcessor.reset(drawState.getGeometryProcessor()); |
100 | 122 |
101 // Create XferProcessor from DS's XPFactory | |
102 const GrXferProcessor* xpProcessor = drawState.getXPFactory()->createXferPro
cessor(); | |
103 fXferProcessor.reset(xpProcessor); | |
104 xpProcessor->unref(); | |
105 | |
106 // Copy Stages from DS to ODS | 123 // Copy Stages from DS to ODS |
107 for (int i = firstColorStageIdx; i < drawState.numColorStages(); ++i) { | 124 for (int i = firstColorStageIdx; i < drawState.numColorStages(); ++i) { |
108 SkNEW_APPEND_TO_TARRAY(&fFragmentStages, | 125 SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
109 GrPendingFragmentStage, | 126 GrPendingFragmentStage, |
110 (drawState.fColorStages[i], hasLocalCoords)); | 127 (drawState.fColorStages[i], hasLocalCoords)); |
111 } | 128 } |
| 129 |
112 fNumColorStages = fFragmentStages.count(); | 130 fNumColorStages = fFragmentStages.count(); |
113 for (int i = firstCoverageStageIdx; i < drawState.numCoverageStages(); ++i)
{ | 131 for (int i = firstCoverageStageIdx; i < drawState.numCoverageStages(); ++i)
{ |
114 SkNEW_APPEND_TO_TARRAY(&fFragmentStages, | 132 SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
115 GrPendingFragmentStage, | 133 GrPendingFragmentStage, |
116 (drawState.fCoverageStages[i], hasLocalCoords)); | 134 (drawState.fCoverageStages[i], hasLocalCoords)); |
117 } | 135 } |
118 | 136 |
119 this->setOutputStateInfo(drawState, blendOpt, caps); | |
120 | |
121 // let the GP init the batch tracker | 137 // let the GP init the batch tracker |
122 if (drawState.hasGeometryProcessor()) { | 138 if (drawState.hasGeometryProcessor()) { |
123 GrGeometryProcessor::InitBT init; | 139 GrGeometryProcessor::InitBT init; |
124 init.fOutputColor = fDescInfo.fInputColorIsUsed; | 140 init.fOutputColor = fDescInfo.fInputColorIsUsed; |
125 init.fOutputCoverage = fDescInfo.fInputCoverageIsUsed; | 141 init.fOutputCoverage = fDescInfo.fInputCoverageIsUsed; |
126 init.fColor = this->getColor(); | 142 init.fColor = this->getColor(); |
127 init.fCoverage = this->getCoverage(); | 143 init.fCoverage = this->getCoverage(); |
128 fGeometryProcessor->initBatchTracker(&fBatchTracker, init); | 144 fGeometryProcessor->initBatchTracker(&fBatchTracker, init); |
129 } | 145 } |
| 146 |
| 147 this->setOutputStateInfo(drawState, optFlags, caps); |
130 } | 148 } |
131 | 149 |
132 void GrOptDrawState::setOutputStateInfo(const GrDrawState& ds, | 150 void GrOptDrawState::setOutputStateInfo(const GrDrawState& ds, |
133 GrDrawState::BlendOpt blendOpt, | 151 GrXferProcessor::OptFlags optFlags, |
134 const GrDrawTargetCaps& caps) { | 152 const GrDrawTargetCaps& caps) { |
135 // Set this default and then possibly change our mind if there is coverage. | 153 // Set this default and then possibly change our mind if there is coverage. |
136 fDescInfo.fPrimaryOutputType = GrProgramDesc::kModulate_PrimaryOutputType; | 154 fDescInfo.fPrimaryOutputType = GrProgramDesc::kModulate_PrimaryOutputType; |
137 fDescInfo.fSecondaryOutputType = GrProgramDesc::kNone_SecondaryOutputType; | 155 fDescInfo.fSecondaryOutputType = GrProgramDesc::kNone_SecondaryOutputType; |
138 | 156 |
139 // Determine whether we should use dual source blending or shader code to ke
ep coverage | 157 // Determine whether we should use dual source blending or shader code to ke
ep coverage |
140 // separate from color. | 158 // separate from color. |
141 bool keepCoverageSeparate = !(GrDrawState::kCoverageAsAlpha_BlendOpt == blen
dOpt || | 159 bool keepCoverageSeparate = !(optFlags & GrXferProcessor::kSetCoverageDrawin
g_OptFlag); |
142 GrDrawState::kEmitCoverage_BlendOpt == blendOp
t); | |
143 if (keepCoverageSeparate && !ds.hasSolidCoverage()) { | 160 if (keepCoverageSeparate && !ds.hasSolidCoverage()) { |
144 if (caps.dualSourceBlendingSupport()) { | 161 if (caps.dualSourceBlendingSupport()) { |
145 if (kZero_GrBlendCoeff == fDstBlend) { | 162 if (kZero_GrBlendCoeff == fDstBlend) { |
146 // write the coverage value to second color | 163 // write the coverage value to second color |
147 fDescInfo.fSecondaryOutputType = GrProgramDesc::kCoverage_Second
aryOutputType; | 164 fDescInfo.fSecondaryOutputType = GrProgramDesc::kCoverage_Second
aryOutputType; |
148 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; | 165 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
149 } else if (kSA_GrBlendCoeff == fDstBlend) { | 166 } else if (kSA_GrBlendCoeff == fDstBlend) { |
150 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. | 167 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. |
151 fDescInfo.fSecondaryOutputType = GrProgramDesc::kCoverageISA_Sec
ondaryOutputType; | 168 fDescInfo.fSecondaryOutputType = GrProgramDesc::kCoverageISA_Sec
ondaryOutputType; |
152 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; | 169 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
153 } else if (kSC_GrBlendCoeff == fDstBlend) { | 170 } else if (kSC_GrBlendCoeff == fDstBlend) { |
154 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. | 171 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
covered. |
155 fDescInfo.fSecondaryOutputType = GrProgramDesc::kCoverageISC_Sec
ondaryOutputType; | 172 fDescInfo.fSecondaryOutputType = GrProgramDesc::kCoverageISC_Sec
ondaryOutputType; |
156 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; | 173 fDstBlend = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
157 } | 174 } |
158 } else if (fDescInfo.fReadsDst && | 175 } else if (fDescInfo.fReadsDst && |
159 kOne_GrBlendCoeff == fSrcBlend && | 176 kOne_GrBlendCoeff == fSrcBlend && |
160 kZero_GrBlendCoeff == fDstBlend) { | 177 kZero_GrBlendCoeff == fDstBlend) { |
161 fDescInfo.fPrimaryOutputType = GrProgramDesc::kCombineWithDst_Primar
yOutputType; | 178 fDescInfo.fPrimaryOutputType = GrProgramDesc::kCombineWithDst_Primar
yOutputType; |
162 } | 179 } |
163 } | 180 } |
164 } | 181 } |
165 | 182 |
166 void GrOptDrawState::adjustProgramForBlendOpt(const GrDrawState& ds, | 183 void GrOptDrawState::adjustProgramFromOptimizations(const GrDrawState& ds, |
167 GrDrawState::BlendOpt blendOpt, | 184 GrXferProcessor::OptFlags fl
ags, |
168 int* firstColorStageIdx, | 185 const GrProcOptInfo& colorPO
I, |
169 int* firstCoverageStageIdx) { | 186 const GrProcOptInfo& coverag
ePOI, |
170 switch (blendOpt) { | 187 int* firstColorStageIdx, |
171 case GrDrawState::kNone_BlendOpt: | 188 int* firstCoverageStageIdx)
{ |
172 case GrDrawState::kSkipDraw_BlendOpt: | 189 fDescInfo.fReadsDst = false; |
173 case GrDrawState::kCoverageAsAlpha_BlendOpt: | 190 fDescInfo.fReadsFragPosition = false; |
174 break; | 191 |
175 case GrDrawState::kEmitCoverage_BlendOpt: | 192 if (flags & GrXferProcessor::kClearColorStages_OptFlag) { |
176 fColor = 0xffffffff; | 193 fDescInfo.fInputColorIsUsed = true; |
177 fDescInfo.fInputColorIsUsed = true; | 194 *firstColorStageIdx = ds.numColorStages(); |
178 *firstColorStageIdx = ds.numColorStages(); | 195 fDescInfo.fHasVertexColor = false; |
179 fDescInfo.fHasVertexColor = false; | 196 } else { |
180 break; | 197 fDescInfo.fReadsDst = colorPOI.readsDst(); |
181 case GrDrawState::kEmitTransBlack_BlendOpt: | 198 fDescInfo.fReadsFragPosition = colorPOI.readsFragPosition(); |
182 fColor = 0; | 199 } |
183 fCoverage = 0xff; | 200 |
184 fDescInfo.fInputColorIsUsed = true; | 201 if (flags & GrXferProcessor::kClearCoverageStages_OptFlag) { |
185 fDescInfo.fInputCoverageIsUsed = true; | 202 fDescInfo.fInputCoverageIsUsed = true; |
186 *firstColorStageIdx = ds.numColorStages(); | 203 *firstCoverageStageIdx = ds.numCoverageStages(); |
187 *firstCoverageStageIdx = ds.numCoverageStages(); | 204 fDescInfo.fHasVertexCoverage = false; |
188 fDescInfo.fHasVertexColor = false; | 205 } else { |
189 fDescInfo.fHasVertexCoverage = false; | 206 if (coveragePOI.readsDst()) { |
190 break; | 207 fDescInfo.fReadsDst = true; |
| 208 } |
| 209 if (coveragePOI.readsFragPosition()) { |
| 210 fDescInfo.fReadsFragPosition = true; |
| 211 } |
191 } | 212 } |
192 } | 213 } |
193 | 214 |
194 static void get_stage_stats(const GrFragmentStage& stage, bool* readsDst, bool*
readsFragPosition) { | |
195 if (stage.getProcessor()->willReadDstColor()) { | |
196 *readsDst = true; | |
197 } | |
198 if (stage.getProcessor()->willReadFragmentPosition()) { | |
199 *readsFragPosition = true; | |
200 } | |
201 } | |
202 | |
203 void GrOptDrawState::getStageStats(const GrDrawState& ds, int firstColorStageIdx
, | |
204 int firstCoverageStageIdx, bool hasLocalCoord
s) { | |
205 // We will need a local coord attrib if there is one currently set on the op
tState and we are | |
206 // actually generating some effect code | |
207 fDescInfo.fRequiresLocalCoordAttrib = hasLocalCoords && | |
208 ds.numTotalStages() - firstColorStageIdx - firstCoverageStageIdx > 0; | |
209 | |
210 fDescInfo.fReadsDst = false; | |
211 fDescInfo.fReadsFragPosition = false; | |
212 | |
213 for (int s = firstColorStageIdx; s < ds.numColorStages(); ++s) { | |
214 const GrFragmentStage& stage = ds.getColorStage(s); | |
215 get_stage_stats(stage, &fDescInfo.fReadsDst, &fDescInfo.fReadsFragPositi
on); | |
216 } | |
217 for (int s = firstCoverageStageIdx; s < ds.numCoverageStages(); ++s) { | |
218 const GrFragmentStage& stage = ds.getCoverageStage(s); | |
219 get_stage_stats(stage, &fDescInfo.fReadsDst, &fDescInfo.fReadsFragPositi
on); | |
220 } | |
221 if (ds.hasGeometryProcessor()) { | |
222 const GrGeometryProcessor& gp = *ds.getGeometryProcessor(); | |
223 fDescInfo.fReadsFragPosition = fDescInfo.fReadsFragPosition || gp.willRe
adFragmentPosition(); | |
224 } | |
225 } | |
226 | |
227 void GrOptDrawState::finalize(GrGpu* gpu) { | 215 void GrOptDrawState::finalize(GrGpu* gpu) { |
228 gpu->buildProgramDesc(*this, fDescInfo, fDrawType, &fDesc); | 216 gpu->buildProgramDesc(*this, fDescInfo, fDrawType, &fDesc); |
229 fFinalized = true; | 217 fFinalized = true; |
230 } | 218 } |
231 | 219 |
232 //////////////////////////////////////////////////////////////////////////////// | 220 //////////////////////////////////////////////////////////////////////////////// |
233 | 221 |
234 bool GrOptDrawState::operator== (const GrOptDrawState& that) const { | 222 bool GrOptDrawState::operator== (const GrOptDrawState& that) const { |
235 if (fDescInfo != that.fDescInfo) { | 223 if (fDescInfo != that.fDescInfo) { |
236 return false; | 224 return false; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 SkASSERT(this->numFragmentStages() == that.numFragmentStages()); | 262 SkASSERT(this->numFragmentStages() == that.numFragmentStages()); |
275 for (int i = 0; i < this->numFragmentStages(); i++) { | 263 for (int i = 0; i < this->numFragmentStages(); i++) { |
276 | 264 |
277 if (this->getFragmentStage(i) != that.getFragmentStage(i)) { | 265 if (this->getFragmentStage(i) != that.getFragmentStage(i)) { |
278 return false; | 266 return false; |
279 } | 267 } |
280 } | 268 } |
281 return true; | 269 return true; |
282 } | 270 } |
283 | 271 |
OLD | NEW |