GrDrawState.h revision 75847199c25121c9989e8dba103ac6002d2132d6
19381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com/*
29381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com * Copyright 2011 Google Inc.
39381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com *
49381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com * Use of this source code is governed by a BSD-style license that can be
59381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com * found in the LICENSE file.
69381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com */
79381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
89381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#ifndef GrDrawState_DEFINED
99381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#define GrDrawState_DEFINED
109381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
112eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com#include "GrBackendEffectFactory.h"
129381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#include "GrColor.h"
1308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com#include "GrEffectStage.h"
14cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com#include "GrRefCnt.h"
15cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com#include "GrRenderTarget.h"
169381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#include "GrStencil.h"
17cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com#include "GrTemplates.h"
1864aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com#include "GrTexture.h"
1968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "effects/GrSimpleTextureEffect.h"
209381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
21cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com#include "SkMatrix.h"
229381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#include "SkXfermode.h"
239381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
24af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.comclass GrPaint;
259381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
262e3d144dfdbc4596d33baef7396316f88f412cc8bsalomon@google.comclass GrDrawState : public GrRefCnt {
272e3d144dfdbc4596d33baef7396316f88f412cc8bsalomon@google.compublic:
28fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com    SK_DECLARE_INST_COUNT(GrDrawState)
29d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
309381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    /**
311322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * Total number of effect stages. Each stage can host a GrEffect. A stage is enabled if it has a
321322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * GrEffect. The effect produces an output color in the fragment shader. It's inputs are the
331322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * output from the previous enabled stage and a position. The position is either derived from
341322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * the interpolated vertex positions or explicit per-vertex coords, depending upon the
351322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * GrVertexLayout used to draw.
36bf5cad4e9c5808493b35cb9b0000a2d36b7f9b78robertphillips@google.com     *
371322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * The stages are divided into two sets, color-computing and coverage-computing. The final color
381322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * stage produces the final pixel color. The coverage-computing stages function exactly as the
391322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * color-computing but the output of the final coverage stage is treated as a fractional pixel
401322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * coverage rather than as input to the src/dst color blend step.
411322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     *
421322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * The input color to the first enabled color-stage is either the constant color or interpolated
431322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * per-vertex colors, depending upon GrVertexLayout. The input to the first coverage stage is
441322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * either a constant coverage (usually full-coverage), interpolated per-vertex coverage, or
451322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * edge-AA computed coverage. (This latter is going away as soon as it can be rewritten as a
461322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * GrEffect).
471322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     *
48cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     * See the documentation of kCoverageDrawing_StateBit for information about disabling the
49cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     * the color / coverage distinction.
50cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     *
511322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * Stages 0 through GrPaint::kTotalStages-1 are reserved for stages copied from the client's
521322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * GrPaint. Stages GrPaint::kTotalStages through kNumStages-2 are earmarked for use by
531322134a3a7232e4ae3433733ed425fd9966bd5cbsalomon@google.com     * GrTextContext and GrPathRenderer-derived classes. kNumStages-1 is earmarked for clipping
54dfdb7e5240276493077b7c6e1f3cc8b8a0e195babsalomon@google.com     * by GrClipMaskManager.
559381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     */
569381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    enum {
57580711694654b8edc70028d09c4211445b661466twiz@google.com        kNumStages = 5,
589381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com        kMaxTexCoords = kNumStages
599381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    };
609381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
61ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrDrawState() {
6275847199c25121c9989e8dba103ac6002d2132d6reed@google.com#if GR_DEBUG
6375847199c25121c9989e8dba103ac6002d2132d6reed@google.com        VertexLayoutUnitTest();
6475847199c25121c9989e8dba103ac6002d2132d6reed@google.com#endif
6552a5dcb43b5acbde377f664807b0f75af8bb1666bsalomon@google.com        this->reset();
6652a5dcb43b5acbde377f664807b0f75af8bb1666bsalomon@google.com    }
6746f7afb9867200b568c21736da8a8bbb56b20e30bsalomon@google.com
68ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrDrawState(const GrDrawState& state) {
6946f7afb9867200b568c21736da8a8bbb56b20e30bsalomon@google.com        *this = state;
7046f7afb9867200b568c21736da8a8bbb56b20e30bsalomon@google.com    }
7146f7afb9867200b568c21736da8a8bbb56b20e30bsalomon@google.com
729ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com    virtual ~GrDrawState() {
737d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        this->disableStages();
749ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com    }
759ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
7652a5dcb43b5acbde377f664807b0f75af8bb1666bsalomon@google.com    /**
777d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com     * Resets to the default state.
7808283afc265f1153834256fc1012519813ba6b73bsalomon@google.com     * GrEffects will be removed from all stages.
79d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
8052a5dcb43b5acbde377f664807b0f75af8bb1666bsalomon@google.com    void reset() {
819ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
827d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        this->disableStages();
8352a5dcb43b5acbde377f664807b0f75af8bb1666bsalomon@google.com
84ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fRenderTarget.reset(NULL);
85ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
86ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColor = 0xffffffff;
87ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fViewMatrix.reset();
88ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fSrcBlend = kOne_GrBlendCoeff;
89ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDstBlend = kZero_GrBlendCoeff;
90ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fBlendConstant = 0x0;
91ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits = 0x0;
92ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fVertexEdgeType = kHairLine_EdgeType;
93ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fStencilSettings.setDisabled();
94ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFirstCoverageStage = kNumStages;
95ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fCoverage = 0xffffffff;
96ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
97ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterColor = 0x0;
98ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDrawFace = kBoth_DrawFace;
99af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com    }
100af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com
101af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com    /**
102af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
1031e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * encompasses more than GrPaint. Aspects of GrDrawState that have no
104af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     * GrPaint equivalents are not modified. GrPaint has fewer stages than
105af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     * GrDrawState. The extra GrDrawState stages are disabled.
106af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     */
107af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com    void setFromPaint(const GrPaint& paint);
1088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
1098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
110cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /// @name Vertex Format
111cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    ////
112cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
113cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
114cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * The format of vertices is represented as a bitfield of flags.
115cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Flags that indicate the layout of vertex data. Vertices always contain
116cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * positions and may also contain up to GrDrawState::kMaxTexCoords sets
117cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
118cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Each stage can
119cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * use any of the texture coordinates as its input texture coordinates or it
120cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * may use the positions as texture coordinates.
121cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
122cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * If no texture coordinates are specified for a stage then the stage is
123cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * disabled.
124cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
125cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Only one type of texture coord can be specified per stage. For
126cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * example StageTexCoordVertexLayoutBit(0, 2) and
127cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
128cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
129cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * The order in memory is always (position, texture coord 0, ..., color,
130cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coverage) with any unused fields omitted. Note that this means that if
131cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * only texture coordinates 1 is referenced then there is no texture
132cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coordinates 0 and the order would be (position, texture coordinate 1
133cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * [, color][, coverage]).
134cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
135cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
136cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
137cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Generates a bit indicating that a texture stage uses texture coordinates
138cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
139cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param stageIdx    the stage that will use texture coordinates.
140cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param texCoordIdx the index of the texture coordinates to use
141cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
142cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return the bit to add to a GrVertexLayout bitfield.
143cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
144cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int StageTexCoordVertexLayoutBit(int stageIdx, int texCoordIdx) {
145cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        GrAssert(stageIdx < kNumStages);
146cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        GrAssert(texCoordIdx < kMaxTexCoords);
147cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return 1 << (stageIdx + (texCoordIdx * kNumStages));
148cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
149cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
150cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static bool StageUsesTexCoords(GrVertexLayout layout, int stageIdx);
151cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
152cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.comprivate:
153cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // non-stage bits start at this index.
154cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static const int STAGE_BIT_CNT = kNumStages * kMaxTexCoords;
155cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.compublic:
156cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
157cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
158cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Additional Bits that can be specified in GrVertexLayout.
159cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
160cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    enum VertexLayoutBits {
161cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* vertices have colors (GrColor) */
162cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kColor_VertexLayoutBit              = 1 << (STAGE_BIT_CNT + 0),
163cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* vertices have coverage (GrColor)
164cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         */
165cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kCoverage_VertexLayoutBit           = 1 << (STAGE_BIT_CNT + 1),
166cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* Use text vertices. (Pos and tex coords may be a different type for
167cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         * text [GrGpuTextVertex vs GrPoint].)
168cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         */
169cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kTextFormat_VertexLayoutBit         = 1 << (STAGE_BIT_CNT + 2),
170cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
171cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* Each vertex specificies an edge. Distance to the edge is used to
172cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         * compute a coverage. See GrDrawState::setVertexEdgeType().
173cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         */
174cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kEdge_VertexLayoutBit               = 1 << (STAGE_BIT_CNT + 3),
175cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        // for below assert
176cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kDummyVertexLayoutBit,
177cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
178cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    };
179cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // make sure we haven't exceeded the number of bits in GrVertexLayout.
180cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
181cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
182cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    ////////////////////////////////////////////////////////////////////////////
183cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // Helpers for picking apart vertex layouts
184cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
185cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
186cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the size of a vertex from a vertex layout
187cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return size of a single vertex.
188cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
189cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static size_t VertexSize(GrVertexLayout vertexLayout);
190cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
191cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
192cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function for determining the index of texture coordinates that
193cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * is input for a texture stage. Note that a stage may instead use positions
194cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * as texture coordinates, in which case the result of the function is
195cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * indistinguishable from the case when the stage is disabled.
196cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
197cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param stageIdx      the stage to query
198cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout  layout to query
199cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
200cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return the texture coordinate index or -1 if the stage doesn't use
201cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         separate (non-position) texture coordinates.
202cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
203cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexTexCoordsForStage(int stageIdx, GrVertexLayout vertexLayout);
204cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
205cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
206cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the offset of texture coordinates in a vertex
207cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return offset of texture coordinates in vertex layout or -1 if the
208cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         layout has no texture coordinates. Will be 0 if positions are
209cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         used as texture coordinates for the stage.
210cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
211cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout);
212cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
213cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
214cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the offset of the color in a vertex
215cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return offset of color in vertex layout or -1 if the
216cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         layout has no color.
217cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
218cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexColorOffset(GrVertexLayout vertexLayout);
219cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
220cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
221cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the offset of the coverage in a vertex
222cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return offset of coverage in vertex layout or -1 if the
223cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         layout has no coverage.
224cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
225cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexCoverageOffset(GrVertexLayout vertexLayout);
226cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
227cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     /**
228cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      * Helper function to compute the offset of the edge pts in a vertex
229cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      * @return offset of edge in vertex layout or -1 if the
230cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      *         layout has no edge.
231cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      */
232cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     static int VertexEdgeOffset(GrVertexLayout vertexLayout);
233cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
234cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
235cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to determine if vertex layout contains explicit texture
236cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coordinates of some index.
237cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
238cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param coordIndex    the tex coord index to query
239cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout  layout to query
240cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
241cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return true if vertex specifies texture coordinates for the index,
242cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *              false otherwise.
243cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
244cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static bool VertexUsesTexCoordIdx(int coordIndex,
245cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                      GrVertexLayout vertexLayout);
246cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
247cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
248cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the size of each vertex and the offsets of
249cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * texture coordinates and color. Determines tex coord offsets by tex coord
250cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * index rather than by stage. (Each stage can be mapped to any t.c. index
251cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * by StageTexCoordVertexLayoutBit.)
252cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
253cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout          the layout to query
254cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param texCoordOffsetsByIdx  after return it is the offset of each
255cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              tex coord index in the vertex or -1 if
256cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              index isn't used. (optional)
257cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param colorOffset           after return it is the offset of the
258cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              color field in each vertex, or -1 if
259cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              there aren't per-vertex colors. (optional)
260cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param coverageOffset        after return it is the offset of the
261cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              coverage field in each vertex, or -1 if
262cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              there aren't per-vertex coeverages.
263cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              (optional)
264cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param edgeOffset            after return it is the offset of the
265cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              edge eq field in each vertex, or -1 if
266cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              there aren't per-vertex edge equations.
267cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              (optional)
268cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return size of a single vertex
269cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
270cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
271cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int texCoordOffsetsByIdx[kMaxTexCoords],
272cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int *colorOffset,
273cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int *coverageOffset,
274cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* edgeOffset);
275cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
276cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
277cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the size of each vertex and the offsets of
278cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * texture coordinates and color. Determines tex coord offsets by stage
279cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * rather than by index. (Each stage can be mapped to any t.c. index
280cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
281cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * tex coords then that stage's offset will be 0 (positions are always at 0).
282cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
283cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout              the layout to query
284cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param texCoordOffsetsByStage    after return it is the offset of each
285cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  tex coord index in the vertex or -1 if
286cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  index isn't used. (optional)
287cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param colorOffset               after return it is the offset of the
288cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  color field in each vertex, or -1 if
289cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  there aren't per-vertex colors.
290cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  (optional)
291cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param coverageOffset            after return it is the offset of the
292cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  coverage field in each vertex, or -1 if
293cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  there aren't per-vertex coeverages.
294cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  (optional)
295cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param edgeOffset                after return it is the offset of the
296cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  edge eq field in each vertex, or -1 if
297cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  there aren't per-vertex edge equations.
298cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  (optional)
299cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return size of a single vertex
300cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
301cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
302cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int texCoordOffsetsByStage[kNumStages],
303cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* colorOffset,
304cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* coverageOffset,
305cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* edgeOffset);
306cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
307cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // determine if src alpha is guaranteed to be one for all src pixels
308cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
309cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
310cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
311cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Accessing positions, texture coords, or colors, of a vertex within an
312cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * array is a hassle involving casts and simple math. These helpers exist
313cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * to keep GrDrawTarget clients' code a bit nicer looking.
314cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
315cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
316cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
317cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Gets a pointer to a GrPoint of a vertex's position or texture
318cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coordinate.
319cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertices      the vetex array
320cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexIndex   the index of the vertex in the array
321cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexSize    the size of each vertex in the array
322cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param offset        the offset in bytes of the vertex component.
323cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                      Defaults to zero (corresponding to vertex position)
324cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return pointer to the vertex component as a GrPoint
325cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
326cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static GrPoint* GetVertexPoint(void* vertices,
327cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexIndex,
328cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexSize,
329cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int offset = 0) {
330cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        intptr_t start = GrTCast<intptr_t>(vertices);
331cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<GrPoint*>(start + offset +
332cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                 vertexIndex * vertexSize);
333cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
334cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static const GrPoint* GetVertexPoint(const void* vertices,
335cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexIndex,
336cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexSize,
337cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int offset = 0) {
338cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        intptr_t start = GrTCast<intptr_t>(vertices);
339cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<const GrPoint*>(start + offset +
340cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                       vertexIndex * vertexSize);
341cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
342cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
343cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
344cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Gets a pointer to a GrColor inside a vertex within a vertex array.
345cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertices      the vetex array
346cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexIndex   the index of the vertex in the array
347cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexSize    the size of each vertex in the array
348cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param offset        the offset in bytes of the vertex color
349cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return pointer to the vertex component as a GrColor
350cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
351cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static GrColor* GetVertexColor(void* vertices,
352cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexIndex,
353cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexSize,
354cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int offset) {
355cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        intptr_t start = GrTCast<intptr_t>(vertices);
356cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<GrColor*>(start + offset +
357cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                 vertexIndex * vertexSize);
358cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
359cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static const GrColor* GetVertexColor(const void* vertices,
360cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexIndex,
361cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexSize,
362cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int offset) {
363cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        const intptr_t start = GrTCast<intptr_t>(vertices);
364cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<const GrColor*>(start + offset +
365cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                       vertexIndex * vertexSize);
366cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
367cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
368cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static void VertexLayoutUnitTest();
369cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
370cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /// @}
371cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
372cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    ///////////////////////////////////////////////////////////////////////////
3738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Color
3748f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
3758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
3768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
3778f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Sets color for next draw to a premultiplied-alpha color.
3788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
3798f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param color    the color to set.
3808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
381ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void setColor(GrColor color) { fCommon.fColor = color; }
3828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
383ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrColor getColor() const { return fCommon.fColor; }
3848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
3858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
3868f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Sets the color to be used for the next draw to be
3878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  (r,g,b,a) = (alpha, alpha, alpha, alpha).
3888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
3898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param alpha The alpha value to set as the color.
3908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
3918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setAlpha(uint8_t a) {
3928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        this->setColor((a << 24) | (a << 16) | (a << 8) | a);
3938f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
3948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
3958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
3968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Add a color filter that can be represented by a color and a mode. Applied
3978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * after color-computing texture stages.
3988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
3998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setColorFilter(GrColor c, SkXfermode::Mode mode) {
400ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterColor = c;
401ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterMode = mode;
4028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
4038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
404ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
405ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
4068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
4075b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    /**
4085b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     * Constructor sets the color to be 'color' which is undone by the destructor.
4095b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     */
4105b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    class AutoColorRestore : public ::GrNoncopyable {
4115b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    public:
4125b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        AutoColorRestore(GrDrawState* drawState, GrColor color) {
4135b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fDrawState = drawState;
4145b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fOldColor = fDrawState->getColor();
4155b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fDrawState->setColor(color);
4165b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        }
4175b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        ~AutoColorRestore() {
4185b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fDrawState->setColor(fOldColor);
4195b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        }
4205b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    private:
4215b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        GrDrawState*    fDrawState;
4225b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        GrColor         fOldColor;
4235b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    };
4245b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
4258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
4268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
4278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
4282401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /// @name Coverage
4292401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    ////
4302401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4312401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /**
432d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Sets a constant fractional coverage to be applied to the draw. The
4332401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * initial value (after construction or reset()) is 0xff. The constant
4342401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * coverage is ignored when per-vertex coverage is provided.
4352401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     */
4362401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    void setCoverage(uint8_t coverage) {
437ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
4382401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    }
4392401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4402401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /**
4412401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * Version of above that specifies 4 channel per-vertex color. The value
4422401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * should be premultiplied.
4432401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     */
4442401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    void setCoverage4(GrColor coverage) {
445ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fCoverage = coverage;
4462401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    }
4472401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4482401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    GrColor getCoverage() const {
449ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return fCommon.fCoverage;
4502401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    }
4512401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4522401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /// @}
4532401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4542401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
455adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    /// @name Effect Stages
4568f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
4578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
458adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
459adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        fStages[stageIdx].setEffect(effect);
460adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        return effect;
461adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    }
462adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com
4638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
46468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com     * Creates a GrSimpleTextureEffect.
4651e8f016305805d4d8cad74aba3a21b78486f9d6ftomhudson@google.com     */
466b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
46708283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrAssert(!this->getStage(stageIdx).getEffect());
46868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
469adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        this->setEffect(stageIdx, effect)->unref();
470dfdb7e5240276493077b7c6e1f3cc8b8a0e195babsalomon@google.com    }
47108283afc265f1153834256fc1012519813ba6b73bsalomon@google.com    void createTextureEffect(int stageIdx,
47208283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                             GrTexture* texture,
473b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                             const SkMatrix& matrix,
474dfdb7e5240276493077b7c6e1f3cc8b8a0e195babsalomon@google.com                             const GrTextureParams& params) {
47508283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrAssert(!this->getStage(stageIdx).getEffect());
47668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
477adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        this->setEffect(stageIdx, effect)->unref();
4781ce49fc91714ce8974d11246d29ebe7b97b5fe98bsalomon@google.com    }
4791ce49fc91714ce8974d11246d29ebe7b97b5fe98bsalomon@google.com
4807d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    bool stagesDisabled() {
4817d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        for (int i = 0; i < kNumStages; ++i) {
48208283afc265f1153834256fc1012519813ba6b73bsalomon@google.com            if (NULL != fStages[i].getEffect()) {
4837d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com                return false;
4847d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com            }
4857d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        }
4863eee8fbe0f280bc1dea59dc0b0ebd8021b51137ftomhudson@google.com        return true;
4877d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    }
488676e66096c60615bac52f365111596de5c4ca8a6tomhudson@google.com
489adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    void disableStage(int stageIdx) { this->setEffect(stageIdx, NULL); }
490676e66096c60615bac52f365111596de5c4ca8a6tomhudson@google.com
491972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    /**
492f271cc7183fe48ac64d2d9a454eb013c91b42d53bsalomon@google.com     * Release all the GrEffects referred to by this draw state.
493972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com     */
4947d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    void disableStages() {
495972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        for (int i = 0; i < kNumStages; ++i) {
496676e66096c60615bac52f365111596de5c4ca8a6tomhudson@google.com            this->disableStage(i);
497972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        }
498972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    }
499972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com
5007d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    class AutoStageDisable : public ::GrNoncopyable {
501972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    public:
5027d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
5037d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        ~AutoStageDisable() {
504972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com            if (NULL != fDrawState) {
5057d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com                fDrawState->disableStages();
506972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com            }
507972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        }
508972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    private:
509972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        GrDrawState* fDrawState;
510972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    };
511972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com
5128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
51308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com     * Returns the current stage by index.
5148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
51508283afc265f1153834256fc1012519813ba6b73bsalomon@google.com    const GrEffectStage& getStage(int stageIdx) const {
51608283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrAssert((unsigned)stageIdx < kNumStages);
51708283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        return fStages[stageIdx];
5188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
5198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
521288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * Called when the source coord system is changing. preConcat gives the transformation from the
522288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * old coord system to the new coord system.
5238f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
524b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    void preConcatStageMatrices(const SkMatrix& preConcat) {
525cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com        this->preConcatStageMatrices(~0U, preConcat);
526cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com    }
527cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com    /**
528cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com     * Version of above that applies the update matrix selectively to stages via a mask.
529cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com     */
530cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com    void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) {
5318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        for (int i = 0; i < kNumStages; ++i) {
532cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com            if (((1 << i) & stageMask) && this->isStageEnabled(i)) {
53308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                fStages[i].preConcatCoordChange(preConcat);
5348f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
5358f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
5368f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
5378f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
538e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com    /**
539288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * Called when the source coord system is changing. preConcatInverse is the inverse of the
540288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * transformation from the old coord system to the new coord system. Returns false if the matrix
541288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * cannot be inverted.
542e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com     */
543b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
544b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix inv;
545e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        bool computed = false;
546e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        for (int i = 0; i < kNumStages; ++i) {
547e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com            if (this->isStageEnabled(i)) {
548288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com                if (!computed && !preConcatInverse.invert(&inv)) {
549e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                    return false;
550e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                } else {
551e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                    computed = true;
552e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                }
55308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                fStages[i].preConcatCoordChange(preConcatInverse);
554e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com            }
555e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        }
556e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        return true;
557e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com    }
558e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com
5598f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
5608f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5618f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
5628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Coverage / Color Stages
5638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
5648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
5668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * A common pattern is to compute a color with the initial stages and then
5678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * modulate that color by a coverage value in later stage(s) (AA, mask-
568d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
569d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * computed based on the pre-coverage-modulated color. The division of
570d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * stages between color-computing and coverage-computing is specified by
5718f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * this method. Initially this is kNumStages (all stages
5728f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * are color-computing).
5738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
5748f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setFirstCoverageStage(int firstCoverageStage) {
5758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        GrAssert((unsigned)firstCoverageStage <= kNumStages);
576ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFirstCoverageStage = firstCoverageStage;
5778f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
5788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5798f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
5808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Gets the index of the first coverage-computing stage.
5818f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
5828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    int getFirstCoverageStage() const {
583ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return fCommon.fFirstCoverageStage;
5848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
5858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5868f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///@}
5878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
5898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Blending
5908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
5918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
5931e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * Sets the blending function coefficients.
5948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
5958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * The blend function will be:
5968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *    D' = sat(S*srcCoef + D*dstCoef)
5978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
5988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *   where D is the existing destination color, S is the incoming source
5998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *   color, and D' is the new destination color that will be written. sat()
6008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *   is the saturation function.
6018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6021e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param srcCoef coefficient applied to the src color.
6031e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param dstCoef coefficient applied to the dst color.
6048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
6058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
606ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fSrcBlend = srcCoeff;
607ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDstBlend = dstCoeff;
6088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    #if GR_DEBUG
6098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        switch (dstCoeff) {
61047059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kDC_GrBlendCoeff:
61147059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kIDC_GrBlendCoeff:
61247059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kDA_GrBlendCoeff:
61347059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kIDA_GrBlendCoeff:
6148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
6158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                     "coverage stages.\n");
6168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        default:
6188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
6208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        switch (srcCoeff) {
62147059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kSC_GrBlendCoeff:
62247059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kISC_GrBlendCoeff:
62347059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kSA_GrBlendCoeff:
62447059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kISA_GrBlendCoeff:
6258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            GrPrintf("Unexpected src blend coeff. Won't work correctly with"
6268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                     "coverage stages.\n");
6278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6288f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        default:
6298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6308f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
6318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    #endif
6328f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
6338f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
634ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
635ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
6368f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6378f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
6388f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                          GrBlendCoeff* dstBlendCoeff) const {
639ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        *srcBlendCoeff = fCommon.fSrcBlend;
640ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        *dstBlendCoeff = fCommon.fDstBlend;
6418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
6428f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6448f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Sets the blending function constant referenced by the following blending
6451e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * coefficients:
64647059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kConstC_GrBlendCoeff
64747059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kIConstC_GrBlendCoeff
64847059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kConstA_GrBlendCoeff
64947059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kIConstA_GrBlendCoeff
6508f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param constant the constant to set
6528f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
653ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
6548f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6568f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Retrieves the last value set by setBlendConstant()
6578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return the blending constant value
6588f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
659ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
6608f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6618f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
6628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
6648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name View Matrix
6658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
6668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
668a72eef322c686954cdffa849dc26d8133b802f1drobertphillips@google.com     * Sets the matrix applied to vertex positions.
6698f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6708f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * In the post-view-matrix space the rectangle [0,w]x[0,h]
6718f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * fully covers the render target. (w and h are the width and height of the
672ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * the render-target.)
6738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
674ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
6758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6778f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Gets a writable pointer to the view matrix.
6788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
679ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
6808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6818f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Multiplies the current view matrix by a matrix
6838f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  After this call V' = V*m where V is the old view matrix,
6858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  m is the parameter to this function, and V' is the new view matrix.
6868f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  (We consider positions to be column vectors so position vector p is
6878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  transformed by matrix X as p' = X*p.)
6888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param m the matrix used to modify the view matrix.
6908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
691ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
6928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6938f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Multiplies the current view matrix by a matrix
6958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  After this call V' = m*V where V is the old view matrix,
6978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  m is the parameter to this function, and V' is the new view matrix.
6988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  (We consider positions to be column vectors so position vector p is
6998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  transformed by matrix X as p' = X*p.)
7008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param m the matrix used to modify the view matrix.
7028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
703ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
7048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Retrieves the current view matrix
7078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return the current view matrix.
7088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
709ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
7108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Retrieves the inverse of the current view matrix.
7138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  If the current view matrix is invertible, return true, and if matrix
7158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  is non-null, copy the inverse into it. If the current view matrix is
7168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  non-invertible, return false and ignore the matrix parameter.
7178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param matrix if not null, will receive a copy of the current inverse.
7198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
720b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    bool getViewInverse(SkMatrix* matrix) const {
7218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        // TODO: determine whether we really need to leave matrix unmodified
7228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        // at call sites when inversion fails.
723b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix inverse;
724ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        if (fCommon.fViewMatrix.invert(&inverse)) {
7258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            if (matrix) {
7268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                *matrix = inverse;
7278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
7288f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            return true;
7298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
7308f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        return false;
7318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
7328f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7335b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    ////////////////////////////////////////////////////////////////////////////
7345b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
7355b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    /**
7362fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * Preconcats the current view matrix and restores the previous view matrix in the destructor.
737c196b522d06919885c6bbe28b7b06d2e5b2cb9bfbsalomon@google.com     * Effect matrices are automatically adjusted to compensate.
7385b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     */
7398f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    class AutoViewMatrixRestore : public ::GrNoncopyable {
7408f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    public:
7418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        AutoViewMatrixRestore() : fDrawState(NULL) {}
7422fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
7432fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        AutoViewMatrixRestore(GrDrawState* ds,
744b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                              const SkMatrix& preconcatMatrix,
7452fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com                              uint32_t explicitCoordStageMask = 0) {
7468f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            fDrawState = NULL;
7472fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com            this->set(ds, preconcatMatrix, explicitCoordStageMask);
7488f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
7492fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
7502fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        ~AutoViewMatrixRestore() { this->restore(); }
7512fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
752a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
753a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Can be called prior to destructor to restore the original matrix.
754a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
7552fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        void restore();
756f467ce7bc33af5f496e0619387551aedec6d2517skia.committer@gmail.com
7572fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        void set(GrDrawState* drawState,
758b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                 const SkMatrix& preconcatMatrix,
7592fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com                 uint32_t explicitCoordStageMask = 0);
7602fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
761ded4f4b163f5aa19c22c871178c55ecb34623846bsalomon@google.com        bool isSet() const { return NULL != fDrawState; }
7622fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
7638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    private:
764288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        GrDrawState*                        fDrawState;
765b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix                            fViewMatrix;
76608283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrEffectStage::SavedCoordChange     fSavedCoordChanges[GrDrawState::kNumStages];
767288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        uint32_t                            fRestoreMask;
7689381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    };
7699381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
7705b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    ////////////////////////////////////////////////////////////////////////////
7715b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
7725b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    /**
7732fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * This sets the view matrix to identity and adjusts stage matrices to compensate. The
7742fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * destructor undoes the changes, restoring the view matrix that was set before the
7752fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * constructor. It is similar to passing the inverse of the current view matrix to
7762fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
7775b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     */
7785b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    class AutoDeviceCoordDraw : ::GrNoncopyable {
7795b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    public:
7802fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        AutoDeviceCoordDraw() : fDrawState(NULL) {}
7815b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        /**
7822fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com         * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
7832fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com         * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
7842fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com         * to specify such stages.
7855b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com         */
7865b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        AutoDeviceCoordDraw(GrDrawState* drawState,
7872fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com                            uint32_t explicitCoordStageMask = 0) {
7882fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com            fDrawState = NULL;
7892fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com            this->set(drawState, explicitCoordStageMask);
7902fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        }
7912fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
792a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        ~AutoDeviceCoordDraw() { this->restore(); }
793a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com
7942fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
7952fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
796a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
797a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Returns true if this object was successfully initialized on to a GrDrawState. It may
798a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * return false because a non-default constructor or set() were never called or because
799a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * the view matrix was not invertible.
800a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
8015b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        bool succeeded() const { return NULL != fDrawState; }
8022fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
803a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
804a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Returns the matrix that was set previously set on the drawState. This is only valid
805a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * if succeeded returns true.
806a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
807b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        const SkMatrix& getOriginalMatrix() const {
808a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com            GrAssert(this->succeeded());
809a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com            return fViewMatrix;
810a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        }
8112fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
812a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
813a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Can be called prior to destructor to restore the original matrix.
814a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
815a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        void restore();
8162fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
8175b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    private:
818288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        GrDrawState*                        fDrawState;
819b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix                            fViewMatrix;
82008283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrEffectStage::SavedCoordChange     fSavedCoordChanges[GrDrawState::kNumStages];
821288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        uint32_t                            fRestoreMask;
8225b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    };
8235b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
8248f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
8258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
8278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Render Target
8288f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
8298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8308f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
831ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * Sets the render-target used at the next drawing call
8328f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
8338f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param target  The render target to set.
8348f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
835d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    void setRenderTarget(GrRenderTarget* target) {
836ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fRenderTarget.reset(SkSafeRef(target));
8379ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com    }
8388f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8398f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
840ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * Retrieves the currently set render-target.
8418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
8428f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return    The currently set render target.
8438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
844ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
845ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
8468f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8478f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    class AutoRenderTargetRestore : public ::GrNoncopyable {
8488f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    public:
849cadbcb8e536f89babb4e165bfdca18384e97d582bsalomon@google.com        AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
8508f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
8518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            fDrawState = NULL;
8527460b378d68217167013ca889a4cdcae742908e7robertphillips@google.com            fSavedTarget = NULL;
8538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            this->set(ds, newTarget);
8548f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
8559ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        ~AutoRenderTargetRestore() { this->restore(); }
8569ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
8579ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        void restore() {
8588f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            if (NULL != fDrawState) {
8598f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                fDrawState->setRenderTarget(fSavedTarget);
8609ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                fDrawState = NULL;
8618f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
8629ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com            GrSafeSetNull(fSavedTarget);
8639ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        }
8649ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
8659ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        void set(GrDrawState* ds, GrRenderTarget* newTarget) {
8669ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com            this->restore();
8679ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
8688f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            if (NULL != ds) {
8699ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                GrAssert(NULL == fSavedTarget);
8708f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                fSavedTarget = ds->getRenderTarget();
8719ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                SkSafeRef(fSavedTarget);
8728f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                ds->setRenderTarget(newTarget);
8739ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                fDrawState = ds;
8748f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
8758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
8768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    private:
8778f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        GrDrawState* fDrawState;
8788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        GrRenderTarget* fSavedTarget;
8798f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    };
8808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8818f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
8828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8838f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
8848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Stencil
8858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
8868f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
8888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Sets the stencil settings to use for the next draw.
8898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Changing the clip has the side-effect of possibly zeroing
8908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * out the client settable stencil bits. So multipass algorithms
8918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * using stencil should not change the clip between passes.
8928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param settings  the stencil settings to use.
8938f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
8948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setStencil(const GrStencilSettings& settings) {
895ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fStencilSettings = settings;
8968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
8978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
8998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Shortcut to disable stencil testing and ops.
9008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
9018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void disableStencil() {
902ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fStencilSettings.setDisabled();
9038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
9048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
905ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
9068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
907ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
9088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
9108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
9128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    // @name Edge AA
9131e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com    // Edge equations can be specified to perform anti-aliasing. Because the
9147ffe6810c6787f7a353ef3fe8fab3fc6440aae19bsalomon@google.com    // edges are specified as per-vertex data, vertices that are shared by
9157ffe6810c6787f7a353ef3fe8fab3fc6440aae19bsalomon@google.com    // multiple edges must be split.
9168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    //
9178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
9188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
9209381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     * When specifying edges as vertex data this enum specifies what type of
92181712883419f76e25d2ffec38a9438284a45a48dbsalomon@google.com     * edges are in use. The edges are always 4 SkScalars in memory, even when
9229381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     * the edge type requires fewer than 4.
92393c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     *
92493c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
92593c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     *       (either adjust in VS or use origin_upper_left in GLSL)
9269381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     */
9279381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    enum VertexEdgeType {
9289381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com        /* 1-pixel wide line
9299381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com           2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
9309381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com        kHairLine_EdgeType,
931d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        /* Quadratic specified by u^2-v canonical coords (only 2
93269cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com           components used). Coverage based on signed distance with negative
93393c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com           being inside, positive outside. Edge specified in window space
93493c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com           (y-down) */
93569cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        kQuad_EdgeType,
93669cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        /* Same as above but for hairline quadratics. Uses unsigned distance.
93769cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com           Coverage is min(0, 1-distance). */
93869cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        kHairQuad_EdgeType,
93993c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com        /* Circle specified as center_x, center_y, outer_radius, inner_radius
94093c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com           all in window space (y-down). */
94193c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com        kCircle_EdgeType,
94246d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com        /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius
94346d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com           all in window space (y-down). */
94446d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com        kEllipse_EdgeType,
94569cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com
94669cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        kVertexEdgeTypeCnt
9479381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    };
9489381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
9499381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    /**
950d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Determines the interpretation per-vertex edge data when the
9518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
9528f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * are not specified the value of this setting has no effect.
9538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
9548f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setVertexEdgeType(VertexEdgeType type) {
95569cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
956ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fVertexEdgeType = type;
9578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
9588f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
959ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; }
9608f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9618f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
9628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
9648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name State Flags
9658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
9668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
9688f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Flags that affect rendering. Controlled using enable/disableState(). All
9698f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  default to disabled.
9708f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
9718f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    enum StateBits {
9728f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
9738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * Perform dithering. TODO: Re-evaluate whether we need this bit
9748f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
9758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kDither_StateBit        = 0x01,
9768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
977cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
978cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
979cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * the 3D API.
9808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
9818f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kHWAntialias_StateBit   = 0x02,
9828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
9838f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * Draws will respect the clip, otherwise the clip is ignored.
9848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
9858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kClip_StateBit          = 0x04,
9868f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
9878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * Disables writing to the color buffer. Useful when performing stencil
9888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * operations.
9898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
9908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kNoColorWrites_StateBit = 0x08,
9910342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com
992cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com        /**
993cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * Usually coverage is applied after color blending. The color is blended using the coeffs
994cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
995cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
996cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * this case there is no distinction between coverage and color and the caller needs direct
997cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * control over the blend coeffs. When set, there will be a single blend step controlled by
998cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * setBlendFunc() which will use coverage*color as the src color.
999cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         */
1000cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         kCoverageDrawing_StateBit = 0x10,
1001cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com
10028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        // Users of the class may add additional bits to the vector
10038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kDummyStateBit,
10048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kLastPublicStateBit = kDummyStateBit-1,
10058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    };
10068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void resetStateFlags() {
1008ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits = 0;
10090fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com    }
10100fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Enable render state settings.
10138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
10141e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param stateBits bitfield of StateBits specifying the states to enable
10158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
10168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void enableState(uint32_t stateBits) {
1017ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits |= stateBits;
10188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10190fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Disable render state settings.
10228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
10231e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param stateBits bitfield of StateBits specifying the states to disable
10248f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
10258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void disableState(uint32_t stateBits) {
1026ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits &= ~(stateBits);
10278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10280fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
1029d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com    /**
1030d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     * Enable or disable stateBits based on a boolean.
1031d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     *
10321e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param stateBits bitfield of StateBits to enable or disable
1033d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     * @param enable    if true enable stateBits, otherwise disable
1034d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     */
1035d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com    void setState(uint32_t stateBits, bool enable) {
1036d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com        if (enable) {
1037d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com            this->enableState(stateBits);
1038d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com        } else {
1039d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com            this->disableState(stateBits);
1040d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com        }
1041d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com    }
1042d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com
10438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isDitherState() const {
1044ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kDither_StateBit);
10458f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10460fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10478f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isHWAntialiasState() const {
1048ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
10498f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10500fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isClipState() const {
1052ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kClip_StateBit);
10538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10540fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isColorWriteDisabled() const {
1056ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
10578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10588f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
1059cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    bool isCoverageDrawing() const {
1060ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
1061cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    }
1062cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com
10638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isStateFlagEnabled(uint32_t stateBit) const {
1064ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (stateBit & fCommon.fFlagBits);
10658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
10688f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10698f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
10708f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Face Culling
10718f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
10728f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    enum DrawFace {
1074978c8c61ba1e792567e9d8e6629c2c4ee59727b7bsalomon@google.com        kInvalid_DrawFace = -1,
1075978c8c61ba1e792567e9d8e6629c2c4ee59727b7bsalomon@google.com
10768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kBoth_DrawFace,
10778f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kCCW_DrawFace,
10788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kCW_DrawFace,
10798f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    };
10808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10818f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Controls whether clockwise, counterclockwise, or both faces are drawn.
10838f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param face  the face(s) to draw.
10848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
10858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setDrawFace(DrawFace face) {
1086978c8c61ba1e792567e9d8e6629c2c4ee59727b7bsalomon@google.com        GrAssert(kInvalid_DrawFace != face);
1087ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDrawFace = face;
10888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Gets whether the target is drawing clockwise, counterclockwise,
10928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * or both faces.
10938f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return the current draw face(s).
10948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
1095ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    DrawFace getDrawFace() const { return fCommon.fDrawFace; }
1096d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
10978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
10988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
110062b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
1101f13f58804659175925042a291304d483a4fd9278tomhudson@google.com    bool isStageEnabled(int s) const {
1102f13f58804659175925042a291304d483a4fd9278tomhudson@google.com        GrAssert((unsigned)s < kNumStages);
110308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        return (NULL != fStages[s].getEffect());
1104f13f58804659175925042a291304d483a4fd9278tomhudson@google.com    }
1105f13f58804659175925042a291304d483a4fd9278tomhudson@google.com
110662b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com    // Most stages are usually not used, so conditionals here
110762b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com    // reduce the expected number of bytes touched by 50%.
11089381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    bool operator ==(const GrDrawState& s) const {
1109ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
11108fe84b53a64b5d92f3aabdd8e7fc7b2ee15c0a75bsalomon@google.com            return false;
11118fe84b53a64b5d92f3aabdd8e7fc7b2ee15c0a75bsalomon@google.com        }
111262b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
111362b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        for (int i = 0; i < kNumStages; i++) {
1114f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com            bool enabled = this->isStageEnabled(i);
1115f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com            if (enabled != s.isStageEnabled(i)) {
1116f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com                return false;
1117f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com            }
111808283afc265f1153834256fc1012519813ba6b73bsalomon@google.com            if (enabled && this->fStages[i] != s.fStages[i]) {
111962b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com                return false;
112062b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com            }
112162b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        }
112262b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        return true;
11239381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    }
11249381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    bool operator !=(const GrDrawState& s) const { return !(*this == s); }
112562b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
1126ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrDrawState& operator= (const GrDrawState& s) {
1127ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        this->setRenderTarget(s.fRenderTarget.get());
1128ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon = s.fCommon;
112962b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        for (int i = 0; i < kNumStages; i++) {
1130e742bf0ab19659145325ac894f7e0b78c8efbd89tomhudson@google.com            if (s.isStageEnabled(i)) {
113108283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                this->fStages[i] = s.fStages[i];
113262b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com            }
113362b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        }
113462b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        return *this;
113562b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com    }
113662b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
113762b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.comprivate:
11382e3d144dfdbc4596d33baef7396316f88f412cc8bsalomon@google.com
1139ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
1140ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    struct CommonState {
1141ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        // These fields are roughly sorted by decreasing likelihood of being different in op==
1142ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fColor;
1143ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        SkMatrix                        fViewMatrix;
1144ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrBlendCoeff                    fSrcBlend;
1145ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrBlendCoeff                    fDstBlend;
1146ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fBlendConstant;
1147ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        uint32_t                        fFlagBits;
1148ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        VertexEdgeType                  fVertexEdgeType;
1149ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrStencilSettings               fStencilSettings;
1150ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        int                             fFirstCoverageStage;
1151ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fCoverage;
1152ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        SkXfermode::Mode                fColorFilterMode;
1153ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fColorFilterColor;
1154ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        DrawFace                        fDrawFace;
1155ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        bool operator== (const CommonState& other) const {
1156ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            return fColor == other.fColor &&
1157ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
1158ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fSrcBlend == other.fSrcBlend &&
1159ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fDstBlend == other.fDstBlend &&
1160ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fBlendConstant == other.fBlendConstant &&
1161ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fFlagBits == other.fFlagBits &&
1162ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fVertexEdgeType == other.fVertexEdgeType &&
1163ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fStencilSettings == other.fStencilSettings &&
1164ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fFirstCoverageStage == other.fFirstCoverageStage &&
1165ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fCoverage == other.fCoverage &&
1166ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fColorFilterMode == other.fColorFilterMode &&
1167ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fColorFilterColor == other.fColorFilterColor &&
1168ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fDrawFace == other.fDrawFace;
1169ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1170ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        bool operator!= (const CommonState& other) const { return !(*this == other); }
1171ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    };
1172ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1173ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
1174ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        DeferredState must directly reference GrEffects, however. */
1175ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    struct SavedEffectStage {
1176ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        SavedEffectStage() : fEffect(NULL) {}
1177ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        const GrEffect*                    fEffect;
1178ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrEffectStage::SavedCoordChange    fCoordChange;
1179ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    };
1180ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1181ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.compublic:
1182ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    /**
1183ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
1184ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
1185ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * dispose mechanism returns them to the cache. This allows recycling resources through the
1186ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * the cache while they are in a deferred draw queue.
1187ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     */
1188ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    class DeferredState {
1189ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    public:
1190ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        DeferredState() : fRenderTarget(NULL) {
1191ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            GR_DEBUGCODE(fInitialized = false;)
1192ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1193ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        // TODO: Remove this when DeferredState no longer holds a ref to the RT
1194ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        ~DeferredState() { SkSafeUnref(fRenderTarget); }
1195ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1196ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        void saveFrom(const GrDrawState& drawState) {
1197ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            fCommon = drawState.fCommon;
1198ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1199ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            fRenderTarget = drawState.fRenderTarget.get();
1200ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            SkSafeRef(fRenderTarget);
1201ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1202ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1203ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // and recycle them to the cache (if no one else is holding a ref to the resources).
1204ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            for (int i = 0; i < kNumStages; ++i) {
1205ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                fStages[i].saveFrom(drawState.fStages[i]);
1206ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1207ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            GR_DEBUGCODE(fInitialized = true;)
1208ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1209ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1210ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        void restoreTo(GrDrawState* drawState) {
1211ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            GrAssert(fInitialized);
1212ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            drawState->fCommon = fCommon;
1213ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            drawState->setRenderTarget(fRenderTarget);
1214ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            for (int i = 0; i < kNumStages; ++i) {
1215ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                fStages[i].restoreTo(&drawState->fStages[i]);
1216ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1217ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1218ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1219ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        bool isEqual(const GrDrawState& state) const {
1220ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
1221ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                return false;
1222ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1223ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            for (int i = 0; i < kNumStages; ++i) {
1224dcd69bfca1d8e85ef5abc4e54f1e4b820d38e428bsalomon@google.com                if (!fStages[i].isEqual(state.fStages[i])) {
1225ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                    return false;
1226ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                }
1227ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1228ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            return true;
1229ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1230ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1231ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    private:
1232ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrRenderTarget*                 fRenderTarget;
1233ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        CommonState                     fCommon;
1234ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrEffectStage::DeferredStage    fStages[kNumStages];
1235ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1236ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GR_DEBUGCODE(bool fInitialized;)
1237ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    };
1238ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1239ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.comprivate:
1240ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    SkAutoTUnref<GrRenderTarget>    fRenderTarget;
1241ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    CommonState                     fCommon;
1242ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrEffectStage                   fStages[kNumStages];
12438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
1244fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com    typedef GrRefCnt INHERITED;
12459381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com};
12469381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
12479381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#endif
1248