121aed57023bad4b04076c59e37097c7563efc028egdaniel/*
221aed57023bad4b04076c59e37097c7563efc028egdaniel * Copyright 2014 Google Inc.
321aed57023bad4b04076c59e37097c7563efc028egdaniel *
421aed57023bad4b04076c59e37097c7563efc028egdaniel * Use of this source code is governed by a BSD-style license that can be
521aed57023bad4b04076c59e37097c7563efc028egdaniel * found in the LICENSE file.
621aed57023bad4b04076c59e37097c7563efc028egdaniel */
721aed57023bad4b04076c59e37097c7563efc028egdaniel
821aed57023bad4b04076c59e37097c7563efc028egdaniel#ifndef GrRODrawState_DEFINED
921aed57023bad4b04076c59e37097c7563efc028egdaniel#define GrRODrawState_DEFINED
1021aed57023bad4b04076c59e37097c7563efc028egdaniel
11b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#include "GrProcessorStage.h"
122a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon#include "GrRenderTarget.h"
132a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon#include "GrStencil.h"
1421aed57023bad4b04076c59e37097c7563efc028egdaniel#include "SkMatrix.h"
1521aed57023bad4b04076c59e37097c7563efc028egdaniel
163658f382cc129e463d7f40a7e68214d04d50fe14egdanielclass GrDrawState;
1721aed57023bad4b04076c59e37097c7563efc028egdanielclass GrDrawTargetCaps;
1821aed57023bad4b04076c59e37097c7563efc028egdanielclass GrPaint;
1921aed57023bad4b04076c59e37097c7563efc028egdanielclass GrTexture;
2021aed57023bad4b04076c59e37097c7563efc028egdaniel
2121aed57023bad4b04076c59e37097c7563efc028egdaniel/**
2221aed57023bad4b04076c59e37097c7563efc028egdaniel * Read-only base class for GrDrawState. This class contains all the necessary data to represent a
2321aed57023bad4b04076c59e37097c7563efc028egdaniel * canonical DrawState. All methods in the class are const, thus once created the data in the class
2421aed57023bad4b04076c59e37097c7563efc028egdaniel * cannot be changed.
2521aed57023bad4b04076c59e37097c7563efc028egdaniel */
2621aed57023bad4b04076c59e37097c7563efc028egdanielclass GrRODrawState : public SkRefCnt {
2721aed57023bad4b04076c59e37097c7563efc028egdanielpublic:
2821aed57023bad4b04076c59e37097c7563efc028egdaniel    SK_DECLARE_INST_COUNT(GrRODrawState)
2921aed57023bad4b04076c59e37097c7563efc028egdaniel
303658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    GrRODrawState() {}
313658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
323658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    GrRODrawState& operator= (const GrRODrawState& that);
333658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
3421aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
3521aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Vertex Attributes
3621aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
3721aed57023bad4b04076c59e37097c7563efc028egdaniel
3821aed57023bad4b04076c59e37097c7563efc028egdaniel    enum {
3921aed57023bad4b04076c59e37097c7563efc028egdaniel        kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4,
4021aed57023bad4b04076c59e37097c7563efc028egdaniel    };
4121aed57023bad4b04076c59e37097c7563efc028egdaniel
4221aed57023bad4b04076c59e37097c7563efc028egdaniel    const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; }
4321aed57023bad4b04076c59e37097c7563efc028egdaniel    int getVertexAttribCount() const { return fVACount; }
4421aed57023bad4b04076c59e37097c7563efc028egdaniel
457b3d5ee72c2238aa239bce4d5b3aea98a437ca7aegdaniel    size_t getVertexStride() const { return fVAStride; }
4621aed57023bad4b04076c59e37097c7563efc028egdaniel
4721aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
4821aed57023bad4b04076c59e37097c7563efc028egdaniel     * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the
4921aed57023bad4b04076c59e37097c7563efc028egdaniel     * binding does not appear in the current attribs. These bindings should appear only once in
5021aed57023bad4b04076c59e37097c7563efc028egdaniel     * the attrib array.
5121aed57023bad4b04076c59e37097c7563efc028egdaniel     */
5221aed57023bad4b04076c59e37097c7563efc028egdaniel
5321aed57023bad4b04076c59e37097c7563efc028egdaniel    int positionAttributeIndex() const {
5421aed57023bad4b04076c59e37097c7563efc028egdaniel        return fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding];
5521aed57023bad4b04076c59e37097c7563efc028egdaniel    }
5621aed57023bad4b04076c59e37097c7563efc028egdaniel    int localCoordAttributeIndex() const {
5721aed57023bad4b04076c59e37097c7563efc028egdaniel        return fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
5821aed57023bad4b04076c59e37097c7563efc028egdaniel    }
5921aed57023bad4b04076c59e37097c7563efc028egdaniel    int colorVertexAttributeIndex() const {
6021aed57023bad4b04076c59e37097c7563efc028egdaniel        return fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
6121aed57023bad4b04076c59e37097c7563efc028egdaniel    }
6221aed57023bad4b04076c59e37097c7563efc028egdaniel    int coverageVertexAttributeIndex() const {
6321aed57023bad4b04076c59e37097c7563efc028egdaniel        return fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
6421aed57023bad4b04076c59e37097c7563efc028egdaniel    }
6521aed57023bad4b04076c59e37097c7563efc028egdaniel
6621aed57023bad4b04076c59e37097c7563efc028egdaniel    bool hasLocalCoordAttribute() const {
6721aed57023bad4b04076c59e37097c7563efc028egdaniel        return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBinding];
6821aed57023bad4b04076c59e37097c7563efc028egdaniel    }
6921aed57023bad4b04076c59e37097c7563efc028egdaniel    bool hasColorVertexAttribute() const {
7021aed57023bad4b04076c59e37097c7563efc028egdaniel        return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding];
7121aed57023bad4b04076c59e37097c7563efc028egdaniel    }
7221aed57023bad4b04076c59e37097c7563efc028egdaniel    bool hasCoverageVertexAttribute() const {
7321aed57023bad4b04076c59e37097c7563efc028egdaniel        return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding];
7421aed57023bad4b04076c59e37097c7563efc028egdaniel    }
7521aed57023bad4b04076c59e37097c7563efc028egdaniel
763658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    const int* getFixedFunctionVertexAttribIndices() const {
773658f382cc129e463d7f40a7e68214d04d50fe14egdaniel        return fFixedFunctionVertexAttribIndices;
783658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    }
793658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
8021aed57023bad4b04076c59e37097c7563efc028egdaniel    bool validateVertexAttribs() const;
8121aed57023bad4b04076c59e37097c7563efc028egdaniel
8221aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
8321aed57023bad4b04076c59e37097c7563efc028egdaniel
8421aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
8521aed57023bad4b04076c59e37097c7563efc028egdaniel     * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
8621aed57023bad4b04076c59e37097c7563efc028egdaniel     */
8721aed57023bad4b04076c59e37097c7563efc028egdaniel    bool hasSolidCoverage() const;
8821aed57023bad4b04076c59e37097c7563efc028egdaniel
8921aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
9021aed57023bad4b04076c59e37097c7563efc028egdaniel
9121aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
9221aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Color
9321aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
9421aed57023bad4b04076c59e37097c7563efc028egdaniel
9521aed57023bad4b04076c59e37097c7563efc028egdaniel    GrColor getColor() const { return fColor; }
9621aed57023bad4b04076c59e37097c7563efc028egdaniel
9721aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
9821aed57023bad4b04076c59e37097c7563efc028egdaniel
9921aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
10021aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Coverage
10121aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
10221aed57023bad4b04076c59e37097c7563efc028egdaniel
10321aed57023bad4b04076c59e37097c7563efc028egdaniel    uint8_t getCoverage() const { return fCoverage; }
10421aed57023bad4b04076c59e37097c7563efc028egdaniel
10521aed57023bad4b04076c59e37097c7563efc028egdaniel    GrColor getCoverageColor() const {
10621aed57023bad4b04076c59e37097c7563efc028egdaniel        return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
10721aed57023bad4b04076c59e37097c7563efc028egdaniel    }
10821aed57023bad4b04076c59e37097c7563efc028egdaniel
10921aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
11021aed57023bad4b04076c59e37097c7563efc028egdaniel
11121aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
11221aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Effect Stages
113b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    /// Each stage hosts a GrProcessor. The effect produces an output color or coverage in the
114b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    /// fragment shader. Its inputs are the output from the previous stage as well as some variables
11521aed57023bad4b04076c59e37097c7563efc028egdaniel    /// available to it in the fragment and vertex shader (e.g. the vertex position, the dst color,
11621aed57023bad4b04076c59e37097c7563efc028egdaniel    /// the fragment position, local coordinates).
11721aed57023bad4b04076c59e37097c7563efc028egdaniel    ///
11821aed57023bad4b04076c59e37097c7563efc028egdaniel    /// The stages are divided into two sets, color-computing and coverage-computing. The final
11921aed57023bad4b04076c59e37097c7563efc028egdaniel    /// color stage produces the final pixel color. The coverage-computing stages function exactly
12021aed57023bad4b04076c59e37097c7563efc028egdaniel    /// as the color-computing but the output of the final coverage stage is treated as a fractional
12121aed57023bad4b04076c59e37097c7563efc028egdaniel    /// pixel coverage rather than as input to the src/dst color blend step.
12221aed57023bad4b04076c59e37097c7563efc028egdaniel    ///
12321aed57023bad4b04076c59e37097c7563efc028egdaniel    /// The input color to the first color-stage is either the constant color or interpolated
12421aed57023bad4b04076c59e37097c7563efc028egdaniel    /// per-vertex colors. The input to the first coverage stage is either a constant coverage
12521aed57023bad4b04076c59e37097c7563efc028egdaniel    /// (usually full-coverage) or interpolated per-vertex coverage.
12621aed57023bad4b04076c59e37097c7563efc028egdaniel    ///
12721aed57023bad4b04076c59e37097c7563efc028egdaniel    /// See the documentation of kCoverageDrawing_StateBit for information about disabling the
12821aed57023bad4b04076c59e37097c7563efc028egdaniel    /// the color / coverage distinction.
12921aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
13021aed57023bad4b04076c59e37097c7563efc028egdaniel
13121aed57023bad4b04076c59e37097c7563efc028egdaniel    int numColorStages() const { return fColorStages.count(); }
13221aed57023bad4b04076c59e37097c7563efc028egdaniel    int numCoverageStages() const { return fCoverageStages.count(); }
133bd769d0f1c8cf6ccbb2738dfad1624a4c828e4ebjoshualitt    int numTotalStages() const {
134bd769d0f1c8cf6ccbb2738dfad1624a4c828e4ebjoshualitt         return this->numColorStages() + this->numCoverageStages() +
135bd769d0f1c8cf6ccbb2738dfad1624a4c828e4ebjoshualitt                 (this->hasGeometryProcessor() ? 1 : 0);
136bd769d0f1c8cf6ccbb2738dfad1624a4c828e4ebjoshualitt    }
13721aed57023bad4b04076c59e37097c7563efc028egdaniel
13849f085dddff10473b6ebf832a974288300224e60bsalomon    bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()); }
139b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    const GrGeometryStage* getGeometryProcessor() const { return fGeometryProcessor.get(); }
140b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    const GrFragmentStage& getColorStage(int stageIdx) const { return fColorStages[stageIdx]; }
141b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    const GrFragmentStage& getCoverageStage(int stageIdx) const { return fCoverageStages[stageIdx]; }
14221aed57023bad4b04076c59e37097c7563efc028egdaniel
14321aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
14421aed57023bad4b04076c59e37097c7563efc028egdaniel     * Checks whether any of the effects will read the dst pixel color.
14521aed57023bad4b04076c59e37097c7563efc028egdaniel     */
14621aed57023bad4b04076c59e37097c7563efc028egdaniel    bool willEffectReadDstColor() const;
14721aed57023bad4b04076c59e37097c7563efc028egdaniel
14821aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
14921aed57023bad4b04076c59e37097c7563efc028egdaniel
15021aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
15121aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Blending
15221aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
15321aed57023bad4b04076c59e37097c7563efc028egdaniel
15421aed57023bad4b04076c59e37097c7563efc028egdaniel    GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; }
15521aed57023bad4b04076c59e37097c7563efc028egdaniel    GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; }
15621aed57023bad4b04076c59e37097c7563efc028egdaniel
15721aed57023bad4b04076c59e37097c7563efc028egdaniel    void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
15821aed57023bad4b04076c59e37097c7563efc028egdaniel                          GrBlendCoeff* dstBlendCoeff) const {
15921aed57023bad4b04076c59e37097c7563efc028egdaniel        *srcBlendCoeff = fSrcBlend;
16021aed57023bad4b04076c59e37097c7563efc028egdaniel        *dstBlendCoeff = fDstBlend;
16121aed57023bad4b04076c59e37097c7563efc028egdaniel    }
16221aed57023bad4b04076c59e37097c7563efc028egdaniel
16321aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
16421aed57023bad4b04076c59e37097c7563efc028egdaniel     * Retrieves the last value set by setBlendConstant()
16521aed57023bad4b04076c59e37097c7563efc028egdaniel     * @return the blending constant value
16621aed57023bad4b04076c59e37097c7563efc028egdaniel     */
16721aed57023bad4b04076c59e37097c7563efc028egdaniel    GrColor getBlendConstant() const { return fBlendConstant; }
16821aed57023bad4b04076c59e37097c7563efc028egdaniel
16921aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
17021aed57023bad4b04076c59e37097c7563efc028egdaniel     * Determines whether multiplying the computed per-pixel color by the pixel's fractional
17121aed57023bad4b04076c59e37097c7563efc028egdaniel     * coverage before the blend will give the correct final destination color. In general it
17221aed57023bad4b04076c59e37097c7563efc028egdaniel     * will not as coverage is applied after blending.
17321aed57023bad4b04076c59e37097c7563efc028egdaniel     */
17421aed57023bad4b04076c59e37097c7563efc028egdaniel    bool canTweakAlphaForCoverage() const;
17521aed57023bad4b04076c59e37097c7563efc028egdaniel
17621aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
17721aed57023bad4b04076c59e37097c7563efc028egdaniel
17821aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
17921aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name View Matrix
18021aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
18121aed57023bad4b04076c59e37097c7563efc028egdaniel
18221aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
18321aed57023bad4b04076c59e37097c7563efc028egdaniel     * Retrieves the current view matrix
18421aed57023bad4b04076c59e37097c7563efc028egdaniel     * @return the current view matrix.
18521aed57023bad4b04076c59e37097c7563efc028egdaniel     */
18621aed57023bad4b04076c59e37097c7563efc028egdaniel    const SkMatrix& getViewMatrix() const { return fViewMatrix; }
18721aed57023bad4b04076c59e37097c7563efc028egdaniel
18821aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
18921aed57023bad4b04076c59e37097c7563efc028egdaniel     *  Retrieves the inverse of the current view matrix.
19021aed57023bad4b04076c59e37097c7563efc028egdaniel     *
19121aed57023bad4b04076c59e37097c7563efc028egdaniel     *  If the current view matrix is invertible, return true, and if matrix
19221aed57023bad4b04076c59e37097c7563efc028egdaniel     *  is non-null, copy the inverse into it. If the current view matrix is
19321aed57023bad4b04076c59e37097c7563efc028egdaniel     *  non-invertible, return false and ignore the matrix parameter.
19421aed57023bad4b04076c59e37097c7563efc028egdaniel     *
19521aed57023bad4b04076c59e37097c7563efc028egdaniel     * @param matrix if not null, will receive a copy of the current inverse.
19621aed57023bad4b04076c59e37097c7563efc028egdaniel     */
19721aed57023bad4b04076c59e37097c7563efc028egdaniel    bool getViewInverse(SkMatrix* matrix) const {
19821aed57023bad4b04076c59e37097c7563efc028egdaniel        // TODO: determine whether we really need to leave matrix unmodified
19921aed57023bad4b04076c59e37097c7563efc028egdaniel        // at call sites when inversion fails.
20021aed57023bad4b04076c59e37097c7563efc028egdaniel        SkMatrix inverse;
20121aed57023bad4b04076c59e37097c7563efc028egdaniel        if (fViewMatrix.invert(&inverse)) {
20221aed57023bad4b04076c59e37097c7563efc028egdaniel            if (matrix) {
20321aed57023bad4b04076c59e37097c7563efc028egdaniel                *matrix = inverse;
20421aed57023bad4b04076c59e37097c7563efc028egdaniel            }
20521aed57023bad4b04076c59e37097c7563efc028egdaniel            return true;
20621aed57023bad4b04076c59e37097c7563efc028egdaniel        }
20721aed57023bad4b04076c59e37097c7563efc028egdaniel        return false;
20821aed57023bad4b04076c59e37097c7563efc028egdaniel    }
20921aed57023bad4b04076c59e37097c7563efc028egdaniel
21021aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
21121aed57023bad4b04076c59e37097c7563efc028egdaniel
21221aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
21321aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Render Target
21421aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
21521aed57023bad4b04076c59e37097c7563efc028egdaniel
21621aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
21721aed57023bad4b04076c59e37097c7563efc028egdaniel     * Retrieves the currently set render-target.
21821aed57023bad4b04076c59e37097c7563efc028egdaniel     *
21921aed57023bad4b04076c59e37097c7563efc028egdaniel     * @return    The currently set render target.
22021aed57023bad4b04076c59e37097c7563efc028egdaniel     */
2212a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon    GrRenderTarget* getRenderTarget() const {
2222a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon        return static_cast<GrRenderTarget*>(fRenderTarget.getResource());
2232a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon    }
22421aed57023bad4b04076c59e37097c7563efc028egdaniel
22521aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
22621aed57023bad4b04076c59e37097c7563efc028egdaniel
22721aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
22821aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Stencil
22921aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
23021aed57023bad4b04076c59e37097c7563efc028egdaniel
23121aed57023bad4b04076c59e37097c7563efc028egdaniel    const GrStencilSettings& getStencil() const { return fStencilSettings; }
23221aed57023bad4b04076c59e37097c7563efc028egdaniel
23321aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
23421aed57023bad4b04076c59e37097c7563efc028egdaniel
23521aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
23621aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name State Flags
23721aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
23821aed57023bad4b04076c59e37097c7563efc028egdaniel
23921aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
24021aed57023bad4b04076c59e37097c7563efc028egdaniel     *  Flags that affect rendering. Controlled using enable/disableState(). All
24121aed57023bad4b04076c59e37097c7563efc028egdaniel     *  default to disabled.
24221aed57023bad4b04076c59e37097c7563efc028egdaniel     */
24321aed57023bad4b04076c59e37097c7563efc028egdaniel    enum StateBits {
24421aed57023bad4b04076c59e37097c7563efc028egdaniel        /**
24521aed57023bad4b04076c59e37097c7563efc028egdaniel         * Perform dithering. TODO: Re-evaluate whether we need this bit
24621aed57023bad4b04076c59e37097c7563efc028egdaniel         */
24721aed57023bad4b04076c59e37097c7563efc028egdaniel        kDither_StateBit        = 0x01,
24821aed57023bad4b04076c59e37097c7563efc028egdaniel        /**
24921aed57023bad4b04076c59e37097c7563efc028egdaniel         * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
25021aed57023bad4b04076c59e37097c7563efc028egdaniel         * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
25121aed57023bad4b04076c59e37097c7563efc028egdaniel         * the 3D API.
25221aed57023bad4b04076c59e37097c7563efc028egdaniel         */
25321aed57023bad4b04076c59e37097c7563efc028egdaniel        kHWAntialias_StateBit   = 0x02,
25421aed57023bad4b04076c59e37097c7563efc028egdaniel        /**
25521aed57023bad4b04076c59e37097c7563efc028egdaniel         * Draws will respect the clip, otherwise the clip is ignored.
25621aed57023bad4b04076c59e37097c7563efc028egdaniel         */
25721aed57023bad4b04076c59e37097c7563efc028egdaniel        kClip_StateBit          = 0x04,
25821aed57023bad4b04076c59e37097c7563efc028egdaniel        /**
25921aed57023bad4b04076c59e37097c7563efc028egdaniel         * Disables writing to the color buffer. Useful when performing stencil
26021aed57023bad4b04076c59e37097c7563efc028egdaniel         * operations.
26121aed57023bad4b04076c59e37097c7563efc028egdaniel         */
26221aed57023bad4b04076c59e37097c7563efc028egdaniel        kNoColorWrites_StateBit = 0x08,
26321aed57023bad4b04076c59e37097c7563efc028egdaniel
26421aed57023bad4b04076c59e37097c7563efc028egdaniel        /**
26521aed57023bad4b04076c59e37097c7563efc028egdaniel         * Usually coverage is applied after color blending. The color is blended using the coeffs
26621aed57023bad4b04076c59e37097c7563efc028egdaniel         * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
26721aed57023bad4b04076c59e37097c7563efc028egdaniel         * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
26821aed57023bad4b04076c59e37097c7563efc028egdaniel         * this case there is no distinction between coverage and color and the caller needs direct
26921aed57023bad4b04076c59e37097c7563efc028egdaniel         * control over the blend coeffs. When set, there will be a single blend step controlled by
27021aed57023bad4b04076c59e37097c7563efc028egdaniel         * setBlendFunc() which will use coverage*color as the src color.
27121aed57023bad4b04076c59e37097c7563efc028egdaniel         */
27221aed57023bad4b04076c59e37097c7563efc028egdaniel         kCoverageDrawing_StateBit = 0x10,
27321aed57023bad4b04076c59e37097c7563efc028egdaniel
27421aed57023bad4b04076c59e37097c7563efc028egdaniel        // Users of the class may add additional bits to the vector
27521aed57023bad4b04076c59e37097c7563efc028egdaniel        kDummyStateBit,
27621aed57023bad4b04076c59e37097c7563efc028egdaniel        kLastPublicStateBit = kDummyStateBit-1,
27721aed57023bad4b04076c59e37097c7563efc028egdaniel    };
27821aed57023bad4b04076c59e37097c7563efc028egdaniel
2793658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    uint32_t getFlagBits() const { return fFlagBits; }
2803658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
28121aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & fFlagBits); }
28221aed57023bad4b04076c59e37097c7563efc028egdaniel
28321aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); }
28421aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_StateBit); }
28521aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); }
28621aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_StateBit); }
28721aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_StateBit); }
28821aed57023bad4b04076c59e37097c7563efc028egdaniel
28921aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
29021aed57023bad4b04076c59e37097c7563efc028egdaniel
29121aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
29221aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @name Face Culling
29321aed57023bad4b04076c59e37097c7563efc028egdaniel    ////
29421aed57023bad4b04076c59e37097c7563efc028egdaniel
29521aed57023bad4b04076c59e37097c7563efc028egdaniel    enum DrawFace {
29621aed57023bad4b04076c59e37097c7563efc028egdaniel        kInvalid_DrawFace = -1,
29721aed57023bad4b04076c59e37097c7563efc028egdaniel
29821aed57023bad4b04076c59e37097c7563efc028egdaniel        kBoth_DrawFace,
29921aed57023bad4b04076c59e37097c7563efc028egdaniel        kCCW_DrawFace,
30021aed57023bad4b04076c59e37097c7563efc028egdaniel        kCW_DrawFace,
30121aed57023bad4b04076c59e37097c7563efc028egdaniel    };
30221aed57023bad4b04076c59e37097c7563efc028egdaniel
30321aed57023bad4b04076c59e37097c7563efc028egdaniel    /**
30421aed57023bad4b04076c59e37097c7563efc028egdaniel     * Gets whether the target is drawing clockwise, counterclockwise,
30521aed57023bad4b04076c59e37097c7563efc028egdaniel     * or both faces.
30621aed57023bad4b04076c59e37097c7563efc028egdaniel     * @return the current draw face(s).
30721aed57023bad4b04076c59e37097c7563efc028egdaniel     */
30821aed57023bad4b04076c59e37097c7563efc028egdaniel    DrawFace getDrawFace() const { return fDrawFace; }
30921aed57023bad4b04076c59e37097c7563efc028egdaniel
31021aed57023bad4b04076c59e37097c7563efc028egdaniel    /// @}
31121aed57023bad4b04076c59e37097c7563efc028egdaniel
31221aed57023bad4b04076c59e37097c7563efc028egdaniel    ///////////////////////////////////////////////////////////////////////////
3133658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    /// @name Hints
3143658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    /// Hints that when provided can enable optimizations.
3153658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    ////
3163658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
3173658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    enum Hints { kVertexColorsAreOpaque_Hint = 0x1, };
3183658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
3193658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    bool vertexColorsAreOpaque() const { return kVertexColorsAreOpaque_Hint & fHints; }
3203658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
3213658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    /// @}
3223658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
3233658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    ///////////////////////////////////////////////////////////////////////////
32421aed57023bad4b04076c59e37097c7563efc028egdaniel
32521aed57023bad4b04076c59e37097c7563efc028egdaniel    /** Return type for CombineIfPossible. */
32621aed57023bad4b04076c59e37097c7563efc028egdaniel    enum CombinedState {
32721aed57023bad4b04076c59e37097c7563efc028egdaniel        /** The GrDrawStates cannot be combined. */
32821aed57023bad4b04076c59e37097c7563efc028egdaniel        kIncompatible_CombinedState,
32921aed57023bad4b04076c59e37097c7563efc028egdaniel        /** Either draw state can be used in place of the other. */
33021aed57023bad4b04076c59e37097c7563efc028egdaniel        kAOrB_CombinedState,
33121aed57023bad4b04076c59e37097c7563efc028egdaniel        /** Use the first draw state. */
33221aed57023bad4b04076c59e37097c7563efc028egdaniel        kA_CombinedState,
33321aed57023bad4b04076c59e37097c7563efc028egdaniel        /** Use the second draw state. */
33421aed57023bad4b04076c59e37097c7563efc028egdaniel        kB_CombinedState,
33521aed57023bad4b04076c59e37097c7563efc028egdaniel    };
33621aed57023bad4b04076c59e37097c7563efc028egdaniel
33721aed57023bad4b04076c59e37097c7563efc028egdanielprotected:
3382a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon    /**
3392a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     * Converts refs on GrGpuResources owned directly or indirectly by this GrRODrawState into
3402a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     * pending reads and writes. This should be called when a GrDrawState is recorded into
3412a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     * a GrDrawTarget for later execution. Subclasses of GrRODrawState may add setters. However,
3422a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     * once this call has been made the GrRODrawState is immutable. It is also no longer copyable.
3432a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     * In the future this conversion will automatically happen when converting a GrDrawState into
3442a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     * an optimized draw state.
3452a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon     */
3462a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon    void convertToPendingExec();
3472a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon
3482a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon    friend class GrDrawTarget;
3492a9ca782ba5db5a7ebdc315655d9a30dcd6d9845bsalomon
3503658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    explicit GrRODrawState(const GrRODrawState& drawState);
3513658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
35221aed57023bad4b04076c59e37097c7563efc028egdaniel    bool isEqual(const GrRODrawState& that) const;
35321aed57023bad4b04076c59e37097c7563efc028egdaniel
354170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel    /**
355170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * Optimizations for blending / coverage to that can be applied based on the current state.
356170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     */
357170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel    enum BlendOptFlags {
358170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        /**
359170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * No optimization
360170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         */
361170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        kNone_BlendOpt                  = 0,
362170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        /**
363170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * Don't draw at all
364170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         */
365170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        kSkipDraw_BlendOptFlag          = 0x1,
366170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        /**
367170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * The coverage value does not have to be computed separately from alpha, the the output
368170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * color can be the modulation of the two.
369170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         */
370170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        kCoverageAsAlpha_BlendOptFlag   = 0x2,
371170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        /**
372170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * Instead of emitting a src color, emit coverage in the alpha channel and r,g,b are
373170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * "don't cares".
374170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         */
375170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        kEmitCoverage_BlendOptFlag      = 0x4,
376170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        /**
377170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         * Emit transparent black instead of the src color, no need to compute coverage.
378170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel         */
379170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel        kEmitTransBlack_BlendOptFlag    = 0x8,
380170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel    };
381170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel    GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
382170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel
383170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel    /**
384170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * Determines what optimizations can be applied based on the blend. The coefficients may have
385170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
386170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * params that receive the tweaked coefficients. Normally the function looks at the current
387170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
388170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * determine the blend optimizations that would be used if there was partial pixel coverage.
389170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     *
390170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * Subclasses of GrDrawTarget that actually draw (as opposed to those that just buffer for
391170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * playback) must call this function and respect the flags that replace the output color.
392170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     *
393170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * If the cached BlendOptFlags does not have the invalidate bit set, then getBlendOpts will
394170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     * simply returned the cached flags and coefficients. Otherwise it will calculate the values.
395170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel     */
396170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel    BlendOptFlags getBlendOpts(bool forceCoverage = false,
397170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel                               GrBlendCoeff* srcCoeff = NULL,
398170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel                               GrBlendCoeff* dstCoeff = NULL) const;
399170f90b4576f291879371ecd6ae4bc2b1d85c64aegdaniel
400f96ba02513eadd9fa24d75396ec9f2d6682e464cbsalomon    typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget;
40121aed57023bad4b04076c59e37097c7563efc028egdaniel    // These fields are roughly sorted by decreasing likelihood of being different in op==
402c49233410e7278f0b95b8b0d28a80105a2930a88bsalomon    ProgramRenderTarget                 fRenderTarget;
40321aed57023bad4b04076c59e37097c7563efc028egdaniel    GrColor                             fColor;
40421aed57023bad4b04076c59e37097c7563efc028egdaniel    SkMatrix                            fViewMatrix;
40521aed57023bad4b04076c59e37097c7563efc028egdaniel    GrColor                             fBlendConstant;
40621aed57023bad4b04076c59e37097c7563efc028egdaniel    uint32_t                            fFlagBits;
40721aed57023bad4b04076c59e37097c7563efc028egdaniel    const GrVertexAttrib*               fVAPtr;
40821aed57023bad4b04076c59e37097c7563efc028egdaniel    int                                 fVACount;
4097b3d5ee72c2238aa239bce4d5b3aea98a437ca7aegdaniel    size_t                              fVAStride;
41021aed57023bad4b04076c59e37097c7563efc028egdaniel    GrStencilSettings                   fStencilSettings;
41121aed57023bad4b04076c59e37097c7563efc028egdaniel    uint8_t                             fCoverage;
41221aed57023bad4b04076c59e37097c7563efc028egdaniel    DrawFace                            fDrawFace;
41321aed57023bad4b04076c59e37097c7563efc028egdaniel    GrBlendCoeff                        fSrcBlend;
41421aed57023bad4b04076c59e37097c7563efc028egdaniel    GrBlendCoeff                        fDstBlend;
41521aed57023bad4b04076c59e37097c7563efc028egdaniel
416b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    typedef SkSTArray<4, GrFragmentStage>   FragmentStageArray;
417b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    SkAutoTDelete<GrGeometryStage>          fGeometryProcessor;
418b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    FragmentStageArray                      fColorStages;
419b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    FragmentStageArray                      fCoverageStages;
42021aed57023bad4b04076c59e37097c7563efc028egdaniel
4213658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    uint32_t                            fHints;
4223658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
42321aed57023bad4b04076c59e37097c7563efc028egdaniel    // This is simply a different representation of info in fVertexAttribs and thus does
42421aed57023bad4b04076c59e37097c7563efc028egdaniel    // not need to be compared in op==.
42521aed57023bad4b04076c59e37097c7563efc028egdaniel    int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt];
42621aed57023bad4b04076c59e37097c7563efc028egdaniel
42721aed57023bad4b04076c59e37097c7563efc028egdanielprivate:
4283658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    /**
4293658f382cc129e463d7f40a7e68214d04d50fe14egdaniel     * Determines whether src alpha is guaranteed to be one for all src pixels
4303658f382cc129e463d7f40a7e68214d04d50fe14egdaniel     */
4313658f382cc129e463d7f40a7e68214d04d50fe14egdaniel    bool srcAlphaWillBeOne() const;
4323658f382cc129e463d7f40a7e68214d04d50fe14egdaniel
43321aed57023bad4b04076c59e37097c7563efc028egdaniel    typedef SkRefCnt INHERITED;
43421aed57023bad4b04076c59e37097c7563efc028egdaniel};
43521aed57023bad4b04076c59e37097c7563efc028egdaniel
43621aed57023bad4b04076c59e37097c7563efc028egdanielGR_MAKE_BITFIELD_OPS(GrRODrawState::BlendOptFlags);
43721aed57023bad4b04076c59e37097c7563efc028egdaniel
43821aed57023bad4b04076c59e37097c7563efc028egdaniel#endif
439