2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrRODrawState_DEFINED
9 #define GrRODrawState_DEFINED
11 #include "GrProcessorStage.h"
12 #include "GrRenderTarget.h"
13 #include "GrStencil.h"
17 class GrDrawTargetCaps;
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
26 class GrRODrawState : public SkRefCnt {
28 SK_DECLARE_INST_COUNT(GrRODrawState)
32 GrRODrawState& operator= (const GrRODrawState& that);
34 ///////////////////////////////////////////////////////////////////////////
35 /// @name Vertex Attributes
39 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
42 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; }
43 int getVertexAttribCount() const { return fVACount; }
45 size_t getVertexStride() const { return fVAStride; }
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
53 int positionAttributeIndex() const {
54 return fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
56 int localCoordAttributeIndex() const {
57 return fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
59 int colorVertexAttributeIndex() const {
60 return fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
62 int coverageVertexAttributeIndex() const {
63 return fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
66 bool hasLocalCoordAttribute() const {
67 return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
69 bool hasColorVertexAttribute() const {
70 return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
72 bool hasCoverageVertexAttribute() const {
73 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
76 const int* getFixedFunctionVertexAttribIndices() const {
77 return fFixedFunctionVertexAttribIndices;
80 bool validateVertexAttribs() const;
85 * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
87 bool hasSolidCoverage() const;
91 ///////////////////////////////////////////////////////////////////////////
95 GrColor getColor() const { return fColor; }
99 ///////////////////////////////////////////////////////////////////////////
103 uint8_t getCoverage() const { return fCoverage; }
105 GrColor getCoverageColor() const {
106 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
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).
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.
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.
127 /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
128 /// the color / coverage distinction.
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);
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]; }
144 * Checks whether any of the effects will read the dst pixel color.
146 bool willEffectReadDstColor() const;
150 ///////////////////////////////////////////////////////////////////////////
154 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
155 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
157 void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
158 GrBlendCoeff* dstBlendCoeff) const {
159 *srcBlendCoeff = fSrcBlend;
160 *dstBlendCoeff = fDstBlend;
164 * Retrieves the last value set by setBlendConstant()
165 * @return the blending constant value
167 GrColor getBlendConstant() const { return fBlendConstant; }
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.
174 bool canTweakAlphaForCoverage() const;
178 ///////////////////////////////////////////////////////////////////////////
179 /// @name View Matrix
183 * Retrieves the current view matrix
184 * @return the current view matrix.
186 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
189 * Retrieves the inverse of the current view matrix.
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.
195 * @param matrix if not null, will receive a copy of the current inverse.
197 bool getViewInverse(SkMatrix* matrix) const {
198 // TODO: determine whether we really need to leave matrix unmodified
199 // at call sites when inversion fails.
201 if (fViewMatrix.invert(&inverse)) {
212 ///////////////////////////////////////////////////////////////////////////
213 /// @name Render Target
217 * Retrieves the currently set render-target.
219 * @return The currently set render target.
221 GrRenderTarget* getRenderTarget() const {
222 return static_cast<GrRenderTarget*>(fRenderTarget.getResource());
227 ///////////////////////////////////////////////////////////////////////////
231 const GrStencilSettings& getStencil() const { return fStencilSettings; }
235 ///////////////////////////////////////////////////////////////////////////
236 /// @name State Flags
240 * Flags that affect rendering. Controlled using enable/disableState(). All
241 * default to disabled.
245 * Perform dithering. TODO: Re-evaluate whether we need this bit
247 kDither_StateBit = 0x01,
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
253 kHWAntialias_StateBit = 0x02,
255 * Draws will respect the clip, otherwise the clip is ignored.
257 kClip_StateBit = 0x04,
259 * Disables writing to the color buffer. Useful when performing stencil
262 kNoColorWrites_StateBit = 0x08,
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.
272 kCoverageDrawing_StateBit = 0x10,
274 // Users of the class may add additional bits to the vector
276 kLastPublicStateBit = kDummyStateBit-1,
279 uint32_t getFlagBits() const { return fFlagBits; }
281 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); }
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); }
291 ///////////////////////////////////////////////////////////////////////////
292 /// @name Face Culling
296 kInvalid_DrawFace = -1,
304 * Gets whether the target is drawing clockwise, counterclockwise,
306 * @return the current draw face(s).
308 DrawFace getDrawFace() const { return fDrawFace; }
312 ///////////////////////////////////////////////////////////////////////////
314 /// Hints that when provided can enable optimizations.
317 enum Hints { kVertexColorsAreOpaque_Hint = 0x1, };
319 bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
323 ///////////////////////////////////////////////////////////////////////////
325 /** Return type for CombineIfPossible. */
327 /** The GrDrawStates cannot be combined. */
328 kIncompatible_CombinedState,
329 /** Either draw state can be used in place of the other. */
331 /** Use the first draw state. */
333 /** Use the second draw state. */
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.
346 void convertToPendingExec();
348 friend class GrDrawTarget;
350 explicit GrRODrawState(const GrRODrawState& drawState);
352 bool isEqual(const GrRODrawState& that) const;
355 * Optimizations for blending / coverage to that can be applied based on the current state.
365 kSkipDraw_BlendOptFlag = 0x1,
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.
370 kCoverageAsAlpha_BlendOptFlag = 0x2,
372 * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
375 kEmitCoverage_BlendOptFlag = 0x4,
377 * Emit transparent black instead of the src color, no need to compute coverage.
379 kEmitTransBlack_BlendOptFlag = 0x8,
381 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
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.
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.
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.
396 BlendOptFlags getBlendOpts(bool forceCoverage = false,
397 GrBlendCoeff* srcCoeff = NULL,
398 GrBlendCoeff* dstCoeff = NULL) const;
400 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget;
401 // These fields are roughly sorted by decreasing likelihood of being different in op==
402 ProgramRenderTarget fRenderTarget;
404 SkMatrix fViewMatrix;
405 GrColor fBlendConstant;
407 const GrVertexAttrib* fVAPtr;
410 GrStencilSettings fStencilSettings;
413 GrBlendCoeff fSrcBlend;
414 GrBlendCoeff fDstBlend;
416 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray;
417 SkAutoTDelete<GrGeometryStage> fGeometryProcessor;
418 FragmentStageArray fColorStages;
419 FragmentStageArray fCoverageStages;
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];
429 * Determines whether src alpha is guaranteed to be one for all src pixels
431 bool srcAlphaWillBeOne() const;
433 typedef SkRefCnt INHERITED;
436 GR_MAKE_BITFIELD_OPS(GrRODrawState::BlendOptFlags);