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 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 40 | size_t getVertexStride() const { return fVAStride; } |
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(); } |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame^] | 124 | int numTotalStages() const { |
| 125 | return this->numColorStages() + this->numCoverageStages() + |
| 126 | (this->hasGeometryProcessor() ? 1 : 0); |
| 127 | } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 128 | |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame^] | 129 | bool hasGeometryProcessor() const { return NULL != fGeometryProcessor.get(); } |
| 130 | const GrEffectStage* getGeometryProcessor() const { return fGeometryProcessor.get(); } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 131 | const GrEffectStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; } |
| 132 | const GrEffectStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; } |
| 133 | |
| 134 | /** |
| 135 | * Checks whether any of the effects will read the dst pixel color. |
| 136 | */ |
| 137 | bool willEffectReadDstColor() const; |
| 138 | |
| 139 | /// @} |
| 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////// |
| 142 | /// @name Blending |
| 143 | //// |
| 144 | |
| 145 | GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 146 | GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 147 | |
| 148 | void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff, |
| 149 | GrBlendCoeff* dstBlendCoeff) const { |
| 150 | *srcBlendCoeff = fSrcBlend; |
| 151 | *dstBlendCoeff = fDstBlend; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Retrieves the last value set by setBlendConstant() |
| 156 | * @return the blending constant value |
| 157 | */ |
| 158 | GrColor getBlendConstant() const { return fBlendConstant; } |
| 159 | |
| 160 | /** |
| 161 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 162 | * coverage before the blend will give the correct final destination color. In general it |
| 163 | * will not as coverage is applied after blending. |
| 164 | */ |
| 165 | bool canTweakAlphaForCoverage() const; |
| 166 | |
| 167 | /** |
| 168 | * Optimizations for blending / coverage to that can be applied based on the current state. |
| 169 | */ |
| 170 | enum BlendOptFlags { |
| 171 | /** |
| 172 | * No optimization |
| 173 | */ |
| 174 | kNone_BlendOpt = 0, |
| 175 | /** |
| 176 | * Don't draw at all |
| 177 | */ |
| 178 | kSkipDraw_BlendOptFlag = 0x1, |
| 179 | /** |
| 180 | * The coverage value does not have to be computed separately from alpha, the output |
| 181 | * color can be the modulation of the two. |
| 182 | */ |
| 183 | kCoverageAsAlpha_BlendOptFlag = 0x2, |
| 184 | /** |
| 185 | * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are |
| 186 | * "don't cares". |
| 187 | */ |
| 188 | kEmitCoverage_BlendOptFlag = 0x4, |
| 189 | /** |
| 190 | * Emit transparent black instead of the src color, no need to compute coverage. |
| 191 | */ |
| 192 | kEmitTransBlack_BlendOptFlag = 0x8, |
| 193 | /** |
| 194 | * Flag used to invalidate the cached BlendOptFlags, OptSrcCoeff, and OptDstCoeff cached by |
| 195 | * the get BlendOpts function. |
| 196 | */ |
| 197 | kInvalid_BlendOptFlag = 1 << 31, |
| 198 | }; |
| 199 | GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 200 | |
| 201 | /// @} |
| 202 | |
| 203 | /////////////////////////////////////////////////////////////////////////// |
| 204 | /// @name View Matrix |
| 205 | //// |
| 206 | |
| 207 | /** |
| 208 | * Retrieves the current view matrix |
| 209 | * @return the current view matrix. |
| 210 | */ |
| 211 | const SkMatrix& getViewMatrix() const { return fViewMatrix; } |
| 212 | |
| 213 | /** |
| 214 | * Retrieves the inverse of the current view matrix. |
| 215 | * |
| 216 | * If the current view matrix is invertible, return true, and if matrix |
| 217 | * is non-null, copy the inverse into it. If the current view matrix is |
| 218 | * non-invertible, return false and ignore the matrix parameter. |
| 219 | * |
| 220 | * @param matrix if not null, will receive a copy of the current inverse. |
| 221 | */ |
| 222 | bool getViewInverse(SkMatrix* matrix) const { |
| 223 | // TODO: determine whether we really need to leave matrix unmodified |
| 224 | // at call sites when inversion fails. |
| 225 | SkMatrix inverse; |
| 226 | if (fViewMatrix.invert(&inverse)) { |
| 227 | if (matrix) { |
| 228 | *matrix = inverse; |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | /// @} |
| 236 | |
| 237 | /////////////////////////////////////////////////////////////////////////// |
| 238 | /// @name Render Target |
| 239 | //// |
| 240 | |
| 241 | /** |
| 242 | * Retrieves the currently set render-target. |
| 243 | * |
| 244 | * @return The currently set render target. |
| 245 | */ |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 246 | GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 247 | |
| 248 | /// @} |
| 249 | |
| 250 | /////////////////////////////////////////////////////////////////////////// |
| 251 | /// @name Stencil |
| 252 | //// |
| 253 | |
| 254 | const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 255 | |
| 256 | /// @} |
| 257 | |
| 258 | /////////////////////////////////////////////////////////////////////////// |
| 259 | /// @name State Flags |
| 260 | //// |
| 261 | |
| 262 | /** |
| 263 | * Flags that affect rendering. Controlled using enable/disableState(). All |
| 264 | * default to disabled. |
| 265 | */ |
| 266 | enum StateBits { |
| 267 | /** |
| 268 | * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 269 | */ |
| 270 | kDither_StateBit = 0x01, |
| 271 | /** |
| 272 | * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target, |
| 273 | * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by |
| 274 | * the 3D API. |
| 275 | */ |
| 276 | kHWAntialias_StateBit = 0x02, |
| 277 | /** |
| 278 | * Draws will respect the clip, otherwise the clip is ignored. |
| 279 | */ |
| 280 | kClip_StateBit = 0x04, |
| 281 | /** |
| 282 | * Disables writing to the color buffer. Useful when performing stencil |
| 283 | * operations. |
| 284 | */ |
| 285 | kNoColorWrites_StateBit = 0x08, |
| 286 | |
| 287 | /** |
| 288 | * Usually coverage is applied after color blending. The color is blended using the coeffs |
| 289 | * specified by setBlendFunc(). The blended color is then combined with dst using coeffs |
| 290 | * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In |
| 291 | * this case there is no distinction between coverage and color and the caller needs direct |
| 292 | * control over the blend coeffs. When set, there will be a single blend step controlled by |
| 293 | * setBlendFunc() which will use coverage*color as the src color. |
| 294 | */ |
| 295 | kCoverageDrawing_StateBit = 0x10, |
| 296 | |
| 297 | // Users of the class may add additional bits to the vector |
| 298 | kDummyStateBit, |
| 299 | kLastPublicStateBit = kDummyStateBit-1, |
| 300 | }; |
| 301 | |
| 302 | bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); } |
| 303 | |
| 304 | bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); } |
| 305 | bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_StateBit); } |
| 306 | bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } |
| 307 | bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); } |
| 308 | bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); } |
| 309 | |
| 310 | /// @} |
| 311 | |
| 312 | /////////////////////////////////////////////////////////////////////////// |
| 313 | /// @name Face Culling |
| 314 | //// |
| 315 | |
| 316 | enum DrawFace { |
| 317 | kInvalid_DrawFace = -1, |
| 318 | |
| 319 | kBoth_DrawFace, |
| 320 | kCCW_DrawFace, |
| 321 | kCW_DrawFace, |
| 322 | }; |
| 323 | |
| 324 | /** |
| 325 | * Gets whether the target is drawing clockwise, counterclockwise, |
| 326 | * or both faces. |
| 327 | * @return the current draw face(s). |
| 328 | */ |
| 329 | DrawFace getDrawFace() const { return fDrawFace; } |
| 330 | |
| 331 | /// @} |
| 332 | |
| 333 | /////////////////////////////////////////////////////////////////////////// |
| 334 | |
| 335 | /** Return type for CombineIfPossible. */ |
| 336 | enum CombinedState { |
| 337 | /** The GrDrawStates cannot be combined. */ |
| 338 | kIncompatible_CombinedState, |
| 339 | /** Either draw state can be used in place of the other. */ |
| 340 | kAOrB_CombinedState, |
| 341 | /** Use the first draw state. */ |
| 342 | kA_CombinedState, |
| 343 | /** Use the second draw state. */ |
| 344 | kB_CombinedState, |
| 345 | }; |
| 346 | |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 347 | protected: |
| 348 | bool isEqual(const GrRODrawState& that) const; |
| 349 | |
| 350 | // These fields are roughly sorted by decreasing likelihood of being different in op== |
| 351 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 352 | GrColor fColor; |
| 353 | SkMatrix fViewMatrix; |
| 354 | GrColor fBlendConstant; |
| 355 | uint32_t fFlagBits; |
| 356 | const GrVertexAttrib* fVAPtr; |
| 357 | int fVACount; |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 358 | size_t fVAStride; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 359 | GrStencilSettings fStencilSettings; |
| 360 | uint8_t fCoverage; |
| 361 | DrawFace fDrawFace; |
| 362 | GrBlendCoeff fSrcBlend; |
| 363 | GrBlendCoeff fDstBlend; |
| 364 | |
| 365 | typedef SkSTArray<4, GrEffectStage> EffectStageArray; |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame^] | 366 | SkAutoTDelete<GrEffectStage> fGeometryProcessor; |
egdaniel | 21aed57 | 2014-08-26 12:24:06 -0700 | [diff] [blame] | 367 | EffectStageArray fColorStages; |
| 368 | EffectStageArray fCoverageStages; |
| 369 | |
| 370 | mutable GrBlendCoeff fOptSrcBlend; |
| 371 | mutable GrBlendCoeff fOptDstBlend; |
| 372 | mutable BlendOptFlags fBlendOptFlags; |
| 373 | |
| 374 | // This is simply a different representation of info in fVertexAttribs and thus does |
| 375 | // not need to be compared in op==. |
| 376 | int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt]; |
| 377 | |
| 378 | private: |
| 379 | typedef SkRefCnt INHERITED; |
| 380 | }; |
| 381 | |
| 382 | GR_MAKE_BITFIELD_OPS(GrRODrawState::BlendOptFlags); |
| 383 | |
| 384 | #endif |