blob: 6e5bc6764a8380bb1c366a0938776d110cc99f26 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrDrawTarget_DEFINED
12#define GrDrawTarget_DEFINED
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrClip.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000015#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000016#include "GrIndexBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000017#include "GrMatrix.h"
18#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
Scroggo97c88c22011-05-11 14:05:25 +000020#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000021#include "SkTLazy.h"
Scroggo97c88c22011-05-11 14:05:25 +000022
reed@google.comac10a2d2010-12-22 21:39:39 +000023class GrClipIterator;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000024class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27class GrDrawTarget : public GrRefCnt {
28public:
29 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000030 * Represents the draw target capabilities.
31 */
32 struct Caps {
33 Caps() { memset(this, 0, sizeof(Caps)); }
34 Caps(const Caps& c) { *this = c; }
35 Caps& operator= (const Caps& c) {
36 memcpy(this, &c, sizeof(Caps));
37 return *this;
38 }
39 void print() const;
40 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000041 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000042 bool fTwoSidedStencilSupport : 1;
43 bool fStencilWrapOpsSupport : 1;
44 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000045 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000046 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000047 bool fFSAASupport : 1;
48 bool fDualSourceBlendingSupport : 1;
49 bool fBufferLockSupport : 1;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000050 bool fPathStencilingSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000051 int fMaxRenderTargetSize;
52 int fMaxTextureSize;
53 };
54
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000055 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000056 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000057
reed@google.comac10a2d2010-12-22 21:39:39 +000058 ///////////////////////////////////////////////////////////////////////////
59
60 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000061 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000062
63 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000064 * Gets the capabilities of the draw target.
65 */
66 const Caps& getCaps() const { return fCaps; }
67
68 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000069 * Sets the current clip to the region specified by clip. All draws will be
70 * clipped against this clip if kClip_StateBit is enabled.
71 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000072 * Setting the clip may (or may not) zero out the client's stencil bits.
73 *
reed@google.comac10a2d2010-12-22 21:39:39 +000074 * @param description of the clipping region
75 */
76 void setClip(const GrClip& clip);
77
78 /**
79 * Gets the current clip.
80 *
81 * @return the clip.
82 */
83 const GrClip& getClip() const;
84
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000085 /**
86 * Sets the draw state object for the draw target. Note that this does not
87 * make a copy. The GrDrawTarget will take a reference to passed object.
88 * Passing NULL will cause the GrDrawTarget to use its own internal draw
89 * state object rather than an externally provided one.
90 */
91 void setDrawState(GrDrawState* drawState);
92
93 /**
94 * Read-only access to the GrDrawTarget's current draw state.
95 */
96 const GrDrawState& getDrawState() const { return *fDrawState; }
97
98 /**
99 * Read-write access to the GrDrawTarget's current draw state. Note that
100 * this doesn't ref.
101 */
102 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000103
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000104 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000105 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
106 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000107 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000108 * @param matrix the matrix to concat
109 */
110 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
111 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000112 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000113 }
114
115 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000116 * Color alpha and coverage are two inputs to the drawing pipeline. For some
117 * blend modes it is safe to fold the coverage into constant or per-vertex
118 * color alpha value. For other blend modes they must be handled separately.
119 * Depending on features available in the underlying 3D API this may or may
120 * not be possible.
121 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000122 * This function considers the current draw state and the draw target's
123 * capabilities to determine whether coverage can be handled correctly. The
124 * following assumptions are made:
125 * 1. The caller intends to somehow specify coverage. This can be
126 * specified either by enabling a coverage stage on the GrDrawState or
127 * via the vertex layout.
128 * 2. Other than enabling coverage stages, the current configuration of
129 * the target's GrDrawState is as it will be at draw time.
130 * 3. If a vertex source has not yet been specified then all stages with
131 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000132 */
133 bool canApplyCoverage() const;
134
135 /**
136 * Determines whether incorporating partial pixel coverage into the constant
137 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000138 * blending result. If a vertex source has not yet been specified then
139 * the function assumes that all stages with non-NULL textures will be
140 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000141 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000142 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000143
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000144 /**
bsalomon@google.come79c8152012-03-29 19:07:12 +0000145 * Given the current draw state and hw support, will HW AA lines be used
146 * (if line primitive type is drawn)? If a vertex source has not yet been
147 * specified then the function assumes that all stages with non-NULL
148 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000149 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000150 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000151
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000152 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000153 * The format of vertices is represented as a bitfield of flags.
154 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000155 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
156 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
157 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000158 * use any of the texture coordinates as its input texture coordinates or it
159 * may use the positions as texture coordinates.
160 *
161 * If no texture coordinates are specified for a stage then the stage is
162 * disabled.
163 *
164 * Only one type of texture coord can be specified per stage. For
165 * example StageTexCoordVertexLayoutBit(0, 2) and
166 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
167 *
168 * The order in memory is always (position, texture coord 0, ..., color,
169 * coverage) with any unused fields omitted. Note that this means that if
170 * only texture coordinates 1 is referenced then there is no texture
171 * coordinates 0 and the order would be (position, texture coordinate 1
172 * [, color][, coverage]).
173 */
174
175 /**
176 * Generates a bit indicating that a texture stage uses texture coordinates
177 *
178 * @param stage the stage that will use texture coordinates.
179 * @param texCoordIdx the index of the texture coordinates to use
180 *
181 * @return the bit to add to a GrVertexLayout bitfield.
182 */
183 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000184 GrAssert(stage < GrDrawState::kNumStages);
185 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
186 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000187 }
188
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000189 virtual void postClipPush() {};
190 virtual void preClipPop() {};
191
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000192private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000193 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
194 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000195
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000196public:
197 /**
198 * Generates a bit indicating that a texture stage uses the position
199 * as its texture coordinate.
200 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000201 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000202 * coordinates.
203 *
204 * @return the bit to add to a GrVertexLayout bitfield.
205 */
206 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000207 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000208 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000209 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000210
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000211 /**
212 * Modify the existing vertex layout. Realistically the only thing that
213 * can be added w/o recomputing the vertex layout is one of the
214 * StagePosAsTexCoordVertexLayoutBit flags
215 */
216 void addToVertexLayout(int flag) {
217 GrAssert((1 << TEX_COORD_BIT_CNT) == flag ||
218 (1 << (TEX_COORD_BIT_CNT + 1)) == flag ||
219 (1 << (TEX_COORD_BIT_CNT + 2)) == flag ||
220 (1 << (TEX_COORD_BIT_CNT + 3)) == flag);
221 fGeoSrcStateStack.back().fVertexLayout |= flag;
222 }
223
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000224private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000225 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
226 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000227
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000228public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000229
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000230 /**
231 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 */
233 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000234 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000235 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000236 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000237 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000238 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000240 * text [GrGpuTextVertex vs GrPoint].)
241 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000242 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000244 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000245 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000246 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000247 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000249 kDummyVertexLayoutBit,
250 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000251 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000252 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000253 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000254
255 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000256 * There are three types of "sources" of geometry (vertices and indices) for
257 * draw calls made on the target. When performing an indexed draw, the
258 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000259 * specified it can be used for multiple draws. However, the time at which
260 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000261 *
262 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000263 * already specified geometry that it isn't finished with. So there are push
264 * and pop methods. This allows the client to push the sources, draw
265 * something using alternate sources, and then pop to restore the original
266 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000267 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000268 * Aside from pushes and pops, a source remains valid until another source
269 * is set or resetVertexSource / resetIndexSource is called. Drawing from
270 * a reset source is an error.
271 *
272 * The three types of sources are:
273 *
274 * 1. A cpu array (set*SourceToArray). This is useful when the caller
275 * already provided vertex data in a format compatible with a
276 * GrVertexLayout. The data in the array is consumed at the time that
277 * set*SourceToArray is called and subsequent edits to the array will not
278 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000279 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000280 * 2. Reserve. This is most useful when the caller has data it must
281 * transform before drawing and is not long-lived. The caller requests
282 * that the draw target make room for some amount of vertex and/or index
283 * data. The target provides ptrs to hold the vertex and/or index data.
284 *
285 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000286 * drawIndexedInstances, or pushGeometrySource. At this point the data is
287 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000288 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000289 * Where the space is allocated and how it is uploaded to the GPU is
290 * subclass-dependent.
291 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000292 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000293 * is long-lived. When the data in the buffer is consumed depends on the
294 * GrDrawTarget subclass. For deferred subclasses the caller has to
295 * guarantee that the data is still available in the buffers at playback.
296 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000297 */
298
299 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000300 * Reserves space for vertices and/or indices. Zero can be specifed as
301 * either the vertex or index count if the caller desires to only reserve
302 * space for only indices or only vertices. If zero is specifed for
303 * vertexCount then the vertex source will be unmodified and likewise for
304 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000306 * If the function returns true then the reserve suceeded and the vertices
307 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000308 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000309 * If the target cannot make space for the request then this function will
310 * return false. If vertexCount was non-zero then upon failure the vertex
311 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000313 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000314 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
315 * popGeomtrySource is called. At that point logically a snapshot of the
316 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000317 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000319 * @param vertexCount the number of vertices to reserve space for. Can be
320 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000321 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000322 * @param vertices will point to reserved vertex space if vertexCount is
323 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000324 * @param indices will point to reserved index space if indexCount is
325 * non-zero. Illegal to pass NULL if indexCount > 0.
326 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000327 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
328 int vertexCount,
329 int indexCount,
330 void** vertices,
331 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000332
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 /**
334 * Provides hints to caller about the number of vertices and indices
335 * that can be allocated cheaply. This can be useful if caller is reserving
336 * space but doesn't know exactly how much geometry is needed.
337 *
338 * Also may hint whether the draw target should be flushed first. This is
339 * useful for deferred targets.
340 *
341 * @param vertexLayout layout of vertices caller would like to reserve
342 * @param vertexCount in: hint about how many vertices the caller would
343 * like to allocate.
344 * out: a hint about the number of vertices that can be
345 * allocated cheaply. Negative means no hint.
346 * Ignored if NULL.
347 * @param indexCount in: hint about how many indices the caller would
348 * like to allocate.
349 * out: a hint about the number of indices that can be
350 * allocated cheaply. Negative means no hint.
351 * Ignored if NULL.
352 *
353 * @return true if target should be flushed based on the input values.
354 */
355 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000356 int* vertexCount,
357 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000358
359 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000360 * Sets source of vertex data for the next draw. Array must contain
361 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 *
363 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000364 * @param size size of the vertex data.
365 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000367 void setVertexSourceToArray(GrVertexLayout vertexLayout,
368 const void* vertexArray,
369 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000370
371 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000372 * Sets source of index data for the next indexed draw. Array must contain
373 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000374 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000375 * @param array cpu array containing index data.
376 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000378 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
380 /**
381 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000382 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000383 *
384 * @param buffer vertex buffer containing vertex data. Must be
385 * unlocked before draw call.
386 * @param vertexLayout layout of the vertex data in the buffer.
387 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000388 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
389 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000390
391 /**
392 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000393 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 *
395 * @param buffer index buffer containing indices. Must be unlocked
396 * before indexed draw call.
397 */
398 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000399
400 /**
401 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
402 * source to reserved, array, or buffer before next draw. May be able to free
403 * up temporary storage allocated by setVertexSourceToArray or
404 * reserveVertexSpace.
405 */
406 void resetVertexSource();
407
408 /**
409 * Resets index source. Indexed Drawing from reset indices is illegal. Set
410 * index source to reserved, array, or buffer before next indexed draw. May
411 * be able to free up temporary storage allocated by setIndexSourceToArray
412 * or reserveIndexSpace.
413 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000414 void resetIndexSource();
415
416 /**
417 * Query to find out if the vertex or index source is reserved.
418 */
419 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000420 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000421 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
422 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
424 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000425 * Pushes and resets the vertex/index sources. Any reserved vertex / index
426 * data is finalized (i.e. cannot be updated after the matching pop but can
427 * be drawn from). Must be balanced by a pop.
428 */
429 void pushGeometrySource();
430
431 /**
432 * Pops the vertex / index sources from the matching push.
433 */
434 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000435
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000436 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 * Draws indexed geometry using the current state and current vertex / index
438 * sources.
439 *
440 * @param type The type of primitives to draw.
441 * @param startVertex the vertex in the vertex array/buffer corresponding
442 * to index 0
443 * @param startIndex first index to read from index src.
444 * @param vertexCount one greater than the max index.
445 * @param indexCount the number of index elements to read. The index count
446 * is effectively trimmed to the last completely
447 * specified primitive.
448 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000449 void drawIndexed(GrPrimitiveType type,
450 int startVertex,
451 int startIndex,
452 int vertexCount,
453 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
455 /**
456 * Draws non-indexed geometry using the current state and current vertex
457 * sources.
458 *
459 * @param type The type of primitives to draw.
460 * @param startVertex the vertex in the vertex array/buffer corresponding
461 * to index 0
462 * @param vertexCount one greater than the max index.
463 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000464 void drawNonIndexed(GrPrimitiveType type,
465 int startVertex,
466 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000468 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000469 * Draws path into the stencil buffer. The fill must be either even/odd or
470 * winding (not inverse or hairline). It will respect the HW antialias flag
471 * on the draw state (if possible in the 3D API).
472 */
473 void stencilPath(const GrPath& path, GrPathFill fill);
474
475 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000476 * Helper function for drawing rects. This does not use the current index
477 * and vertex sources. After returning, the vertex and index sources may
478 * have changed. They should be reestablished before the next drawIndexed
479 * or drawNonIndexed. This cannot be called between reserving and releasing
480 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000481 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000482 * drawNonIndexed.
483 * @param rect the rect to draw
484 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000485 * @param stageMask bitmask indicating which stages are enabled.
486 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000487 * @param srcRects specifies rects for stages enabled by stageEnableMask.
488 * if stageEnableMask bit i is 1, srcRects is not NULL,
489 * and srcRects[i] is not NULL, then srcRects[i] will be
490 * used as coordinates for stage i. Otherwise, if stage i
491 * is enabled then rect is used as the coordinates.
492 * @param srcMatrices optional matrices applied to srcRects. If
493 * srcRect[i] is non-NULL and srcMatrices[i] is
494 * non-NULL then srcRect[i] will be transformed by
495 * srcMatrix[i]. srcMatrices can be NULL when no
496 * srcMatrices are desired.
497 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000498 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000499 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000500 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000501 const GrRect* srcRects[],
502 const GrMatrix* srcMatrices[]);
503
504 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000505 * This call is used to draw multiple instances of some geometry with a
506 * given number of vertices (V) and indices (I) per-instance. The indices in
507 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
508 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
509 * concrete example, the following index buffer for drawing a series of
510 * quads each as two triangles each satisfies these conditions with V=4 and
511 * I=6:
512 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
513 *
514 * The call assumes that the pattern of indices fills the entire index
515 * source. The size of the index buffer limits the number of instances that
516 * can be drawn by the GPU in a single draw. However, the caller may specify
517 * any (positive) number for instanceCount and if necessary multiple GPU
518 * draws will be issued. Morever, when drawIndexedInstances is called
519 * multiple times it may be possible for GrDrawTarget to group them into a
520 * single GPU draw.
521 *
522 * @param type the type of primitives to draw
523 * @param instanceCount the number of instances to draw. Each instance
524 * consists of verticesPerInstance vertices indexed by
525 * indicesPerInstance indices drawn as the primitive
526 * type specified by type.
527 * @param verticesPerInstance The number of vertices in each instance (V
528 * in the above description).
529 * @param indicesPerInstance The number of indices in each instance (I
530 * in the above description).
531 */
532 virtual void drawIndexedInstances(GrPrimitiveType type,
533 int instanceCount,
534 int verticesPerInstance,
535 int indicesPerInstance);
536
537 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000538 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000539 * matrices.
540 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000541 void drawSimpleRect(const GrRect& rect,
542 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000543 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000544 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000545 }
546
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000547 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000548 * Clear the render target. Ignores the clip and all other draw state
549 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
550 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000551 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000552 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000553
robertphillips@google.comff175842012-05-14 19:31:39 +0000554 /**
555 * Release any resources that are cached but not currently in use. This
556 * is intended to give an application some recourse when resources are low.
557 */
558 virtual void purgeResources() {};
559
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000560 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000561
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000562 /**
563 * See AutoStateRestore below.
564 */
565 enum ASRInit {
566 kPreserve_ASRInit,
567 kReset_ASRInit
568 };
569
570 /**
571 * Saves off the current state and restores it in the destructor. It will
572 * install a new GrDrawState object on the target (setDrawState) and restore
573 * the previous one in the destructor. The caller should call drawState() to
574 * get the new draw state after the ASR is installed.
575 *
576 * GrDrawState* state = target->drawState();
577 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
578 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
579 * // target before asr was initialized.
580 * // Therefore, rt is set on the GrDrawState
581 * // that will be restored after asr's
582 * // destructor rather than target's current
583 * // GrDrawState.
584 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 class AutoStateRestore : ::GrNoncopyable {
586 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000587 /**
588 * Default ASR will have no effect unless set() is subsequently called.
589 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000590 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000591
592 /**
593 * Saves the state on target. The state will be restored when the ASR
594 * is destroyed. If this constructor is used do not call set().
595 *
596 * @param init Should the newly installed GrDrawState be a copy of the
597 * previous state or a default-initialized GrDrawState.
598 */
599 AutoStateRestore(GrDrawTarget* target, ASRInit init);
600
reed@google.comac10a2d2010-12-22 21:39:39 +0000601 ~AutoStateRestore();
602
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000603 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000604 * Saves the state on target. The state will be restored when the ASR
605 * is destroyed. This should only be called once per ASR object and only
606 * when the default constructor was used. For nested saves use multiple
607 * ASR objects.
608 *
609 * @param init Should the newly installed GrDrawState be a copy of the
610 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000611 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000612 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000613
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000615 GrDrawTarget* fDrawTarget;
616 SkTLazy<GrDrawState> fTempState;
617 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 };
619
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000620 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000621
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000622 /**
623 * Sets the view matrix to I and preconcats all stage matrices enabled in
624 * mask by the view inverse. Destructor undoes these changes.
625 */
626 class AutoDeviceCoordDraw : ::GrNoncopyable {
627 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000628 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000629 ~AutoDeviceCoordDraw();
630 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000631 GrDrawTarget* fDrawTarget;
632 GrMatrix fViewMatrix;
633 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
634 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000635 };
636
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000638
reed@google.comac10a2d2010-12-22 21:39:39 +0000639 class AutoReleaseGeometry : ::GrNoncopyable {
640 public:
641 AutoReleaseGeometry(GrDrawTarget* target,
642 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643 int vertexCount,
644 int indexCount);
645 AutoReleaseGeometry();
646 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000647 bool set(GrDrawTarget* target,
648 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000649 int vertexCount,
650 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000651 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000652 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
653 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000654 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000655 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 }
657
658 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000659 void reset();
660
reed@google.comac10a2d2010-12-22 21:39:39 +0000661 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000662 void* fVertices;
663 void* fIndices;
664 };
665
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000666 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000667
668 class AutoClipRestore : ::GrNoncopyable {
669 public:
670 AutoClipRestore(GrDrawTarget* target) {
671 fTarget = target;
672 fClip = fTarget->getClip();
673 }
674
675 ~AutoClipRestore() {
676 fTarget->setClip(fClip);
677 }
678 private:
679 GrDrawTarget* fTarget;
680 GrClip fClip;
681 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682
683 ////////////////////////////////////////////////////////////////////////////
684
685 class AutoGeometryPush : ::GrNoncopyable {
686 public:
687 AutoGeometryPush(GrDrawTarget* target) {
688 GrAssert(NULL != target);
689 fTarget = target;
690 target->pushGeometrySource();
691 }
692 ~AutoGeometryPush() {
693 fTarget->popGeometrySource();
694 }
695 private:
696 GrDrawTarget* fTarget;
697 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000698
699 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000700 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000701
reed@google.comac10a2d2010-12-22 21:39:39 +0000702 /**
703 * Helper function to compute the size of a vertex from a vertex layout
704 * @return size of a single vertex.
705 */
706 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000707
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000708 /**
709 * Helper function for determining the index of texture coordinates that
710 * is input for a texture stage. Note that a stage may instead use positions
711 * as texture coordinates, in which case the result of the function is
712 * indistinguishable from the case when the stage is disabled.
713 *
714 * @param stage the stage to query
715 * @param vertexLayout layout to query
716 *
717 * @return the texture coordinate index or -1 if the stage doesn't use
718 * separate (non-position) texture coordinates.
719 */
720 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000721
722 /**
723 * Helper function to compute the offset of texture coordinates in a vertex
724 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000725 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000726 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000727 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000728 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000729
730 /**
731 * Helper function to compute the offset of the color in a vertex
732 * @return offset of color in vertex layout or -1 if the
733 * layout has no color.
734 */
735 static int VertexColorOffset(GrVertexLayout vertexLayout);
736
bsalomon@google.coma3108262011-10-10 14:08:47 +0000737 /**
738 * Helper function to compute the offset of the coverage in a vertex
739 * @return offset of coverage in vertex layout or -1 if the
740 * layout has no coverage.
741 */
742 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
743
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000744 /**
745 * Helper function to compute the offset of the edge pts in a vertex
746 * @return offset of edge in vertex layout or -1 if the
747 * layout has no edge.
748 */
749 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
750
reed@google.comac10a2d2010-12-22 21:39:39 +0000751 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000752 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000753 * coordinates of some index.
754 *
755 * @param coordIndex the tex coord index to query
756 * @param vertexLayout layout to query
757 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000758 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000759 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000760 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000761 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000762 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000763
reed@google.comac10a2d2010-12-22 21:39:39 +0000764 /**
765 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000766 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000767 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000768 * @param stage the stage to query
769 * @param vertexLayout layout to query
770 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000771 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000772 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000773 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000774 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000775
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000776 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000777 * Helper function to compute the size of each vertex and the offsets of
778 * texture coordinates and color. Determines tex coord offsets by tex coord
779 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000780 * by StageTexCoordVertexLayoutBit.)
781 *
782 * @param vertexLayout the layout to query
783 * @param texCoordOffsetsByIdx after return it is the offset of each
784 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000785 * index isn't used. (optional)
786 * @param colorOffset after return it is the offset of the
787 * color field in each vertex, or -1 if
788 * there aren't per-vertex colors. (optional)
789 * @param coverageOffset after return it is the offset of the
790 * coverage field in each vertex, or -1 if
791 * there aren't per-vertex coeverages.
792 * (optional)
793 * @param edgeOffset after return it is the offset of the
794 * edge eq field in each vertex, or -1 if
795 * there aren't per-vertex edge equations.
796 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000797 * @return size of a single vertex
798 */
799 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000800 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
801 int *colorOffset,
802 int *coverageOffset,
803 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000804
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000805 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000806 * Helper function to compute the size of each vertex and the offsets of
807 * texture coordinates and color. Determines tex coord offsets by stage
808 * rather than by index. (Each stage can be mapped to any t.c. index
809 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000810 * tex coords then that stage's offset will be 0 (positions are always at 0).
811 *
812 * @param vertexLayout the layout to query
813 * @param texCoordOffsetsByStage after return it is the offset of each
814 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000815 * index isn't used. (optional)
816 * @param colorOffset after return it is the offset of the
817 * color field in each vertex, or -1 if
818 * there aren't per-vertex colors.
819 * (optional)
820 * @param coverageOffset after return it is the offset of the
821 * coverage field in each vertex, or -1 if
822 * there aren't per-vertex coeverages.
823 * (optional)
824 * @param edgeOffset after return it is the offset of the
825 * edge eq field in each vertex, or -1 if
826 * there aren't per-vertex edge equations.
827 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000828 * @return size of a single vertex
829 */
830 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000831 int texCoordOffsetsByStage[GrDrawState::kNumStages],
832 int* colorOffset,
833 int* coverageOffset,
834 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000835
836 /**
837 * Accessing positions, texture coords, or colors, of a vertex within an
838 * array is a hassle involving casts and simple math. These helpers exist
839 * to keep GrDrawTarget clients' code a bit nicer looking.
840 */
841
842 /**
843 * Gets a pointer to a GrPoint of a vertex's position or texture
844 * coordinate.
845 * @param vertices the vetex array
846 * @param vertexIndex the index of the vertex in the array
847 * @param vertexSize the size of each vertex in the array
848 * @param offset the offset in bytes of the vertex component.
849 * Defaults to zero (corresponding to vertex position)
850 * @return pointer to the vertex component as a GrPoint
851 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000852 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000853 int vertexIndex,
854 int vertexSize,
855 int offset = 0) {
856 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000857 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000858 vertexIndex * vertexSize);
859 }
860 static const GrPoint* GetVertexPoint(const void* vertices,
861 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000862 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000863 int offset = 0) {
864 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000865 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000866 vertexIndex * vertexSize);
867 }
868
869 /**
870 * Gets a pointer to a GrColor inside a vertex within a vertex array.
871 * @param vertices the vetex array
872 * @param vertexIndex the index of the vertex in the array
873 * @param vertexSize the size of each vertex in the array
874 * @param offset the offset in bytes of the vertex color
875 * @return pointer to the vertex component as a GrColor
876 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000877 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000878 int vertexIndex,
879 int vertexSize,
880 int offset) {
881 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000882 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000883 vertexIndex * vertexSize);
884 }
885 static const GrColor* GetVertexColor(const void* vertices,
886 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000887 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000888 int offset) {
889 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000890 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000891 vertexIndex * vertexSize);
892 }
893
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000894 static void VertexLayoutUnitTest();
895
reed@google.comac10a2d2010-12-22 21:39:39 +0000896protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000897
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000898 /**
899 * Optimizations for blending / coverage to be applied based on the current
900 * state.
901 * Subclasses that actually draw (as opposed to those that just buffer for
902 * playback) must implement the flags that replace the output color.
903 */
904 enum BlendOptFlags {
905 /**
906 * No optimization
907 */
908 kNone_BlendOpt = 0,
909 /**
910 * Don't draw at all
911 */
912 kSkipDraw_BlendOptFlag = 0x2,
913 /**
914 * Emit the src color, disable HW blending (replace dst with src)
915 */
916 kDisableBlend_BlendOptFlag = 0x4,
917 /**
918 * The coverage value does not have to be computed separately from
919 * alpha, the the output color can be the modulation of the two.
920 */
921 kCoverageAsAlpha_BlendOptFlag = 0x1,
922 /**
923 * Instead of emitting a src color, emit coverage in the alpha channel
924 * and r,g,b are "don't cares".
925 */
926 kEmitCoverage_BlendOptFlag = 0x10,
927 /**
928 * Emit transparent black instead of the src color, no need to compute
929 * coverage.
930 */
931 kEmitTransBlack_BlendOptFlag = 0x8,
932 };
933 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000934
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000935 // Determines what optimizations can be applied based on the blend.
936 // The coeffecients may have to be tweaked in order for the optimization
937 // to work. srcCoeff and dstCoeff are optional params that receive the
938 // tweaked coeffecients.
939 // Normally the function looks at the current state to see if coverage
940 // is enabled. By setting forceCoverage the caller can speculatively
941 // determine the blend optimizations that would be used if there was
942 // partial pixel coverage
943 BlendOptFlags getBlendOpts(bool forceCoverage = false,
944 GrBlendCoeff* srcCoeff = NULL,
945 GrBlendCoeff* dstCoeff = NULL) const;
946
947 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000948 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000949
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000950 enum GeometrySrcType {
951 kNone_GeometrySrcType, //<! src has not been specified
952 kReserved_GeometrySrcType, //<! src was set using reserve*Space
953 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
954 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
955 };
956
957 struct GeometrySrcState {
958 GeometrySrcType fVertexSrc;
959 union {
960 // valid if src type is buffer
961 const GrVertexBuffer* fVertexBuffer;
962 // valid if src type is reserved or array
963 int fVertexCount;
964 };
965
966 GeometrySrcType fIndexSrc;
967 union {
968 // valid if src type is buffer
969 const GrIndexBuffer* fIndexBuffer;
970 // valid if src type is reserved or array
971 int fIndexCount;
972 };
973
974 GrVertexLayout fVertexLayout;
975 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000976
977 int indexCountInCurrentSource() const {
978 const GeometrySrcState& src = this->getGeomSrc();
979 switch (src.fIndexSrc) {
980 case kNone_GeometrySrcType:
981 return 0;
982 case kReserved_GeometrySrcType:
983 case kArray_GeometrySrcType:
984 return src.fIndexCount;
985 case kBuffer_GeometrySrcType:
986 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
987 default:
988 GrCrash("Unexpected Index Source.");
989 return 0;
990 }
991 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000992 // given a vertex layout and a draw state, will a stage be used?
993 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000994 const GrDrawState& state) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000995 return NULL != state.getTexture(stage) &&
996 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000997 }
998
999 bool isStageEnabled(int stage) const {
bsalomon@google.come79c8152012-03-29 19:07:12 +00001000 return StageWillBeUsed(stage, this->getVertexLayout(),
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001001 this->getDrawState());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001002 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001003
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001004 StageMask enabledStages() const {
1005 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +00001006 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001007 mask |= this->isStageEnabled(s) ? 1 : 0;
1008 }
1009 return mask;
1010 }
1011
bsalomon@google.com97805382012-03-13 14:32:07 +00001012 // A sublcass can optionally overload this function to be notified before
1013 // vertex and index space is reserved.
1014 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
1015 int vertexCount,
1016 int indexCount) {}
1017
1018
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001019 // implemented by subclass to allocate space for reserved geom
1020 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1021 int vertexCount,
1022 void** vertices) = 0;
1023 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1024 // implemented by subclass to handle release of reserved geom space
1025 virtual void releaseReservedVertexSpace() = 0;
1026 virtual void releaseReservedIndexSpace() = 0;
1027 // subclass must consume array contents when set
1028 virtual void onSetVertexSourceToArray(const void* vertexArray,
1029 int vertexCount) = 0;
1030 virtual void onSetIndexSourceToArray(const void* indexArray,
1031 int indexCount) = 0;
1032 // subclass is notified that geom source will be set away from an array
1033 virtual void releaseVertexArray() = 0;
1034 virtual void releaseIndexArray() = 0;
1035 // subclass overrides to be notified just before geo src state
1036 // is pushed/popped.
1037 virtual void geometrySourceWillPush() = 0;
1038 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1039 // subclass called to perform drawing
1040 virtual void onDrawIndexed(GrPrimitiveType type,
1041 int startVertex,
1042 int startIndex,
1043 int vertexCount,
1044 int indexCount) = 0;
1045 virtual void onDrawNonIndexed(GrPrimitiveType type,
1046 int startVertex,
1047 int vertexCount) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001048 virtual void onStencilPath(const GrPath& path, GrPathFill fill) = 0;
1049
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001050 // subclass overrides to be notified when clip is set. Must call
1051 // INHERITED::clipwillBeSet
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001052 virtual void clipWillBeSet(const GrClip& clip) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001053
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001054 // Helpers for drawRect, protected so subclasses that override drawRect
1055 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001056 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001057 const GrRect* srcRects[]);
1058
1059 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001060 const GrMatrix* matrix,
1061 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001062 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001063 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001064 void* vertices);
1065
bsalomon@google.come79c8152012-03-29 19:07:12 +00001066 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001067 const GeometrySrcState& getGeomSrc() const {
1068 return fGeoSrcStateStack.back();
1069 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001070 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1071 // because of the assert.
1072 GrVertexLayout getVertexLayout() const {
1073 // the vertex layout is only valid if a vertex source has been
1074 // specified.
1075 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1076 return this->getGeomSrc().fVertexLayout;
1077 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001078
1079 GrClip fClip;
1080
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001081 GrDrawState* fDrawState;
1082 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001084 Caps fCaps;
1085
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001086 // subclasses must call this in their destructors to ensure all vertex
1087 // and index sources have been released (including those held by
1088 // pushGeometrySource())
1089 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001090
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001091private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001092 // helpers for reserving vertex and index space.
1093 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1094 int vertexCount,
1095 void** vertices);
1096 bool reserveIndexSpace(int indexCount, void** indices);
1097
bsalomon@google.come8262622011-11-07 02:30:51 +00001098 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1099 // indicate non-indexed drawing.
1100 bool checkDraw(GrPrimitiveType type, int startVertex,
1101 int startIndex, int vertexCount,
1102 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001103 // called when setting a new vert/idx source to unref prev vb/ib
1104 void releasePreviousVertexSource();
1105 void releasePreviousIndexSource();
1106
1107 enum {
1108 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001110 SkSTArray<kPreallocGeoSrcStateStackCnt,
1111 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001112
reed@google.comac10a2d2010-12-22 21:39:39 +00001113};
1114
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001115GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1116
reed@google.comac10a2d2010-12-22 21:39:39 +00001117#endif