GrDrawState.h revision b75b0a0b8492e14c7728e0a0881f87dc64ce60f9
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;
87b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        fCommon.fVertexLayout = kDefault_VertexLayout;
88ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fViewMatrix.reset();
89ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fSrcBlend = kOne_GrBlendCoeff;
90ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDstBlend = kZero_GrBlendCoeff;
91ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fBlendConstant = 0x0;
92ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits = 0x0;
93ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fVertexEdgeType = kHairLine_EdgeType;
94ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fStencilSettings.setDisabled();
95ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFirstCoverageStage = kNumStages;
96ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fCoverage = 0xffffffff;
97ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterMode = SkXfermode::kDst_Mode;
98ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterColor = 0x0;
99ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDrawFace = kBoth_DrawFace;
100af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com    }
101af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com
102af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com    /**
103af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
1041e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * encompasses more than GrPaint. Aspects of GrDrawState that have no
105af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     * GrPaint equivalents are not modified. GrPaint has fewer stages than
106af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     * GrDrawState. The extra GrDrawState stages are disabled.
107af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com     */
108af84e748cedbf43e2d0e8d2eac9b7800c8d8ca19bsalomon@google.com    void setFromPaint(const GrPaint& paint);
1098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
1108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
111b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    /// @name Vertex Layout
112cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    ////
113cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
114cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
115cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * The format of vertices is represented as a bitfield of flags.
116cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Flags that indicate the layout of vertex data. Vertices always contain
117cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * positions and may also contain up to GrDrawState::kMaxTexCoords sets
118cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
119cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Each stage can
120cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * use any of the texture coordinates as its input texture coordinates or it
121cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * may use the positions as texture coordinates.
122cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
123cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * If no texture coordinates are specified for a stage then the stage is
124cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * disabled.
125cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
126cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Only one type of texture coord can be specified per stage. For
127cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * example StageTexCoordVertexLayoutBit(0, 2) and
128cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
129cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
130cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * The order in memory is always (position, texture coord 0, ..., color,
131cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coverage) with any unused fields omitted. Note that this means that if
132cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * only texture coordinates 1 is referenced then there is no texture
133cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coordinates 0 and the order would be (position, texture coordinate 1
134cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * [, color][, coverage]).
135cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
136cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
137cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
138cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Generates a bit indicating that a texture stage uses texture coordinates
139cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
140cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param stageIdx    the stage that will use texture coordinates.
141cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param texCoordIdx the index of the texture coordinates to use
142cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
143cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return the bit to add to a GrVertexLayout bitfield.
144cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
145cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int StageTexCoordVertexLayoutBit(int stageIdx, int texCoordIdx) {
146cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        GrAssert(stageIdx < kNumStages);
147cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        GrAssert(texCoordIdx < kMaxTexCoords);
148cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return 1 << (stageIdx + (texCoordIdx * kNumStages));
149cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
150cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
151cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static bool StageUsesTexCoords(GrVertexLayout layout, int stageIdx);
152cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com
153cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.comprivate:
154cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // non-stage bits start at this index.
155cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static const int STAGE_BIT_CNT = kNumStages * kMaxTexCoords;
156cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.compublic:
157cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
158cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
159cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Additional Bits that can be specified in GrVertexLayout.
160cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
161cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    enum VertexLayoutBits {
162cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* vertices have colors (GrColor) */
163cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kColor_VertexLayoutBit              = 1 << (STAGE_BIT_CNT + 0),
164cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* vertices have coverage (GrColor)
165cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         */
166cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kCoverage_VertexLayoutBit           = 1 << (STAGE_BIT_CNT + 1),
167cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* Use text vertices. (Pos and tex coords may be a different type for
168cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         * text [GrGpuTextVertex vs GrPoint].)
169cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         */
170cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kTextFormat_VertexLayoutBit         = 1 << (STAGE_BIT_CNT + 2),
171cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
172cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        /* Each vertex specificies an edge. Distance to the edge is used to
173cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         * compute a coverage. See GrDrawState::setVertexEdgeType().
174cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com         */
175cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kEdge_VertexLayoutBit               = 1 << (STAGE_BIT_CNT + 3),
176cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        // for below assert
177cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kDummyVertexLayoutBit,
178cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
179cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    };
180cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // make sure we haven't exceeded the number of bits in GrVertexLayout.
181cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
182cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
183b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    enum VertexLayout {
184b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        kDefault_VertexLayout = 0
185b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    };
186b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com
187b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    /**
188b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *  Sets vertex layout for next draw.
189b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *
190b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *  @param layout    the vertex layout to set.
191b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     */
192b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    void setVertexLayout(GrVertexLayout layout) { fCommon.fVertexLayout = layout; }
193b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com
194b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    GrVertexLayout getVertexLayout() const { return fCommon.fVertexLayout; }
195b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    size_t getVertexSize() const { return VertexSize(fCommon.fVertexLayout); }
196b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com
197b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com
198cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    ////////////////////////////////////////////////////////////////////////////
199cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    // Helpers for picking apart vertex layouts
200cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
201cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
202cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the size of a vertex from a vertex layout
203cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return size of a single vertex.
204cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
205cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static size_t VertexSize(GrVertexLayout vertexLayout);
206cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
207cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
208cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function for determining the index of texture coordinates that
209cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * is input for a texture stage. Note that a stage may instead use positions
210cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * as texture coordinates, in which case the result of the function is
211cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * indistinguishable from the case when the stage is disabled.
212cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
213cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param stageIdx      the stage to query
214cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout  layout to query
215cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
216cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return the texture coordinate index or -1 if the stage doesn't use
217cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         separate (non-position) texture coordinates.
218cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
219cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexTexCoordsForStage(int stageIdx, GrVertexLayout vertexLayout);
220cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
221cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
222cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the offset of texture coordinates in a vertex
223cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return offset of texture coordinates in vertex layout or -1 if the
224cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         layout has no texture coordinates. Will be 0 if positions are
225cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         used as texture coordinates for the stage.
226cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
227cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout);
228cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
229cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
230cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the offset of the color in a vertex
231cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return offset of color in vertex layout or -1 if the
232cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         layout has no color.
233cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
234cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexColorOffset(GrVertexLayout vertexLayout);
235cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
236cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
237cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the offset of the coverage in a vertex
238cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return offset of coverage in vertex layout or -1 if the
239cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *         layout has no coverage.
240cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
241cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexCoverageOffset(GrVertexLayout vertexLayout);
242cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
243cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     /**
244cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      * Helper function to compute the offset of the edge pts in a vertex
245cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      * @return offset of edge in vertex layout or -1 if the
246cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      *         layout has no edge.
247cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com      */
248cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     static int VertexEdgeOffset(GrVertexLayout vertexLayout);
249cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
250cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
251cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to determine if vertex layout contains explicit texture
252cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coordinates of some index.
253cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
254cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param coordIndex    the tex coord index to query
255cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout  layout to query
256cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
257cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return true if vertex specifies texture coordinates for the index,
258cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *              false otherwise.
259cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
260cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static bool VertexUsesTexCoordIdx(int coordIndex,
261cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                      GrVertexLayout vertexLayout);
262cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
263cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
264cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the size of each vertex and the offsets of
265cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * texture coordinates and color. Determines tex coord offsets by tex coord
266cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * index rather than by stage. (Each stage can be mapped to any t.c. index
267cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * by StageTexCoordVertexLayoutBit.)
268cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
269cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout          the layout to query
270cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param texCoordOffsetsByIdx  after return it is the offset of each
271cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              tex coord index in the vertex or -1 if
272cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              index isn't used. (optional)
273cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param colorOffset           after return it is the offset of the
274cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              color field in each vertex, or -1 if
275cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              there aren't per-vertex colors. (optional)
276cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param coverageOffset        after return it is the offset of the
277cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              coverage field in each vertex, or -1 if
278cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              there aren't per-vertex coeverages.
279cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              (optional)
280cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param edgeOffset            after return it is the offset of the
281cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              edge eq field in each vertex, or -1 if
282cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              there aren't per-vertex edge equations.
283cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                              (optional)
284cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return size of a single vertex
285cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
286cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
287cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int texCoordOffsetsByIdx[kMaxTexCoords],
288cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int *colorOffset,
289cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int *coverageOffset,
290cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* edgeOffset);
291cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
292cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
293cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Helper function to compute the size of each vertex and the offsets of
294cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * texture coordinates and color. Determines tex coord offsets by stage
295cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * rather than by index. (Each stage can be mapped to any t.c. index
296cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
297cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * tex coords then that stage's offset will be 0 (positions are always at 0).
298cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *
299cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexLayout              the layout to query
300cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param texCoordOffsetsByStage    after return it is the offset of each
301cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  tex coord index in the vertex or -1 if
302cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  index isn't used. (optional)
303cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param colorOffset               after return it is the offset of the
304cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  color field in each vertex, or -1 if
305cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  there aren't per-vertex colors.
306cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  (optional)
307cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param coverageOffset            after return it is the offset of the
308cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  coverage field in each vertex, or -1 if
309cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  there aren't per-vertex coeverages.
310cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  (optional)
311cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param edgeOffset                after return it is the offset of the
312cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  edge eq field in each vertex, or -1 if
313cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  there aren't per-vertex edge equations.
314cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                                  (optional)
315cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return size of a single vertex
316cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
317cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
318cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int texCoordOffsetsByStage[kNumStages],
319cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* colorOffset,
320cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* coverageOffset,
321cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                   int* edgeOffset);
322cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
323d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    /**
324d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * Determines whether src alpha is guaranteed to be one for all src pixels
325d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     */
326d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    bool srcAlphaWillBeOne(GrVertexLayout) const;
327d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
328d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    /**
329d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * Determines whether the output coverage is guaranteed to be one for all pixels hit by a draw.
330d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     */
331d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    bool hasSolidCoverage(GrVertexLayout) const;
332cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
333cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
334cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Accessing positions, texture coords, or colors, of a vertex within an
335cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * array is a hassle involving casts and simple math. These helpers exist
336cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * to keep GrDrawTarget clients' code a bit nicer looking.
337cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
338cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
339cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
340cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Gets a pointer to a GrPoint of a vertex's position or texture
341cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * coordinate.
342cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertices      the vetex array
343cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexIndex   the index of the vertex in the array
344cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexSize    the size of each vertex in the array
345cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param offset        the offset in bytes of the vertex component.
346cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     *                      Defaults to zero (corresponding to vertex position)
347cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return pointer to the vertex component as a GrPoint
348cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
349cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static GrPoint* GetVertexPoint(void* vertices,
350cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexIndex,
351cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexSize,
352cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int offset = 0) {
353cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        intptr_t start = GrTCast<intptr_t>(vertices);
354cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<GrPoint*>(start + offset +
355cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                 vertexIndex * vertexSize);
356cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
357cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static const GrPoint* GetVertexPoint(const void* vertices,
358cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexIndex,
359cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexSize,
360cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int offset = 0) {
361cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        intptr_t start = GrTCast<intptr_t>(vertices);
362cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<const GrPoint*>(start + offset +
363cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                       vertexIndex * vertexSize);
364cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
365cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
366cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /**
367cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * Gets a pointer to a GrColor inside a vertex within a vertex array.
368cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertices      the vetex array
369cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexIndex   the index of the vertex in the array
370cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param vertexSize    the size of each vertex in the array
371cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @param offset        the offset in bytes of the vertex color
372cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     * @return pointer to the vertex component as a GrColor
373cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com     */
374cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static GrColor* GetVertexColor(void* vertices,
375cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexIndex,
376cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int vertexSize,
377cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                   int offset) {
378cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        intptr_t start = GrTCast<intptr_t>(vertices);
379cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<GrColor*>(start + offset +
380cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                 vertexIndex * vertexSize);
381cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
382cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static const GrColor* GetVertexColor(const void* vertices,
383cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexIndex,
384cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int vertexSize,
385cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                         int offset) {
386cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        const intptr_t start = GrTCast<intptr_t>(vertices);
387cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com        return GrTCast<const GrColor*>(start + offset +
388cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com                                       vertexIndex * vertexSize);
389cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    }
390cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
391cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    static void VertexLayoutUnitTest();
392cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
393cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    /// @}
394cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com
395cc78238f0b6aa1a7b3fc767758d9eeef4c1bffa9jvanverth@google.com    ///////////////////////////////////////////////////////////////////////////
3968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Color
3978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
3988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
3998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
4008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Sets color for next draw to a premultiplied-alpha color.
4018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
4028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param color    the color to set.
4038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
404ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void setColor(GrColor color) { fCommon.fColor = color; }
4058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
406ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrColor getColor() const { return fCommon.fColor; }
4078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
4088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
4098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Sets the color to be used for the next draw to be
4108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  (r,g,b,a) = (alpha, alpha, alpha, alpha).
4118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
4128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param alpha The alpha value to set as the color.
4138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
4148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setAlpha(uint8_t a) {
4158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        this->setColor((a << 24) | (a << 16) | (a << 8) | a);
4168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
4178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
4188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
4198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Add a color filter that can be represented by a color and a mode. Applied
4208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * after color-computing texture stages.
4218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
4228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setColorFilter(GrColor c, SkXfermode::Mode mode) {
423ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterColor = c;
424ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fColorFilterMode = mode;
4258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
4268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
427ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrColor getColorFilterColor() const { return fCommon.fColorFilterColor; }
428ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    SkXfermode::Mode getColorFilterMode() const { return fCommon.fColorFilterMode; }
4298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
4305b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    /**
4315b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     * Constructor sets the color to be 'color' which is undone by the destructor.
4325b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     */
4335b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    class AutoColorRestore : public ::GrNoncopyable {
4345b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    public:
435d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        AutoColorRestore() : fDrawState(NULL) {}
436d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
4375b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        AutoColorRestore(GrDrawState* drawState, GrColor color) {
438d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            fDrawState = NULL;
439d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            this->set(drawState, color);
440d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        }
441d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
442d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void reset() {
443d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            if (NULL != fDrawState) {
444d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                fDrawState->setColor(fOldColor);
445d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                fDrawState = NULL;
446d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            }
447d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        }
448d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
449d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void set(GrDrawState* drawState, GrColor color) {
450d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            this->reset();
4515b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fDrawState = drawState;
4525b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fOldColor = fDrawState->getColor();
4535b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com            fDrawState->setColor(color);
4545b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        }
455d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
456d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        ~AutoColorRestore() { this->reset(); }
4575b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    private:
4585b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        GrDrawState*    fDrawState;
4595b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        GrColor         fOldColor;
4605b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    };
4615b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
4628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
4638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
4648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
4652401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /// @name Coverage
4662401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    ////
4672401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4682401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /**
469d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Sets a constant fractional coverage to be applied to the draw. The
4702401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * initial value (after construction or reset()) is 0xff. The constant
4712401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * coverage is ignored when per-vertex coverage is provided.
4722401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     */
4732401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    void setCoverage(uint8_t coverage) {
474ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fCoverage = GrColorPackRGBA(coverage, coverage, coverage, coverage);
4752401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    }
4762401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4772401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /**
4782401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * Version of above that specifies 4 channel per-vertex color. The value
4792401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     * should be premultiplied.
4802401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com     */
4812401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    void setCoverage4(GrColor coverage) {
482ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fCoverage = coverage;
4832401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    }
4842401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4852401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    GrColor getCoverage() const {
486ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return fCommon.fCoverage;
4872401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    }
4882401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4892401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    /// @}
4902401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com
4912401ae8fd3c1f6282f571b5a7e0a499e9f4c09abbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
492adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    /// @name Effect Stages
4938f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
4948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
495adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
496adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        fStages[stageIdx].setEffect(effect);
497adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        return effect;
498adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    }
499adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com
5008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
50168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com     * Creates a GrSimpleTextureEffect.
5021e8f016305805d4d8cad74aba3a21b78486f9d6ftomhudson@google.com     */
503b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
50408283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrAssert(!this->getStage(stageIdx).getEffect());
50568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
506adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        this->setEffect(stageIdx, effect)->unref();
507dfdb7e5240276493077b7c6e1f3cc8b8a0e195babsalomon@google.com    }
50808283afc265f1153834256fc1012519813ba6b73bsalomon@google.com    void createTextureEffect(int stageIdx,
50908283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                             GrTexture* texture,
510b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                             const SkMatrix& matrix,
511dfdb7e5240276493077b7c6e1f3cc8b8a0e195babsalomon@google.com                             const GrTextureParams& params) {
51208283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrAssert(!this->getStage(stageIdx).getEffect());
51368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
514adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com        this->setEffect(stageIdx, effect)->unref();
5151ce49fc91714ce8974d11246d29ebe7b97b5fe98bsalomon@google.com    }
5161ce49fc91714ce8974d11246d29ebe7b97b5fe98bsalomon@google.com
5177d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    bool stagesDisabled() {
5187d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        for (int i = 0; i < kNumStages; ++i) {
51908283afc265f1153834256fc1012519813ba6b73bsalomon@google.com            if (NULL != fStages[i].getEffect()) {
5207d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com                return false;
5217d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com            }
5227d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        }
5233eee8fbe0f280bc1dea59dc0b0ebd8021b51137ftomhudson@google.com        return true;
5247d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    }
525676e66096c60615bac52f365111596de5c4ca8a6tomhudson@google.com
526adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    void disableStage(int stageIdx) { this->setEffect(stageIdx, NULL); }
527676e66096c60615bac52f365111596de5c4ca8a6tomhudson@google.com
528972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    /**
529f271cc7183fe48ac64d2d9a454eb013c91b42d53bsalomon@google.com     * Release all the GrEffects referred to by this draw state.
530972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com     */
5317d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    void disableStages() {
532972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        for (int i = 0; i < kNumStages; ++i) {
533676e66096c60615bac52f365111596de5c4ca8a6tomhudson@google.com            this->disableStage(i);
534972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        }
535972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    }
536972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com
5377d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com    class AutoStageDisable : public ::GrNoncopyable {
538972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    public:
5397d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        AutoStageDisable(GrDrawState* ds) : fDrawState(ds) {}
5407d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com        ~AutoStageDisable() {
541972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com            if (NULL != fDrawState) {
5427d6afdd795eb4c7ce8f5a327117cfdba5f957ddbtomhudson@google.com                fDrawState->disableStages();
543972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com            }
544972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        }
545972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    private:
546972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com        GrDrawState* fDrawState;
547972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com    };
548972265db219ce25b5159879c75e6c62efaf0fa79robertphillips@google.com
5498f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
55008283afc265f1153834256fc1012519813ba6b73bsalomon@google.com     * Returns the current stage by index.
5518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
55208283afc265f1153834256fc1012519813ba6b73bsalomon@google.com    const GrEffectStage& getStage(int stageIdx) const {
55308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrAssert((unsigned)stageIdx < kNumStages);
55408283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        return fStages[stageIdx];
5558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
5568f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
558288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * Called when the source coord system is changing. preConcat gives the transformation from the
559288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * old coord system to the new coord system.
5608f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
561b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    void preConcatStageMatrices(const SkMatrix& preConcat) {
562cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com        this->preConcatStageMatrices(~0U, preConcat);
563cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com    }
564cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com    /**
565cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com     * Version of above that applies the update matrix selectively to stages via a mask.
566cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com     */
567cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com    void preConcatStageMatrices(uint32_t stageMask, const SkMatrix& preConcat) {
5688f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        for (int i = 0; i < kNumStages; ++i) {
569cabe00eaced87016647029b6fee08f24dcd062f3bsalomon@google.com            if (((1 << i) & stageMask) && this->isStageEnabled(i)) {
57008283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                fStages[i].preConcatCoordChange(preConcat);
5718f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
5728f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
5738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
5748f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
575e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com    /**
576288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * Called when the source coord system is changing. preConcatInverse is the inverse of the
577288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * transformation from the old coord system to the new coord system. Returns false if the matrix
578288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com     * cannot be inverted.
579e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com     */
580b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    bool preConcatStageMatricesWithInverse(const SkMatrix& preConcatInverse) {
581b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix inv;
582e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        bool computed = false;
583e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        for (int i = 0; i < kNumStages; ++i) {
584e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com            if (this->isStageEnabled(i)) {
585288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com                if (!computed && !preConcatInverse.invert(&inv)) {
586e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                    return false;
587e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                } else {
588e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                    computed = true;
589e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com                }
59008283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                fStages[i].preConcatCoordChange(preConcatInverse);
591e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com            }
592e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        }
593e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com        return true;
594e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com    }
595e3d3216fe17b6afb2e613271b5246a2766e12df6bsalomon@google.com
5968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
5978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
5988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
5998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Coverage / Color Stages
6008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
6018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * A common pattern is to compute a color with the initial stages and then
6048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * modulate that color by a coverage value in later stage(s) (AA, mask-
605d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
606d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * computed based on the pre-coverage-modulated color. The division of
607d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * stages between color-computing and coverage-computing is specified by
6088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * this method. Initially this is kNumStages (all stages
6098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * are color-computing).
6108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
6118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setFirstCoverageStage(int firstCoverageStage) {
6128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        GrAssert((unsigned)firstCoverageStage <= kNumStages);
613ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFirstCoverageStage = firstCoverageStage;
6148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
6158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Gets the index of the first coverage-computing stage.
6188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
6198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    int getFirstCoverageStage() const {
620ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return fCommon.fFirstCoverageStage;
6218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
6228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6238f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///@}
6248f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
6268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Blending
6278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
6288f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6301e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * Sets the blending function coefficients.
6318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6328f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * The blend function will be:
6338f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *    D' = sat(S*srcCoef + D*dstCoef)
6348f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6358f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *   where D is the existing destination color, S is the incoming source
6368f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *   color, and D' is the new destination color that will be written. sat()
6378f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *   is the saturation function.
6388f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6391e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param srcCoef coefficient applied to the src color.
6401e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param dstCoef coefficient applied to the dst color.
6418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
6428f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
643ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fSrcBlend = srcCoeff;
644ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDstBlend = dstCoeff;
6458f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    #if GR_DEBUG
6468f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        switch (dstCoeff) {
64747059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kDC_GrBlendCoeff:
64847059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kIDC_GrBlendCoeff:
64947059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kDA_GrBlendCoeff:
65047059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kIDA_GrBlendCoeff:
6518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            GrPrintf("Unexpected dst blend coeff. Won't work correctly with"
6528f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                     "coverage stages.\n");
6538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6548f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        default:
6558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6568f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
6578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        switch (srcCoeff) {
65847059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kSC_GrBlendCoeff:
65947059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kISC_GrBlendCoeff:
66047059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kSA_GrBlendCoeff:
66147059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com        case kISA_GrBlendCoeff:
6628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            GrPrintf("Unexpected src blend coeff. Won't work correctly with"
6638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                     "coverage stages.\n");
6648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        default:
6668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            break;
6678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
6688f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    #endif
6698f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
6708f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
671ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrBlendCoeff getSrcBlendCoeff() const { return fCommon.fSrcBlend; }
672ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrBlendCoeff getDstBlendCoeff() const { return fCommon.fDstBlend; }
6738f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6748f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void getDstBlendCoeff(GrBlendCoeff* srcBlendCoeff,
6758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                          GrBlendCoeff* dstBlendCoeff) const {
676ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        *srcBlendCoeff = fCommon.fSrcBlend;
677ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        *dstBlendCoeff = fCommon.fDstBlend;
6788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
6798f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6818f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Sets the blending function constant referenced by the following blending
6821e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * coefficients:
68347059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kConstC_GrBlendCoeff
68447059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kIConstC_GrBlendCoeff
68547059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kConstA_GrBlendCoeff
68647059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com     *      kIConstA_GrBlendCoeff
6878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
6888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param constant the constant to set
6898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
690ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void setBlendConstant(GrColor constant) { fCommon.fBlendConstant = constant; }
6918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
6938f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Retrieves the last value set by setBlendConstant()
6948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return the blending constant value
6958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
696ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrColor getBlendConstant() const { return fCommon.fBlendConstant; }
6978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
6988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
6998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
7018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name View Matrix
7028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
7038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
705a72eef322c686954cdffa849dc26d8133b802f1drobertphillips@google.com     * Sets the matrix applied to vertex positions.
7068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * In the post-view-matrix space the rectangle [0,w]x[0,h]
7088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * fully covers the render target. (w and h are the width and height of the
709ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * the render-target.)
7108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
711ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void setViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix = m; }
7128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Gets a writable pointer to the view matrix.
7158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
716ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    SkMatrix* viewMatrix() { return &fCommon.fViewMatrix; }
7178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Multiplies the current view matrix by a matrix
7208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  After this call V' = V*m where V is the old view matrix,
7228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  m is the parameter to this function, and V' is the new view matrix.
7238f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  (We consider positions to be column vectors so position vector p is
7248f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  transformed by matrix X as p' = X*p.)
7258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param m the matrix used to modify the view matrix.
7278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
728ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void preConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.preConcat(m); }
7298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7308f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Multiplies the current view matrix by a matrix
7328f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7338f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  After this call V' = m*V where V is the old view matrix,
7348f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  m is the parameter to this function, and V' is the new view matrix.
7358f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  (We consider positions to be column vectors so position vector p is
7368f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  transformed by matrix X as p' = X*p.)
7378f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7388f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  @param m the matrix used to modify the view matrix.
7398f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
740ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    void postConcatViewMatrix(const SkMatrix& m) { fCommon.fViewMatrix.postConcat(m); }
7418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7428f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Retrieves the current view matrix
7448f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return the current view matrix.
7458f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
746ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    const SkMatrix& getViewMatrix() const { return fCommon.fViewMatrix; }
7478f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7488f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
7498f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Retrieves the inverse of the current view matrix.
7508f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7518f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  If the current view matrix is invertible, return true, and if matrix
7528f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  is non-null, copy the inverse into it. If the current view matrix is
7538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  non-invertible, return false and ignore the matrix parameter.
7548f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
7558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param matrix if not null, will receive a copy of the current inverse.
7568f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
757b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    bool getViewInverse(SkMatrix* matrix) const {
7588f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        // TODO: determine whether we really need to leave matrix unmodified
7598f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        // at call sites when inversion fails.
760b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix inverse;
761ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        if (fCommon.fViewMatrix.invert(&inverse)) {
7628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            if (matrix) {
7638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                *matrix = inverse;
7648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
7658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            return true;
7668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
7678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        return false;
7688f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
7698f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
7705b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    ////////////////////////////////////////////////////////////////////////////
7715b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
7725b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    /**
7732fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * Preconcats the current view matrix and restores the previous view matrix in the destructor.
774c196b522d06919885c6bbe28b7b06d2e5b2cb9bfbsalomon@google.com     * Effect matrices are automatically adjusted to compensate.
7755b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     */
7768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    class AutoViewMatrixRestore : public ::GrNoncopyable {
7778f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    public:
7788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        AutoViewMatrixRestore() : fDrawState(NULL) {}
7792fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
7802fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        AutoViewMatrixRestore(GrDrawState* ds,
781b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                              const SkMatrix& preconcatMatrix,
7822fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com                              uint32_t explicitCoordStageMask = 0) {
7838f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            fDrawState = NULL;
7842fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com            this->set(ds, preconcatMatrix, explicitCoordStageMask);
7858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
7862fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
7872fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        ~AutoViewMatrixRestore() { this->restore(); }
7882fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
789a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
790a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Can be called prior to destructor to restore the original matrix.
791a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
7922fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        void restore();
793f467ce7bc33af5f496e0619387551aedec6d2517skia.committer@gmail.com
7942fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        void set(GrDrawState* drawState,
795b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                 const SkMatrix& preconcatMatrix,
7962fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com                 uint32_t explicitCoordStageMask = 0);
7972fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
798ded4f4b163f5aa19c22c871178c55ecb34623846bsalomon@google.com        bool isSet() const { return NULL != fDrawState; }
7992fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
8008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    private:
801288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        GrDrawState*                        fDrawState;
802b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix                            fViewMatrix;
80308283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrEffectStage::SavedCoordChange     fSavedCoordChanges[GrDrawState::kNumStages];
804288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        uint32_t                            fRestoreMask;
8059381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    };
8069381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
8075b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    ////////////////////////////////////////////////////////////////////////////
8085b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
8095b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    /**
8102fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * This sets the view matrix to identity and adjusts stage matrices to compensate. The
8112fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * destructor undoes the changes, restoring the view matrix that was set before the
8122fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * constructor. It is similar to passing the inverse of the current view matrix to
8132fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com     * AutoViewMatrixRestore, but lazily computes the inverse only if necessary.
8145b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com     */
8155b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    class AutoDeviceCoordDraw : ::GrNoncopyable {
8165b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    public:
8172fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        AutoDeviceCoordDraw() : fDrawState(NULL) {}
8185b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        /**
8192fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com         * If a stage's texture matrix is applied to explicit per-vertex coords, rather than to
8202fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com         * positions, then we don't want to modify its matrix. The explicitCoordStageMask is used
8212fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com         * to specify such stages.
8225b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com         */
8235b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        AutoDeviceCoordDraw(GrDrawState* drawState,
8242fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com                            uint32_t explicitCoordStageMask = 0) {
8252fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com            fDrawState = NULL;
8262fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com            this->set(drawState, explicitCoordStageMask);
8272fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        }
8282fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
829a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        ~AutoDeviceCoordDraw() { this->restore(); }
830a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com
8312fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com        bool set(GrDrawState* drawState, uint32_t explicitCoordStageMask = 0);
8322fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
833a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
834a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Returns true if this object was successfully initialized on to a GrDrawState. It may
835a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * return false because a non-default constructor or set() were never called or because
836a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * the view matrix was not invertible.
837a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
8385b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com        bool succeeded() const { return NULL != fDrawState; }
8392fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
840a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
841a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Returns the matrix that was set previously set on the drawState. This is only valid
842a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * if succeeded returns true.
843a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
844b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        const SkMatrix& getOriginalMatrix() const {
845a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com            GrAssert(this->succeeded());
846a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com            return fViewMatrix;
847a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        }
8482fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
849a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        /**
850a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         * Can be called prior to destructor to restore the original matrix.
851a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com         */
852a834746cc1bd92301fd0840a221ca1623c0bbb29bsalomon@google.com        void restore();
8532fdcdeb86788206c23410109b3e2b7976747fd11bsalomon@google.com
8545b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    private:
855288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        GrDrawState*                        fDrawState;
856b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix                            fViewMatrix;
85708283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        GrEffectStage::SavedCoordChange     fSavedCoordChanges[GrDrawState::kNumStages];
858288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com        uint32_t                            fRestoreMask;
8595b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com    };
8605b3e890c376f2211218c43edd11939cfc78fd60absalomon@google.com
8618f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
8628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8638f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
8648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Render Target
8658f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
8668f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8678f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
868ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * Sets the render-target used at the next drawing call
8698f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
8708f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param target  The render target to set.
8718f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
872d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    void setRenderTarget(GrRenderTarget* target) {
873ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fRenderTarget.reset(SkSafeRef(target));
8749ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com    }
8758f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8768f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
877ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * Retrieves the currently set render-target.
8788f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
8798f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return    The currently set render target.
8808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
881ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
882ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
8838f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
8848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    class AutoRenderTargetRestore : public ::GrNoncopyable {
8858f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    public:
886cadbcb8e536f89babb4e165bfdca18384e97d582bsalomon@google.com        AutoRenderTargetRestore() : fDrawState(NULL), fSavedTarget(NULL) {}
8878f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        AutoRenderTargetRestore(GrDrawState* ds, GrRenderTarget* newTarget) {
8888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            fDrawState = NULL;
8897460b378d68217167013ca889a4cdcae742908e7robertphillips@google.com            fSavedTarget = NULL;
8908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            this->set(ds, newTarget);
8918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
8929ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        ~AutoRenderTargetRestore() { this->restore(); }
8939ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
8949ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        void restore() {
8958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            if (NULL != fDrawState) {
8968f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                fDrawState->setRenderTarget(fSavedTarget);
8979ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                fDrawState = NULL;
8988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
8999ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com            GrSafeSetNull(fSavedTarget);
9009ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        }
9019ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
9029ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        void set(GrDrawState* ds, GrRenderTarget* newTarget) {
9039ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com            this->restore();
9049ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
9058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            if (NULL != ds) {
9069ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                GrAssert(NULL == fSavedTarget);
9078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                fSavedTarget = ds->getRenderTarget();
9089ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                SkSafeRef(fSavedTarget);
9098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com                ds->setRenderTarget(newTarget);
9109ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com                fDrawState = ds;
9118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com            }
9128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        }
9138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    private:
9148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        GrDrawState* fDrawState;
9158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        GrRenderTarget* fSavedTarget;
9168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    };
9178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
9198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
9218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Stencil
9228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
9238f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9248f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
9258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Sets the stencil settings to use for the next draw.
9268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Changing the clip has the side-effect of possibly zeroing
9278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * out the client settable stencil bits. So multipass algorithms
9288f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * using stencil should not change the clip between passes.
9298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param settings  the stencil settings to use.
9308f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
9318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setStencil(const GrStencilSettings& settings) {
932ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fStencilSettings = settings;
9338f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
9348f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9358f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
9368f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Shortcut to disable stencil testing and ops.
9378f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
9388f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void disableStencil() {
939ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fStencilSettings.setDisabled();
9408f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
9418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
942ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    const GrStencilSettings& getStencil() const { return fCommon.fStencilSettings; }
9438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
944ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrStencilSettings* stencil() { return &fCommon.fStencilSettings; }
9458f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9468f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
9478f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9488f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
9498f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    // @name Edge AA
9501e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com    // Edge equations can be specified to perform anti-aliasing. Because the
9517ffe6810c6787f7a353ef3fe8fab3fc6440aae19bsalomon@google.com    // edges are specified as per-vertex data, vertices that are shared by
9527ffe6810c6787f7a353ef3fe8fab3fc6440aae19bsalomon@google.com    // multiple edges must be split.
9538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    //
9548f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
9558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9568f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
9579381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     * When specifying edges as vertex data this enum specifies what type of
95881712883419f76e25d2ffec38a9438284a45a48dbsalomon@google.com     * edges are in use. The edges are always 4 SkScalars in memory, even when
9599381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     * the edge type requires fewer than 4.
96093c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     *
96193c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     * TODO: Fix the fact that HairLine and Circle edge types use y-down coords.
96293c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     *       (either adjust in VS or use origin_upper_left in GLSL)
9639381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com     */
9649381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    enum VertexEdgeType {
9659381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com        /* 1-pixel wide line
9669381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com           2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
9679381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com        kHairLine_EdgeType,
968d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        /* Quadratic specified by u^2-v canonical coords (only 2
96969cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com           components used). Coverage based on signed distance with negative
97093c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com           being inside, positive outside. Edge specified in window space
97193c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com           (y-down) */
97269cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        kQuad_EdgeType,
97369cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        /* Same as above but for hairline quadratics. Uses unsigned distance.
97469cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com           Coverage is min(0, 1-distance). */
97569cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        kHairQuad_EdgeType,
97693c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com        /* Circle specified as center_x, center_y, outer_radius, inner_radius
97793c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com           all in window space (y-down). */
97893c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com        kCircle_EdgeType,
97946d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com        /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius
98046d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com           all in window space (y-down). */
98146d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com        kEllipse_EdgeType,
98269cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com
98369cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        kVertexEdgeTypeCnt
9849381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    };
9859381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
9869381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    /**
987d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Determines the interpretation per-vertex edge data when the
9888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * kEdge_VertexLayoutBit is set (see GrDrawTarget). When per-vertex edges
9898f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * are not specified the value of this setting has no effect.
9908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
9918f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setVertexEdgeType(VertexEdgeType type) {
99269cc6ad20ed03f35f9d3c8119a2c32187669a22bbsalomon@google.com        GrAssert(type >=0 && type < kVertexEdgeTypeCnt);
993ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fVertexEdgeType = type;
9948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
9958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
996ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    VertexEdgeType getVertexEdgeType() const { return fCommon.fVertexEdgeType; }
9978f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
9988f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
9998f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
10018f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name State Flags
10028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
10038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  Flags that affect rendering. Controlled using enable/disableState(). All
10068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *  default to disabled.
10078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
10088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    enum StateBits {
10098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
10108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * Perform dithering. TODO: Re-evaluate whether we need this bit
10118f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
10128f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kDither_StateBit        = 0x01,
10138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
1014cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
1015cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
1016cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * the 3D API.
10178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
10188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kHWAntialias_StateBit   = 0x02,
10198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
10208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * Draws will respect the clip, otherwise the clip is ignored.
10218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
10228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kClip_StateBit          = 0x04,
10238f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        /**
10248f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * Disables writing to the color buffer. Useful when performing stencil
10258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         * operations.
10268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com         */
10278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kNoColorWrites_StateBit = 0x08,
10280342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com
1029cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com        /**
1030cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * Usually coverage is applied after color blending. The color is blended using the coeffs
1031cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * specified by setBlendFunc(). The blended color is then combined with dst using coeffs
1032cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing a coverage mask. In
1033cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * this case there is no distinction between coverage and color and the caller needs direct
1034cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * control over the blend coeffs. When set, there will be a single blend step controlled by
1035cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         * setBlendFunc() which will use coverage*color as the src color.
1036cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         */
1037cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com         kCoverageDrawing_StateBit = 0x10,
1038cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com
10398f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        // Users of the class may add additional bits to the vector
10408f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kDummyStateBit,
10418f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kLastPublicStateBit = kDummyStateBit-1,
10428f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    };
10438f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
10448f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void resetStateFlags() {
1045ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits = 0;
10460fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com    }
10470fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10488f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10498f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Enable render state settings.
10508f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
10511e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param stateBits bitfield of StateBits specifying the states to enable
10528f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
10538f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void enableState(uint32_t stateBits) {
1054ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits |= stateBits;
10558f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10560fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10578f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
10588f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Disable render state settings.
10598f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     *
10601e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param stateBits bitfield of StateBits specifying the states to disable
10618f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
10628f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void disableState(uint32_t stateBits) {
1063ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fFlagBits &= ~(stateBits);
10648f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10650fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
1066d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com    /**
1067d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     * Enable or disable stateBits based on a boolean.
1068d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     *
10691e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param stateBits bitfield of StateBits to enable or disable
1070d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     * @param enable    if true enable stateBits, otherwise disable
1071d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com     */
1072d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com    void setState(uint32_t stateBits, bool enable) {
1073d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com        if (enable) {
1074d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com            this->enableState(stateBits);
1075d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com        } else {
1076d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com            this->disableState(stateBits);
1077d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com        }
1078d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com    }
1079d5d69ffaea117428972db48796f7e75f0d1dab34bsalomon@google.com
10808f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isDitherState() const {
1081ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kDither_StateBit);
10828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10830fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10848f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isHWAntialiasState() const {
1085ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kHWAntialias_StateBit);
10868f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10870fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10888f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isClipState() const {
1089ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kClip_StateBit);
10908f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10910fec61d19ca9088d54f58bd0a67150171b83d66cbsalomon@google.com
10928f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isColorWriteDisabled() const {
1093ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kNoColorWrites_StateBit);
10948f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
10958f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
1096cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    bool isCoverageDrawing() const {
1097ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (fCommon.fFlagBits & kCoverageDrawing_StateBit);
1098cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    }
1099cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com
11008f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    bool isStateFlagEnabled(uint32_t stateBit) const {
1101ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        return 0 != (stateBit & fCommon.fFlagBits);
11028f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
11038f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
11048f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
11058f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
11068f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
11078f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @name Face Culling
11088f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ////
11098f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
11108f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    enum DrawFace {
1111978c8c61ba1e792567e9d8e6629c2c4ee59727b7bsalomon@google.com        kInvalid_DrawFace = -1,
1112978c8c61ba1e792567e9d8e6629c2c4ee59727b7bsalomon@google.com
11138f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kBoth_DrawFace,
11148f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kCCW_DrawFace,
11158f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com        kCW_DrawFace,
11168f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    };
11178f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
11188f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
11198f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Controls whether clockwise, counterclockwise, or both faces are drawn.
11208f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @param face  the face(s) to draw.
11218f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
11228f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    void setDrawFace(DrawFace face) {
1123978c8c61ba1e792567e9d8e6629c2c4ee59727b7bsalomon@google.com        GrAssert(kInvalid_DrawFace != face);
1124ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon.fDrawFace = face;
11258f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    }
11268f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
11278f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /**
11288f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * Gets whether the target is drawing clockwise, counterclockwise,
11298f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * or both faces.
11308f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     * @return the current draw face(s).
11318f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com     */
1132ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    DrawFace getDrawFace() const { return fCommon.fDrawFace; }
1133d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
11348f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    /// @}
11358f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
11368f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
113762b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
1138f13f58804659175925042a291304d483a4fd9278tomhudson@google.com    bool isStageEnabled(int s) const {
1139f13f58804659175925042a291304d483a4fd9278tomhudson@google.com        GrAssert((unsigned)s < kNumStages);
114008283afc265f1153834256fc1012519813ba6b73bsalomon@google.com        return (NULL != fStages[s].getEffect());
1141f13f58804659175925042a291304d483a4fd9278tomhudson@google.com    }
1142f13f58804659175925042a291304d483a4fd9278tomhudson@google.com
114362b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com    // Most stages are usually not used, so conditionals here
114462b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com    // reduce the expected number of bytes touched by 50%.
11459381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    bool operator ==(const GrDrawState& s) const {
1146ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        if (fRenderTarget.get() != s.fRenderTarget.get() || fCommon != s.fCommon) {
11478fe84b53a64b5d92f3aabdd8e7fc7b2ee15c0a75bsalomon@google.com            return false;
11488fe84b53a64b5d92f3aabdd8e7fc7b2ee15c0a75bsalomon@google.com        }
114962b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
115062b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        for (int i = 0; i < kNumStages; i++) {
1151f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com            bool enabled = this->isStageEnabled(i);
1152f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com            if (enabled != s.isStageEnabled(i)) {
1153f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com                return false;
1154f2f8fc37265a6ef06897117451a3a9238287c13bbsalomon@google.com            }
115508283afc265f1153834256fc1012519813ba6b73bsalomon@google.com            if (enabled && this->fStages[i] != s.fStages[i]) {
115662b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com                return false;
115762b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com            }
115862b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        }
115962b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        return true;
11609381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    }
11619381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    bool operator !=(const GrDrawState& s) const { return !(*this == s); }
116262b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
1163ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrDrawState& operator= (const GrDrawState& s) {
1164ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        this->setRenderTarget(s.fRenderTarget.get());
1165ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        fCommon = s.fCommon;
116662b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        for (int i = 0; i < kNumStages; i++) {
1167e742bf0ab19659145325ac894f7e0b78c8efbd89tomhudson@google.com            if (s.isStageEnabled(i)) {
116808283afc265f1153834256fc1012519813ba6b73bsalomon@google.com                this->fStages[i] = s.fStages[i];
116962b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com            }
117062b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        }
117162b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com        return *this;
117262b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com    }
117362b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.com
117462b09687857c923b1044be0aef4dd900f8379b22tomhudson@google.comprivate:
11752e3d144dfdbc4596d33baef7396316f88f412cc8bsalomon@google.com
1176ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    /** Fields that are identical in GrDrawState and GrDrawState::DeferredState. */
1177ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    struct CommonState {
1178ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        // These fields are roughly sorted by decreasing likelihood of being different in op==
1179ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fColor;
1180b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        GrVertexLayout                  fVertexLayout;
1181ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        SkMatrix                        fViewMatrix;
1182ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrBlendCoeff                    fSrcBlend;
1183ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrBlendCoeff                    fDstBlend;
1184ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fBlendConstant;
1185ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        uint32_t                        fFlagBits;
1186ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        VertexEdgeType                  fVertexEdgeType;
1187ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrStencilSettings               fStencilSettings;
1188ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        int                             fFirstCoverageStage;
1189ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fCoverage;
1190ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        SkXfermode::Mode                fColorFilterMode;
1191ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrColor                         fColorFilterColor;
1192ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        DrawFace                        fDrawFace;
1193ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        bool operator== (const CommonState& other) const {
1194ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            return fColor == other.fColor &&
1195b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com                   fVertexLayout == other.fVertexLayout &&
1196ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fViewMatrix.cheapEqualTo(other.fViewMatrix) &&
1197ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fSrcBlend == other.fSrcBlend &&
1198ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fDstBlend == other.fDstBlend &&
1199ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fBlendConstant == other.fBlendConstant &&
1200ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fFlagBits == other.fFlagBits &&
1201ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fVertexEdgeType == other.fVertexEdgeType &&
1202ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fStencilSettings == other.fStencilSettings &&
1203ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fFirstCoverageStage == other.fFirstCoverageStage &&
1204ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fCoverage == other.fCoverage &&
1205ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fColorFilterMode == other.fColorFilterMode &&
1206ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fColorFilterColor == other.fColorFilterColor &&
1207ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                   fDrawFace == other.fDrawFace;
1208ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1209ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        bool operator!= (const CommonState& other) const { return !(*this == other); }
1210ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    };
1211ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1212ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    /** GrDrawState uses GrEffectStages to hold stage state which holds a ref on GrEffectRef.
1213ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        DeferredState must directly reference GrEffects, however. */
1214ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    struct SavedEffectStage {
1215ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        SavedEffectStage() : fEffect(NULL) {}
1216ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        const GrEffect*                    fEffect;
1217ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrEffectStage::SavedCoordChange    fCoordChange;
1218ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    };
1219ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1220ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.compublic:
1221ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    /**
1222ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * DeferredState contains all of the data of a GrDrawState but does not hold refs on GrResource
1223ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * objects. Resources are allowed to hit zero ref count while in DeferredStates. Their internal
1224ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * dispose mechanism returns them to the cache. This allows recycling resources through the
1225ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     * the cache while they are in a deferred draw queue.
1226ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com     */
1227ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    class DeferredState {
1228ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    public:
1229ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        DeferredState() : fRenderTarget(NULL) {
1230ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            GR_DEBUGCODE(fInitialized = false;)
1231ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1232ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        // TODO: Remove this when DeferredState no longer holds a ref to the RT
1233ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        ~DeferredState() { SkSafeUnref(fRenderTarget); }
1234ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1235ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        void saveFrom(const GrDrawState& drawState) {
1236ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            fCommon = drawState.fCommon;
1237ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // TODO: Here we will copy the GrRenderTarget pointer without taking a ref.
1238ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            fRenderTarget = drawState.fRenderTarget.get();
1239ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            SkSafeRef(fRenderTarget);
1240ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // Here we ref the effects directly rather than the effect-refs. TODO: When the effect-
1241ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // ref gets fully unref'ed it will cause the underlying effect to unref its resources
1242ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            // and recycle them to the cache (if no one else is holding a ref to the resources).
1243ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            for (int i = 0; i < kNumStages; ++i) {
1244ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                fStages[i].saveFrom(drawState.fStages[i]);
1245ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1246ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            GR_DEBUGCODE(fInitialized = true;)
1247ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1248ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1249ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        void restoreTo(GrDrawState* drawState) {
1250ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            GrAssert(fInitialized);
1251ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            drawState->fCommon = fCommon;
1252ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            drawState->setRenderTarget(fRenderTarget);
1253ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            for (int i = 0; i < kNumStages; ++i) {
1254ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                fStages[i].restoreTo(&drawState->fStages[i]);
1255ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1256ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1257ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1258ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        bool isEqual(const GrDrawState& state) const {
1259ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            if (fRenderTarget != state.fRenderTarget.get() || fCommon != state.fCommon) {
1260ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                return false;
1261ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1262ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            for (int i = 0; i < kNumStages; ++i) {
1263dcd69bfca1d8e85ef5abc4e54f1e4b820d38e428bsalomon@google.com                if (!fStages[i].isEqual(state.fStages[i])) {
1264ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                    return false;
1265ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com                }
1266ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            }
1267ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com            return true;
1268ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        }
1269ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1270ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    private:
1271ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrRenderTarget*                 fRenderTarget;
1272ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        CommonState                     fCommon;
1273ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GrEffectStage::DeferredStage    fStages[kNumStages];
1274ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1275ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com        GR_DEBUGCODE(bool fInitialized;)
1276ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    };
1277ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com
1278ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.comprivate:
1279ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    SkAutoTUnref<GrRenderTarget>    fRenderTarget;
1280ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    CommonState                     fCommon;
1281ca43208e4aaac817fbc8265542f995f24a3fb7bfbsalomon@google.com    GrEffectStage                   fStages[kNumStages];
12828f9cbd62ec108d410b91155dcf6a4789c641246fbsalomon@google.com
1283fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com    typedef GrRefCnt INHERITED;
12849381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com};
12859381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com
12869381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#endif
1287