| 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 #ifndef GrOptDrawState_DEFINED | 8 #ifndef GrOptDrawState_DEFINED |
| 9 #define GrOptDrawState_DEFINED | 9 #define GrOptDrawState_DEFINED |
| 10 | 10 |
| 11 #include "GrColor.h" | 11 #include "GrColor.h" |
| 12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
| 13 #include "GrProcessorStage.h" | 13 #include "GrPendingFragmentStage.h" |
| 14 #include "GrProgramDesc.h" | 14 #include "GrProgramDesc.h" |
| 15 #include "GrStencil.h" | 15 #include "GrStencil.h" |
| 16 #include "GrTypesPriv.h" | 16 #include "GrTypesPriv.h" |
| 17 #include "SkMatrix.h" | 17 #include "SkMatrix.h" |
| 18 #include "SkRefCnt.h" | 18 #include "SkRefCnt.h" |
| 19 | 19 |
| 20 class GrDeviceCoordTexture; | 20 class GrDeviceCoordTexture; |
| 21 class GrDrawState; | 21 class GrDrawState; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Class that holds an optimized version of a GrDrawState. It is meant to be an
immutable class, | 24 * 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. | 25 * and contains all data needed to set the state for a gpu draw. |
| 26 */ | 26 */ |
| 27 class GrOptDrawState : public SkRefCnt { | 27 class GrOptDrawState : public SkRefCnt { |
| 28 public: | 28 public: |
| 29 SK_DECLARE_INST_COUNT(GrOptDrawState) |
| 30 |
| 29 /** | 31 /** |
| 30 * Returns a snapshot of the current optimized state. The GrOptDrawState is
reffed and ownership | 32 * Returns a snapshot of the current optimized state. The GrOptDrawState is
reffed and ownership |
| 31 * is given to the caller. | 33 * is given to the caller. |
| 32 */ | 34 */ |
| 33 static GrOptDrawState* Create(const GrDrawState& drawState, | 35 static GrOptDrawState* Create(const GrDrawState& drawState, |
| 34 GrGpu*, | 36 GrGpu*, |
| 35 const GrDeviceCoordTexture* dstCopy, | 37 const GrDeviceCoordTexture* dstCopy, |
| 36 GrGpu::DrawType drawType); | 38 GrGpu::DrawType drawType); |
| 37 | 39 |
| 38 bool operator== (const GrOptDrawState& that) const; | 40 bool operator== (const GrOptDrawState& that) const; |
| 41 bool operator!= (const GrOptDrawState& that) const { return !(*this == that)
; } |
| 39 | 42 |
| 40 /////////////////////////////////////////////////////////////////////////// | 43 /////////////////////////////////////////////////////////////////////////// |
| 41 /// @name Vertex Attributes | 44 /// @name Vertex Attributes |
| 42 //// | 45 //// |
| 43 | 46 |
| 44 enum { | 47 enum { |
| 45 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, | 48 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } | 51 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 /// the fragment position, local coordinates). | 83 /// the fragment position, local coordinates). |
| 81 /// | 84 /// |
| 82 /// The stages are divided into two sets, color-computing and coverage-compu
ting. The final | 85 /// The stages are divided into two sets, color-computing and coverage-compu
ting. The final |
| 83 /// color stage produces the final pixel color. The coverage-computing stage
s function exactly | 86 /// color stage produces the final pixel color. The coverage-computing stage
s function exactly |
| 84 /// as the color-computing but the output of the final coverage stage is tre
ated as a fractional | 87 /// as the color-computing but the output of the final coverage stage is tre
ated as a fractional |
| 85 /// pixel coverage rather than as input to the src/dst color blend step. | 88 /// pixel coverage rather than as input to the src/dst color blend step. |
| 86 /// | 89 /// |
| 87 /// The input color to the first color-stage is either the constant color or
interpolated | 90 /// The input color to the first color-stage is either the constant color or
interpolated |
| 88 /// per-vertex colors. The input to the first coverage stage is either a con
stant coverage | 91 /// per-vertex colors. The input to the first coverage stage is either a con
stant coverage |
| 89 /// (usually full-coverage) or interpolated per-vertex coverage. | 92 /// (usually full-coverage) or interpolated per-vertex coverage. |
| 90 /// | |
| 91 /// See the documentation of kCoverageDrawing_StateBit for information about
disabling the | |
| 92 /// the color / coverage distinction. | |
| 93 //// | 93 //// |
| 94 | 94 |
| 95 int numColorStages() const { return fNumColorStages; } | 95 int numColorStages() const { return fNumColorStages; } |
| 96 int numCoverageStages() const { return fFragmentStages.count() - fNumColorSt
ages; } | 96 int numCoverageStages() const { return fFragmentStages.count() - fNumColorSt
ages; } |
| 97 int numFragmentStages() const { return fFragmentStages.count(); } | 97 int numFragmentStages() const { return fFragmentStages.count(); } |
| 98 int numTotalStages() const { | 98 int numTotalStages() const { |
| 99 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 :
0); | 99 return this->numFragmentStages() + (this->hasGeometryProcessor() ? 1 :
0); |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()
); } | 102 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()
); } |
| 103 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryPr
ocessor.get(); } | 103 const GrGeometryProcessor* getGeometryProcessor() const { return fGeometryPr
ocessor.get(); } |
| 104 const GrFragmentStage& getColorStage(int idx) const { | 104 const GrPendingFragmentStage& getColorStage(int idx) const { |
| 105 SkASSERT(idx < this->numColorStages()); | 105 SkASSERT(idx < this->numColorStages()); |
| 106 return fFragmentStages[idx]; | 106 return fFragmentStages[idx]; |
| 107 } | 107 } |
| 108 const GrFragmentStage& getCoverageStage(int idx) const { | 108 const GrPendingFragmentStage& getCoverageStage(int idx) const { |
| 109 SkASSERT(idx < this->numCoverageStages()); | 109 SkASSERT(idx < this->numCoverageStages()); |
| 110 return fFragmentStages[fNumColorStages + idx]; | 110 return fFragmentStages[fNumColorStages + idx]; |
| 111 } | 111 } |
| 112 const GrFragmentStage& getFragmentStage(int idx) const { return fFragmentSta
ges[idx]; } | 112 const GrPendingFragmentStage& getFragmentStage(int idx) const { |
| 113 return fFragmentStages[idx]; |
| 114 } |
| 113 | 115 |
| 114 /// @} | 116 /// @} |
| 115 | 117 |
| 116 /////////////////////////////////////////////////////////////////////////// | 118 /////////////////////////////////////////////////////////////////////////// |
| 117 /// @name Blending | 119 /// @name Blending |
| 118 //// | 120 //// |
| 119 | 121 |
| 120 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } | 122 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 121 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } | 123 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 122 | 124 |
| 123 /** | 125 /** |
| 124 * Retrieves the last value set by setBlendConstant() | 126 * Retrieves the last value set by setBlendConstant() |
| 125 * @return the blending constant value | 127 * @return the blending constant value |
| 126 */ | 128 */ |
| 127 GrColor getBlendConstant() const { return fBlendConstant; } | 129 GrColor getBlendConstant() const { return fBlendConstant; } |
| 128 | 130 |
| 129 /// @} | 131 /// @} |
| 130 | 132 |
| 131 /////////////////////////////////////////////////////////////////////////// | 133 /////////////////////////////////////////////////////////////////////////// |
| 132 /// @name View Matrix | 134 /// @name View Matrix |
| 133 //// | 135 //// |
| 134 | 136 |
| 135 /** | 137 /** |
| 136 * Retrieves the current view matrix | 138 * Retrieves the current view matrix |
| 137 * @return the current view matrix. | 139 * @return the current view matrix. |
| 138 */ | 140 */ |
| 139 const SkMatrix& getViewMatrix() const { return fViewMatrix; } | 141 const SkMatrix& getViewMatrix() const { return fViewMatrix; } |
| 140 | 142 |
| 141 /** | |
| 142 * Retrieves the inverse of the current view matrix. | |
| 143 * | |
| 144 * If the current view matrix is invertible, return true, and if matrix | |
| 145 * is non-null, copy the inverse into it. If the current view matrix is | |
| 146 * non-invertible, return false and ignore the matrix parameter. | |
| 147 * | |
| 148 * @param matrix if not null, will receive a copy of the current inverse. | |
| 149 */ | |
| 150 bool getViewInverse(SkMatrix* matrix) const { | |
| 151 SkMatrix inverse; | |
| 152 if (fViewMatrix.invert(&inverse)) { | |
| 153 if (matrix) { | |
| 154 *matrix = inverse; | |
| 155 } | |
| 156 return true; | |
| 157 } | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 /// @} | 143 /// @} |
| 162 | 144 |
| 163 /////////////////////////////////////////////////////////////////////////// | 145 /////////////////////////////////////////////////////////////////////////// |
| 164 /// @name Render Target | 146 /// @name Render Target |
| 165 //// | 147 //// |
| 166 | 148 |
| 167 /** | 149 /** |
| 168 * Retrieves the currently set render-target. | 150 * Retrieves the currently set render-target. |
| 169 * | 151 * |
| 170 * @return The currently set render target. | 152 * @return The currently set render target. |
| 171 */ | 153 */ |
| 172 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } | 154 GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
| 173 | 155 |
| 174 /// @} | 156 /// @} |
| 175 | 157 |
| 176 /////////////////////////////////////////////////////////////////////////// | 158 /////////////////////////////////////////////////////////////////////////// |
| 177 /// @name Stencil | 159 /// @name Stencil |
| 178 //// | 160 //// |
| 179 | 161 |
| 180 const GrStencilSettings& getStencil() const { return fStencilSettings; } | 162 const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 181 | 163 |
| 182 /// @} | 164 /// @} |
| 183 | 165 |
| 184 /////////////////////////////////////////////////////////////////////////// | 166 /////////////////////////////////////////////////////////////////////////// |
| 185 /// @name State Flags | 167 /// @name State Flags |
| 186 //// | 168 //// |
| 187 | 169 |
| 188 /** | |
| 189 * Flags that affect rendering. Controlled using enable/disableState(). All | |
| 190 * default to disabled. | |
| 191 */ | |
| 192 enum StateBits { | |
| 193 /** | |
| 194 * Perform dithering. TODO: Re-evaluate whether we need this bit | |
| 195 */ | |
| 196 kDither_StateBit = 0x01, | |
| 197 /** | |
| 198 * Perform HW anti-aliasing. This means either HW FSAA, if supported by
the render target, | |
| 199 * or smooth-line rendering if a line primitive is drawn and line smooth
ing is supported by | |
| 200 * the 3D API. | |
| 201 */ | |
| 202 kHWAntialias_StateBit = 0x02, | |
| 203 /** | |
| 204 * Draws will respect the clip, otherwise the clip is ignored. | |
| 205 */ | |
| 206 kClip_StateBit = 0x04, | |
| 207 /** | |
| 208 * Disables writing to the color buffer. Useful when performing stencil | |
| 209 * operations. | |
| 210 */ | |
| 211 kNoColorWrites_StateBit = 0x08, | |
| 212 | |
| 213 /** | |
| 214 * Usually coverage is applied after color blending. The color is blende
d using the coeffs | |
| 215 * specified by setBlendFunc(). The blended color is then combined with
dst using coeffs | |
| 216 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing
a coverage mask. In | |
| 217 * this case there is no distinction between coverage and color and the
caller needs direct | |
| 218 * control over the blend coeffs. When set, there will be a single blend
step controlled by | |
| 219 * setBlendFunc() which will use coverage*color as the src color. | |
| 220 */ | |
| 221 kCoverageDrawing_StateBit = 0x10, | |
| 222 | |
| 223 // Users of the class may add additional bits to the vector | |
| 224 kDummyStateBit, | |
| 225 kLastPublicStateBit = kDummyStateBit-1, | |
| 226 }; | |
| 227 | |
| 228 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & f
FlagBits); } | |
| 229 | |
| 230 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); } | 170 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); } |
| 231 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_Stat
eBit); } | 171 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_Stat
eBit); } |
| 232 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } | |
| 233 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_
StateBit); } | 172 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_
StateBit); } |
| 234 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_S
tateBit); } | 173 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_S
tateBit); } |
| 235 | 174 |
| 236 /// @} | 175 /// @} |
| 237 | 176 |
| 238 /////////////////////////////////////////////////////////////////////////// | 177 /////////////////////////////////////////////////////////////////////////// |
| 239 /// @name Face Culling | 178 /// @name Face Culling |
| 240 //// | 179 //// |
| 241 | 180 |
| 242 enum DrawFace { | 181 enum DrawFace { |
| 243 kInvalid_DrawFace = -1, | 182 kInvalid_DrawFace = -1, |
| 244 | 183 |
| 245 kBoth_DrawFace, | 184 kBoth_DrawFace, |
| 246 kCCW_DrawFace, | 185 kCCW_DrawFace, |
| 247 kCW_DrawFace, | 186 kCW_DrawFace, |
| 248 }; | 187 }; |
| 249 | 188 |
| 250 /** | 189 /** |
| 251 * Gets whether the target is drawing clockwise, counterclockwise, | 190 * Gets whether the target is drawing clockwise, counterclockwise, |
| 252 * or both faces. | 191 * or both faces. |
| 253 * @return the current draw face(s). | 192 * @return the current draw face(s). |
| 254 */ | 193 */ |
| 255 DrawFace getDrawFace() const { return fDrawFace; } | 194 DrawFace getDrawFace() const { return fDrawFace; } |
| 256 | 195 |
| 257 /// @} | 196 /// @} |
| 258 | 197 |
| 259 /////////////////////////////////////////////////////////////////////////// | 198 /////////////////////////////////////////////////////////////////////////// |
| 260 | 199 |
| 261 /** Return type for CombineIfPossible. */ | |
| 262 enum CombinedState { | |
| 263 /** The GrDrawStates cannot be combined. */ | |
| 264 kIncompatible_CombinedState, | |
| 265 /** Either draw state can be used in place of the other. */ | |
| 266 kAOrB_CombinedState, | |
| 267 /** Use the first draw state. */ | |
| 268 kA_CombinedState, | |
| 269 /** Use the second draw state. */ | |
| 270 kB_CombinedState, | |
| 271 }; | |
| 272 | |
| 273 /// @} | |
| 274 | 200 |
| 275 const GrProgramDesc& programDesc() const { return fDesc; } | 201 const GrProgramDesc& programDesc() const { return fDesc; } |
| 276 | 202 |
| 277 private: | 203 private: |
| 204 // This is lifted from GrDrawState. This should be revised and made specific
to this class/ |
| 205 enum StateBits { |
| 206 /** |
| 207 * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 208 */ |
| 209 kDither_StateBit = 0x01, |
| 210 /** |
| 211 * Perform HW anti-aliasing. This means either HW FSAA, if supported by
the render target, |
| 212 * or smooth-line rendering if a line primitive is drawn and line smooth
ing is supported by |
| 213 * the 3D API. |
| 214 */ |
| 215 kHWAntialias_StateBit = 0x02, |
| 216 /** |
| 217 * Draws will respect the clip, otherwise the clip is ignored. |
| 218 */ |
| 219 kClip_StateBit = 0x04, |
| 220 /** |
| 221 * Disables writing to the color buffer. Useful when performing stencil |
| 222 * operations. |
| 223 */ |
| 224 kNoColorWrites_StateBit = 0x08, |
| 225 |
| 226 /** |
| 227 * Usually coverage is applied after color blending. The color is blende
d using the coeffs |
| 228 * specified by setBlendFunc(). The blended color is then combined with
dst using coeffs |
| 229 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing
a coverage mask. In |
| 230 * this case there is no distinction between coverage and color and the
caller needs direct |
| 231 * control over the blend coeffs. When set, there will be a single blend
step controlled by |
| 232 * setBlendFunc() which will use coverage*color as the src color. |
| 233 */ |
| 234 kCoverageDrawing_StateBit = 0x10 |
| 235 }; |
| 236 |
| 278 /** | 237 /** |
| 279 * Optimizations for blending / coverage to that can be applied based on the
current state. | 238 * Optimizations for blending / coverage to that can be applied based on the
current state. |
| 280 */ | 239 */ |
| 281 enum BlendOptFlags { | 240 enum BlendOptFlags { |
| 282 /** | 241 /** |
| 283 * No optimization | 242 * No optimization |
| 284 */ | 243 */ |
| 285 kNone_BlendOpt = 0, | 244 kNone_BlendOpt = 0, |
| 286 /** | 245 /** |
| 287 * Don't draw at all | 246 * Don't draw at all |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 GrProgramDesc::DescInfo*); | 310 GrProgramDesc::DescInfo*); |
| 352 | 311 |
| 353 /** | 312 /** |
| 354 * Calculates the primary and secondary output types of the shader. For cert
ain output types | 313 * Calculates the primary and secondary output types of the shader. For cert
ain output types |
| 355 * the function may adjust the blend coefficients. After this function is ca
lled the src and dst | 314 * the function may adjust the blend coefficients. After this function is ca
lled the src and dst |
| 356 * blend coeffs will represent those used by backend API. | 315 * blend coeffs will represent those used by backend API. |
| 357 */ | 316 */ |
| 358 void setOutputStateInfo(const GrDrawState& ds, const GrDrawTargetCaps&, | 317 void setOutputStateInfo(const GrDrawState& ds, const GrDrawTargetCaps&, |
| 359 GrProgramDesc::DescInfo*); | 318 GrProgramDesc::DescInfo*); |
| 360 | 319 |
| 361 bool isEqual(const GrOptDrawState& that) const; | 320 typedef GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> RenderTarget; |
| 362 | 321 typedef SkSTArray<8, GrPendingFragmentStage> FragmentStageArray; |
| 363 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== | 322 typedef GrPendingProgramElement<const GrGeometryProcessor> ProgramGeometryPr
ocessor; |
| 364 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget; | 323 RenderTarget fRenderTarget; |
| 365 ProgramRenderTarget fRenderTarget; | |
| 366 GrColor fColor; | 324 GrColor fColor; |
| 367 SkMatrix fViewMatrix; | 325 SkMatrix fViewMatrix; |
| 368 GrColor fBlendConstant; | 326 GrColor fBlendConstant; |
| 369 uint32_t fFlagBits; | 327 uint32_t fFlagBits; |
| 370 const GrVertexAttrib* fVAPtr; | 328 const GrVertexAttrib* fVAPtr; |
| 371 int fVACount; | 329 int fVACount; |
| 372 size_t fVAStride; | 330 size_t fVAStride; |
| 373 GrStencilSettings fStencilSettings; | 331 GrStencilSettings fStencilSettings; |
| 374 uint8_t fCoverage; | 332 uint8_t fCoverage; |
| 375 DrawFace fDrawFace; | 333 DrawFace fDrawFace; |
| 376 GrBlendCoeff fSrcBlend; | 334 GrBlendCoeff fSrcBlend; |
| 377 GrBlendCoeff fDstBlend; | 335 GrBlendCoeff fDstBlend; |
| 378 | 336 |
| 379 typedef SkSTArray<8, GrFragmentStage> FragmentStageArray; | |
| 380 typedef GrProgramElementRef<const GrGeometryProcessor> ProgramGeometryProces
sor; | |
| 381 ProgramGeometryProcessor fGeometryProcessor; | 337 ProgramGeometryProcessor fGeometryProcessor; |
| 382 FragmentStageArray fFragmentStages; | 338 FragmentStageArray fFragmentStages; |
| 383 | 339 |
| 384 // This function is equivalent to the offset into fFragmentStages where cove
rage stages begin. | 340 // This function is equivalent to the offset into fFragmentStages where cove
rage stages begin. |
| 385 int fNumColorStages; | 341 int fNumColorStages; |
| 386 | 342 |
| 387 SkAutoSTArray<4, GrVertexAttrib> fOptVA; | 343 SkAutoSTArray<4, GrVertexAttrib> fOptVA; |
| 388 | 344 |
| 389 BlendOptFlags fBlendOptFlags; | 345 BlendOptFlags fBlendOptFlags; |
| 390 | 346 |
| 391 GrProgramDesc fDesc; | 347 GrProgramDesc fDesc; |
| 392 | 348 |
| 393 typedef SkRefCnt INHERITED; | 349 typedef SkRefCnt INHERITED; |
| 394 }; | 350 }; |
| 395 | 351 |
| 396 GR_MAKE_BITFIELD_OPS(GrOptDrawState::BlendOptFlags); | 352 GR_MAKE_BITFIELD_OPS(GrOptDrawState::BlendOptFlags); |
| 397 | 353 |
| 398 #endif | 354 #endif |
| 399 | 355 |
| OLD | NEW |