GrDrawTarget.h revision 3eee3834fba1d2e29dfb61adcb97e4624b6a3ac8
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com */
7ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
8ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#ifndef GrDrawTarget_DEFINED
9ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#define GrDrawTarget_DEFINED
10ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
118d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com#include "GrClipData.h"
122a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#include "GrContext.h"
139381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com#include "GrDrawState.h"
14934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com#include "GrIndexBuffer.h"
152a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#include "GrTraceMarker.h"
16ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
178d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com#include "SkClipStack.h"
18a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org#include "SkMatrix.h"
1912b4e27ae1a29460e91a59f38122483e1faec697sugoi@google.com#include "SkPath.h"
209b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org#include "SkStrokeRec.h"
21a2d71482db8b6d752a51c96da74768d7dfc27932robertphillips@google.com#include "SkTArray.h"
22a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org#include "SkTLazy.h"
23a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "SkTypes.h"
248d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com#include "SkXfermode.h"
2597c88c255cff3dbb8343c5d090526fdbedad6dd6Scroggo
26a2d71482db8b6d752a51c96da74768d7dfc27932robertphillips@google.comclass GrClipData;
27c26d94fd7dc0b00cd6d0e42d28285f4a38aff021bsalomon@google.comclass GrDrawTargetCaps;
2864aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.comclass GrPath;
29ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrVertexBuffer;
3012b4e27ae1a29460e91a59f38122483e1faec697sugoi@google.com
31a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.orgclass GrDrawTarget : public SkRefCnt {
32f66018798099750e639a8fa131fece492a050997bsalomon@google.comprotected:
33bcce8926524827775539874346dd424a9510dbc9bsalomon@google.com    class DrawInfo;
34bcce8926524827775539874346dd424a9510dbc9bsalomon@google.com
35bcce8926524827775539874346dd424a9510dbc9bsalomon@google.compublic:
36bcce8926524827775539874346dd424a9510dbc9bsalomon@google.com    SK_DECLARE_INST_COUNT(GrDrawTarget)
37bcce8926524827775539874346dd424a9510dbc9bsalomon@google.com
38ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    ///////////////////////////////////////////////////////////////////////////
39ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
406e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    // The context may not be fully constructed and should not be used during GrDrawTarget
416e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    // construction.
426e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrDrawTarget(GrContext* context);
4325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual ~GrDrawTarget();
44ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
45ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
4618c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com     * Gets the capabilities of the draw target.
4718c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com     */
48c26d94fd7dc0b00cd6d0e42d28285f4a38aff021bsalomon@google.com    const GrDrawTargetCaps* caps() const { return fCaps.get(); }
4918c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com
5018c9c198f571997463d9a7134dbd88298e592ec2bsalomon@google.com    /**
51ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Sets the current clip to the region specified by clip. All draws will be
52ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * clipped against this clip if kClip_StateBit is enabled.
53ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
545aaa69e4339e229adfb05e96084a8ec0a590238bbsalomon@google.com     * Setting the clip may (or may not) zero out the client's stencil bits.
555aaa69e4339e229adfb05e96084a8ec0a590238bbsalomon@google.com     *
56ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param description of the clipping region
57ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
58beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com    void setClip(const GrClipData* clip);
59ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
60ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
61ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Gets the current clip.
62ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
63ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @return the clip.
64ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
65beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com    const GrClipData* getClip() const;
66ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
67a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    /**
68a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Sets the draw state object for the draw target. Note that this does not
69a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * make a copy. The GrDrawTarget will take a reference to passed object.
70a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Passing NULL will cause the GrDrawTarget to use its own internal draw
71a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * state object rather than an externally provided one.
72a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     */
73a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    void setDrawState(GrDrawState*  drawState);
74a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com
75a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    /**
76a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Read-only access to the GrDrawTarget's current draw state.
77a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     */
78a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    const GrDrawState& getDrawState() const { return *fDrawState; }
79a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com
80a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    /**
81a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * Read-write access to the GrDrawTarget's current draw state. Note that
82a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     * this doesn't ref.
83a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com     */
84a5d056ae0b04021dfb44c2c7a3d6a34e060261b8bsalomon@google.com    GrDrawState* drawState() { return fDrawState; }
853d0835b6ac0003c18147b6e9ca76a497b92d1d40bsalomon@google.com
863d0835b6ac0003c18147b6e9ca76a497b92d1d40bsalomon@google.com    /**
87a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * Color alpha and coverage are two inputs to the drawing pipeline. For some
88a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * blend modes it is safe to fold the coverage into constant or per-vertex
89a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * color alpha value. For other blend modes they must be handled separately.
90a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * Depending on features available in the underlying 3D API this may or may
91a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     * not be possible.
92a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     *
93e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     * This function considers the current draw state and the draw target's
94e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     * capabilities to determine whether coverage can be handled correctly. The
95e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     * following assumptions are made:
96e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     *    1. The caller intends to somehow specify coverage. This can be
97e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     *       specified either by enabling a coverage stage on the GrDrawState or
98e79c815bca39fa552983b7a8107219aa5084acdbbsalomon@google.com     *       via the vertex layout.
992b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     *    2. Other than enabling coverage stages or enabling coverage in the
1002b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     *       layout, the current configuration of the target's GrDrawState is as
1012b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     *       it will be at draw time.
102a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     */
103a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com    bool canApplyCoverage() const;
104a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com
105e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org    /** When we're using coverage AA but the blend is incompatible (given gpu
106e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org     * limitations) we should disable AA. */
107e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org    bool shouldDisableCoverageAAForBlend() {
108e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org        // Enable below if we should draw with AA even when it produces
109e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org        // incorrect blending.
110e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org        // return false;
111e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org        return !this->canApplyCoverage();
112e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org    }
113e0a868c84ebc34c5a16b5faa1546016abb9ca0accommit-bot@chromium.org
114a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com    /**
1152b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     * Given the current draw state and hw support, will HW AA lines be used (if
1162b446734cfa8201e5478648988de86b646cb9544bsalomon@google.com     * a line primitive type is drawn)?
117a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com     */
1189381363050ec9d3e724076a8e9152bfa9a8de1d1tomhudson@google.com    bool willUseHWAALines() const;
119a31082685544d0ae4c0b203d7f5ff960640ed8dfbsalomon@google.com
1203d0835b6ac0003c18147b6e9ca76a497b92d1d40bsalomon@google.com    /**
121e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * There are three types of "sources" of geometry (vertices and indices) for
122e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * draw calls made on the target. When performing an indexed draw, the
123e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * indices and vertices can use different source types. Once a source is
124934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * specified it can be used for multiple draws. However, the time at which
125934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * the geometry data is no longer editable depends on the source type.
12625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *
12725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Sometimes it is necessary to perform a draw while upstack code has
128e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * already specified geometry that it isn't finished with. So there are push
129e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * and pop methods. This allows the client to push the sources, draw
130e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * something using alternate sources, and then pop to restore the original
131e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * sources.
132e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *
133e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * Aside from pushes and pops, a source remains valid until another source
134e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * is set or resetVertexSource / resetIndexSource is called. Drawing from
135e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * a reset source is an error.
136e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *
137e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * The three types of sources are:
1381c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
139e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * 1. A cpu array (set*SourceToArray). This is useful when the caller
140e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    already provided vertex data in a format compatible with a
141e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    GrVertexLayout. The data in the array is consumed at the time that
142e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    set*SourceToArray is called and subsequent edits to the array will not
143e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    be reflected in draws.
1441c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
14525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * 2. Reserve. This is most useful when the caller has data it must
14625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *    transform before drawing and is not long-lived. The caller requests
14725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *    that the draw target make room for some amount of vertex and/or index
14825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *    data. The target provides ptrs to hold the vertex and/or index data.
14925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *
150d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *    The data is writable up until the next drawIndexed, drawNonIndexed,
151e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     *    drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At
152e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     *    this point the data is frozen and the ptrs are no longer valid.
1531c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
154e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    Where the space is allocated and how it is uploaded to the GPU is
155e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    subclass-dependent.
156e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *
1571c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * 3. Vertex and Index Buffers. This is most useful for geometry that will
158e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    is long-lived. When the data in the buffer is consumed depends on the
159d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *    GrDrawTarget subclass. For deferred subclasses the caller has to
160e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    guarantee that the data is still available in the buffers at playback.
161e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     *    (TODO: Make this more automatic as we have done for read/write pixels)
162b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *
163b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     * The size of each vertex is determined by querying the current GrDrawState.
1641c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     */
1651c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com
1661c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com    /**
167e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * Reserves space for vertices and/or indices. Zero can be specifed as
168e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * either the vertex or index count if the caller desires to only reserve
169d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * space for only indices or only vertices. If zero is specifed for
170e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * vertexCount then the vertex source will be unmodified and likewise for
171e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * indexCount.
172ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
173e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * If the function returns true then the reserve suceeded and the vertices
174e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * and indices pointers will point to the space created.
175ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
176e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * If the target cannot make space for the request then this function will
177e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * return false. If vertexCount was non-zero then upon failure the vertex
178e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * source is reset and likewise for indexCount.
179ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
180e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * The pointers to the space allocated for vertices and indices remain valid
181e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect,
182e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * copySurface, or push/popGeomtrySource is called. At that point logically a
183e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * snapshot of the data is made and the pointers are invalid.
1841c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     *
185e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * @param vertexCount  the number of vertices to reserve space for. Can be
186b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                     0. Vertex size is queried from the current GrDrawState.
18725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * @param indexCount   the number of indices to reserve space for. Can be 0.
188e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com     * @param vertices     will point to reserved vertex space if vertexCount is
189d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *                     non-zero. Illegal to pass NULL if vertexCount > 0.
19025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * @param indices      will point to reserved index space if indexCount is
19125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     *                     non-zero. Illegal to pass NULL if indexCount > 0.
19225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
193b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     bool reserveVertexAndIndexSpace(int vertexCount,
19497805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com                                     int indexCount,
19597805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com                                     void** vertices,
19697805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com                                     void** indices);
19725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com
198ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
199ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Provides hints to caller about the number of vertices and indices
200ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * that can be allocated cheaply. This can be useful if caller is reserving
201ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * space but doesn't know exactly how much geometry is needed.
202ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
203ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Also may hint whether the draw target should be flushed first. This is
204ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * useful for deferred targets.
205ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
206ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param vertexCount  in: hint about how many vertices the caller would
207b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                     like to allocate. Vertex size is queried from the
208b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                     current GrDrawState.
209ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     out: a hint about the number of vertices that can be
210ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     allocated cheaply. Negative means no hint.
211ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     Ignored if NULL.
212ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param indexCount   in: hint about how many indices the caller would
213ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     like to allocate.
214ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     out: a hint about the number of indices that can be
215ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     allocated cheaply. Negative means no hint.
216ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     Ignored if NULL.
217ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
218ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @return  true if target should be flushed based on the input values.
219ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
220b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    virtual bool geometryHints(int* vertexCount,
2211c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com                               int* indexCount) const;
222ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
223ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
2241c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * Sets source of vertex data for the next draw. Array must contain
2251c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * the vertex data when this is called.
226ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
227b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     * @param vertexArray   cpu array containing vertex data.
228ae683921ffda9108147a29da7319c7eee4dc9245skia.committer@gmail.com     * @param vertexCount   the number of vertices in the array. Vertex size is
229b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                      queried from the current GrDrawState.
230ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
231b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    void setVertexSourceToArray(const void* vertexArray, int vertexCount);
232ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
233ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
2341c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * Sets source of index data for the next indexed draw. Array must contain
2351c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * the indices when this is called.
236ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
237b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     * @param indexArray    cpu array containing index data.
2381c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com     * @param indexCount    the number of indices in the array.
239ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
2401c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com    void setIndexSourceToArray(const void* indexArray, int indexCount);
241ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
242ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
243ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Sets source of vertex data for the next draw. Data does not have to be
244934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
245ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
246ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param buffer        vertex buffer containing vertex data. Must be
247ae683921ffda9108147a29da7319c7eee4dc9245skia.committer@gmail.com     *                      unlocked before draw call. Vertex size is queried
248b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com     *                      from current GrDrawState.
249ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
250b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
251ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
252ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
253ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Sets source of index data for the next indexed draw. Data does not have
254934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * to be in the buffer until drawIndexed.
255ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
256ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param buffer index buffer containing indices. Must be unlocked
257ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *               before indexed draw call.
258ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
259ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
260d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
26125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
26225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
26325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * source to reserved, array, or buffer before next draw. May be able to free
26425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * up temporary storage allocated by setVertexSourceToArray or
26525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * reserveVertexSpace.
26625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
26725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void resetVertexSource();
268d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
26925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
27025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Resets index source. Indexed Drawing from reset indices is illegal. Set
27125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * index source to reserved, array, or buffer before next indexed draw. May
27225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * be able to free up temporary storage allocated by setIndexSourceToArray
27325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * or reserveIndexSpace.
27425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
27597805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    void resetIndexSource();
276d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
27797805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    /**
27897805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com     * Query to find out if the vertex or index source is reserved.
27997805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com     */
28097805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    bool hasReservedVerticesOrIndices() const {
28173d98aace4f74acc39d8de33484c8aec8e917739bsalomon@google.com        return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
28297805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com        kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
28397805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com    }
284ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
285ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
28625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Pushes and resets the vertex/index sources. Any reserved vertex / index
28725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * data is finalized (i.e. cannot be updated after the matching pop but can
28825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * be drawn from). Must be balanced by a pop.
28925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
29025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void pushGeometrySource();
29125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com
29225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
29325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     * Pops the vertex / index sources from the matching push.
29425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com     */
29525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void popGeometrySource();
29664aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com
29725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    /**
298ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Draws indexed geometry using the current state and current vertex / index
299ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * sources.
300ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
301ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param type         The type of primitives to draw.
302ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param startVertex  the vertex in the vertex array/buffer corresponding
303ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     to index 0
304ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param startIndex   first index to read from index src.
305ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param vertexCount  one greater than the max index.
306ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param indexCount   the number of index elements to read. The index count
307ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     is effectively trimmed to the last completely
308ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     specified primitive.
309d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * @param devBounds    optional bounds hint. This is a promise from the caller,
310d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     *                     not a request for clipping.
311ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
31225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void drawIndexed(GrPrimitiveType type,
31325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                     int startVertex,
31425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                     int startIndex,
31525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                     int vertexCount,
316d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                     int indexCount,
317d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                     const SkRect* devBounds = NULL);
318ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
319ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    /**
320ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * Draws non-indexed geometry using the current state and current vertex
321ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * sources.
322ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *
323ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param type         The type of primitives to draw.
324ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param startVertex  the vertex in the vertex array/buffer corresponding
325ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     *                     to index 0
326ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     * @param vertexCount  one greater than the max index.
327d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * @param devBounds    optional bounds hint. This is a promise from the caller,
328d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     *                     not a request for clipping.
329ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com     */
33025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void drawNonIndexed(GrPrimitiveType type,
33125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                        int startVertex,
332d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                        int vertexCount,
333d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                        const SkRect* devBounds = NULL);
334ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
33586afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com    /**
33664aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     * Draws path into the stencil buffer. The fill must be either even/odd or
33764aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     * winding (not inverse or hairline). It will respect the HW antialias flag
33864aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     * on the draw state (if possible in the 3D API).
33964aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com     */
34032184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org    void stencilPath(const GrPath*, SkPath::FillType fill);
34164aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com
34264aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com    /**
34332184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org     * Draws a path. Fill must not be a hairline. It will respect the HW
344c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org     * antialias flag on the draw state (if possible in the 3D API).
345c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org     */
34632184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org    void drawPath(const GrPath*, SkPath::FillType fill);
347c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org
348c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org    /**
3499b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     * Draws many paths. It will respect the HW
3509b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     * antialias flag on the draw state (if possible in the 3D API).
3519b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     *
3529b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     * @param transforms array of 2d affine transformations, one for each path.
3539b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     * @param fill the fill type for drawing all the paths. Fill must not be a
3549b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     *             hairline.
3559b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     * @param stroke the stroke for drawing all the paths.
3569b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     */
357ecc45365b6429ee4cbe1748dc6667b862d5f0f71commit-bot@chromium.org    void drawPaths(int pathCount, const GrPath** paths,
3589b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                   const SkMatrix* transforms, SkPath::FillType fill,
3599b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                   SkStrokeRec::Style stroke);
3609b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org
3619b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org    /**
3620406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     * Helper function for drawing rects. It performs a geometry src push and pop
3630406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     * and thus will finalize any reserved geometry.
364044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *
365c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param rect        the rect to draw
366c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param matrix      optional matrix applied to rect (before viewMatrix)
367c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param localRect   optional rect that specifies local coords to map onto
368c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     *                    rect. If NULL then rect serves as the local coords.
369c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param localMatrix optional matrix applied to localRect. If
370044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *                    srcRect is non-NULL and srcMatrix is non-NULL
371044679ef8c08e1f01afadf5bc08251fe8597df81skia.committer@gmail.com     *                    then srcRect will be transformed by srcMatrix.
3723976825a21532e254311b90b4a9046e25717e335jvanverth@google.com     *                    srcMatrix can be NULL when no srcMatrix is desired.
37386afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com     */
374fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    void drawRect(const SkRect& rect,
3750406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com                  const SkMatrix* matrix,
376fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                  const SkRect* localRect,
3770406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com                  const SkMatrix* localMatrix) {
3780406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        AutoGeometryPush agp(this);
3790406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        this->onDrawRect(rect, matrix, localRect, localMatrix);
3800406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    }
3813976825a21532e254311b90b4a9046e25717e335jvanverth@google.com
382cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    /**
383c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * Helper for drawRect when the caller doesn't need separate local rects or matrices.
384cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com     */
385fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    void drawSimpleRect(const SkRect& rect, const SkMatrix* matrix = NULL) {
386eb6879f50a5564eeb981ec5616b55bf685eb76fcbsalomon@google.com        this->drawRect(rect, matrix, NULL, NULL);
387cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    }
388fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    void drawSimpleRect(const SkIRect& irect, const SkMatrix* matrix = NULL) {
3894469938e92d779dff05e745559e67907bbf21e78reed@google.com        SkRect rect = SkRect::Make(irect);
390c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        this->drawRect(rect, matrix, NULL, NULL);
391cf939ae54842fc7408ee68a41427086962b4c3dcbsalomon@google.com    }
39286afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com
39386afc2ae27fec84c01eb0e81a32766bdaf67dca8bsalomon@google.com    /**
394934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * This call is used to draw multiple instances of some geometry with a
395934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * given number of vertices (V) and indices (I) per-instance. The indices in
396934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * the index source must have the form i[k+I] == i[k] + V. Also, all indices
397934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
398934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * concrete example, the following index buffer for drawing a series of
399934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * quads each as two triangles each satisfies these conditions with V=4 and
400934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * I=6:
401934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *      (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
402934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *
403934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * The call assumes that the pattern of indices fills the entire index
404934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * source. The size of the index buffer limits the number of instances that
405934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * can be drawn by the GPU in a single draw. However, the caller may specify
406934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * any (positive) number for instanceCount and if necessary multiple GPU
407d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * draws will be issued. Moreover, when drawIndexedInstances is called
408934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * multiple times it may be possible for GrDrawTarget to group them into a
409934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * single GPU draw.
410934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *
411934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param type          the type of primitives to draw
412934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param instanceCount the number of instances to draw. Each instance
413934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                      consists of verticesPerInstance vertices indexed by
414934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                      indicesPerInstance indices drawn as the primitive
415934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                      type specified by type.
416934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param verticesPerInstance   The number of vertices in each instance (V
417934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                              in the above description).
418934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     * @param indicesPerInstance    The number of indices in each instance (I
419934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     *                              in the above description).
420d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * @param devBounds    optional bounds hint. This is a promise from the caller,
421d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     *                     not a request for clipping.
422934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com     */
423d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    void drawIndexedInstances(GrPrimitiveType type,
424d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              int instanceCount,
425d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              int verticesPerInstance,
426d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              int indicesPerInstance,
427d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com                              const SkRect* devBounds = NULL);
428934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com
429934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com    /**
430d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Clear the current render target if one isn't passed in. Ignores the
431d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * clip and all other draw state (blend mode, stages, etc). Clears the
43256ce48ade325f6f49acb0da31d6252806e4ed7efrobertphillips@google.com     * whole thing if rect is NULL, otherwise just the rect. If canIgnoreRect
43356ce48ade325f6f49acb0da31d6252806e4ed7efrobertphillips@google.com     * is set then the entire render target can be optionally cleared.
4340b335c1ac100aeacf79a4c98a052286fd46661e7bsalomon@google.com     */
435fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    virtual void clear(const SkIRect* rect,
436c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com                       GrColor color,
43756ce48ade325f6f49acb0da31d6252806e4ed7efrobertphillips@google.com                       bool canIgnoreRect,
438c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com                       GrRenderTarget* renderTarget = NULL) = 0;
439a9493a3c78f6eb82635ece960b4f16918b885428skia.committer@gmail.com
440e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    /**
44128361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org     * Discards the contents render target. NULL indicates that the current render target should
44228361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org     * be discarded.
44328361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org     **/
44428361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    virtual void discard(GrRenderTarget* = NULL) = 0;
44528361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org
44628361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    /**
4472a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org     * Called at start and end of gpu trace marking
4482a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org     * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
4492a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org     * and end of a code block respectively
450a3baf3be0e2a3128fb73bd41d40d130f75a4dc86commit-bot@chromium.org     */
4513eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel    void addGpuTraceMarker(const GrGpuTraceMarker* marker);
4523eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel    void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
4533eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel
4543eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel    /**
4553eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     * Takes the current active set of markers and stores them for later use. Any current marker
4563eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     * in the active set is removed from the active set and the targets remove function is called.
4573eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     * These functions do not work as a stack so you cannot call save a second time before calling
4583eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     * restore. Also, it is assumed that when restore is called the current active set of markers
4593eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     * is empty. When the stored markers are added back into the active set, the targets add marker
4603eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     * is called.
4613eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel     */
4623eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel    void saveActiveTraceMarkers();
4633eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel    void restoreActiveTraceMarkers();
464a3baf3be0e2a3128fb73bd41d40d130f75a4dc86commit-bot@chromium.org
465a3baf3be0e2a3128fb73bd41d40d130f75a4dc86commit-bot@chromium.org    /**
466e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * Copies a pixel rectangle from one surface to another. This call may finalize
467e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * reserved vertex/index data (as though a draw call was made). The src pixels
468e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * copied are specified by srcRect. They are copied to a rect of the same
469e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * size in dst with top left at dstPoint. If the src rect is clipped by the
470e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * src bounds then  pixel values in the dst rect corresponding to area clipped
471e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * by the src rect are not overwritten. This method can fail and return false
472e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * depending on the type of surface, configs, etc, and the backend-specific
473e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * limitations. If rect is clipped out entirely by the src or dst bounds then
474e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     * true is returned since there is no actual copy necessary to succeed.
475e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com     */
476e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    bool copySurface(GrSurface* dst,
477e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com                     GrSurface* src,
478e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com                     const SkIRect& srcRect,
479e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com                     const SkIPoint& dstPoint);
480116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com    /**
481116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com     * Function that determines whether a copySurface call would succeed without
482116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com     * performing the copy.
483116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com     */
484116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com    bool canCopySurface(GrSurface* dst,
485116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com                        GrSurface* src,
486116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com                        const SkIRect& srcRect,
487116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com                        const SkIPoint& dstPoint);
4880b335c1ac100aeacf79a4c98a052286fd46661e7bsalomon@google.com
489ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com    /**
490eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com     * This is can be called before allocating a texture to be a dst for copySurface. It will
491eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com     * populate the origin, config, and flags fields of the desc such that copySurface is more
492eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com     * likely to succeed and be efficient.
493eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com     */
494eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com    virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc);
495eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com
496eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com
497eb85117c05471e1a55ce387cbc38279f857a4584bsalomon@google.com    /**
498ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com     * Release any resources that are cached but not currently in use. This
499ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com     * is intended to give an application some recourse when resources are low.
500ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com     */
501ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com    virtual void purgeResources() {};
502ff17584e1c15f68ccc296be517e8a6776a9ddabdrobertphillips@google.com
503d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    /**
504d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * For subclass internal use to invoke a call to onDraw(). See DrawInfo below.
505d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     */
506d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    void executeDraw(const DrawInfo& info) { this->onDraw(info); }
507d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
508c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org    /**
50932184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org     * For subclass internal use to invoke a call to onDrawPath().
510c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org     */
51132184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org    void executeDrawPath(const GrPath* path, SkPath::FillType fill,
51232184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org                         const GrDeviceCoordTexture* dstCopy) {
51332184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org        this->onDrawPath(path, fill, dstCopy);
514c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org    }
515c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org
5169b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org    /**
5179b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     * For subclass internal use to invoke a call to onDrawPaths().
5189b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org     */
519ecc45365b6429ee4cbe1748dc6667b862d5f0f71commit-bot@chromium.org    void executeDrawPaths(int pathCount, const GrPath** paths,
5209b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                          const SkMatrix* transforms, SkPath::FillType fill,
5219b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                          SkStrokeRec::Style stroke,
5229b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                          const GrDeviceCoordTexture* dstCopy) {
5239b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org        this->onDrawPaths(pathCount, paths, transforms, fill, stroke, dstCopy);
5249b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org    }
525db35dab49e0e6bf16ccad5052bf6e6826daad27dskia.committer@gmail.com
5262a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    inline bool isGpuTracingEnabled() const {
5272a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org        return this->getContext()->isGpuTracingEnabled();
5282a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    }
5299b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org
53025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
531ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
532873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    /**
533873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * See AutoStateRestore below.
534873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     */
535873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    enum ASRInit {
536873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        kPreserve_ASRInit,
537873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        kReset_ASRInit
538873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    };
539873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com
540873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com    /**
541873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * Saves off the current state and restores it in the destructor. It will
542873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * install a new GrDrawState object on the target (setDrawState) and restore
543873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * the previous one in the destructor. The caller should call drawState() to
544873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * get the new draw state after the ASR is installed.
545873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *
546873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * GrDrawState* state = target->drawState();
547873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
548873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     * state->setRenderTarget(rt); // state refers to the GrDrawState set on
549873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // target before asr was initialized.
550873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // Therefore, rt is set on the GrDrawState
551873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // that will be restored after asr's
552873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     *                             // destructor rather than target's current
553d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *                             // GrDrawState.
554873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com     */
555a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoStateRestore : public ::SkNoncopyable {
556ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    public:
557873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        /**
558873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * Default ASR will have no effect unless set() is subsequently called.
559873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         */
56006afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com        AutoStateRestore();
561873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com
562873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com        /**
563873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * Saves the state on target. The state will be restored when the ASR
564873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * is destroyed. If this constructor is used do not call set().
565873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *
566873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * @param init  Should the newly installed GrDrawState be a copy of the
567873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *              previous state or a default-initialized GrDrawState.
568137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
569137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         *                   matrix will be preconcat'ed with the param. All stages will be
570137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com                             updated to compensate for the matrix change. If init == kReset
571137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com                             then the draw state's matrix will be this matrix.
572873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         */
573137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
574873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com
575ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        ~AutoStateRestore();
576ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
57706afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com        /**
578873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * Saves the state on target. The state will be restored when the ASR
579873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * is destroyed. This should only be called once per ASR object and only
580873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * when the default constructor was used. For nested saves use multiple
581873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * ASR objects.
582873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *
583873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         * @param init  Should the newly installed GrDrawState be a copy of the
584873ea0c93f202600ec2591bc1e2e5d7a1e05f59dbsalomon@google.com         *              previous state or a default-initialized GrDrawState.
585137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's
586137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         *                   matrix will be preconcat'ed with the param. All stages will be
587137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com                             updated to compensate for the matrix change. If init == kReset
588137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com                             then the draw state's matrix will be this matrix.
58906afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com         */
590137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL);
591137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com
592137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        /**
593137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         * Like set() but makes the view matrix identity. When init is kReset it is as though
594137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though
595137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix
596137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         * is not invertible then this may fail.
597137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com         */
598137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        bool setIdentity(GrDrawTarget* target, ASRInit init);
59906afe7b5a1ef03bfc6494c51ab2a1f7a386de5c2bsalomon@google.com
600ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    private:
601137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        GrDrawTarget*                       fDrawTarget;
602137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        SkTLazy<GrDrawState>                fTempState;
603137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        GrDrawState*                        fSavedState;
604ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
605ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
60625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
6071c13c9668a889e56a0c85b51b9f28139c25b76ffbsalomon@google.com
608a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoReleaseGeometry : public ::SkNoncopyable {
609ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    public:
610ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        AutoReleaseGeometry(GrDrawTarget*  target,
61125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                            int            vertexCount,
61225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                            int            indexCount);
61325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        AutoReleaseGeometry();
61425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        ~AutoReleaseGeometry();
6155782d712ffc31557d0cb12d5a220cebb783f6895bsalomon@google.com        bool set(GrDrawTarget*  target,
61625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                 int            vertexCount,
61725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com                 int            indexCount);
618a47a48dca5045d71cbc5de343404045209a13e15bsalomon@google.com        bool succeeded() const { return NULL != fTarget; }
619f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        void* vertices() const { SkASSERT(this->succeeded()); return fVertices; }
620f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        void* indices() const { SkASSERT(this->succeeded()); return fIndices; }
621972f9cd7a063d0544f8c919fd12b9a3adbd12b24commit-bot@chromium.org        SkPoint* positions() const {
622972f9cd7a063d0544f8c919fd12b9a3adbd12b24commit-bot@chromium.org            return static_cast<SkPoint*>(this->vertices());
623ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
624ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
625ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    private:
62625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        void reset();
627d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
628ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        GrDrawTarget* fTarget;
629ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        void*         fVertices;
630ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        void*         fIndices;
631ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
632ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
63325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
634ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
635a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoClipRestore : public ::SkNoncopyable {
636ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    public:
637ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        AutoClipRestore(GrDrawTarget* target) {
638ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com            fTarget = target;
639ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com            fClip = fTarget->getClip();
640ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
641ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
6428d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
6438d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com
644ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        ~AutoClipRestore() {
645ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com            fTarget->setClip(fClip);
646ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
647ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    private:
6488d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        GrDrawTarget*           fTarget;
6498d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        const GrClipData*       fClip;
6508d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        SkTLazy<SkClipStack>    fStack;
6518d67c0711b52226db59158a0cfce09f35badd96absalomon@google.com        GrClipData              fReplacementClip;
652ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
653d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
65425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    ////////////////////////////////////////////////////////////////////////////
655d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
6560406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    /**
6570406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     * Saves the geometry src state at construction and restores in the destructor. It also saves
6580406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     * and then restores the vertex attrib state.
6590406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     */
660a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoGeometryPush : public ::SkNoncopyable {
6610406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    public:
6620406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        AutoGeometryPush(GrDrawTarget* target)
6630406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com            : fAttribRestore(target->drawState()) {
664f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(NULL != target);
6650406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com            fTarget = target;
6660406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com            target->pushGeometrySource();
6670406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        }
6680406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com
6690406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        ~AutoGeometryPush() { fTarget->popGeometrySource(); }
6700406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com
6710406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    private:
6720406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        GrDrawTarget*                           fTarget;
6730406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        GrDrawState::AutoVertexAttribRestore    fAttribRestore;
6740406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    };
6750406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com
6760406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    /**
6770406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default
6780406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     * state regardless of ASRInit value.
6790406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com     */
680a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoGeometryAndStatePush : public ::SkNoncopyable {
68125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    public:
682137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com        AutoGeometryAndStatePush(GrDrawTarget* target,
683137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com                                 ASRInit init,
684137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com                                 const SkMatrix* viewMatrix = NULL)
685137f1347abaf0bb6a945e91c2f6cb49f0ee69bc3bsalomon@google.com            : fState(target, init, viewMatrix) {
686f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(NULL != target);
68725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            fTarget = target;
68825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            target->pushGeometrySource();
6890406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com            if (kPreserve_ASRInit == init) {
6900406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com                target->drawState()->setDefaultVertexAttribs();
6910406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com            }
69225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        }
6930406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com
6940406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); }
6950406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com
69625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    private:
697b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        AutoStateRestore fState;
6980406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com        GrDrawTarget*    fTarget;
69925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    };
700ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
701a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    ///////////////////////////////////////////////////////////////////////////
702a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    // Draw execution tracking (for font atlases and other resources)
703a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    class DrawToken {
704a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    public:
705a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org        DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) :
706a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org                  fDrawTarget(drawTarget), fDrawID(drawID) {}
7077475811143e190e172bf83d13c4bdba85704b604skia.committer@gmail.com
708a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org        bool isIssued() { return NULL != fDrawTarget && fDrawTarget->isIssued(fDrawID); }
7097475811143e190e172bf83d13c4bdba85704b604skia.committer@gmail.com
710a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    private:
711a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org        GrDrawTarget*  fDrawTarget;
712a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org        uint32_t       fDrawID;   // this may wrap, but we're doing direct comparison
713a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org                                  // so that should be okay
714a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    };
7157475811143e190e172bf83d13c4bdba85704b604skia.committer@gmail.com
716a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); }
7177475811143e190e172bf83d13c4bdba85704b604skia.committer@gmail.com
718ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comprotected:
719471d471dcd7422e5dd9c822c1092b2ba4721dcfebsalomon@google.com
72025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    enum GeometrySrcType {
72125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kNone_GeometrySrcType,     //<! src has not been specified
72225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kReserved_GeometrySrcType, //<! src was set using reserve*Space
72325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kArray_GeometrySrcType,    //<! src was set using set*SourceToArray
72425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kBuffer_GeometrySrcType    //<! src was set using set*SourceToBuffer
72525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    };
726d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
72725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    struct GeometrySrcState {
72825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        GeometrySrcType         fVertexSrc;
72925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        union {
73025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is buffer
73125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            const GrVertexBuffer*   fVertexBuffer;
73225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is reserved or array
73325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            int                     fVertexCount;
73425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        };
735d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
73625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        GeometrySrcType         fIndexSrc;
73725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        union {
73825fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is buffer
73925fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            const GrIndexBuffer*    fIndexBuffer;
74025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            // valid if src type is reserved or array
74125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com            int                     fIndexCount;
74225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        };
743d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
744b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        size_t                  fVertexSize;
74525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    };
746934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com
747934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com    int indexCountInCurrentSource() const {
748934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com        const GeometrySrcState& src = this->getGeomSrc();
749934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com        switch (src.fIndexSrc) {
750934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kNone_GeometrySrcType:
751934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return 0;
752934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kReserved_GeometrySrcType:
753934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kArray_GeometrySrcType:
754934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return src.fIndexCount;
755934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            case kBuffer_GeometrySrcType:
756089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org                return static_cast<int>(src.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
757934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com            default:
75888cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org                SkFAIL("Unexpected Index Source.");
759934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com                return 0;
760934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com        }
761934c570297c1b1f99cb4ca8d012d468a7eddf73fbsalomon@google.com    }
762a47a48dca5045d71cbc5de343404045209a13e15bsalomon@google.com
763e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // This method is called by copySurface  The srcRect is guaranteed to be entirely within the
764e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
765e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // entirely within the dst. The default implementation will draw a rect from the src to the
766e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // dst if the src is a texture and the dst is a render target and fail otherwise.
767e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    virtual bool onCopySurface(GrSurface* dst,
768e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com                               GrSurface* src,
769e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com                               const SkIRect& srcRect,
770e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com                               const SkIPoint& dstPoint);
771e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com
772e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // Called to determine whether an onCopySurface call would succeed or not. This is useful for
773e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
774e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // classes must keep this consistent with their implementation of onCopySurface(). The inputs
775e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
776e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com    // and dst bounds.
777116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com    virtual bool onCanCopySurface(GrSurface* dst,
778116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com                                  GrSurface* src,
779116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com                                  const SkIRect& srcRect,
780116ad84d3126b0db22b2312ca59ed70e5c56f6fcbsalomon@google.com                                  const SkIPoint& dstPoint);
781e4617bf6d45cdde07f89e341ebf5c485916bf0b2bsalomon@google.com
7826e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrContext* getContext() { return fContext; }
7836e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    const GrContext* getContext() const { return fContext; }
7846e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com
78502ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // A subclass may override this function if it wishes to be notified when the clip is changed.
78602ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // The override should call INHERITED::clipWillBeSet().
78702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    virtual void clipWillBeSet(const GrClipData* clipData);
78802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
78902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // subclasses must call this in their destructors to ensure all vertex
79002ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // and index sources have been released (including those held by
79102ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // pushGeometrySource())
79202ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    void releaseGeometry();
79302ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
79402ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // accessors for derived classes
79502ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
796b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
797b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    size_t getVertexSize() const {
79802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com        // the vertex layout is only valid if a vertex source has been specified.
799f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
800b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com        return this->getGeomSrc().fVertexSize;
801a47a48dca5045d71cbc5de343404045209a13e15bsalomon@google.com    }
8025782d712ffc31557d0cb12d5a220cebb783f6895bsalomon@google.com
803bcce8926524827775539874346dd424a9510dbc9bsalomon@google.com    // Subclass must initialize this in its constructor.
804c26d94fd7dc0b00cd6d0e42d28285f4a38aff021bsalomon@google.com    SkAutoTUnref<const GrDrawTargetCaps> fCaps;
80502ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com
8062a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
8072a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
808d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com    /**
809d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     * Used to communicate draws to subclass's onDraw function.
810d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com     */
81174749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    class DrawInfo {
81274749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    public:
81374749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        DrawInfo(const DrawInfo& di) { (*this) = di; }
814d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        DrawInfo& operator =(const DrawInfo& di);
81574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
81674749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        GrPrimitiveType primitiveType() const { return fPrimitiveType; }
81774749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int startVertex() const { return fStartVertex; }
81874749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int startIndex() const { return fStartIndex; }
81974749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int vertexCount() const { return fVertexCount; }
82074749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        int indexCount() const { return fIndexCount; }
821d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int verticesPerInstance() const { return fVerticesPerInstance; }
822d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int indicesPerInstance() const { return fIndicesPerInstance; }
823d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        int instanceCount() const { return fInstanceCount; }
82474749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
82574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        bool isIndexed() const { return fIndexCount > 0; }
826515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG
827d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        bool isInstanced() const; // this version is longer because of asserts
828d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com#else
829d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        bool isInstanced() const { return fInstanceCount > 0; }
830d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com#endif
831d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
832d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        // adds or remove instances
833d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void adjustInstanceCount(int instanceOffset);
834d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        // shifts the start vertex
835d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void adjustStartVertex(int vertexOffset);
836d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        // shifts the start index
837d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void adjustStartIndex(int indexOffset);
838d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
839d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        void setDevBounds(const SkRect& bounds) {
840d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            fDevBoundsStorage = bounds;
841d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com            fDevBounds = &fDevBoundsStorage;
842d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        }
843d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        const SkRect* getDevBounds() const { return fDevBounds; }
84405a2ee052c9ef4c781b7b590b00b3d2da3b3449askia.committer@gmail.com
84526e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        // NULL if no copy of the dst is needed for the draw.
84626e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        const GrDeviceCoordTexture* getDstCopy() const {
84726e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com            if (NULL != fDstCopy.texture()) {
84826e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com                return &fDstCopy;
84926e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com            } else {
85026e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com                return NULL;
85126e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com            }
85226e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        }
85326e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com
85474749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    private:
855d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com        DrawInfo() { fDevBounds = NULL; }
856d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
85774749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com        friend class GrDrawTarget;
858d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
85926e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        GrPrimitiveType         fPrimitiveType;
86074749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
86126e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fStartVertex;
86226e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fStartIndex;
86326e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fVertexCount;
86426e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fIndexCount;
865d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
86626e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fInstanceCount;
86726e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fVerticesPerInstance;
86826e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        int                     fIndicesPerInstance;
869d62e88e5af39347a8fc2a5abdf5feb67d7ea256dbsalomon@google.com
87026e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        SkRect                  fDevBoundsStorage;
87126e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        SkRect*                 fDevBounds;
87226e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com
87326e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com        GrDeviceCoordTexture    fDstCopy;
87474749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    };
87574749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com
87602ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.comprivate:
87702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // A subclass can optionally overload this function to be notified before
87802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // vertex and index space is reserved.
879b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {}
88097805382d89b717de3355312a79a957ea4a864c9bsalomon@google.com
88125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // implemented by subclass to allocate space for reserved geom
882a63389843dd18003382d61c2e4610af09ed07d38jvanverth@google.com    virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0;
88325fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
88425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // implemented by subclass to handle release of reserved geom space
88525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseReservedVertexSpace() = 0;
88625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseReservedIndexSpace() = 0;
88725fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // subclass must consume array contents when set
88802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0;
88902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0;
89025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // subclass is notified that geom source will be set away from an array
89125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseVertexArray() = 0;
89225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void releaseIndexArray() = 0;
89302ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    // subclass overrides to be notified just before geo src state is pushed/popped.
89425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void geometrySourceWillPush() = 0;
89525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
89625fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // subclass called to perform drawing
89774749cd45c29b4f5300e2518f2c2c765ce8ae208bsalomon@google.com    virtual void onDraw(const DrawInfo&) = 0;
8980406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    // Implementation of drawRect. The geometry src and vertex attribs will already
8990406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    // be saved before this is called and restored afterwards. A subclass may override
9000406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    // this to perform more optimal rect rendering. Its draws should be funneled through
9010406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed,
9020406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    // drawIndexedInstances, ...). The base class draws a two triangle fan using
9030406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com    // drawNonIndexed from reserved vertex space.
904fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    virtual void onDrawRect(const SkRect& rect,
9050406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com                            const SkMatrix* matrix,
906fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                            const SkRect* localRect,
9070406b9e1faee06c6ecb2732a1bcf3b0e53104e07bsalomon@google.com                            const SkMatrix* localMatrix);
90832184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org
90932184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org    virtual void onStencilPath(const GrPath*, SkPath::FillType) = 0;
91032184d81629e39809bb9e915286d8fe971a8ed68commit-bot@chromium.org    virtual void onDrawPath(const GrPath*, SkPath::FillType,
911c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org                            const GrDeviceCoordTexture* dstCopy) = 0;
912ecc45365b6429ee4cbe1748dc6667b862d5f0f71commit-bot@chromium.org    virtual void onDrawPaths(int, const GrPath**, const SkMatrix*,
9139b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                             SkPath::FillType, SkStrokeRec::Style,
9149b62aa156bcf1db6f11af9302bf8bb8ef2567142commit-bot@chromium.org                             const GrDeviceCoordTexture* dstCopy) = 0;
91564aef2bacd1f5c25ffd9347aabd6265c9b60c0f4bsalomon@google.com
9162a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    virtual void didAddGpuTraceMarker() = 0;
9172a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    virtual void didRemoveGpuTraceMarker() = 0;
918a3baf3be0e2a3128fb73bd41d40d130f75a4dc86commit-bot@chromium.org
919e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com    // helpers for reserving vertex and index space.
920b75b0a0b8492e14c7728e0a0881f87dc64ce60f9jvanverth@google.com    bool reserveVertexSpace(size_t vertexSize,
921e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com                            int vertexCount,
922e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com                            void** vertices);
923e3d7095c2374a423815e662020459832f389a40fbsalomon@google.com    bool reserveIndexSpace(int indexCount, void** indices);
924d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
925e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com    // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
926e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com    // indicate non-indexed drawing.
927e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com    bool checkDraw(GrPrimitiveType type, int startVertex,
928e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com                   int startIndex, int vertexCount,
929e826262939dac3e67cb5ce66d4b9faeb8e2538e6bsalomon@google.com                   int indexCount) const;
93025fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    // called when setting a new vert/idx source to unref prev vb/ib
93125fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void releasePreviousVertexSource();
93225fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    void releasePreviousIndexSource();
933d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
93426e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com    // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
93526e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com    // but couldn't be made. Otherwise, returns true.
936c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org    bool setupDstReadIfNecessary(DrawInfo* info) {
937c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org        return this->setupDstReadIfNecessary(&info->fDstCopy, info->getDevBounds());
938c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org    }
939c4dc0ad8e252a7e30d19b47d3d0d9f2c69faf854commit-bot@chromium.org    bool setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds);
94026e18b593ab65e4d92dfbce92579d8bc180d4c2cbsalomon@google.com
941a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    // Check to see if this set of draw commands has been sent out
942a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    virtual bool       isIssued(uint32_t drawID) { return true; }
9437475811143e190e172bf83d13c4bdba85704b604skia.committer@gmail.com
94425fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com    enum {
94525fb21f5df904c6f111bbf8f07e6a6c339416d09bsalomon@google.com        kPreallocGeoSrcStateStackCnt = 4,
946ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    };
94702ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack;
94802ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    const GrClipData*                                               fClip;
94902ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    GrDrawState*                                                    fDrawState;
95002ddc8b85ace91b15feb329a6a1d5d62b2b846c6bsalomon@google.com    GrDrawState                                                     fDefaultDrawState;
9516e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
9526e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrContext*                                                      fContext;
9532a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    // To keep track that we always have at least as many debug marker adds as removes
9542a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    int                                                             fGpuTraceMarkerCount;
9552a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    GrTraceMarkerSet                                                fActiveTraceMarkers;
9563eee3834fba1d2e29dfb61adcb97e4624b6a3ac8egdaniel    GrTraceMarkerSet                                                fStoredTraceMarkers;
957fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com
958a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org    typedef SkRefCnt INHERITED;
959ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com};
960ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
961ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#endif
962