blob: 3f1ad5984dc7eee81c54079aea8e591796c57943 [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"
bsalomon@google.coma3201942012-06-21 19:58:20 +000019#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
Scroggo97c88c22011-05-11 14:05:25 +000021#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000022#include "SkTLazy.h"
Scroggo97c88c22011-05-11 14:05:25 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024class GrClipIterator;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000025class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28class GrDrawTarget : public GrRefCnt {
29public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000030 SK_DECLARE_INST_COUNT(GrDrawTarget)
31
reed@google.comac10a2d2010-12-22 21:39:39 +000032 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033 * Represents the draw target capabilities.
34 */
35 struct Caps {
36 Caps() { memset(this, 0, sizeof(Caps)); }
37 Caps(const Caps& c) { *this = c; }
38 Caps& operator= (const Caps& c) {
39 memcpy(this, &c, sizeof(Caps));
40 return *this;
41 }
42 void print() const;
43 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000044 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000045 bool fTwoSidedStencilSupport : 1;
46 bool fStencilWrapOpsSupport : 1;
47 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000048 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000049 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000050 bool fFSAASupport : 1;
51 bool fDualSourceBlendingSupport : 1;
52 bool fBufferLockSupport : 1;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000053 bool fPathStencilingSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000054 int fMaxRenderTargetSize;
55 int fMaxTextureSize;
56 };
57
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000058 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000059 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000060
reed@google.comac10a2d2010-12-22 21:39:39 +000061 ///////////////////////////////////////////////////////////////////////////
62
63 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000064 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000065
66 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000067 * Gets the capabilities of the draw target.
68 */
69 const Caps& getCaps() const { return fCaps; }
70
71 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000072 * Sets the current clip to the region specified by clip. All draws will be
73 * clipped against this clip if kClip_StateBit is enabled.
74 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000075 * Setting the clip may (or may not) zero out the client's stencil bits.
76 *
reed@google.comac10a2d2010-12-22 21:39:39 +000077 * @param description of the clipping region
78 */
79 void setClip(const GrClip& clip);
80
81 /**
82 * Gets the current clip.
83 *
84 * @return the clip.
85 */
86 const GrClip& getClip() const;
87
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000088 /**
89 * Sets the draw state object for the draw target. Note that this does not
90 * make a copy. The GrDrawTarget will take a reference to passed object.
91 * Passing NULL will cause the GrDrawTarget to use its own internal draw
92 * state object rather than an externally provided one.
93 */
94 void setDrawState(GrDrawState* drawState);
95
96 /**
97 * Read-only access to the GrDrawTarget's current draw state.
98 */
99 const GrDrawState& getDrawState() const { return *fDrawState; }
100
101 /**
102 * Read-write access to the GrDrawTarget's current draw state. Note that
103 * this doesn't ref.
104 */
105 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000106
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000107 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000108 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
109 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000110 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000111 * @param matrix the matrix to concat
112 */
113 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
114 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000115 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000116 }
117
118 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000119 * Color alpha and coverage are two inputs to the drawing pipeline. For some
120 * blend modes it is safe to fold the coverage into constant or per-vertex
121 * color alpha value. For other blend modes they must be handled separately.
122 * Depending on features available in the underlying 3D API this may or may
123 * not be possible.
124 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000125 * This function considers the current draw state and the draw target's
126 * capabilities to determine whether coverage can be handled correctly. The
127 * following assumptions are made:
128 * 1. The caller intends to somehow specify coverage. This can be
129 * specified either by enabling a coverage stage on the GrDrawState or
130 * via the vertex layout.
131 * 2. Other than enabling coverage stages, the current configuration of
132 * the target's GrDrawState is as it will be at draw time.
133 * 3. If a vertex source has not yet been specified then all stages with
134 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000135 */
136 bool canApplyCoverage() const;
137
138 /**
139 * Determines whether incorporating partial pixel coverage into the constant
140 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000141 * blending result. If a vertex source has not yet been specified then
142 * the function assumes that all stages with non-NULL textures will be
143 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000144 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000145 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000146
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000147 /**
bsalomon@google.come79c8152012-03-29 19:07:12 +0000148 * Given the current draw state and hw support, will HW AA lines be used
149 * (if line primitive type is drawn)? If a vertex source has not yet been
150 * specified then the function assumes that all stages with non-NULL
151 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000152 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000153 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000154
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000155 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000156 * The format of vertices is represented as a bitfield of flags.
157 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000158 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000159 * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
tomhudson@google.com93813632011-10-27 20:21:16 +0000160 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000161 * use any of the texture coordinates as its input texture coordinates or it
162 * may use the positions as texture coordinates.
163 *
164 * If no texture coordinates are specified for a stage then the stage is
165 * disabled.
166 *
167 * Only one type of texture coord can be specified per stage. For
168 * example StageTexCoordVertexLayoutBit(0, 2) and
169 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
170 *
171 * The order in memory is always (position, texture coord 0, ..., color,
172 * coverage) with any unused fields omitted. Note that this means that if
173 * only texture coordinates 1 is referenced then there is no texture
174 * coordinates 0 and the order would be (position, texture coordinate 1
175 * [, color][, coverage]).
176 */
177
178 /**
179 * Generates a bit indicating that a texture stage uses texture coordinates
180 *
181 * @param stage the stage that will use texture coordinates.
182 * @param texCoordIdx the index of the texture coordinates to use
183 *
184 * @return the bit to add to a GrVertexLayout bitfield.
185 */
186 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000187 GrAssert(stage < GrDrawState::kNumStages);
188 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
189 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000190 }
191
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000192 static bool StageUsesTexCoords(GrVertexLayout layout, int stage);
193
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000194private:
bsalomon@google.com52544c72012-07-10 15:06:59 +0000195 // non-stage bits start at this index.
196 static const int STAGE_BIT_CNT = GrDrawState::kNumStages *
197 GrDrawState::kMaxTexCoords;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000198public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000199
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000200 /**
201 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 */
203 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000204 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000205 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000206 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000207 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000208 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000209 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000210 * text [GrGpuTextVertex vs GrPoint].)
211 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000212 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000214 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000215 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000216 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000217 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000219 kDummyVertexLayoutBit,
220 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000222 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000223 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
225 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000226 * There are three types of "sources" of geometry (vertices and indices) for
227 * draw calls made on the target. When performing an indexed draw, the
228 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000229 * specified it can be used for multiple draws. However, the time at which
230 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000231 *
232 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000233 * already specified geometry that it isn't finished with. So there are push
234 * and pop methods. This allows the client to push the sources, draw
235 * something using alternate sources, and then pop to restore the original
236 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000237 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000238 * Aside from pushes and pops, a source remains valid until another source
239 * is set or resetVertexSource / resetIndexSource is called. Drawing from
240 * a reset source is an error.
241 *
242 * The three types of sources are:
243 *
244 * 1. A cpu array (set*SourceToArray). This is useful when the caller
245 * already provided vertex data in a format compatible with a
246 * GrVertexLayout. The data in the array is consumed at the time that
247 * set*SourceToArray is called and subsequent edits to the array will not
248 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000249 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000250 * 2. Reserve. This is most useful when the caller has data it must
251 * transform before drawing and is not long-lived. The caller requests
252 * that the draw target make room for some amount of vertex and/or index
253 * data. The target provides ptrs to hold the vertex and/or index data.
254 *
255 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000256 * drawIndexedInstances, or pushGeometrySource. At this point the data is
257 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000258 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000259 * Where the space is allocated and how it is uploaded to the GPU is
260 * subclass-dependent.
261 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000263 * is long-lived. When the data in the buffer is consumed depends on the
264 * GrDrawTarget subclass. For deferred subclasses the caller has to
265 * guarantee that the data is still available in the buffers at playback.
266 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000267 */
268
269 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000270 * Reserves space for vertices and/or indices. Zero can be specifed as
271 * either the vertex or index count if the caller desires to only reserve
272 * space for only indices or only vertices. If zero is specifed for
273 * vertexCount then the vertex source will be unmodified and likewise for
274 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000276 * If the function returns true then the reserve suceeded and the vertices
277 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000279 * If the target cannot make space for the request then this function will
280 * return false. If vertexCount was non-zero then upon failure the vertex
281 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000283 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000284 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
285 * popGeomtrySource is called. At that point logically a snapshot of the
286 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000287 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000289 * @param vertexCount the number of vertices to reserve space for. Can be
290 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000291 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000292 * @param vertices will point to reserved vertex space if vertexCount is
293 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000294 * @param indices will point to reserved index space if indexCount is
295 * non-zero. Illegal to pass NULL if indexCount > 0.
296 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000297 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
298 int vertexCount,
299 int indexCount,
300 void** vertices,
301 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000302
reed@google.comac10a2d2010-12-22 21:39:39 +0000303 /**
304 * Provides hints to caller about the number of vertices and indices
305 * that can be allocated cheaply. This can be useful if caller is reserving
306 * space but doesn't know exactly how much geometry is needed.
307 *
308 * Also may hint whether the draw target should be flushed first. This is
309 * useful for deferred targets.
310 *
311 * @param vertexLayout layout of vertices caller would like to reserve
312 * @param vertexCount in: hint about how many vertices the caller would
313 * like to allocate.
314 * out: a hint about the number of vertices that can be
315 * allocated cheaply. Negative means no hint.
316 * Ignored if NULL.
317 * @param indexCount in: hint about how many indices the caller would
318 * like to allocate.
319 * out: a hint about the number of indices that can be
320 * allocated cheaply. Negative means no hint.
321 * Ignored if NULL.
322 *
323 * @return true if target should be flushed based on the input values.
324 */
325 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000326 int* vertexCount,
327 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
329 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000330 * Sets source of vertex data for the next draw. Array must contain
331 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 *
333 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000334 * @param size size of the vertex data.
335 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000336 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000337 void setVertexSourceToArray(GrVertexLayout vertexLayout,
338 const void* vertexArray,
339 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000340
341 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000342 * Sets source of index data for the next indexed draw. Array must contain
343 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000344 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000345 * @param array cpu array containing index data.
346 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000348 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000349
350 /**
351 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000352 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 *
354 * @param buffer vertex buffer containing vertex data. Must be
355 * unlocked before draw call.
356 * @param vertexLayout layout of the vertex data in the buffer.
357 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000358 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
359 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000360
361 /**
362 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000363 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 *
365 * @param buffer index buffer containing indices. Must be unlocked
366 * before indexed draw call.
367 */
368 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000369
370 /**
371 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
372 * source to reserved, array, or buffer before next draw. May be able to free
373 * up temporary storage allocated by setVertexSourceToArray or
374 * reserveVertexSpace.
375 */
376 void resetVertexSource();
377
378 /**
379 * Resets index source. Indexed Drawing from reset indices is illegal. Set
380 * index source to reserved, array, or buffer before next indexed draw. May
381 * be able to free up temporary storage allocated by setIndexSourceToArray
382 * or reserveIndexSpace.
383 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000384 void resetIndexSource();
385
386 /**
387 * Query to find out if the vertex or index source is reserved.
388 */
389 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000390 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000391 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
392 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
394 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000395 * Pushes and resets the vertex/index sources. Any reserved vertex / index
396 * data is finalized (i.e. cannot be updated after the matching pop but can
397 * be drawn from). Must be balanced by a pop.
398 */
399 void pushGeometrySource();
400
401 /**
402 * Pops the vertex / index sources from the matching push.
403 */
404 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000405
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000406 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 * Draws indexed geometry using the current state and current vertex / index
408 * sources.
409 *
410 * @param type The type of primitives to draw.
411 * @param startVertex the vertex in the vertex array/buffer corresponding
412 * to index 0
413 * @param startIndex first index to read from index src.
414 * @param vertexCount one greater than the max index.
415 * @param indexCount the number of index elements to read. The index count
416 * is effectively trimmed to the last completely
417 * specified primitive.
418 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000419 void drawIndexed(GrPrimitiveType type,
420 int startVertex,
421 int startIndex,
422 int vertexCount,
423 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000424
425 /**
426 * Draws non-indexed geometry using the current state and current vertex
427 * sources.
428 *
429 * @param type The type of primitives to draw.
430 * @param startVertex the vertex in the vertex array/buffer corresponding
431 * to index 0
432 * @param vertexCount one greater than the max index.
433 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000434 void drawNonIndexed(GrPrimitiveType type,
435 int startVertex,
436 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000437
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000438 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000439 * Draws path into the stencil buffer. The fill must be either even/odd or
440 * winding (not inverse or hairline). It will respect the HW antialias flag
441 * on the draw state (if possible in the 3D API).
442 */
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000443 void stencilPath(const GrPath*, GrPathFill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000444
445 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000446 * Helper function for drawing rects. This does not use the current index
447 * and vertex sources. After returning, the vertex and index sources may
448 * have changed. They should be reestablished before the next drawIndexed
449 * or drawNonIndexed. This cannot be called between reserving and releasing
450 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000451 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000452 * drawNonIndexed.
453 * @param rect the rect to draw
454 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000455 * @param stageMask bitmask indicating which stages are enabled.
456 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000457 * @param srcRects specifies rects for stages enabled by stageEnableMask.
458 * if stageEnableMask bit i is 1, srcRects is not NULL,
459 * and srcRects[i] is not NULL, then srcRects[i] will be
460 * used as coordinates for stage i. Otherwise, if stage i
461 * is enabled then rect is used as the coordinates.
462 * @param srcMatrices optional matrices applied to srcRects. If
463 * srcRect[i] is non-NULL and srcMatrices[i] is
464 * non-NULL then srcRect[i] will be transformed by
465 * srcMatrix[i]. srcMatrices can be NULL when no
466 * srcMatrices are desired.
467 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000468 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000469 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000470 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000471 const GrRect* srcRects[],
472 const GrMatrix* srcMatrices[]);
473
474 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000475 * This call is used to draw multiple instances of some geometry with a
476 * given number of vertices (V) and indices (I) per-instance. The indices in
477 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
478 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
479 * concrete example, the following index buffer for drawing a series of
480 * quads each as two triangles each satisfies these conditions with V=4 and
481 * I=6:
482 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
483 *
484 * The call assumes that the pattern of indices fills the entire index
485 * source. The size of the index buffer limits the number of instances that
486 * can be drawn by the GPU in a single draw. However, the caller may specify
487 * any (positive) number for instanceCount and if necessary multiple GPU
488 * draws will be issued. Morever, when drawIndexedInstances is called
489 * multiple times it may be possible for GrDrawTarget to group them into a
490 * single GPU draw.
491 *
492 * @param type the type of primitives to draw
493 * @param instanceCount the number of instances to draw. Each instance
494 * consists of verticesPerInstance vertices indexed by
495 * indicesPerInstance indices drawn as the primitive
496 * type specified by type.
497 * @param verticesPerInstance The number of vertices in each instance (V
498 * in the above description).
499 * @param indicesPerInstance The number of indices in each instance (I
500 * in the above description).
501 */
502 virtual void drawIndexedInstances(GrPrimitiveType type,
503 int instanceCount,
504 int verticesPerInstance,
505 int indicesPerInstance);
506
507 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000508 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000509 * matrices.
510 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000511 void drawSimpleRect(const GrRect& rect,
512 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000513 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000514 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000515 }
516
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000517 /**
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000518 * Clear the current render target if one isn't passed in. Ignores the
519 * clip and all other draw state (blend mode, stages, etc). Clears the
520 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000521 */
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000522 virtual void clear(const GrIRect* rect,
523 GrColor color,
524 GrRenderTarget* renderTarget = NULL) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000525
robertphillips@google.comff175842012-05-14 19:31:39 +0000526 /**
527 * Release any resources that are cached but not currently in use. This
528 * is intended to give an application some recourse when resources are low.
529 */
530 virtual void purgeResources() {};
531
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000532 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000533
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000534 /**
535 * See AutoStateRestore below.
536 */
537 enum ASRInit {
538 kPreserve_ASRInit,
539 kReset_ASRInit
540 };
541
542 /**
543 * Saves off the current state and restores it in the destructor. It will
544 * install a new GrDrawState object on the target (setDrawState) and restore
545 * the previous one in the destructor. The caller should call drawState() to
546 * get the new draw state after the ASR is installed.
547 *
548 * GrDrawState* state = target->drawState();
549 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
550 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
551 * // target before asr was initialized.
552 * // Therefore, rt is set on the GrDrawState
553 * // that will be restored after asr's
554 * // destructor rather than target's current
555 * // GrDrawState.
556 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 class AutoStateRestore : ::GrNoncopyable {
558 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000559 /**
560 * Default ASR will have no effect unless set() is subsequently called.
561 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000562 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000563
564 /**
565 * Saves the state on target. The state will be restored when the ASR
566 * is destroyed. If this constructor is used do not call set().
567 *
568 * @param init Should the newly installed GrDrawState be a copy of the
569 * previous state or a default-initialized GrDrawState.
570 */
571 AutoStateRestore(GrDrawTarget* target, ASRInit init);
572
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 ~AutoStateRestore();
574
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000575 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000576 * Saves the state on target. The state will be restored when the ASR
577 * is destroyed. This should only be called once per ASR object and only
578 * when the default constructor was used. For nested saves use multiple
579 * ASR objects.
580 *
581 * @param init Should the newly installed GrDrawState be a copy of the
582 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000583 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000584 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000585
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000587 GrDrawTarget* fDrawTarget;
588 SkTLazy<GrDrawState> fTempState;
589 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 };
591
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000592 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000593
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000594 /**
595 * Sets the view matrix to I and preconcats all stage matrices enabled in
596 * mask by the view inverse. Destructor undoes these changes.
597 */
598 class AutoDeviceCoordDraw : ::GrNoncopyable {
599 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000600 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000601 ~AutoDeviceCoordDraw();
602 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000603 GrDrawTarget* fDrawTarget;
604 GrMatrix fViewMatrix;
605 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
606 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000607 };
608
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000609 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000610
reed@google.comac10a2d2010-12-22 21:39:39 +0000611 class AutoReleaseGeometry : ::GrNoncopyable {
612 public:
613 AutoReleaseGeometry(GrDrawTarget* target,
614 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000615 int vertexCount,
616 int indexCount);
617 AutoReleaseGeometry();
618 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000619 bool set(GrDrawTarget* target,
620 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000621 int vertexCount,
622 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000623 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000624 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
625 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000627 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 }
629
630 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000631 void reset();
632
reed@google.comac10a2d2010-12-22 21:39:39 +0000633 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 void* fVertices;
635 void* fIndices;
636 };
637
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000638 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000639
640 class AutoClipRestore : ::GrNoncopyable {
641 public:
642 AutoClipRestore(GrDrawTarget* target) {
643 fTarget = target;
644 fClip = fTarget->getClip();
645 }
646
647 ~AutoClipRestore() {
648 fTarget->setClip(fClip);
649 }
650 private:
651 GrDrawTarget* fTarget;
652 GrClip fClip;
653 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000654
655 ////////////////////////////////////////////////////////////////////////////
656
657 class AutoGeometryPush : ::GrNoncopyable {
658 public:
659 AutoGeometryPush(GrDrawTarget* target) {
660 GrAssert(NULL != target);
661 fTarget = target;
662 target->pushGeometrySource();
663 }
664 ~AutoGeometryPush() {
665 fTarget->popGeometrySource();
666 }
667 private:
668 GrDrawTarget* fTarget;
669 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000670
671 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000672 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000673
reed@google.comac10a2d2010-12-22 21:39:39 +0000674 /**
675 * Helper function to compute the size of a vertex from a vertex layout
676 * @return size of a single vertex.
677 */
678 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000680 /**
681 * Helper function for determining the index of texture coordinates that
682 * is input for a texture stage. Note that a stage may instead use positions
683 * as texture coordinates, in which case the result of the function is
684 * indistinguishable from the case when the stage is disabled.
685 *
686 * @param stage the stage to query
687 * @param vertexLayout layout to query
688 *
689 * @return the texture coordinate index or -1 if the stage doesn't use
690 * separate (non-position) texture coordinates.
691 */
692 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000693
694 /**
695 * Helper function to compute the offset of texture coordinates in a vertex
696 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000697 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000698 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000699 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000700 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000701
702 /**
703 * Helper function to compute the offset of the color in a vertex
704 * @return offset of color in vertex layout or -1 if the
705 * layout has no color.
706 */
707 static int VertexColorOffset(GrVertexLayout vertexLayout);
708
bsalomon@google.coma3108262011-10-10 14:08:47 +0000709 /**
710 * Helper function to compute the offset of the coverage in a vertex
711 * @return offset of coverage in vertex layout or -1 if the
712 * layout has no coverage.
713 */
714 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
715
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000716 /**
717 * Helper function to compute the offset of the edge pts in a vertex
718 * @return offset of edge in vertex layout or -1 if the
719 * layout has no edge.
720 */
721 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
722
reed@google.comac10a2d2010-12-22 21:39:39 +0000723 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000724 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000725 * coordinates of some index.
726 *
727 * @param coordIndex the tex coord index to query
728 * @param vertexLayout layout to query
729 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000730 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000731 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000732 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000733 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000734 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000735
reed@google.comac10a2d2010-12-22 21:39:39 +0000736 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000737 * Helper function to compute the size of each vertex and the offsets of
738 * texture coordinates and color. Determines tex coord offsets by tex coord
739 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000740 * by StageTexCoordVertexLayoutBit.)
741 *
742 * @param vertexLayout the layout to query
743 * @param texCoordOffsetsByIdx after return it is the offset of each
744 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000745 * index isn't used. (optional)
746 * @param colorOffset after return it is the offset of the
747 * color field in each vertex, or -1 if
748 * there aren't per-vertex colors. (optional)
749 * @param coverageOffset after return it is the offset of the
750 * coverage field in each vertex, or -1 if
751 * there aren't per-vertex coeverages.
752 * (optional)
753 * @param edgeOffset after return it is the offset of the
754 * edge eq field in each vertex, or -1 if
755 * there aren't per-vertex edge equations.
756 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000757 * @return size of a single vertex
758 */
759 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000760 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
761 int *colorOffset,
762 int *coverageOffset,
763 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000764
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000765 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000766 * Helper function to compute the size of each vertex and the offsets of
767 * texture coordinates and color. Determines tex coord offsets by stage
768 * rather than by index. (Each stage can be mapped to any t.c. index
769 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000770 * tex coords then that stage's offset will be 0 (positions are always at 0).
771 *
772 * @param vertexLayout the layout to query
773 * @param texCoordOffsetsByStage after return it is the offset of each
774 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000775 * index isn't used. (optional)
776 * @param colorOffset after return it is the offset of the
777 * color field in each vertex, or -1 if
778 * there aren't per-vertex colors.
779 * (optional)
780 * @param coverageOffset after return it is the offset of the
781 * coverage field in each vertex, or -1 if
782 * there aren't per-vertex coeverages.
783 * (optional)
784 * @param edgeOffset after return it is the offset of the
785 * edge eq field in each vertex, or -1 if
786 * there aren't per-vertex edge equations.
787 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000788 * @return size of a single vertex
789 */
790 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000791 int texCoordOffsetsByStage[GrDrawState::kNumStages],
792 int* colorOffset,
793 int* coverageOffset,
794 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000795
796 /**
797 * Accessing positions, texture coords, or colors, of a vertex within an
798 * array is a hassle involving casts and simple math. These helpers exist
799 * to keep GrDrawTarget clients' code a bit nicer looking.
800 */
801
802 /**
803 * Gets a pointer to a GrPoint of a vertex's position or texture
804 * coordinate.
805 * @param vertices the vetex array
806 * @param vertexIndex the index of the vertex in the array
807 * @param vertexSize the size of each vertex in the array
808 * @param offset the offset in bytes of the vertex component.
809 * Defaults to zero (corresponding to vertex position)
810 * @return pointer to the vertex component as a GrPoint
811 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000812 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000813 int vertexIndex,
814 int vertexSize,
815 int offset = 0) {
816 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000817 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000818 vertexIndex * vertexSize);
819 }
820 static const GrPoint* GetVertexPoint(const void* vertices,
821 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000822 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000823 int offset = 0) {
824 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000825 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000826 vertexIndex * vertexSize);
827 }
828
829 /**
830 * Gets a pointer to a GrColor inside a vertex within a vertex array.
831 * @param vertices the vetex array
832 * @param vertexIndex the index of the vertex in the array
833 * @param vertexSize the size of each vertex in the array
834 * @param offset the offset in bytes of the vertex color
835 * @return pointer to the vertex component as a GrColor
836 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000837 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000838 int vertexIndex,
839 int vertexSize,
840 int offset) {
841 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000842 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000843 vertexIndex * vertexSize);
844 }
845 static const GrColor* GetVertexColor(const void* vertices,
846 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000847 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000848 int offset) {
849 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000850 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000851 vertexIndex * vertexSize);
852 }
853
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000854 static void VertexLayoutUnitTest();
855
reed@google.comac10a2d2010-12-22 21:39:39 +0000856protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000857
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000858 /**
859 * Optimizations for blending / coverage to be applied based on the current
860 * state.
861 * Subclasses that actually draw (as opposed to those that just buffer for
862 * playback) must implement the flags that replace the output color.
863 */
864 enum BlendOptFlags {
865 /**
866 * No optimization
867 */
868 kNone_BlendOpt = 0,
869 /**
870 * Don't draw at all
871 */
872 kSkipDraw_BlendOptFlag = 0x2,
873 /**
874 * Emit the src color, disable HW blending (replace dst with src)
875 */
876 kDisableBlend_BlendOptFlag = 0x4,
877 /**
878 * The coverage value does not have to be computed separately from
879 * alpha, the the output color can be the modulation of the two.
880 */
881 kCoverageAsAlpha_BlendOptFlag = 0x1,
882 /**
883 * Instead of emitting a src color, emit coverage in the alpha channel
884 * and r,g,b are "don't cares".
885 */
886 kEmitCoverage_BlendOptFlag = 0x10,
887 /**
888 * Emit transparent black instead of the src color, no need to compute
889 * coverage.
890 */
891 kEmitTransBlack_BlendOptFlag = 0x8,
892 };
893 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000894
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000895 // Determines what optimizations can be applied based on the blend.
896 // The coeffecients may have to be tweaked in order for the optimization
897 // to work. srcCoeff and dstCoeff are optional params that receive the
898 // tweaked coeffecients.
899 // Normally the function looks at the current state to see if coverage
900 // is enabled. By setting forceCoverage the caller can speculatively
901 // determine the blend optimizations that would be used if there was
902 // partial pixel coverage
903 BlendOptFlags getBlendOpts(bool forceCoverage = false,
904 GrBlendCoeff* srcCoeff = NULL,
905 GrBlendCoeff* dstCoeff = NULL) const;
906
907 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000908 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000909
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000910 enum GeometrySrcType {
911 kNone_GeometrySrcType, //<! src has not been specified
912 kReserved_GeometrySrcType, //<! src was set using reserve*Space
913 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
914 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
915 };
916
917 struct GeometrySrcState {
918 GeometrySrcType fVertexSrc;
919 union {
920 // valid if src type is buffer
921 const GrVertexBuffer* fVertexBuffer;
922 // valid if src type is reserved or array
923 int fVertexCount;
924 };
925
926 GeometrySrcType fIndexSrc;
927 union {
928 // valid if src type is buffer
929 const GrIndexBuffer* fIndexBuffer;
930 // valid if src type is reserved or array
931 int fIndexCount;
932 };
933
934 GrVertexLayout fVertexLayout;
935 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000936
937 int indexCountInCurrentSource() const {
938 const GeometrySrcState& src = this->getGeomSrc();
939 switch (src.fIndexSrc) {
940 case kNone_GeometrySrcType:
941 return 0;
942 case kReserved_GeometrySrcType:
943 case kArray_GeometrySrcType:
944 return src.fIndexCount;
945 case kBuffer_GeometrySrcType:
946 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
947 default:
948 GrCrash("Unexpected Index Source.");
949 return 0;
950 }
951 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000952
953 bool isStageEnabled(int stage) const {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000954 return this->getDrawState().isStageEnabled(stage);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000955 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000956
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000957 StageMask enabledStages() const {
958 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000959 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000960 mask |= this->isStageEnabled(s) ? 1 : 0;
961 }
962 return mask;
963 }
964
bsalomon@google.com97805382012-03-13 14:32:07 +0000965 // A sublcass can optionally overload this function to be notified before
966 // vertex and index space is reserved.
967 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
968 int vertexCount,
969 int indexCount) {}
970
971
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000972 // implemented by subclass to allocate space for reserved geom
973 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
974 int vertexCount,
975 void** vertices) = 0;
976 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
977 // implemented by subclass to handle release of reserved geom space
978 virtual void releaseReservedVertexSpace() = 0;
979 virtual void releaseReservedIndexSpace() = 0;
980 // subclass must consume array contents when set
981 virtual void onSetVertexSourceToArray(const void* vertexArray,
982 int vertexCount) = 0;
983 virtual void onSetIndexSourceToArray(const void* indexArray,
984 int indexCount) = 0;
985 // subclass is notified that geom source will be set away from an array
986 virtual void releaseVertexArray() = 0;
987 virtual void releaseIndexArray() = 0;
988 // subclass overrides to be notified just before geo src state
989 // is pushed/popped.
990 virtual void geometrySourceWillPush() = 0;
991 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
992 // subclass called to perform drawing
993 virtual void onDrawIndexed(GrPrimitiveType type,
994 int startVertex,
995 int startIndex,
996 int vertexCount,
997 int indexCount) = 0;
998 virtual void onDrawNonIndexed(GrPrimitiveType type,
999 int startVertex,
1000 int vertexCount) = 0;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001001 virtual void onStencilPath(const GrPath*, GrPathFill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001002
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001003 // subclass overrides to be notified when clip is set. Must call
1004 // INHERITED::clipwillBeSet
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001005 virtual void clipWillBeSet(const GrClip& clip) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001006
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001007 // Helpers for drawRect, protected so subclasses that override drawRect
1008 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001009 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001010 const GrRect* srcRects[]);
1011
1012 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001013 const GrMatrix* matrix,
1014 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001015 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001016 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001017 void* vertices);
1018
bsalomon@google.come79c8152012-03-29 19:07:12 +00001019 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001020 const GeometrySrcState& getGeomSrc() const {
1021 return fGeoSrcStateStack.back();
1022 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001023 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1024 // because of the assert.
1025 GrVertexLayout getVertexLayout() const {
1026 // the vertex layout is only valid if a vertex source has been
1027 // specified.
1028 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1029 return this->getGeomSrc().fVertexLayout;
1030 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001031
1032 GrClip fClip;
1033
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001034 GrDrawState* fDrawState;
1035 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001036
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001037 Caps fCaps;
1038
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001039 // subclasses must call this in their destructors to ensure all vertex
1040 // and index sources have been released (including those held by
1041 // pushGeometrySource())
1042 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001043
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001044private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001045 // helpers for reserving vertex and index space.
1046 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1047 int vertexCount,
1048 void** vertices);
1049 bool reserveIndexSpace(int indexCount, void** indices);
1050
bsalomon@google.come8262622011-11-07 02:30:51 +00001051 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1052 // indicate non-indexed drawing.
1053 bool checkDraw(GrPrimitiveType type, int startVertex,
1054 int startIndex, int vertexCount,
1055 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001056 // called when setting a new vert/idx source to unref prev vb/ib
1057 void releasePreviousVertexSource();
1058 void releasePreviousIndexSource();
1059
1060 enum {
1061 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001063 SkSTArray<kPreallocGeoSrcStateStackCnt,
1064 GeometrySrcState, true> fGeoSrcStateStack;
reed@google.comfa35e3d2012-06-26 20:16:17 +00001065
1066 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +00001067};
1068
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001069GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1070
reed@google.comac10a2d2010-12-22 21:39:39 +00001071#endif