GrDrawTarget.h revision 6e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59
1ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com */
8ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
9ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
10ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
11ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#ifndef GrDrawTarget_DEFINED
12ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#define GrDrawTarget_DEFINED
13ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
148d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com#include "GrClipData.h"
159381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#include "GrDrawState.h"
16934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com#include "GrIndexBuffer.h"
17b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com#include "SkMatrix.h"
18ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#include "GrRefCnt.h"
19ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
208d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com#include "SkClipStack.h"
2112b4e27ae1a29460e91a59f38122483e1faec697sugoi@google.com#include "SkPath.h"
2246f7afb9867200b568c21736da8a8bbb56b20e30bsalomon@google.com#include "SkTLazy.h"
23a2d71482db8b6d752a51c96da74768d7dfc27932robertphillips@google.com#include "SkTArray.h"
248d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com#include "SkXfermode.h"
2597c88c255cff3dbb8343c5d090526fdbedad6dd6Scroggo
26a2d71482db8b6d752a51c96da74768d7dfc27932robertphillips@google.comclass GrClipData;
2764aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.comclass GrPath;
28ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrVertexBuffer;
295f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.comclass SkStrokeRec;
3012b4e27ae1a29460e91a59f38122483e1faec697sugoi@google.com
31ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrDrawTarget : public GrRefCnt {
32f66018798099750e639a8fa131fece492a050997bsalomon@google.comprotected:
33f66018798099750e639a8fa131fece492a050997bsalomon@google.com    /** This helper class allows GrDrawTarget subclasses to set the caps values without having to be
34f66018798099750e639a8fa131fece492a050997bsalomon@google.com        made a friend of GrDrawTarget::Caps. */
35f66018798099750e639a8fa131fece492a050997bsalomon@google.com    class CapsInternals {
36f66018798099750e639a8fa131fece492a050997bsalomon@google.com    public:
3718c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool f8BitPaletteSupport        : 1;
3818c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fNPOTTextureTileSupport    : 1;
3918c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fTwoSidedStencilSupport    : 1;
4018c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fStencilWrapOpsSupport     : 1;
4118c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fHWAALineSupport           : 1;
4218c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fShaderDerivativeSupport   : 1;
43edfe1aac5c6f3a3f3830e86ddea8dbceeb0e6df4bsalomon@google.com        bool fGeometryShaderSupport     : 1;
4418c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fFSAASupport               : 1;
4518c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fDualSourceBlendingSupport : 1;
4618c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        bool fBufferLockSupport         : 1;
4764aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com        bool fPathStencilingSupport     : 1;
4818c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        int fMaxRenderTargetSize;
4918c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com        int fMaxTextureSize;
5018c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com    };
5118c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com
52d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    class DrawInfo;
53d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
54f66018798099750e639a8fa131fece492a050997bsalomon@google.compublic:
55f66018798099750e639a8fa131fece492a050997bsalomon@google.com    SK_DECLARE_INST_COUNT(GrDrawTarget)
56f66018798099750e639a8fa131fece492a050997bsalomon@google.com
57f66018798099750e639a8fa131fece492a050997bsalomon@google.com    /**
58f66018798099750e639a8fa131fece492a050997bsalomon@google.com     * Represents the draw target capabilities.
59f66018798099750e639a8fa131fece492a050997bsalomon@google.com     */
60f66018798099750e639a8fa131fece492a050997bsalomon@google.com    class Caps {
61f66018798099750e639a8fa131fece492a050997bsalomon@google.com    public:
62f66018798099750e639a8fa131fece492a050997bsalomon@google.com        Caps() { memset(this, 0, sizeof(Caps)); }
63f66018798099750e639a8fa131fece492a050997bsalomon@google.com        Caps(const Caps& c) { *this = c; }
64f66018798099750e639a8fa131fece492a050997bsalomon@google.com        Caps& operator= (const Caps& c) {
65f66018798099750e639a8fa131fece492a050997bsalomon@google.com            memcpy(this, &c, sizeof(Caps));
66f66018798099750e639a8fa131fece492a050997bsalomon@google.com            return *this;
67f66018798099750e639a8fa131fece492a050997bsalomon@google.com        }
68f66018798099750e639a8fa131fece492a050997bsalomon@google.com        void print() const;
69f66018798099750e639a8fa131fece492a050997bsalomon@google.com
70f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool eightBitPaletteSupport() const { return fInternals.f8BitPaletteSupport; }
71f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool npotTextureTileSupport() const { return fInternals.fNPOTTextureTileSupport; }
72f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool twoSidedStencilSupport() const { return fInternals.fTwoSidedStencilSupport; }
73f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool stencilWrapOpsSupport() const { return  fInternals.fStencilWrapOpsSupport; }
74f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool hwAALineSupport() const { return fInternals.fHWAALineSupport; }
75f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool shaderDerivativeSupport() const { return fInternals.fShaderDerivativeSupport; }
76f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool geometryShaderSupport() const { return fInternals.fGeometryShaderSupport; }
77f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool fsaaSupport() const { return fInternals.fFSAASupport; }
78f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool dualSourceBlendingSupport() const { return fInternals.fDualSourceBlendingSupport; }
79f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool bufferLockSupport() const { return fInternals.fBufferLockSupport; }
80f66018798099750e639a8fa131fece492a050997bsalomon@google.com        bool pathStencilingSupport() const { return fInternals.fPathStencilingSupport; }
81f66018798099750e639a8fa131fece492a050997bsalomon@google.com
82f66018798099750e639a8fa131fece492a050997bsalomon@google.com        int maxRenderTargetSize() const { return fInternals.fMaxRenderTargetSize; }
83f66018798099750e639a8fa131fece492a050997bsalomon@google.com        int maxTextureSize() const { return fInternals.fMaxTextureSize; }
84f66018798099750e639a8fa131fece492a050997bsalomon@google.com    private:
85f66018798099750e639a8fa131fece492a050997bsalomon@google.com        CapsInternals fInternals;
860d82fe5a4be4aef5b9ce7ca6f4864b72c73452a9bsalomon@google.com        friend class GrDrawTarget; // to set values of fInternals
87f66018798099750e639a8fa131fece492a050997bsalomon@google.com    };
88f66018798099750e639a8fa131fece492a050997bsalomon@google.com
89ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    ///////////////////////////////////////////////////////////////////////////
90ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
916e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    // The context may not be fully constructed and should not be used during GrDrawTarget
926e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    // construction.
936e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrDrawTarget(GrContext* context);
9425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual ~GrDrawTarget();
95ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
96ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
9718c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com     * Gets the capabilities of the draw target.
9818c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com     */
9918c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com    const Caps& getCaps() const { return fCaps; }
10018c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com
10118c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com    /**
102ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Sets the current clip to the region specified by clip. All draws will be
103ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * clipped against this clip if kClip_StateBit is enabled.
104ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
1055aaa69e4339e229adfb05e96084a8ec0a590238bbsalomon@google.com     * Setting the clip may (or may not) zero out the client's stencil bits.
1065aaa69e4339e229adfb05e96084a8ec0a590238bbsalomon@google.com     *
107ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param description of the clipping region
108ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
109beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com    void setClip(const GrClipData* clip);
110ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
111ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
112ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Gets the current clip.
113ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
114ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @return the clip.
115ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
116beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com    const GrClipData* getClip() const;
117ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
118a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    /**
119a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Sets the draw state object for the draw target. Note that this does not
120a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * make a copy. The GrDrawTarget will take a reference to passed object.
121a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Passing NULL will cause the GrDrawTarget to use its own internal draw
122a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * state object rather than an externally provided one.
123a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     */
124a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    void setDrawState(GrDrawState*  drawState);
125a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com
126a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    /**
127a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Read-only access to the GrDrawTarget's current draw state.
128a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     */
129a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    const GrDrawState& getDrawState() const { return *fDrawState; }
130a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com
131a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    /**
132a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Read-write access to the GrDrawTarget's current draw state. Note that
133a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * this doesn't ref.
134a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     */
135a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    GrDrawState* drawState() { return fDrawState; }
1363d0835b6ac0003c18147b6e9ca76a497b92d1d40bsalomon@google.com
1373d0835b6ac0003c18147b6e9ca76a497b92d1d40bsalomon@google.com    /**
138a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * Color alpha and coverage are two inputs to the drawing pipeline. For some
139a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * blend modes it is safe to fold the coverage into constant or per-vertex
140a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * color alpha value. For other blend modes they must be handled separately.
141a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * Depending on features available in the underlying 3D API this may or may
142a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * not be possible.
143a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     *
144e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     * This function considers the current draw state and the draw target's
145e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     * capabilities to determine whether coverage can be handled correctly. The
146e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     * following assumptions are made:
147e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     *    1. The caller intends to somehow specify coverage. This can be
148e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     *       specified either by enabling a coverage stage on the GrDrawState or
149e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     *       via the vertex layout.
1502b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     *    2. Other than enabling coverage stages or enabling coverage in the
1512b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     *       layout, the current configuration of the target's GrDrawState is as
1522b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     *       it will be at draw time.
153a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     */
154a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com    bool canApplyCoverage() const;
155a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com
156a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com    /**
1572b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     * Given the current draw state and hw support, will HW AA lines be used (if
1582b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     * a line primitive type is drawn)?
159a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     */
1609381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    bool willUseHWAALines() const;
161a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com
1623d0835b6ac0003c18147b6e9ca76a497b92d1d40bsalomon@google.com    /**
163e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * There are three types of "sources" of geometry (vertices and indices) for
164e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * draw calls made on the target. When performing an indexed draw, the
165e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * indices and vertices can use different source types. Once a source is
166934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * specified it can be used for multiple draws. However, the time at which
167934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * the geometry data is no longer editable depends on the source type.
16825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *
16925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Sometimes it is necessary to perform a draw while upstack code has
170e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * already specified geometry that it isn't finished with. So there are push
171e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * and pop methods. This allows the client to push the sources, draw
172e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * something using alternate sources, and then pop to restore the original
173e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * sources.
174e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *
175e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * Aside from pushes and pops, a source remains valid until another source
176e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * is set or resetVertexSource / resetIndexSource is called. Drawing from
177e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * a reset source is an error.
178e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *
179e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * The three types of sources are:
1801c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
181e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * 1. A cpu array (set*SourceToArray). This is useful when the caller
182e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    already provided vertex data in a format compatible with a
183e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    GrVertexLayout. The data in the array is consumed at the time that
184e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    set*SourceToArray is called and subsequent edits to the array will not
185e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    be reflected in draws.
1861c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
18725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * 2. Reserve. This is most useful when the caller has data it must
18825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *    transform before drawing and is not long-lived. The caller requests
18925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *    that the draw target make room for some amount of vertex and/or index
19025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *    data. The target provides ptrs to hold the vertex and/or index data.
19125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *
192d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *    The data is writable up until the next drawIndexed, drawNonIndexed,
193934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *    drawIndexedInstances, or pushGeometrySource. At this point the data is
194934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *    frozen and the ptrs are no longer valid.
1951c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
196e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    Where the space is allocated and how it is uploaded to the GPU is
197e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    subclass-dependent.
198e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *
1991c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * 3. Vertex and Index Buffers. This is most useful for geometry that will
200e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    is long-lived. When the data in the buffer is consumed depends on the
201d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *    GrDrawTarget subclass. For deferred subclasses the caller has to
202e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    guarantee that the data is still available in the buffers at playback.
203e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    (TODO: Make this more automatic as we have done for read/write pixels)
204b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *
205b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     * The size of each vertex is determined by querying the current GrDrawState.
2061c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     */
2071c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com
2081c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com    /**
209e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * Reserves space for vertices and/or indices. Zero can be specifed as
210e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * either the vertex or index count if the caller desires to only reserve
211d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * space for only indices or only vertices. If zero is specifed for
212e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * vertexCount then the vertex source will be unmodified and likewise for
213e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * indexCount.
214ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
215e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * If the function returns true then the reserve suceeded and the vertices
216e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * and indices pointers will point to the space created.
217ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
218e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * If the target cannot make space for the request then this function will
219e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * return false. If vertexCount was non-zero then upon failure the vertex
220e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * source is reset and likewise for indexCount.
221ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
222e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * The pointers to the space allocated for vertices and indices remain valid
223934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
224934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * popGeomtrySource is called. At that point logically a snapshot of the
225934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * data is made and the pointers are invalid.
2261c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
227e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * @param vertexCount  the number of vertices to reserve space for. Can be
228b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                     0. Vertex size is queried from the current GrDrawState.
22925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * @param indexCount   the number of indices to reserve space for. Can be 0.
230e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * @param vertices     will point to reserved vertex space if vertexCount is
231d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *                     non-zero. Illegal to pass NULL if vertexCount > 0.
23225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * @param indices      will point to reserved index space if indexCount is
23325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *                     non-zero. Illegal to pass NULL if indexCount > 0.
23425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
235b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     bool reserveVertexAndIndexSpace(int vertexCount,
23697805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com                                     int indexCount,
23797805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com                                     void** vertices,
23897805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com                                     void** indices);
23925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com
240ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
241ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Provides hints to caller about the number of vertices and indices
242ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * that can be allocated cheaply. This can be useful if caller is reserving
243ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * space but doesn't know exactly how much geometry is needed.
244ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
245ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Also may hint whether the draw target should be flushed first. This is
246ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * useful for deferred targets.
247ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
248ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param vertexCount  in: hint about how many vertices the caller would
249b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                     like to allocate. Vertex size is queried from the
250b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                     current GrDrawState.
251ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     out: a hint about the number of vertices that can be
252ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     allocated cheaply. Negative means no hint.
253ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     Ignored if NULL.
254ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param indexCount   in: hint about how many indices the caller would
255ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     like to allocate.
256ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     out: a hint about the number of indices that can be
257ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     allocated cheaply. Negative means no hint.
258ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     Ignored if NULL.
259ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
260ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @return  true if target should be flushed based on the input values.
261ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
262b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    virtual bool geometryHints(int* vertexCount,
2631c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com                               int* indexCount) const;
264ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
265ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
2661c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * Sets source of vertex data for the next draw. Array must contain
2671c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * the vertex data when this is called.
268ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
269b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     * @param vertexArray   cpu array containing vertex data.
270ae683921ffda9108147a29da7319c7eee4dc9245skia.committer@gmail.com     * @param vertexCount   the number of vertices in the array. Vertex size is
271b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                      queried from the current GrDrawState.
272ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
273b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    void setVertexSourceToArray(const void* vertexArray, int vertexCount);
274ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
275ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
2761c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * Sets source of index data for the next indexed draw. Array must contain
2771c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * the indices when this is called.
278ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
279b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     * @param indexArray    cpu array containing index data.
2801c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * @param indexCount    the number of indices in the array.
281ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
2821c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com    void setIndexSourceToArray(const void* indexArray, int indexCount);
283ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
284ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
285ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Sets source of vertex data for the next draw. Data does not have to be
286934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
287ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
288ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param buffer        vertex buffer containing vertex data. Must be
289ae683921ffda9108147a29da7319c7eee4dc9245skia.committer@gmail.com     *                      unlocked before draw call. Vertex size is queried
290b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                      from current GrDrawState.
291ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
292b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
293ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
294ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
295ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Sets source of index data for the next indexed draw. Data does not have
296934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * to be in the buffer until drawIndexed.
297ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
298ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param buffer index buffer containing indices. Must be unlocked
299ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *               before indexed draw call.
300ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
301ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
302d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
30325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
30425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
30525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * source to reserved, array, or buffer before next draw. May be able to free
30625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * up temporary storage allocated by setVertexSourceToArray or
30725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * reserveVertexSpace.
30825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
30925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void resetVertexSource();
310d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
31125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
31225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Resets index source. Indexed Drawing from reset indices is illegal. Set
31325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * index source to reserved, array, or buffer before next indexed draw. May
31425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * be able to free up temporary storage allocated by setIndexSourceToArray
31525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * or reserveIndexSpace.
31625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
31797805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    void resetIndexSource();
318d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
31997805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    /**
32097805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com     * Query to find out if the vertex or index source is reserved.
32197805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com     */
32297805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    bool hasReservedVerticesOrIndices() const {
32373d98aace4f74acc39d8de33484c8aec8e917739bsalomon@google.com        return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
32497805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com        kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
32597805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    }
326ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
327ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
32825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Pushes and resets the vertex/index sources. Any reserved vertex / index
32925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * data is finalized (i.e. cannot be updated after the matching pop but can
33025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * be drawn from). Must be balanced by a pop.
33125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
33225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void pushGeometrySource();
33325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com
33425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
33525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Pops the vertex / index sources from the matching push.
33625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
33725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void popGeometrySource();
33864aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com
33925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
340ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Draws indexed geometry using the current state and current vertex / index
341ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * sources.
342ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
343ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param type         The type of primitives to draw.
344ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param startVertex  the vertex in the vertex array/buffer corresponding
345ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     to index 0
346ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param startIndex   first index to read from index src.
347ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param vertexCount  one greater than the max index.
348ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param indexCount   the number of index elements to read. The index count
349ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     is effectively trimmed to the last completely
350ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     specified primitive.
351d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * @param devBounds    optional bounds hint. This is a promise from the caller,
352d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     *                     not a request for clipping.
353ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
35425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void drawIndexed(GrPrimitiveType type,
35525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                     int startVertex,
35625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                     int startIndex,
35725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                     int vertexCount,
358d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                     int indexCount,
359d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                     const SkRect* devBounds = NULL);
360ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
361ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
362ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Draws non-indexed geometry using the current state and current vertex
363ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * sources.
364ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
365ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param type         The type of primitives to draw.
366ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param startVertex  the vertex in the vertex array/buffer corresponding
367ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     to index 0
368ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param vertexCount  one greater than the max index.
369d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * @param devBounds    optional bounds hint. This is a promise from the caller,
370d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     *                     not a request for clipping.
371ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
37225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void drawNonIndexed(GrPrimitiveType type,
37325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                        int startVertex,
374d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                        int vertexCount,
375d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                        const SkRect* devBounds = NULL);
376ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
37786afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com    /**
37864aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     * Draws path into the stencil buffer. The fill must be either even/odd or
37964aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     * winding (not inverse or hairline). It will respect the HW antialias flag
38064aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     * on the draw state (if possible in the 3D API).
38164aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     */
3825f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.com    void stencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill);
38364aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com
38464aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com    /**
38586afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     * Helper function for drawing rects. This does not use the current index
38686afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     * and vertex sources. After returning, the vertex and index sources may
38786afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     * have changed. They should be reestablished before the next drawIndexed
38886afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     * or drawNonIndexed. This cannot be called between reserving and releasing
389d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * geometry.
390044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *
391d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * A subclass may override this to perform more optimal rect rendering. Its
392d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * draws should be funneled through one of the public GrDrawTarget draw methods
393d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * (e.g. drawNonIndexed, drawIndexedInstances, ...). The base class draws a two
394d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * triangle fan using drawNonIndexed from reserved vertex space.
395044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *
39686afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     * @param rect      the rect to draw
39786afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     * @param matrix    optional matrix applied to rect (before viewMatrix)
3983976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     * @param srcRects  specifies rect for explicit texture coordinates.
3993976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     *                  if srcRect is non-NULL then that rect will be used
4003976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     *                  as the coordinates for the given stage.
4013976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     * @param srcMatrix   optional matrix applied to srcRect. If
402044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *                    srcRect is non-NULL and srcMatrix is non-NULL
403044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *                    then srcRect will be transformed by srcMatrix.
4043976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     *                    srcMatrix can be NULL when no srcMatrix is desired.
405044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     * @param stage     the stage to be given explicit texture coordinates.
4063976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     *                  Ignored if srcRect is NULL.
40786afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     */
408d302f1401b3c9aea094804bad4e76de98782cfe8bsalomon@google.com    virtual void drawRect(const GrRect& rect,
409b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                          const SkMatrix* matrix,
4103976825a21532e254311b90b4a9046e25717e335jvanverth@google.com                          const GrRect* srcRect,
4113976825a21532e254311b90b4a9046e25717e335jvanverth@google.com                          const SkMatrix* srcMatrix,
4123976825a21532e254311b90b4a9046e25717e335jvanverth@google.com                          int stage);
4133976825a21532e254311b90b4a9046e25717e335jvanverth@google.com
414cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    /**
415cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     * Helper for drawRect when the caller doesn't need separate src rects or
416cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     * matrices.
417cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     */
418cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    void drawSimpleRect(const GrRect& rect, const SkMatrix* matrix = NULL) {
4193976825a21532e254311b90b4a9046e25717e335jvanverth@google.com        drawRect(rect, matrix, NULL, NULL, 0);
420cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    }
421cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    void drawSimpleRect(const GrIRect& irect, const SkMatrix* matrix = NULL) {
422cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com        SkRect rect = SkRect::MakeFromIRect(irect);
4233976825a21532e254311b90b4a9046e25717e335jvanverth@google.com        this->drawRect(rect, matrix, NULL, NULL, 0);
424cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    }
42586afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com
42686afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com    /**
427934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * This call is used to draw multiple instances of some geometry with a
428934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * given number of vertices (V) and indices (I) per-instance. The indices in
429934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * the index source must have the form i[k+I] == i[k] + V. Also, all indices
430934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
431934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * concrete example, the following index buffer for drawing a series of
432934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * quads each as two triangles each satisfies these conditions with V=4 and
433934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * I=6:
434934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *      (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
435934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *
436934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * The call assumes that the pattern of indices fills the entire index
437934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * source. The size of the index buffer limits the number of instances that
438934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * can be drawn by the GPU in a single draw. However, the caller may specify
439934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * any (positive) number for instanceCount and if necessary multiple GPU
440d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * draws will be issued. Moreover, when drawIndexedInstances is called
441934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * multiple times it may be possible for GrDrawTarget to group them into a
442934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * single GPU draw.
443934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *
444934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param type          the type of primitives to draw
445934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param instanceCount the number of instances to draw. Each instance
446934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                      consists of verticesPerInstance vertices indexed by
447934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                      indicesPerInstance indices drawn as the primitive
448934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                      type specified by type.
449934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param verticesPerInstance   The number of vertices in each instance (V
450934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                              in the above description).
451934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param indicesPerInstance    The number of indices in each instance (I
452934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                              in the above description).
453d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * @param devBounds    optional bounds hint. This is a promise from the caller,
454d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     *                     not a request for clipping.
455934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     */
456d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    void drawIndexedInstances(GrPrimitiveType type,
457d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              int instanceCount,
458d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              int verticesPerInstance,
459d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              int indicesPerInstance,
460d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              const SkRect* devBounds = NULL);
461934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com
462934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com    /**
463d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Clear the current render target if one isn't passed in. Ignores the
464d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * clip and all other draw state (blend mode, stages, etc). Clears the
465c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com     * whole thing if rect is NULL, otherwise just the rect.
4660b335c1ac100aeacf79a4c98a052286fd46661e7bsalomon@google.com     */
467d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    virtual void clear(const GrIRect* rect,
468c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com                       GrColor color,
469c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com                       GrRenderTarget* renderTarget = NULL) = 0;
4700b335c1ac100aeacf79a4c98a052286fd46661e7bsalomon@google.com
471ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com    /**
472ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com     * Release any resources that are cached but not currently in use. This
473ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com     * is intended to give an application some recourse when resources are low.
474ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com     */
475ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com    virtual void purgeResources() {};
476ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com
477d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    /**
478d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
479d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     */
480d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    void executeDraw(const DrawInfo& info) { this->onDraw(info); }
481d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
48225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
483ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
484873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    /**
485873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * See AutoStateRestore below.
486873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     */
487873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    enum ASRInit {
488873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        kPreserve_ASRInit,
489873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        kReset_ASRInit
490873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    };
491873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com
492873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    /**
493873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * Saves off the current state and restores it in the destructor. It will
494873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * install a new GrDrawState object on the target (setDrawState) and restore
495873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * the previous one in the destructor. The caller should call drawState() to
496873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * get the new draw state after the ASR is installed.
497873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *
498873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * GrDrawState* state = target->drawState();
499873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
500873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * state->setRenderTarget(rt); // state refers to the GrDrawState set on
501873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // target before asr was initialized.
502873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // Therefore, rt is set on the GrDrawState
503873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // that will be restored after asr's
504873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // destructor rather than target's current
505d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *                             // GrDrawState.
506873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     */
507ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    class AutoStateRestore : ::GrNoncopyable {
508ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    public:
509873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        /**
510873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * Default ASR will have no effect unless set() is subsequently called.
511873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         */
51206afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com        AutoStateRestore();
513873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com
514873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        /**
515873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * Saves the state on target. The state will be restored when the ASR
516873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * is destroyed. If this constructor is used do not call set().
517873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *
518873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * @param init  Should the newly installed GrDrawState be a copy of the
519873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *              previous state or a default-initialized GrDrawState.
520873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         */
521873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        AutoStateRestore(GrDrawTarget* target, ASRInit init);
522873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com
523ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        ~AutoStateRestore();
524ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
52506afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com        /**
526873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * Saves the state on target. The state will be restored when the ASR
527873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * is destroyed. This should only be called once per ASR object and only
528873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * when the default constructor was used. For nested saves use multiple
529873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * ASR objects.
530873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *
531873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * @param init  Should the newly installed GrDrawState be a copy of the
532873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *              previous state or a default-initialized GrDrawState.
53306afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com         */
534873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        void set(GrDrawTarget* target, ASRInit init);
53506afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com
536ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    private:
537873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        GrDrawTarget*        fDrawTarget;
538873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        SkTLazy<GrDrawState> fTempState;
539873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        GrDrawState*         fSavedState;
540ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
541ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
54225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
5431c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com
544ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    class AutoReleaseGeometry : ::GrNoncopyable {
545ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    public:
546ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        AutoReleaseGeometry(GrDrawTarget*  target,
54725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                            int            vertexCount,
54825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                            int            indexCount);
54925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        AutoReleaseGeometry();
55025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        ~AutoReleaseGeometry();
5515782d712ffc31557d0cb12d5a220cebb783f6895bsalomon@google.com        bool set(GrDrawTarget*  target,
55225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                 int            vertexCount,
55325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                 int            indexCount);
554a47a48dca5045d71cbc5de343404045209a13e15bsalomon@google.com        bool succeeded() const { return NULL != fTarget; }
5556513cd06ae34f5d777b3aaea0b4533014d0a10f2bsalomon@google.com        void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
5566513cd06ae34f5d777b3aaea0b4533014d0a10f2bsalomon@google.com        void* indices() const { GrAssert(this->succeeded()); return fIndices; }
557ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        GrPoint* positions() const {
5586513cd06ae34f5d777b3aaea0b4533014d0a10f2bsalomon@google.com            return static_cast<GrPoint*>(this->vertices());
559ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
560ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
561ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    private:
56225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        void reset();
563d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
564ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        GrDrawTarget* fTarget;
565ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        void*         fVertices;
566ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        void*         fIndices;
567ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
568ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
56925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
570ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
571ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    class AutoClipRestore : ::GrNoncopyable {
572ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    public:
573ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        AutoClipRestore(GrDrawTarget* target) {
574ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com            fTarget = target;
575ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com            fClip = fTarget->getClip();
576ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
577ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
5788d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
5798d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com
580ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        ~AutoClipRestore() {
581ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com            fTarget->setClip(fClip);
582ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
583ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    private:
5848d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        GrDrawTarget*           fTarget;
5858d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        const GrClipData*       fClip;
5868d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        SkTLazy<SkClipStack>    fStack;
5878d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        GrClipData              fReplacementClip;
588ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
589d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
59025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
591d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
592b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    class AutoGeometryAndStatePush : ::GrNoncopyable {
59325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    public:
594ae683921ffda9108147a29da7319c7eee4dc9245skia.committer@gmail.com        AutoGeometryAndStatePush(GrDrawTarget* target, ASRInit init)
595b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com            : fState(target, init) {
59625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            GrAssert(NULL != target);
59725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            fTarget = target;
59825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            target->pushGeometrySource();
59925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        }
600b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        ~AutoGeometryAndStatePush() {
60125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            fTarget->popGeometrySource();
60225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        }
60325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    private:
604b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        GrDrawTarget*    fTarget;
605b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        AutoStateRestore fState;
60625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    };
607ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
608ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comprotected:
609471d471dcd7422e5dd9c822c1092b2ba4721dcfebsalomon@google.com
61025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    enum GeometrySrcType {
61125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kNone_GeometrySrcType,     //<! src has not been specified
61225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kReserved_GeometrySrcType, //<! src was set using reserve*Space
61325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kArray_GeometrySrcType,    //<! src was set using set*SourceToArray
61425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kBuffer_GeometrySrcType    //<! src was set using set*SourceToBuffer
61525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    };
616d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
61725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    struct GeometrySrcState {
61825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        GeometrySrcType         fVertexSrc;
61925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        union {
62025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is buffer
62125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            const GrVertexBuffer*   fVertexBuffer;
62225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is reserved or array
62325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            int                     fVertexCount;
62425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        };
625d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
62625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        GeometrySrcType         fIndexSrc;
62725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        union {
62825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is buffer
62925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            const GrIndexBuffer*    fIndexBuffer;
63025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is reserved or array
63125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            int                     fIndexCount;
63225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        };
633d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
634b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        size_t                  fVertexSize;
63525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    };
636934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com
637934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com    int indexCountInCurrentSource() const {
638934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com        const GeometrySrcState& src = this->getGeomSrc();
639934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com        switch (src.fIndexSrc) {
640934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kNone_GeometrySrcType:
641934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return 0;
642934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kReserved_GeometrySrcType:
643934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kArray_GeometrySrcType:
644934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return src.fIndexCount;
645934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kBuffer_GeometrySrcType:
646934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
647934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            default:
648934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                GrCrash("Unexpected Index Source.");
649934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return 0;
650934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com        }
651934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com    }
652a47a48dca5045d71cbc5de343404045209a13e15bsalomon@google.com
6536e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrContext* getContext() { return fContext; }
6546e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    const GrContext* getContext() const { return fContext; }
6556e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com
65602ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // allows derived class to set the caps
65702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    CapsInternals* capsInternals() { return &fCaps.fInternals; }
65802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
65902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // A subclass may override this function if it wishes to be notified when the clip is changed.
66002ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // The override should call INHERITED::clipWillBeSet().
66102ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    virtual void clipWillBeSet(const GrClipData* clipData);
66202ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
66302ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // subclasses must call this in their destructors to ensure all vertex
66402ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // and index sources have been released (including those held by
66502ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // pushGeometrySource())
66602ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    void releaseGeometry();
66702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
66802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // accessors for derived classes
66902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
670b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
671b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    size_t getVertexSize() const {
67202ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com        // the vertex layout is only valid if a vertex source has been specified.
67302ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com        GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
674b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        return this->getGeomSrc().fVertexSize;
675a47a48dca5045d71cbc5de343404045209a13e15bsalomon@google.com    }
6765782d712ffc31557d0cb12d5a220cebb783f6895bsalomon@google.com
67702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    Caps fCaps;
67802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
679d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    /**
680d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * Used to communicate draws to subclass's onDraw function.
681d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     */
68274749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    class DrawInfo {
68374749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    public:
68474749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        DrawInfo(const DrawInfo& di) { (*this) = di; }
685d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        DrawInfo& operator =(const DrawInfo& di);
68674749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
68774749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        GrPrimitiveType primitiveType() const { return fPrimitiveType; }
68874749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int startVertex() const { return fStartVertex; }
68974749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int startIndex() const { return fStartIndex; }
69074749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int vertexCount() const { return fVertexCount; }
69174749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int indexCount() const { return fIndexCount; }
692d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int verticesPerInstance() const { return fVerticesPerInstance; }
693d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int indicesPerInstance() const { return fIndicesPerInstance; }
694d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int instanceCount() const { return fInstanceCount; }
69574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
69674749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        bool isIndexed() const { return fIndexCount > 0; }
697d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com#if GR_DEBUG
698d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        bool isInstanced() const; // this version is longer because of asserts
699d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com#else
700d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        bool isInstanced() const { return fInstanceCount > 0; }
701d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com#endif
702d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
703d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        // adds or remove instances
704d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void adjustInstanceCount(int instanceOffset);
705d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        // shifts the start vertex
706d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void adjustStartVertex(int vertexOffset);
707d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        // shifts the start index
708d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void adjustStartIndex(int indexOffset);
709d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
710d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void setDevBounds(const SkRect& bounds) {
711d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            fDevBoundsStorage = bounds;
712d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            fDevBounds = &fDevBoundsStorage;
713d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        }
714d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        const SkRect* getDevBounds() const { return fDevBounds; }
71574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
71674749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    private:
717d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        DrawInfo() { fDevBounds = NULL; }
718d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
71974749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        friend class GrDrawTarget;
720d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
72174749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        GrPrimitiveType fPrimitiveType;
72274749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
72374749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int             fStartVertex;
72474749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int             fStartIndex;
72574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int             fVertexCount;
72674749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int             fIndexCount;
727d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
728d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int             fInstanceCount;
729d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int             fVerticesPerInstance;
730d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int             fIndicesPerInstance;
731d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
732d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        SkRect          fDevBoundsStorage;
733d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        SkRect*         fDevBounds;
73474749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    };
73574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
73602ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.comprivate:
73702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // A subclass can optionally overload this function to be notified before
73802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // vertex and index space is reserved.
739b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
74097805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com
74125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // implemented by subclass to allocate space for reserved geom
742a63389843dd18003382d61c2e4610af09ed07d38jvanverth@google.com    virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
74325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
74425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // implemented by subclass to handle release of reserved geom space
74525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseReservedVertexSpace() = 0;
74625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseReservedIndexSpace() = 0;
74725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // subclass must consume array contents when set
74802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
74902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
75025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // subclass is notified that geom source will be set away from an array
75125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseVertexArray() = 0;
75225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseIndexArray() = 0;
75302ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // subclass overrides to be notified just before geo src state is pushed/popped.
75425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void geometrySourceWillPush() = 0;
75525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
75625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // subclass called to perform drawing
75774749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    virtual void onDraw(const DrawInfo&) = 0;
7585f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.com    virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill) = 0;
75964aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com
760e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com    // helpers for reserving vertex and index space.
761b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    bool reserveVertexSpace(size_t vertexSize,
762e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com                            int vertexCount,
763e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com                            void** vertices);
764e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com    bool reserveIndexSpace(int indexCount, void** indices);
765d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
766e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com    // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
767e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com    // indicate non-indexed drawing.
768e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com    bool checkDraw(GrPrimitiveType type, int startVertex,
769e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com                   int startIndex, int vertexCount,
770e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com                   int indexCount) const;
77125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // called when setting a new vert/idx source to unref prev vb/ib
77225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void releasePreviousVertexSource();
77325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void releasePreviousIndexSource();
774d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
77525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    enum {
77625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kPreallocGeoSrcStateStackCnt = 4,
777ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
77802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
77902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    const GrClipData*                                               fClip;
78002ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    GrDrawState*                                                    fDrawState;
78102ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    GrDrawState                                                     fDefaultDrawState;
7826e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
7836e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrContext*                                                      fContext;
784fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com
785fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com    typedef GrRefCnt INHERITED;
786ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com};
787ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
788ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#endif
789