blob: ca499dbff04a36e18311ffaf9355ea3d8ed5e5ae [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"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrColor.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000016#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000017#include "GrIndexBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018#include "GrMatrix.h"
19#include "GrRefCnt.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000020#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000021#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000022#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
Scroggo97c88c22011-05-11 14:05:25 +000024#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000025#include "SkTLazy.h"
Scroggo97c88c22011-05-11 14:05:25 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000028class GrClipIterator;
29class GrVertexBuffer;
30class GrIndexBuffer;
31
32class GrDrawTarget : public GrRefCnt {
33public:
34 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000035 * Represents the draw target capabilities.
36 */
37 struct Caps {
38 Caps() { memset(this, 0, sizeof(Caps)); }
39 Caps(const Caps& c) { *this = c; }
40 Caps& operator= (const Caps& c) {
41 memcpy(this, &c, sizeof(Caps));
42 return *this;
43 }
44 void print() const;
45 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000047 bool fTwoSidedStencilSupport : 1;
48 bool fStencilWrapOpsSupport : 1;
49 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000050 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000051 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000052 bool fFSAASupport : 1;
53 bool fDualSourceBlendingSupport : 1;
54 bool fBufferLockSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000055 int fMaxRenderTargetSize;
56 int fMaxTextureSize;
57 };
58
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000059 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000060 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000061
reed@google.comac10a2d2010-12-22 21:39:39 +000062 ///////////////////////////////////////////////////////////////////////////
63
64 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000065 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000068 * Gets the capabilities of the draw target.
69 */
70 const Caps& getCaps() const { return fCaps; }
71
72 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000073 * Sets the current clip to the region specified by clip. All draws will be
74 * clipped against this clip if kClip_StateBit is enabled.
75 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000076 * Setting the clip may (or may not) zero out the client's stencil bits.
77 *
reed@google.comac10a2d2010-12-22 21:39:39 +000078 * @param description of the clipping region
79 */
80 void setClip(const GrClip& clip);
81
82 /**
83 * Gets the current clip.
84 *
85 * @return the clip.
86 */
87 const GrClip& getClip() const;
88
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000089 /**
90 * Sets the draw state object for the draw target. Note that this does not
91 * make a copy. The GrDrawTarget will take a reference to passed object.
92 * Passing NULL will cause the GrDrawTarget to use its own internal draw
93 * state object rather than an externally provided one.
94 */
95 void setDrawState(GrDrawState* drawState);
96
97 /**
98 * Read-only access to the GrDrawTarget's current draw state.
99 */
100 const GrDrawState& getDrawState() const { return *fDrawState; }
101
102 /**
103 * Read-write access to the GrDrawTarget's current draw state. Note that
104 * this doesn't ref.
105 */
106 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000107
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000108 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000109 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
110 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000111 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000112 * @param matrix the matrix to concat
113 */
114 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
115 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000116 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000117 }
118
119 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000120 * Color alpha and coverage are two inputs to the drawing pipeline. For some
121 * blend modes it is safe to fold the coverage into constant or per-vertex
122 * color alpha value. For other blend modes they must be handled separately.
123 * Depending on features available in the underlying 3D API this may or may
124 * not be possible.
125 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000126 * This function considers the current draw state and the draw target's
127 * capabilities to determine whether coverage can be handled correctly. The
128 * following assumptions are made:
129 * 1. The caller intends to somehow specify coverage. This can be
130 * specified either by enabling a coverage stage on the GrDrawState or
131 * via the vertex layout.
132 * 2. Other than enabling coverage stages, the current configuration of
133 * the target's GrDrawState is as it will be at draw time.
134 * 3. If a vertex source has not yet been specified then all stages with
135 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000136 */
137 bool canApplyCoverage() const;
138
139 /**
140 * Determines whether incorporating partial pixel coverage into the constant
141 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000142 * blending result. If a vertex source has not yet been specified then
143 * the function assumes that all stages with non-NULL textures will be
144 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000145 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000146 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000147
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000148 /**
bsalomon@google.come79c8152012-03-29 19:07:12 +0000149 * Given the current draw state and hw support, will HW AA lines be used
150 * (if line primitive type is drawn)? If a vertex source has not yet been
151 * specified then the function assumes that all stages with non-NULL
152 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000153 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000154 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000155
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000156 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000157 * The format of vertices is represented as a bitfield of flags.
158 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000159 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
160 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
161 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000162 * use any of the texture coordinates as its input texture coordinates or it
163 * may use the positions as texture coordinates.
164 *
165 * If no texture coordinates are specified for a stage then the stage is
166 * disabled.
167 *
168 * Only one type of texture coord can be specified per stage. For
169 * example StageTexCoordVertexLayoutBit(0, 2) and
170 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
171 *
172 * The order in memory is always (position, texture coord 0, ..., color,
173 * coverage) with any unused fields omitted. Note that this means that if
174 * only texture coordinates 1 is referenced then there is no texture
175 * coordinates 0 and the order would be (position, texture coordinate 1
176 * [, color][, coverage]).
177 */
178
179 /**
180 * Generates a bit indicating that a texture stage uses texture coordinates
181 *
182 * @param stage the stage that will use texture coordinates.
183 * @param texCoordIdx the index of the texture coordinates to use
184 *
185 * @return the bit to add to a GrVertexLayout bitfield.
186 */
187 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000188 GrAssert(stage < GrDrawState::kNumStages);
189 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
190 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000191 }
192
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000193private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000194 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
195 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000196
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000197public:
198 /**
199 * Generates a bit indicating that a texture stage uses the position
200 * as its texture coordinate.
201 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000202 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000203 * coordinates.
204 *
205 * @return the bit to add to a GrVertexLayout bitfield.
206 */
207 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000208 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000209 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000210 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000211
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000212private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000213 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
214 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000215
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000216public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000217
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000218 /**
219 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 */
221 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000222 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000223 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000224 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000225 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000226 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000228 * text [GrGpuTextVertex vs GrPoint].)
229 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000230 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000232 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000233 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000234 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000235 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000236 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000237 kDummyVertexLayoutBit,
238 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000240 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000241 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
243 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000244 * There are three types of "sources" of geometry (vertices and indices) for
245 * draw calls made on the target. When performing an indexed draw, the
246 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000247 * specified it can be used for multiple draws. However, the time at which
248 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000249 *
250 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000251 * already specified geometry that it isn't finished with. So there are push
252 * and pop methods. This allows the client to push the sources, draw
253 * something using alternate sources, and then pop to restore the original
254 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000255 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000256 * Aside from pushes and pops, a source remains valid until another source
257 * is set or resetVertexSource / resetIndexSource is called. Drawing from
258 * a reset source is an error.
259 *
260 * The three types of sources are:
261 *
262 * 1. A cpu array (set*SourceToArray). This is useful when the caller
263 * already provided vertex data in a format compatible with a
264 * GrVertexLayout. The data in the array is consumed at the time that
265 * set*SourceToArray is called and subsequent edits to the array will not
266 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000267 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000268 * 2. Reserve. This is most useful when the caller has data it must
269 * transform before drawing and is not long-lived. The caller requests
270 * that the draw target make room for some amount of vertex and/or index
271 * data. The target provides ptrs to hold the vertex and/or index data.
272 *
273 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000274 * drawIndexedInstances, or pushGeometrySource. At this point the data is
275 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000276 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000277 * Where the space is allocated and how it is uploaded to the GPU is
278 * subclass-dependent.
279 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000280 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000281 * is long-lived. When the data in the buffer is consumed depends on the
282 * GrDrawTarget subclass. For deferred subclasses the caller has to
283 * guarantee that the data is still available in the buffers at playback.
284 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000285 */
286
287 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000288 * Reserves space for vertices and/or indices. Zero can be specifed as
289 * either the vertex or index count if the caller desires to only reserve
290 * space for only indices or only vertices. If zero is specifed for
291 * vertexCount then the vertex source will be unmodified and likewise for
292 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000294 * If the function returns true then the reserve suceeded and the vertices
295 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000297 * If the target cannot make space for the request then this function will
298 * return false. If vertexCount was non-zero then upon failure the vertex
299 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000301 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000302 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
303 * popGeomtrySource is called. At that point logically a snapshot of the
304 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000305 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000306 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000307 * @param vertexCount the number of vertices to reserve space for. Can be
308 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000309 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000310 * @param vertices will point to reserved vertex space if vertexCount is
311 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000312 * @param indices will point to reserved index space if indexCount is
313 * non-zero. Illegal to pass NULL if indexCount > 0.
314 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000315 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
316 int vertexCount,
317 int indexCount,
318 void** vertices,
319 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000320
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 /**
322 * Provides hints to caller about the number of vertices and indices
323 * that can be allocated cheaply. This can be useful if caller is reserving
324 * space but doesn't know exactly how much geometry is needed.
325 *
326 * Also may hint whether the draw target should be flushed first. This is
327 * useful for deferred targets.
328 *
329 * @param vertexLayout layout of vertices caller would like to reserve
330 * @param vertexCount in: hint about how many vertices the caller would
331 * like to allocate.
332 * out: a hint about the number of vertices that can be
333 * allocated cheaply. Negative means no hint.
334 * Ignored if NULL.
335 * @param indexCount in: hint about how many indices the caller would
336 * like to allocate.
337 * out: a hint about the number of indices that can be
338 * allocated cheaply. Negative means no hint.
339 * Ignored if NULL.
340 *
341 * @return true if target should be flushed based on the input values.
342 */
343 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000344 int* vertexCount,
345 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000348 * Sets source of vertex data for the next draw. Array must contain
349 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 *
351 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000352 * @param size size of the vertex data.
353 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000355 void setVertexSourceToArray(GrVertexLayout vertexLayout,
356 const void* vertexArray,
357 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000358
359 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000360 * Sets source of index data for the next indexed draw. Array must contain
361 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000363 * @param array cpu array containing index data.
364 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000366 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000367
368 /**
369 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000370 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 *
372 * @param buffer vertex buffer containing vertex data. Must be
373 * unlocked before draw call.
374 * @param vertexLayout layout of the vertex data in the buffer.
375 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000376 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
377 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378
379 /**
380 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000381 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 *
383 * @param buffer index buffer containing indices. Must be unlocked
384 * before indexed draw call.
385 */
386 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387
388 /**
389 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
390 * source to reserved, array, or buffer before next draw. May be able to free
391 * up temporary storage allocated by setVertexSourceToArray or
392 * reserveVertexSpace.
393 */
394 void resetVertexSource();
395
396 /**
397 * Resets index source. Indexed Drawing from reset indices is illegal. Set
398 * index source to reserved, array, or buffer before next indexed draw. May
399 * be able to free up temporary storage allocated by setIndexSourceToArray
400 * or reserveIndexSpace.
401 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000402 void resetIndexSource();
403
404 /**
405 * Query to find out if the vertex or index source is reserved.
406 */
407 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000408 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000409 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
410 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000411
412 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000413 * Pushes and resets the vertex/index sources. Any reserved vertex / index
414 * data is finalized (i.e. cannot be updated after the matching pop but can
415 * be drawn from). Must be balanced by a pop.
416 */
417 void pushGeometrySource();
418
419 /**
420 * Pops the vertex / index sources from the matching push.
421 */
422 void popGeometrySource();
423
424 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000425 * Draws indexed geometry using the current state and current vertex / index
426 * sources.
427 *
428 * @param type The type of primitives to draw.
429 * @param startVertex the vertex in the vertex array/buffer corresponding
430 * to index 0
431 * @param startIndex first index to read from index src.
432 * @param vertexCount one greater than the max index.
433 * @param indexCount the number of index elements to read. The index count
434 * is effectively trimmed to the last completely
435 * specified primitive.
436 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000437 void drawIndexed(GrPrimitiveType type,
438 int startVertex,
439 int startIndex,
440 int vertexCount,
441 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
443 /**
444 * Draws non-indexed geometry using the current state and current vertex
445 * sources.
446 *
447 * @param type The type of primitives to draw.
448 * @param startVertex the vertex in the vertex array/buffer corresponding
449 * to index 0
450 * @param vertexCount one greater than the max index.
451 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000452 void drawNonIndexed(GrPrimitiveType type,
453 int startVertex,
454 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000456 /**
457 * Helper function for drawing rects. This does not use the current index
458 * and vertex sources. After returning, the vertex and index sources may
459 * have changed. They should be reestablished before the next drawIndexed
460 * or drawNonIndexed. This cannot be called between reserving and releasing
461 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000462 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000463 * drawNonIndexed.
464 * @param rect the rect to draw
465 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000466 * @param stageMask bitmask indicating which stages are enabled.
467 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000468 * @param srcRects specifies rects for stages enabled by stageEnableMask.
469 * if stageEnableMask bit i is 1, srcRects is not NULL,
470 * and srcRects[i] is not NULL, then srcRects[i] will be
471 * used as coordinates for stage i. Otherwise, if stage i
472 * is enabled then rect is used as the coordinates.
473 * @param srcMatrices optional matrices applied to srcRects. If
474 * srcRect[i] is non-NULL and srcMatrices[i] is
475 * non-NULL then srcRect[i] will be transformed by
476 * srcMatrix[i]. srcMatrices can be NULL when no
477 * srcMatrices are desired.
478 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000479 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000480 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000481 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000482 const GrRect* srcRects[],
483 const GrMatrix* srcMatrices[]);
484
485 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000486 * This call is used to draw multiple instances of some geometry with a
487 * given number of vertices (V) and indices (I) per-instance. The indices in
488 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
489 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
490 * concrete example, the following index buffer for drawing a series of
491 * quads each as two triangles each satisfies these conditions with V=4 and
492 * I=6:
493 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
494 *
495 * The call assumes that the pattern of indices fills the entire index
496 * source. The size of the index buffer limits the number of instances that
497 * can be drawn by the GPU in a single draw. However, the caller may specify
498 * any (positive) number for instanceCount and if necessary multiple GPU
499 * draws will be issued. Morever, when drawIndexedInstances is called
500 * multiple times it may be possible for GrDrawTarget to group them into a
501 * single GPU draw.
502 *
503 * @param type the type of primitives to draw
504 * @param instanceCount the number of instances to draw. Each instance
505 * consists of verticesPerInstance vertices indexed by
506 * indicesPerInstance indices drawn as the primitive
507 * type specified by type.
508 * @param verticesPerInstance The number of vertices in each instance (V
509 * in the above description).
510 * @param indicesPerInstance The number of indices in each instance (I
511 * in the above description).
512 */
513 virtual void drawIndexedInstances(GrPrimitiveType type,
514 int instanceCount,
515 int verticesPerInstance,
516 int indicesPerInstance);
517
518 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000519 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000520 * matrices.
521 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000522 void drawSimpleRect(const GrRect& rect,
523 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000524 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000525 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000526 }
527
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000528 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000529 * Clear the render target. Ignores the clip and all other draw state
530 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
531 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000532 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000533 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000534
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000535 /**
536 * Returns the maximum number of edges that may be specified in a single
537 * draw call when performing edge antialiasing. This is usually limited
538 * by the number of fragment uniforms which may be uploaded. Must be a
539 * minimum of six, since a triangle's vertices each belong to two boundary
540 * edges which may be distinct.
541 */
542 virtual int getMaxEdges() const { return 6; }
543
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000544 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000546 /**
547 * See AutoStateRestore below.
548 */
549 enum ASRInit {
550 kPreserve_ASRInit,
551 kReset_ASRInit
552 };
553
554 /**
555 * Saves off the current state and restores it in the destructor. It will
556 * install a new GrDrawState object on the target (setDrawState) and restore
557 * the previous one in the destructor. The caller should call drawState() to
558 * get the new draw state after the ASR is installed.
559 *
560 * GrDrawState* state = target->drawState();
561 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
562 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
563 * // target before asr was initialized.
564 * // Therefore, rt is set on the GrDrawState
565 * // that will be restored after asr's
566 * // destructor rather than target's current
567 * // GrDrawState.
568 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 class AutoStateRestore : ::GrNoncopyable {
570 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000571 /**
572 * Default ASR will have no effect unless set() is subsequently called.
573 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000574 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000575
576 /**
577 * Saves the state on target. The state will be restored when the ASR
578 * is destroyed. If this constructor is used do not call set().
579 *
580 * @param init Should the newly installed GrDrawState be a copy of the
581 * previous state or a default-initialized GrDrawState.
582 */
583 AutoStateRestore(GrDrawTarget* target, ASRInit init);
584
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 ~AutoStateRestore();
586
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000587 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000588 * Saves the state on target. The state will be restored when the ASR
589 * is destroyed. This should only be called once per ASR object and only
590 * when the default constructor was used. For nested saves use multiple
591 * ASR objects.
592 *
593 * @param init Should the newly installed GrDrawState be a copy of the
594 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000595 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000596 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000597
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000599 GrDrawTarget* fDrawTarget;
600 SkTLazy<GrDrawState> fTempState;
601 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000602 };
603
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000604 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000605
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000606 /**
607 * Sets the view matrix to I and preconcats all stage matrices enabled in
608 * mask by the view inverse. Destructor undoes these changes.
609 */
610 class AutoDeviceCoordDraw : ::GrNoncopyable {
611 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000612 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000613 ~AutoDeviceCoordDraw();
614 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000615 GrDrawTarget* fDrawTarget;
616 GrMatrix fViewMatrix;
617 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
618 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000619 };
620
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000621 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000622
reed@google.comac10a2d2010-12-22 21:39:39 +0000623 class AutoReleaseGeometry : ::GrNoncopyable {
624 public:
625 AutoReleaseGeometry(GrDrawTarget* target,
626 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000627 int vertexCount,
628 int indexCount);
629 AutoReleaseGeometry();
630 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000631 bool set(GrDrawTarget* target,
632 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000633 int vertexCount,
634 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000635 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000636 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
637 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000639 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000640 }
641
642 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643 void reset();
644
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 void* fVertices;
647 void* fIndices;
648 };
649
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000650 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000651
652 class AutoClipRestore : ::GrNoncopyable {
653 public:
654 AutoClipRestore(GrDrawTarget* target) {
655 fTarget = target;
656 fClip = fTarget->getClip();
657 }
658
659 ~AutoClipRestore() {
660 fTarget->setClip(fClip);
661 }
662 private:
663 GrDrawTarget* fTarget;
664 GrClip fClip;
665 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000666
667 ////////////////////////////////////////////////////////////////////////////
668
669 class AutoGeometryPush : ::GrNoncopyable {
670 public:
671 AutoGeometryPush(GrDrawTarget* target) {
672 GrAssert(NULL != target);
673 fTarget = target;
674 target->pushGeometrySource();
675 }
676 ~AutoGeometryPush() {
677 fTarget->popGeometrySource();
678 }
679 private:
680 GrDrawTarget* fTarget;
681 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000682
683 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000684 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000685
reed@google.comac10a2d2010-12-22 21:39:39 +0000686 /**
687 * Helper function to compute the size of a vertex from a vertex layout
688 * @return size of a single vertex.
689 */
690 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000691
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000692 /**
693 * Helper function for determining the index of texture coordinates that
694 * is input for a texture stage. Note that a stage may instead use positions
695 * as texture coordinates, in which case the result of the function is
696 * indistinguishable from the case when the stage is disabled.
697 *
698 * @param stage the stage to query
699 * @param vertexLayout layout to query
700 *
701 * @return the texture coordinate index or -1 if the stage doesn't use
702 * separate (non-position) texture coordinates.
703 */
704 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000705
706 /**
707 * Helper function to compute the offset of texture coordinates in a vertex
708 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000709 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000710 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000711 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000712 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000713
714 /**
715 * Helper function to compute the offset of the color in a vertex
716 * @return offset of color in vertex layout or -1 if the
717 * layout has no color.
718 */
719 static int VertexColorOffset(GrVertexLayout vertexLayout);
720
bsalomon@google.coma3108262011-10-10 14:08:47 +0000721 /**
722 * Helper function to compute the offset of the coverage in a vertex
723 * @return offset of coverage in vertex layout or -1 if the
724 * layout has no coverage.
725 */
726 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
727
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000728 /**
729 * Helper function to compute the offset of the edge pts in a vertex
730 * @return offset of edge in vertex layout or -1 if the
731 * layout has no edge.
732 */
733 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
734
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000736 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000737 * coordinates of some index.
738 *
739 * @param coordIndex the tex coord index to query
740 * @param vertexLayout layout to query
741 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000742 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000743 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000744 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000745 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000746 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000747
reed@google.comac10a2d2010-12-22 21:39:39 +0000748 /**
749 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000750 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000751 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000752 * @param stage the stage to query
753 * @param vertexLayout layout to query
754 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000755 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000756 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000757 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000758 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000759
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000760 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000761 * Helper function to compute the size of each vertex and the offsets of
762 * texture coordinates and color. Determines tex coord offsets by tex coord
763 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000764 * by StageTexCoordVertexLayoutBit.)
765 *
766 * @param vertexLayout the layout to query
767 * @param texCoordOffsetsByIdx after return it is the offset of each
768 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000769 * index isn't used. (optional)
770 * @param colorOffset after return it is the offset of the
771 * color field in each vertex, or -1 if
772 * there aren't per-vertex colors. (optional)
773 * @param coverageOffset after return it is the offset of the
774 * coverage field in each vertex, or -1 if
775 * there aren't per-vertex coeverages.
776 * (optional)
777 * @param edgeOffset after return it is the offset of the
778 * edge eq field in each vertex, or -1 if
779 * there aren't per-vertex edge equations.
780 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000781 * @return size of a single vertex
782 */
783 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000784 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
785 int *colorOffset,
786 int *coverageOffset,
787 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000788
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000789 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000790 * Helper function to compute the size of each vertex and the offsets of
791 * texture coordinates and color. Determines tex coord offsets by stage
792 * rather than by index. (Each stage can be mapped to any t.c. index
793 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000794 * tex coords then that stage's offset will be 0 (positions are always at 0).
795 *
796 * @param vertexLayout the layout to query
797 * @param texCoordOffsetsByStage after return it is the offset of each
798 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000799 * index isn't used. (optional)
800 * @param colorOffset after return it is the offset of the
801 * color field in each vertex, or -1 if
802 * there aren't per-vertex colors.
803 * (optional)
804 * @param coverageOffset after return it is the offset of the
805 * coverage field in each vertex, or -1 if
806 * there aren't per-vertex coeverages.
807 * (optional)
808 * @param edgeOffset after return it is the offset of the
809 * edge eq field in each vertex, or -1 if
810 * there aren't per-vertex edge equations.
811 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000812 * @return size of a single vertex
813 */
814 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000815 int texCoordOffsetsByStage[GrDrawState::kNumStages],
816 int* colorOffset,
817 int* coverageOffset,
818 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000819
820 /**
821 * Accessing positions, texture coords, or colors, of a vertex within an
822 * array is a hassle involving casts and simple math. These helpers exist
823 * to keep GrDrawTarget clients' code a bit nicer looking.
824 */
825
826 /**
827 * Gets a pointer to a GrPoint of a vertex's position or texture
828 * coordinate.
829 * @param vertices the vetex array
830 * @param vertexIndex the index of the vertex in the array
831 * @param vertexSize the size of each vertex in the array
832 * @param offset the offset in bytes of the vertex component.
833 * Defaults to zero (corresponding to vertex position)
834 * @return pointer to the vertex component as a GrPoint
835 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000836 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000837 int vertexIndex,
838 int vertexSize,
839 int offset = 0) {
840 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000841 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000842 vertexIndex * vertexSize);
843 }
844 static const GrPoint* GetVertexPoint(const void* vertices,
845 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000846 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000847 int offset = 0) {
848 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000849 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000850 vertexIndex * vertexSize);
851 }
852
853 /**
854 * Gets a pointer to a GrColor inside a vertex within a vertex array.
855 * @param vertices the vetex array
856 * @param vertexIndex the index of the vertex in the array
857 * @param vertexSize the size of each vertex in the array
858 * @param offset the offset in bytes of the vertex color
859 * @return pointer to the vertex component as a GrColor
860 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000861 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000862 int vertexIndex,
863 int vertexSize,
864 int offset) {
865 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000866 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000867 vertexIndex * vertexSize);
868 }
869 static const GrColor* GetVertexColor(const void* vertices,
870 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000871 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000872 int offset) {
873 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000874 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000875 vertexIndex * vertexSize);
876 }
877
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000878 static void VertexLayoutUnitTest();
879
reed@google.comac10a2d2010-12-22 21:39:39 +0000880protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000881
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000882 /**
883 * Optimizations for blending / coverage to be applied based on the current
884 * state.
885 * Subclasses that actually draw (as opposed to those that just buffer for
886 * playback) must implement the flags that replace the output color.
887 */
888 enum BlendOptFlags {
889 /**
890 * No optimization
891 */
892 kNone_BlendOpt = 0,
893 /**
894 * Don't draw at all
895 */
896 kSkipDraw_BlendOptFlag = 0x2,
897 /**
898 * Emit the src color, disable HW blending (replace dst with src)
899 */
900 kDisableBlend_BlendOptFlag = 0x4,
901 /**
902 * The coverage value does not have to be computed separately from
903 * alpha, the the output color can be the modulation of the two.
904 */
905 kCoverageAsAlpha_BlendOptFlag = 0x1,
906 /**
907 * Instead of emitting a src color, emit coverage in the alpha channel
908 * and r,g,b are "don't cares".
909 */
910 kEmitCoverage_BlendOptFlag = 0x10,
911 /**
912 * Emit transparent black instead of the src color, no need to compute
913 * coverage.
914 */
915 kEmitTransBlack_BlendOptFlag = 0x8,
916 };
917 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000918
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000919 // Determines what optimizations can be applied based on the blend.
920 // The coeffecients may have to be tweaked in order for the optimization
921 // to work. srcCoeff and dstCoeff are optional params that receive the
922 // tweaked coeffecients.
923 // Normally the function looks at the current state to see if coverage
924 // is enabled. By setting forceCoverage the caller can speculatively
925 // determine the blend optimizations that would be used if there was
926 // partial pixel coverage
927 BlendOptFlags getBlendOpts(bool forceCoverage = false,
928 GrBlendCoeff* srcCoeff = NULL,
929 GrBlendCoeff* dstCoeff = NULL) const;
930
931 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000932 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000933
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000934 enum GeometrySrcType {
935 kNone_GeometrySrcType, //<! src has not been specified
936 kReserved_GeometrySrcType, //<! src was set using reserve*Space
937 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
938 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
939 };
940
941 struct GeometrySrcState {
942 GeometrySrcType fVertexSrc;
943 union {
944 // valid if src type is buffer
945 const GrVertexBuffer* fVertexBuffer;
946 // valid if src type is reserved or array
947 int fVertexCount;
948 };
949
950 GeometrySrcType fIndexSrc;
951 union {
952 // valid if src type is buffer
953 const GrIndexBuffer* fIndexBuffer;
954 // valid if src type is reserved or array
955 int fIndexCount;
956 };
957
958 GrVertexLayout fVertexLayout;
959 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000960
961 int indexCountInCurrentSource() const {
962 const GeometrySrcState& src = this->getGeomSrc();
963 switch (src.fIndexSrc) {
964 case kNone_GeometrySrcType:
965 return 0;
966 case kReserved_GeometrySrcType:
967 case kArray_GeometrySrcType:
968 return src.fIndexCount;
969 case kBuffer_GeometrySrcType:
970 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
971 default:
972 GrCrash("Unexpected Index Source.");
973 return 0;
974 }
975 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000976 // given a vertex layout and a draw state, will a stage be used?
977 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000978 const GrDrawState& state) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000979 return NULL != state.getTexture(stage) &&
980 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000981 }
982
983 bool isStageEnabled(int stage) const {
bsalomon@google.come79c8152012-03-29 19:07:12 +0000984 return StageWillBeUsed(stage, this->getVertexLayout(),
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000985 this->getDrawState());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000986 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000987
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000988 StageMask enabledStages() const {
989 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000990 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000991 mask |= this->isStageEnabled(s) ? 1 : 0;
992 }
993 return mask;
994 }
995
bsalomon@google.com97805382012-03-13 14:32:07 +0000996 // A sublcass can optionally overload this function to be notified before
997 // vertex and index space is reserved.
998 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
999 int vertexCount,
1000 int indexCount) {}
1001
1002
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001003 // implemented by subclass to allocate space for reserved geom
1004 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1005 int vertexCount,
1006 void** vertices) = 0;
1007 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1008 // implemented by subclass to handle release of reserved geom space
1009 virtual void releaseReservedVertexSpace() = 0;
1010 virtual void releaseReservedIndexSpace() = 0;
1011 // subclass must consume array contents when set
1012 virtual void onSetVertexSourceToArray(const void* vertexArray,
1013 int vertexCount) = 0;
1014 virtual void onSetIndexSourceToArray(const void* indexArray,
1015 int indexCount) = 0;
1016 // subclass is notified that geom source will be set away from an array
1017 virtual void releaseVertexArray() = 0;
1018 virtual void releaseIndexArray() = 0;
1019 // subclass overrides to be notified just before geo src state
1020 // is pushed/popped.
1021 virtual void geometrySourceWillPush() = 0;
1022 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1023 // subclass called to perform drawing
1024 virtual void onDrawIndexed(GrPrimitiveType type,
1025 int startVertex,
1026 int startIndex,
1027 int vertexCount,
1028 int indexCount) = 0;
1029 virtual void onDrawNonIndexed(GrPrimitiveType type,
1030 int startVertex,
1031 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001032 // subclass overrides to be notified when clip is set. Must call
1033 // INHERITED::clipwillBeSet
1034 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001035
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001036 // Helpers for drawRect, protected so subclasses that override drawRect
1037 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001038 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001039 const GrRect* srcRects[]);
1040
1041 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001042 const GrMatrix* matrix,
1043 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001044 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001045 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001046 void* vertices);
1047
bsalomon@google.come79c8152012-03-29 19:07:12 +00001048 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001049 const GeometrySrcState& getGeomSrc() const {
1050 return fGeoSrcStateStack.back();
1051 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001052 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1053 // because of the assert.
1054 GrVertexLayout getVertexLayout() const {
1055 // the vertex layout is only valid if a vertex source has been
1056 // specified.
1057 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1058 return this->getGeomSrc().fVertexLayout;
1059 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001060
1061 GrClip fClip;
1062
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001063 GrDrawState* fDrawState;
1064 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001066 Caps fCaps;
1067
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001068 // subclasses must call this in their destructors to ensure all vertex
1069 // and index sources have been released (including those held by
1070 // pushGeometrySource())
1071 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001072
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001073private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001074 // helpers for reserving vertex and index space.
1075 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1076 int vertexCount,
1077 void** vertices);
1078 bool reserveIndexSpace(int indexCount, void** indices);
1079
bsalomon@google.come8262622011-11-07 02:30:51 +00001080 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1081 // indicate non-indexed drawing.
1082 bool checkDraw(GrPrimitiveType type, int startVertex,
1083 int startIndex, int vertexCount,
1084 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001085 // called when setting a new vert/idx source to unref prev vb/ib
1086 void releasePreviousVertexSource();
1087 void releasePreviousIndexSource();
1088
1089 enum {
1090 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001092 SkSTArray<kPreallocGeoSrcStateStackCnt,
1093 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001094
reed@google.comac10a2d2010-12-22 21:39:39 +00001095};
1096
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001097GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1098
reed@google.comac10a2d2010-12-22 21:39:39 +00001099#endif