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