blob: 66256cf87778b06b6a9f484f0d145bd4bb791954 [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:
tomhudson@google.com93813632011-10-27 20:21:16 +0000195 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
196 GrDrawState::kMaxTexCoords;
tomhudson@google.com93813632011-10-27 20:21:16 +0000197 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
198 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000199
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000200public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000201
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000202 /**
203 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 */
205 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000206 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000207 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000208 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000209 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000210 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000211 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000212 * text [GrGpuTextVertex vs GrPoint].)
213 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000214 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000215
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000216 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000217 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000218 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000219 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000221 kDummyVertexLayoutBit,
222 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000224 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000225 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
227 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000228 * There are three types of "sources" of geometry (vertices and indices) for
229 * draw calls made on the target. When performing an indexed draw, the
230 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000231 * specified it can be used for multiple draws. However, the time at which
232 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000233 *
234 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000235 * already specified geometry that it isn't finished with. So there are push
236 * and pop methods. This allows the client to push the sources, draw
237 * something using alternate sources, and then pop to restore the original
238 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000239 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000240 * Aside from pushes and pops, a source remains valid until another source
241 * is set or resetVertexSource / resetIndexSource is called. Drawing from
242 * a reset source is an error.
243 *
244 * The three types of sources are:
245 *
246 * 1. A cpu array (set*SourceToArray). This is useful when the caller
247 * already provided vertex data in a format compatible with a
248 * GrVertexLayout. The data in the array is consumed at the time that
249 * set*SourceToArray is called and subsequent edits to the array will not
250 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000251 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000252 * 2. Reserve. This is most useful when the caller has data it must
253 * transform before drawing and is not long-lived. The caller requests
254 * that the draw target make room for some amount of vertex and/or index
255 * data. The target provides ptrs to hold the vertex and/or index data.
256 *
257 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000258 * drawIndexedInstances, or pushGeometrySource. At this point the data is
259 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000260 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000261 * Where the space is allocated and how it is uploaded to the GPU is
262 * subclass-dependent.
263 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000264 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000265 * is long-lived. When the data in the buffer is consumed depends on the
266 * GrDrawTarget subclass. For deferred subclasses the caller has to
267 * guarantee that the data is still available in the buffers at playback.
268 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000269 */
270
271 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000272 * Reserves space for vertices and/or indices. Zero can be specifed as
273 * either the vertex or index count if the caller desires to only reserve
274 * space for only indices or only vertices. If zero is specifed for
275 * vertexCount then the vertex source will be unmodified and likewise for
276 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000277 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000278 * If the function returns true then the reserve suceeded and the vertices
279 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000281 * If the target cannot make space for the request then this function will
282 * return false. If vertexCount was non-zero then upon failure the vertex
283 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000285 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000286 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
287 * popGeomtrySource is called. At that point logically a snapshot of the
288 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000291 * @param vertexCount the number of vertices to reserve space for. Can be
292 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000293 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000294 * @param vertices will point to reserved vertex space if vertexCount is
295 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000296 * @param indices will point to reserved index space if indexCount is
297 * non-zero. Illegal to pass NULL if indexCount > 0.
298 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000299 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
300 int vertexCount,
301 int indexCount,
302 void** vertices,
303 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 /**
306 * Provides hints to caller about the number of vertices and indices
307 * that can be allocated cheaply. This can be useful if caller is reserving
308 * space but doesn't know exactly how much geometry is needed.
309 *
310 * Also may hint whether the draw target should be flushed first. This is
311 * useful for deferred targets.
312 *
313 * @param vertexLayout layout of vertices caller would like to reserve
314 * @param vertexCount in: hint about how many vertices the caller would
315 * like to allocate.
316 * out: a hint about the number of vertices that can be
317 * allocated cheaply. Negative means no hint.
318 * Ignored if NULL.
319 * @param indexCount in: hint about how many indices the caller would
320 * like to allocate.
321 * out: a hint about the number of indices that can be
322 * allocated cheaply. Negative means no hint.
323 * Ignored if NULL.
324 *
325 * @return true if target should be flushed based on the input values.
326 */
327 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000328 int* vertexCount,
329 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000330
331 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000332 * Sets source of vertex data for the next draw. Array must contain
333 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 *
335 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000336 * @param size size of the vertex data.
337 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000339 void setVertexSourceToArray(GrVertexLayout vertexLayout,
340 const void* vertexArray,
341 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
343 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000344 * Sets source of index data for the next indexed draw. Array must contain
345 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000346 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000347 * @param array cpu array containing index data.
348 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000350 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000351
352 /**
353 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000354 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000355 *
356 * @param buffer vertex buffer containing vertex data. Must be
357 * unlocked before draw call.
358 * @param vertexLayout layout of the vertex data in the buffer.
359 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000360 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
361 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
363 /**
364 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000365 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 *
367 * @param buffer index buffer containing indices. Must be unlocked
368 * before indexed draw call.
369 */
370 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000371
372 /**
373 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
374 * source to reserved, array, or buffer before next draw. May be able to free
375 * up temporary storage allocated by setVertexSourceToArray or
376 * reserveVertexSpace.
377 */
378 void resetVertexSource();
379
380 /**
381 * Resets index source. Indexed Drawing from reset indices is illegal. Set
382 * index source to reserved, array, or buffer before next indexed draw. May
383 * be able to free up temporary storage allocated by setIndexSourceToArray
384 * or reserveIndexSpace.
385 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000386 void resetIndexSource();
387
388 /**
389 * Query to find out if the vertex or index source is reserved.
390 */
391 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000392 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000393 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
394 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
396 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000397 * Pushes and resets the vertex/index sources. Any reserved vertex / index
398 * data is finalized (i.e. cannot be updated after the matching pop but can
399 * be drawn from). Must be balanced by a pop.
400 */
401 void pushGeometrySource();
402
403 /**
404 * Pops the vertex / index sources from the matching push.
405 */
406 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000407
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000408 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 * Draws indexed geometry using the current state and current vertex / index
410 * sources.
411 *
412 * @param type The type of primitives to draw.
413 * @param startVertex the vertex in the vertex array/buffer corresponding
414 * to index 0
415 * @param startIndex first index to read from index src.
416 * @param vertexCount one greater than the max index.
417 * @param indexCount the number of index elements to read. The index count
418 * is effectively trimmed to the last completely
419 * specified primitive.
420 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000421 void drawIndexed(GrPrimitiveType type,
422 int startVertex,
423 int startIndex,
424 int vertexCount,
425 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000426
427 /**
428 * Draws non-indexed geometry using the current state and current vertex
429 * sources.
430 *
431 * @param type The type of primitives to draw.
432 * @param startVertex the vertex in the vertex array/buffer corresponding
433 * to index 0
434 * @param vertexCount one greater than the max index.
435 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000436 void drawNonIndexed(GrPrimitiveType type,
437 int startVertex,
438 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000439
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000440 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000441 * Draws path into the stencil buffer. The fill must be either even/odd or
442 * winding (not inverse or hairline). It will respect the HW antialias flag
443 * on the draw state (if possible in the 3D API).
444 */
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000445 void stencilPath(const GrPath*, GrPathFill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000446
447 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000448 * Helper function for drawing rects. This does not use the current index
449 * and vertex sources. After returning, the vertex and index sources may
450 * have changed. They should be reestablished before the next drawIndexed
451 * or drawNonIndexed. This cannot be called between reserving and releasing
452 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000453 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000454 * drawNonIndexed.
455 * @param rect the rect to draw
456 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000457 * @param stageMask bitmask indicating which stages are enabled.
458 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000459 * @param srcRects specifies rects for stages enabled by stageEnableMask.
460 * if stageEnableMask bit i is 1, srcRects is not NULL,
461 * and srcRects[i] is not NULL, then srcRects[i] will be
462 * used as coordinates for stage i. Otherwise, if stage i
463 * is enabled then rect is used as the coordinates.
464 * @param srcMatrices optional matrices applied to srcRects. If
465 * srcRect[i] is non-NULL and srcMatrices[i] is
466 * non-NULL then srcRect[i] will be transformed by
467 * srcMatrix[i]. srcMatrices can be NULL when no
468 * srcMatrices are desired.
469 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000470 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000471 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000472 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000473 const GrRect* srcRects[],
474 const GrMatrix* srcMatrices[]);
475
476 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000477 * This call is used to draw multiple instances of some geometry with a
478 * given number of vertices (V) and indices (I) per-instance. The indices in
479 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
480 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
481 * concrete example, the following index buffer for drawing a series of
482 * quads each as two triangles each satisfies these conditions with V=4 and
483 * I=6:
484 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
485 *
486 * The call assumes that the pattern of indices fills the entire index
487 * source. The size of the index buffer limits the number of instances that
488 * can be drawn by the GPU in a single draw. However, the caller may specify
489 * any (positive) number for instanceCount and if necessary multiple GPU
490 * draws will be issued. Morever, when drawIndexedInstances is called
491 * multiple times it may be possible for GrDrawTarget to group them into a
492 * single GPU draw.
493 *
494 * @param type the type of primitives to draw
495 * @param instanceCount the number of instances to draw. Each instance
496 * consists of verticesPerInstance vertices indexed by
497 * indicesPerInstance indices drawn as the primitive
498 * type specified by type.
499 * @param verticesPerInstance The number of vertices in each instance (V
500 * in the above description).
501 * @param indicesPerInstance The number of indices in each instance (I
502 * in the above description).
503 */
504 virtual void drawIndexedInstances(GrPrimitiveType type,
505 int instanceCount,
506 int verticesPerInstance,
507 int indicesPerInstance);
508
509 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000510 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000511 * matrices.
512 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000513 void drawSimpleRect(const GrRect& rect,
514 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000515 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000516 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000517 }
518
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000519 /**
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000520 * Clear the current render target if one isn't passed in. Ignores the
521 * clip and all other draw state (blend mode, stages, etc). Clears the
522 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000523 */
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000524 virtual void clear(const GrIRect* rect,
525 GrColor color,
526 GrRenderTarget* renderTarget = NULL) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000527
robertphillips@google.comff175842012-05-14 19:31:39 +0000528 /**
529 * Release any resources that are cached but not currently in use. This
530 * is intended to give an application some recourse when resources are low.
531 */
532 virtual void purgeResources() {};
533
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000534 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000536 /**
537 * See AutoStateRestore below.
538 */
539 enum ASRInit {
540 kPreserve_ASRInit,
541 kReset_ASRInit
542 };
543
544 /**
545 * Saves off the current state and restores it in the destructor. It will
546 * install a new GrDrawState object on the target (setDrawState) and restore
547 * the previous one in the destructor. The caller should call drawState() to
548 * get the new draw state after the ASR is installed.
549 *
550 * GrDrawState* state = target->drawState();
551 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
552 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
553 * // target before asr was initialized.
554 * // Therefore, rt is set on the GrDrawState
555 * // that will be restored after asr's
556 * // destructor rather than target's current
557 * // GrDrawState.
558 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000559 class AutoStateRestore : ::GrNoncopyable {
560 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000561 /**
562 * Default ASR will have no effect unless set() is subsequently called.
563 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000564 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000565
566 /**
567 * Saves the state on target. The state will be restored when the ASR
568 * is destroyed. If this constructor is used do not call set().
569 *
570 * @param init Should the newly installed GrDrawState be a copy of the
571 * previous state or a default-initialized GrDrawState.
572 */
573 AutoStateRestore(GrDrawTarget* target, ASRInit init);
574
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 ~AutoStateRestore();
576
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000577 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000578 * Saves the state on target. The state will be restored when the ASR
579 * is destroyed. This should only be called once per ASR object and only
580 * when the default constructor was used. For nested saves use multiple
581 * ASR objects.
582 *
583 * @param init Should the newly installed GrDrawState be a copy of the
584 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000585 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000586 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000587
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000589 GrDrawTarget* fDrawTarget;
590 SkTLazy<GrDrawState> fTempState;
591 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 };
593
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000594 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000595
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000596 /**
597 * Sets the view matrix to I and preconcats all stage matrices enabled in
598 * mask by the view inverse. Destructor undoes these changes.
599 */
600 class AutoDeviceCoordDraw : ::GrNoncopyable {
601 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000602 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000603 ~AutoDeviceCoordDraw();
604 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000605 GrDrawTarget* fDrawTarget;
606 GrMatrix fViewMatrix;
607 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
608 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000609 };
610
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000611 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000612
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 class AutoReleaseGeometry : ::GrNoncopyable {
614 public:
615 AutoReleaseGeometry(GrDrawTarget* target,
616 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000617 int vertexCount,
618 int indexCount);
619 AutoReleaseGeometry();
620 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000621 bool set(GrDrawTarget* target,
622 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000623 int vertexCount,
624 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000625 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000626 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
627 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000629 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000630 }
631
632 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000633 void reset();
634
reed@google.comac10a2d2010-12-22 21:39:39 +0000635 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000636 void* fVertices;
637 void* fIndices;
638 };
639
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000640 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000641
642 class AutoClipRestore : ::GrNoncopyable {
643 public:
644 AutoClipRestore(GrDrawTarget* target) {
645 fTarget = target;
646 fClip = fTarget->getClip();
647 }
648
649 ~AutoClipRestore() {
650 fTarget->setClip(fClip);
651 }
652 private:
653 GrDrawTarget* fTarget;
654 GrClip fClip;
655 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000656
657 ////////////////////////////////////////////////////////////////////////////
658
659 class AutoGeometryPush : ::GrNoncopyable {
660 public:
661 AutoGeometryPush(GrDrawTarget* target) {
662 GrAssert(NULL != target);
663 fTarget = target;
664 target->pushGeometrySource();
665 }
666 ~AutoGeometryPush() {
667 fTarget->popGeometrySource();
668 }
669 private:
670 GrDrawTarget* fTarget;
671 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000672
673 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000674 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000675
reed@google.comac10a2d2010-12-22 21:39:39 +0000676 /**
677 * Helper function to compute the size of a vertex from a vertex layout
678 * @return size of a single vertex.
679 */
680 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000682 /**
683 * Helper function for determining the index of texture coordinates that
684 * is input for a texture stage. Note that a stage may instead use positions
685 * as texture coordinates, in which case the result of the function is
686 * indistinguishable from the case when the stage is disabled.
687 *
688 * @param stage the stage to query
689 * @param vertexLayout layout to query
690 *
691 * @return the texture coordinate index or -1 if the stage doesn't use
692 * separate (non-position) texture coordinates.
693 */
694 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000695
696 /**
697 * Helper function to compute the offset of texture coordinates in a vertex
698 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000699 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000700 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000701 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000702 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000703
704 /**
705 * Helper function to compute the offset of the color in a vertex
706 * @return offset of color in vertex layout or -1 if the
707 * layout has no color.
708 */
709 static int VertexColorOffset(GrVertexLayout vertexLayout);
710
bsalomon@google.coma3108262011-10-10 14:08:47 +0000711 /**
712 * Helper function to compute the offset of the coverage in a vertex
713 * @return offset of coverage in vertex layout or -1 if the
714 * layout has no coverage.
715 */
716 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
717
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000718 /**
719 * Helper function to compute the offset of the edge pts in a vertex
720 * @return offset of edge in vertex layout or -1 if the
721 * layout has no edge.
722 */
723 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
724
reed@google.comac10a2d2010-12-22 21:39:39 +0000725 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000726 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000727 * coordinates of some index.
728 *
729 * @param coordIndex the tex coord index to query
730 * @param vertexLayout layout to query
731 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000732 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000733 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000734 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000735 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000736 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000737
reed@google.comac10a2d2010-12-22 21:39:39 +0000738 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000739 * Helper function to compute the size of each vertex and the offsets of
740 * texture coordinates and color. Determines tex coord offsets by tex coord
741 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000742 * by StageTexCoordVertexLayoutBit.)
743 *
744 * @param vertexLayout the layout to query
745 * @param texCoordOffsetsByIdx after return it is the offset of each
746 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000747 * index isn't used. (optional)
748 * @param colorOffset after return it is the offset of the
749 * color field in each vertex, or -1 if
750 * there aren't per-vertex colors. (optional)
751 * @param coverageOffset after return it is the offset of the
752 * coverage field in each vertex, or -1 if
753 * there aren't per-vertex coeverages.
754 * (optional)
755 * @param edgeOffset after return it is the offset of the
756 * edge eq field in each vertex, or -1 if
757 * there aren't per-vertex edge equations.
758 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000759 * @return size of a single vertex
760 */
761 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000762 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
763 int *colorOffset,
764 int *coverageOffset,
765 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000766
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000767 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000768 * Helper function to compute the size of each vertex and the offsets of
769 * texture coordinates and color. Determines tex coord offsets by stage
770 * rather than by index. (Each stage can be mapped to any t.c. index
771 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000772 * tex coords then that stage's offset will be 0 (positions are always at 0).
773 *
774 * @param vertexLayout the layout to query
775 * @param texCoordOffsetsByStage after return it is the offset of each
776 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000777 * index isn't used. (optional)
778 * @param colorOffset after return it is the offset of the
779 * color field in each vertex, or -1 if
780 * there aren't per-vertex colors.
781 * (optional)
782 * @param coverageOffset after return it is the offset of the
783 * coverage field in each vertex, or -1 if
784 * there aren't per-vertex coeverages.
785 * (optional)
786 * @param edgeOffset after return it is the offset of the
787 * edge eq field in each vertex, or -1 if
788 * there aren't per-vertex edge equations.
789 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000790 * @return size of a single vertex
791 */
792 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000793 int texCoordOffsetsByStage[GrDrawState::kNumStages],
794 int* colorOffset,
795 int* coverageOffset,
796 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000797
798 /**
799 * Accessing positions, texture coords, or colors, of a vertex within an
800 * array is a hassle involving casts and simple math. These helpers exist
801 * to keep GrDrawTarget clients' code a bit nicer looking.
802 */
803
804 /**
805 * Gets a pointer to a GrPoint of a vertex's position or texture
806 * coordinate.
807 * @param vertices the vetex array
808 * @param vertexIndex the index of the vertex in the array
809 * @param vertexSize the size of each vertex in the array
810 * @param offset the offset in bytes of the vertex component.
811 * Defaults to zero (corresponding to vertex position)
812 * @return pointer to the vertex component as a GrPoint
813 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000814 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000815 int vertexIndex,
816 int vertexSize,
817 int offset = 0) {
818 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000819 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000820 vertexIndex * vertexSize);
821 }
822 static const GrPoint* GetVertexPoint(const void* vertices,
823 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000824 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000825 int offset = 0) {
826 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000827 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000828 vertexIndex * vertexSize);
829 }
830
831 /**
832 * Gets a pointer to a GrColor inside a vertex within a vertex array.
833 * @param vertices the vetex array
834 * @param vertexIndex the index of the vertex in the array
835 * @param vertexSize the size of each vertex in the array
836 * @param offset the offset in bytes of the vertex color
837 * @return pointer to the vertex component as a GrColor
838 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000839 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000840 int vertexIndex,
841 int vertexSize,
842 int offset) {
843 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000844 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000845 vertexIndex * vertexSize);
846 }
847 static const GrColor* GetVertexColor(const void* vertices,
848 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000849 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000850 int offset) {
851 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000852 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000853 vertexIndex * vertexSize);
854 }
855
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000856 static void VertexLayoutUnitTest();
857
reed@google.comac10a2d2010-12-22 21:39:39 +0000858protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000859
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000860 /**
861 * Optimizations for blending / coverage to be applied based on the current
862 * state.
863 * Subclasses that actually draw (as opposed to those that just buffer for
864 * playback) must implement the flags that replace the output color.
865 */
866 enum BlendOptFlags {
867 /**
868 * No optimization
869 */
870 kNone_BlendOpt = 0,
871 /**
872 * Don't draw at all
873 */
874 kSkipDraw_BlendOptFlag = 0x2,
875 /**
876 * Emit the src color, disable HW blending (replace dst with src)
877 */
878 kDisableBlend_BlendOptFlag = 0x4,
879 /**
880 * The coverage value does not have to be computed separately from
881 * alpha, the the output color can be the modulation of the two.
882 */
883 kCoverageAsAlpha_BlendOptFlag = 0x1,
884 /**
885 * Instead of emitting a src color, emit coverage in the alpha channel
886 * and r,g,b are "don't cares".
887 */
888 kEmitCoverage_BlendOptFlag = 0x10,
889 /**
890 * Emit transparent black instead of the src color, no need to compute
891 * coverage.
892 */
893 kEmitTransBlack_BlendOptFlag = 0x8,
894 };
895 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000896
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000897 // Determines what optimizations can be applied based on the blend.
898 // The coeffecients may have to be tweaked in order for the optimization
899 // to work. srcCoeff and dstCoeff are optional params that receive the
900 // tweaked coeffecients.
901 // Normally the function looks at the current state to see if coverage
902 // is enabled. By setting forceCoverage the caller can speculatively
903 // determine the blend optimizations that would be used if there was
904 // partial pixel coverage
905 BlendOptFlags getBlendOpts(bool forceCoverage = false,
906 GrBlendCoeff* srcCoeff = NULL,
907 GrBlendCoeff* dstCoeff = NULL) const;
908
909 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000910 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000911
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000912 enum GeometrySrcType {
913 kNone_GeometrySrcType, //<! src has not been specified
914 kReserved_GeometrySrcType, //<! src was set using reserve*Space
915 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
916 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
917 };
918
919 struct GeometrySrcState {
920 GeometrySrcType fVertexSrc;
921 union {
922 // valid if src type is buffer
923 const GrVertexBuffer* fVertexBuffer;
924 // valid if src type is reserved or array
925 int fVertexCount;
926 };
927
928 GeometrySrcType fIndexSrc;
929 union {
930 // valid if src type is buffer
931 const GrIndexBuffer* fIndexBuffer;
932 // valid if src type is reserved or array
933 int fIndexCount;
934 };
935
936 GrVertexLayout fVertexLayout;
937 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000938
939 int indexCountInCurrentSource() const {
940 const GeometrySrcState& src = this->getGeomSrc();
941 switch (src.fIndexSrc) {
942 case kNone_GeometrySrcType:
943 return 0;
944 case kReserved_GeometrySrcType:
945 case kArray_GeometrySrcType:
946 return src.fIndexCount;
947 case kBuffer_GeometrySrcType:
948 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
949 default:
950 GrCrash("Unexpected Index Source.");
951 return 0;
952 }
953 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000954
955 bool isStageEnabled(int stage) const {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000956 return this->getDrawState().isStageEnabled(stage);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000957 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000958
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000959 StageMask enabledStages() const {
960 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000961 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000962 mask |= this->isStageEnabled(s) ? 1 : 0;
963 }
964 return mask;
965 }
966
bsalomon@google.com97805382012-03-13 14:32:07 +0000967 // A sublcass can optionally overload this function to be notified before
968 // vertex and index space is reserved.
969 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
970 int vertexCount,
971 int indexCount) {}
972
973
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000974 // implemented by subclass to allocate space for reserved geom
975 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
976 int vertexCount,
977 void** vertices) = 0;
978 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
979 // implemented by subclass to handle release of reserved geom space
980 virtual void releaseReservedVertexSpace() = 0;
981 virtual void releaseReservedIndexSpace() = 0;
982 // subclass must consume array contents when set
983 virtual void onSetVertexSourceToArray(const void* vertexArray,
984 int vertexCount) = 0;
985 virtual void onSetIndexSourceToArray(const void* indexArray,
986 int indexCount) = 0;
987 // subclass is notified that geom source will be set away from an array
988 virtual void releaseVertexArray() = 0;
989 virtual void releaseIndexArray() = 0;
990 // subclass overrides to be notified just before geo src state
991 // is pushed/popped.
992 virtual void geometrySourceWillPush() = 0;
993 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
994 // subclass called to perform drawing
995 virtual void onDrawIndexed(GrPrimitiveType type,
996 int startVertex,
997 int startIndex,
998 int vertexCount,
999 int indexCount) = 0;
1000 virtual void onDrawNonIndexed(GrPrimitiveType type,
1001 int startVertex,
1002 int vertexCount) = 0;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001003 virtual void onStencilPath(const GrPath*, GrPathFill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001004
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001005 // subclass overrides to be notified when clip is set. Must call
1006 // INHERITED::clipwillBeSet
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001007 virtual void clipWillBeSet(const GrClip& clip) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001008
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001009 // Helpers for drawRect, protected so subclasses that override drawRect
1010 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001011 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001012 const GrRect* srcRects[]);
1013
1014 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001015 const GrMatrix* matrix,
1016 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001017 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001018 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001019 void* vertices);
1020
bsalomon@google.come79c8152012-03-29 19:07:12 +00001021 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001022 const GeometrySrcState& getGeomSrc() const {
1023 return fGeoSrcStateStack.back();
1024 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001025 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1026 // because of the assert.
1027 GrVertexLayout getVertexLayout() const {
1028 // the vertex layout is only valid if a vertex source has been
1029 // specified.
1030 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1031 return this->getGeomSrc().fVertexLayout;
1032 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001033
1034 GrClip fClip;
1035
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001036 GrDrawState* fDrawState;
1037 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001038
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001039 Caps fCaps;
1040
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001041 // subclasses must call this in their destructors to ensure all vertex
1042 // and index sources have been released (including those held by
1043 // pushGeometrySource())
1044 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001045
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001046private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001047 // helpers for reserving vertex and index space.
1048 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1049 int vertexCount,
1050 void** vertices);
1051 bool reserveIndexSpace(int indexCount, void** indices);
1052
bsalomon@google.come8262622011-11-07 02:30:51 +00001053 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1054 // indicate non-indexed drawing.
1055 bool checkDraw(GrPrimitiveType type, int startVertex,
1056 int startIndex, int vertexCount,
1057 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001058 // called when setting a new vert/idx source to unref prev vb/ib
1059 void releasePreviousVertexSource();
1060 void releasePreviousIndexSource();
1061
1062 enum {
1063 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001065 SkSTArray<kPreallocGeoSrcStateStackCnt,
1066 GeometrySrcState, true> fGeoSrcStateStack;
reed@google.comfa35e3d2012-06-26 20:16:17 +00001067
1068 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069};
1070
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001071GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1072
reed@google.comac10a2d2010-12-22 21:39:39 +00001073#endif