egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 1 | /* |
| 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 GrRODrawState_DEFINED |
| 9 | #define GrRODrawState_DEFINED |
| 10 | |
| 11 | #include "GrStencil.h" |
| 12 | #include "GrEffectStage.h" |
| 13 | #include "SkMatrix.h" |
| 14 | |
| 15 | class GrDrawTargetCaps; |
| 16 | class GrPaint; |
| 17 | class GrRenderTarget; |
| 18 | class GrTexture; |
| 19 | |
| 20 | /** |
| 21 | * Read-only base class for GrDrawState. This class contains all the necessary data to represent a |
| 22 | * canonical DrawState. All methods in the class are const, thus once created the data in the class |
| 23 | * cannot be changed. |
| 24 | */ |
| 25 | class GrRODrawState : public SkRefCnt { |
| 26 | public: |
| 27 | SK_DECLARE_INST_COUNT(GrRODrawState) |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////// |
| 30 | /// @name Vertex Attributes |
| 31 | //// |
| 32 | |
| 33 | enum { |
| 34 | kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
| 35 | }; |
| 36 | |
| 37 | const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } |
| 38 | int getVertexAttribCount() const { return fVACount; } |
| 39 | |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 40 | size_t getVertexSize() const { return fVertexSize; } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the |
| 44 | * binding does not appear in the current attribs. These bindings should appear only once in |
| 45 | * the attrib array. |
| 46 | */ |
| 47 | |
| 48 | int positionAttributeIndex() const { |
| 49 | return fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding]; |
| 50 | } |
| 51 | int localCoordAttributeIndex() const { |
| 52 | return fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding]; |
| 53 | } |
| 54 | int colorVertexAttributeIndex() const { |
| 55 | return fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; |
| 56 | } |
| 57 | int coverageVertexAttributeIndex() const { |
| 58 | return fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding]; |
| 59 | } |
| 60 | |
| 61 | bool hasLocalCoordAttribute() const { |
| 62 | return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding]; |
| 63 | } |
| 64 | bool hasColorVertexAttribute() const { |
| 65 | return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; |
| 66 | } |
| 67 | bool hasCoverageVertexAttribute() const { |
| 68 | return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding]; |
| 69 | } |
| 70 | |
| 71 | bool validateVertexAttribs() const; |
| 72 | |
| 73 | /// @} |
| 74 | |
| 75 | /** |
| 76 | * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw. |
| 77 | */ |
| 78 | bool hasSolidCoverage() const; |
| 79 | |
| 80 | /// @} |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////// |
| 83 | /// @name Color |
| 84 | //// |
| 85 | |
| 86 | GrColor getColor() const { return fColor; } |
| 87 | |
| 88 | /// @} |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////// |
| 91 | /// @name Coverage |
| 92 | //// |
| 93 | |
| 94 | uint8_t getCoverage() const { return fCoverage; } |
| 95 | |
| 96 | GrColor getCoverageColor() const { |
| 97 | return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage); |
| 98 | } |
| 99 | |
| 100 | /// @} |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////// |
| 103 | /// @name Effect Stages |
| 104 | /// Each stage hosts a GrEffect. The effect produces an output color or coverage in the fragment |
| 105 | /// shader. Its inputs are the output from the previous stage as well as some variables |
| 106 | /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color, |
| 107 | /// the fragment position, local coordinates). |
| 108 | /// |
| 109 | /// The stages are divided into two sets, color-computing and coverage-computing. The final |
| 110 | /// color stage produces the final pixel color. The coverage-computing stages function exactly |
| 111 | /// as the color-computing but the output of the final coverage stage is treated as a fractional |
| 112 | /// pixel coverage rather than as input to the src/dst color blend step. |
| 113 | /// |
| 114 | /// The input color to the first color-stage is either the constant color or interpolated |
| 115 | /// per-vertex colors. The input to the first coverage stage is either a constant coverage |
| 116 | /// (usually full-coverage) or interpolated per-vertex coverage. |
| 117 | /// |
| 118 | /// See the documentation of kCoverageDrawing_StateBit for information about disabling the |
| 119 | /// the color / coverage distinction. |
| 120 | //// |
| 121 | |
| 122 | int numColorStages() const { return fColorStages.count(); } |
| 123 | int numCoverageStages() const { return fCoverageStages.count(); } |
| 124 | int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); } |
| 125 | |
| 126 | const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; } |
| 127 | const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; } |
| 128 | |
| 129 | /** |
| 130 | * Checks whether any of the effects will read the dst pixel color. |
| 131 | */ |
| 132 | bool willEffectReadDstColor() const; |
| 133 | |
| 134 | /// @} |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////// |
| 137 | /// @name Blending |
| 138 | //// |
| 139 | |
| 140 | GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 141 | GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 142 | |
| 143 | void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff, |
| 144 | GrBlendCoeff* dstBlendCoeff) const { |
| 145 | *srcBlendCoeff = fSrcBlend; |
| 146 | *dstBlendCoeff = fDstBlend; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Retrieves the last value set by setBlendConstant() |
| 151 | * @return the blending constant value |
| 152 | */ |
| 153 | GrColor getBlendConstant() const { return fBlendConstant; } |
| 154 | |
| 155 | /** |
| 156 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 157 | * coverage before the blend will give the correct final destination color. In general it |
| 158 | * will not as coverage is applied after blending. |
| 159 | */ |
| 160 | bool canTweakAlphaForCoverage() const; |
| 161 | |
| 162 | /** |
| 163 | * Optimizations for blending / coverage to that can be applied based on the current state. |
| 164 | */ |
| 165 | enum BlendOptFlags { |
| 166 | /** |
| 167 | * No optimization |
| 168 | */ |
| 169 | kNone_BlendOpt = 0, |
| 170 | /** |
| 171 | * Don't draw at all |
| 172 | */ |
| 173 | kSkipDraw_BlendOptFlag = 0x1, |
| 174 | /** |
| 175 | * The coverage value does not have to be computed separately from alpha, the output |
| 176 | * color can be the modulation of the two. |
| 177 | */ |
| 178 | kCoverageAsAlpha_BlendOptFlag = 0x2, |
| 179 | /** |
| 180 | * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are |
| 181 | * "don't cares". |
| 182 | */ |
| 183 | kEmitCoverage_BlendOptFlag = 0x4, |
| 184 | /** |
| 185 | * Emit transparent black instead of the src color, no need to compute coverage. |
| 186 | */ |
| 187 | kEmitTransBlack_BlendOptFlag = 0x8, |
| 188 | /** |
| 189 | * Flag used to invalidate the cached BlendOptFlags, OptSrcCoeff, and OptDstCoeff cached by |
| 190 | * the get BlendOpts function. |
| 191 | */ |
| 192 | kInvalid_BlendOptFlag = 1 << 31, |
| 193 | }; |
| 194 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 195 | |
| 196 | /// @} |
| 197 | |
| 198 | /////////////////////////////////////////////////////////////////////////// |
| 199 | /// @name View Matrix |
| 200 | //// |
| 201 | |
| 202 | /** |
| 203 | * Retrieves the current view matrix |
| 204 | * @return the current view matrix. |
| 205 | */ |
| 206 | const SkMatrix& getViewMatrix() const { return fViewMatrix; } |
| 207 | |
| 208 | /** |
| 209 | * Retrieves the inverse of the current view matrix. |
| 210 | * |
| 211 | * If the current view matrix is invertible, return true, and if matrix |
| 212 | * is non-null, copy the inverse into it. If the current view matrix is |
| 213 | * non-invertible, return false and ignore the matrix parameter. |
| 214 | * |
| 215 | * @param matrix if not null, will receive a copy of the current inverse. |
| 216 | */ |
| 217 | bool getViewInverse(SkMatrix* matrix) const { |
| 218 | // TODO: determine whether we really need to leave matrix unmodified |
| 219 | // at call sites when inversion fails. |
| 220 | SkMatrix inverse; |
| 221 | if (fViewMatrix.invert(&inverse)) { |
| 222 | if (matrix) { |
| 223 | *matrix = inverse; |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /// @} |
| 231 | |
| 232 | /////////////////////////////////////////////////////////////////////////// |
| 233 | /// @name Render Target |
| 234 | //// |
| 235 | |
| 236 | /** |
| 237 | * Retrieves the currently set render-target. |
| 238 | * |
| 239 | * @return The currently set render target. |
| 240 | */ |
| 241 | const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
| 242 | GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } |
| 243 | |
| 244 | /// @} |
| 245 | |
| 246 | /////////////////////////////////////////////////////////////////////////// |
| 247 | /// @name Stencil |
| 248 | //// |
| 249 | |
| 250 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 251 | |
| 252 | /// @} |
| 253 | |
| 254 | /////////////////////////////////////////////////////////////////////////// |
| 255 | /// @name State Flags |
| 256 | //// |
| 257 | |
| 258 | /** |
| 259 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 260 | * default to disabled. |
| 261 | */ |
| 262 | enum StateBits { |
| 263 | /** |
| 264 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 265 | */ |
| 266 | kDither_StateBit = 0x01, |
| 267 | /** |
| 268 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 269 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 270 | * the 3D API. |
| 271 | */ |
| 272 | kHWAntialias_StateBit = 0x02, |
| 273 | /** |
| 274 | * Draws will respect the clip, otherwise the clip is ignored. |
| 275 | */ |
| 276 | kClip_StateBit = 0x04, |
| 277 | /** |
| 278 | * Disables writing to the color buffer. Useful when performing stencil |
| 279 | * operations. |
| 280 | */ |
| 281 | kNoColorWrites_StateBit = 0x08, |
| 282 | |
| 283 | /** |
| 284 | * Usually coverage is applied after color blending. The color is blended using the coeffs |
| 285 | * specified by setBlendFunc(). The blended color is then combined with dst using coeffs |
| 286 | * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In |
| 287 | * this case there is no distinction between coverage and color and the caller needs direct |
| 288 | * control over the blend coeffs. When set, there will be a single blend step controlled by |
| 289 | * setBlendFunc() which will use coverage*color as the src color. |
| 290 | */ |
| 291 | kCoverageDrawing_StateBit = 0x10, |
| 292 | |
| 293 | // Users of the class may add additional bits to the vector |
| 294 | kDummyStateBit, |
| 295 | kLastPublicStateBit = kDummyStateBit-1, |
| 296 | }; |
| 297 | |
| 298 | bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); } |
| 299 | |
| 300 | bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); } |
| 301 | bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_StateBit); } |
| 302 | bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } |
| 303 | bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); } |
| 304 | bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); } |
| 305 | |
| 306 | /// @} |
| 307 | |
| 308 | /////////////////////////////////////////////////////////////////////////// |
| 309 | /// @name Face Culling |
| 310 | //// |
| 311 | |
| 312 | enum DrawFace { |
| 313 | kInvalid_DrawFace = -1, |
| 314 | |
| 315 | kBoth_DrawFace, |
| 316 | kCCW_DrawFace, |
| 317 | kCW_DrawFace, |
| 318 | }; |
| 319 | |
| 320 | /** |
| 321 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 322 | * or both faces. |
| 323 | * @return the current draw face(s). |
| 324 | */ |
| 325 | DrawFace getDrawFace() const { return fDrawFace; } |
| 326 | |
| 327 | /// @} |
| 328 | |
| 329 | /////////////////////////////////////////////////////////////////////////// |
| 330 | |
| 331 | /** Return type for CombineIfPossible. */ |
| 332 | enum CombinedState { |
| 333 | /** The GrDrawStates cannot be combined. */ |
| 334 | kIncompatible_CombinedState, |
| 335 | /** Either draw state can be used in place of the other. */ |
| 336 | kAOrB_CombinedState, |
| 337 | /** Use the first draw state. */ |
| 338 | kA_CombinedState, |
| 339 | /** Use the second draw state. */ |
| 340 | kB_CombinedState, |
| 341 | }; |
| 342 | |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 343 | GrRODrawState& operator= (const GrRODrawState& that); |
| 344 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 345 | protected: |
| 346 | bool isEqual(const GrRODrawState& that) const; |
| 347 | |
| 348 | // These fields are roughly sorted by decreasing likelihood of being different in op== |
| 349 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 350 | GrColor fColor; |
| 351 | SkMatrix fViewMatrix; |
| 352 | GrColor fBlendConstant; |
| 353 | uint32_t fFlagBits; |
| 354 | const GrVertexAttrib* fVAPtr; |
| 355 | int fVACount; |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 356 | size_t fVertexSize; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 357 | GrStencilSettings fStencilSettings; |
| 358 | uint8_t fCoverage; |
| 359 | DrawFace fDrawFace; |
| 360 | GrBlendCoeff fSrcBlend; |
| 361 | GrBlendCoeff fDstBlend; |
| 362 | |
| 363 | typedef SkSTArray<4, GrEffectStage> EffectStageArray; |
| 364 | EffectStageArray fColorStages; |
| 365 | EffectStageArray fCoverageStages; |
| 366 | |
| 367 | mutable GrBlendCoeff fOptSrcBlend; |
| 368 | mutable GrBlendCoeff fOptDstBlend; |
| 369 | mutable BlendOptFlags fBlendOptFlags; |
| 370 | |
| 371 | // This is simply a different representation of info in fVertexAttribs and thus does |
| 372 | // not need to be compared in op==. |
| 373 | int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt]; |
| 374 | |
| 375 | private: |
| 376 | typedef SkRefCnt INHERITED; |
| 377 | }; |
| 378 | |
| 379 | GR_MAKE_BITFIELD_OPS(GrRODrawState::BlendOptFlags); |
| 380 | |
| 381 | #endif |