blob: 8f5be3c5002f2d7a11262b72943302a535cd8461 [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 * Used to save and restore the GrGpu's drawing state
158 */
159 struct SavedDrawState {
160 private:
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000161 SkTLazy<GrDrawState> fState;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000162 friend class GrDrawTarget;
163 };
164
165 /**
166 * Saves the current draw state. The state can be restored at a later time
167 * with restoreDrawState.
168 *
169 * See also AutoStateRestore class.
170 *
171 * @param state will hold the state after the function returns.
172 */
173 void saveCurrentDrawState(SavedDrawState* state) const;
174
175 /**
176 * Restores previously saved draw state. The client guarantees that state
177 * was previously passed to saveCurrentDrawState and that the rendertarget
178 * and texture set at save are still valid.
179 *
180 * See also AutoStateRestore class.
181 *
182 * @param state the previously saved state to restore.
183 */
184 void restoreDrawState(const SavedDrawState& state);
185
186 /**
187 * Copies the draw state from another target to this target.
188 *
189 * @param srcTarget draw target used as src of the draw state.
190 */
bsalomon@google.com7c3578a2012-03-19 21:18:04 +0000191 void copyDrawState(const GrDrawTarget& srcTarget);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000192
193 /**
194 * The format of vertices is represented as a bitfield of flags.
195 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000196 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
197 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
198 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000199 * use any of the texture coordinates as its input texture coordinates or it
200 * may use the positions as texture coordinates.
201 *
202 * If no texture coordinates are specified for a stage then the stage is
203 * disabled.
204 *
205 * Only one type of texture coord can be specified per stage. For
206 * example StageTexCoordVertexLayoutBit(0, 2) and
207 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
208 *
209 * The order in memory is always (position, texture coord 0, ..., color,
210 * coverage) with any unused fields omitted. Note that this means that if
211 * only texture coordinates 1 is referenced then there is no texture
212 * coordinates 0 and the order would be (position, texture coordinate 1
213 * [, color][, coverage]).
214 */
215
216 /**
217 * Generates a bit indicating that a texture stage uses texture coordinates
218 *
219 * @param stage the stage that will use texture coordinates.
220 * @param texCoordIdx the index of the texture coordinates to use
221 *
222 * @return the bit to add to a GrVertexLayout bitfield.
223 */
224 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000225 GrAssert(stage < GrDrawState::kNumStages);
226 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
227 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000228 }
229
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000230private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000231 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
232 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000233
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000234public:
235 /**
236 * Generates a bit indicating that a texture stage uses the position
237 * as its texture coordinate.
238 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000239 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000240 * coordinates.
241 *
242 * @return the bit to add to a GrVertexLayout bitfield.
243 */
244 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000245 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000246 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000247 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000248
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000249private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000250 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
251 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000252
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000253public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000254
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000255 /**
256 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 */
258 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000259 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000260 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000261 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000262 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000263 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000264 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000265 * text [GrGpuTextVertex vs GrPoint].)
266 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000267 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000268
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000269 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000270 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000271 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000272 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000274 kDummyVertexLayoutBit,
275 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000277 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000278 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
280 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000281 * There are three types of "sources" of geometry (vertices and indices) for
282 * draw calls made on the target. When performing an indexed draw, the
283 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000284 * specified it can be used for multiple draws. However, the time at which
285 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000286 *
287 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000288 * already specified geometry that it isn't finished with. So there are push
289 * and pop methods. This allows the client to push the sources, draw
290 * something using alternate sources, and then pop to restore the original
291 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000292 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000293 * Aside from pushes and pops, a source remains valid until another source
294 * is set or resetVertexSource / resetIndexSource is called. Drawing from
295 * a reset source is an error.
296 *
297 * The three types of sources are:
298 *
299 * 1. A cpu array (set*SourceToArray). This is useful when the caller
300 * already provided vertex data in a format compatible with a
301 * GrVertexLayout. The data in the array is consumed at the time that
302 * set*SourceToArray is called and subsequent edits to the array will not
303 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000304 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000305 * 2. Reserve. This is most useful when the caller has data it must
306 * transform before drawing and is not long-lived. The caller requests
307 * that the draw target make room for some amount of vertex and/or index
308 * data. The target provides ptrs to hold the vertex and/or index data.
309 *
310 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000311 * drawIndexedInstances, or pushGeometrySource. At this point the data is
312 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000313 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000314 * Where the space is allocated and how it is uploaded to the GPU is
315 * subclass-dependent.
316 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000317 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000318 * is long-lived. When the data in the buffer is consumed depends on the
319 * GrDrawTarget subclass. For deferred subclasses the caller has to
320 * guarantee that the data is still available in the buffers at playback.
321 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000322 */
323
324 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000325 * Reserves space for vertices and/or indices. Zero can be specifed as
326 * either the vertex or index count if the caller desires to only reserve
327 * space for only indices or only vertices. If zero is specifed for
328 * vertexCount then the vertex source will be unmodified and likewise for
329 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000330 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000331 * If the function returns true then the reserve suceeded and the vertices
332 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000334 * If the target cannot make space for the request then this function will
335 * return false. If vertexCount was non-zero then upon failure the vertex
336 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000338 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000339 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
340 * popGeomtrySource is called. At that point logically a snapshot of the
341 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000342 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000343 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000344 * @param vertexCount the number of vertices to reserve space for. Can be
345 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000346 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000347 * @param vertices will point to reserved vertex space if vertexCount is
348 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000349 * @param indices will point to reserved index space if indexCount is
350 * non-zero. Illegal to pass NULL if indexCount > 0.
351 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000352 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
353 int vertexCount,
354 int indexCount,
355 void** vertices,
356 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000357
reed@google.comac10a2d2010-12-22 21:39:39 +0000358 /**
359 * Provides hints to caller about the number of vertices and indices
360 * that can be allocated cheaply. This can be useful if caller is reserving
361 * space but doesn't know exactly how much geometry is needed.
362 *
363 * Also may hint whether the draw target should be flushed first. This is
364 * useful for deferred targets.
365 *
366 * @param vertexLayout layout of vertices caller would like to reserve
367 * @param vertexCount in: hint about how many vertices the caller would
368 * like to allocate.
369 * out: a hint about the number of vertices that can be
370 * allocated cheaply. Negative means no hint.
371 * Ignored if NULL.
372 * @param indexCount in: hint about how many indices the caller would
373 * like to allocate.
374 * out: a hint about the number of indices that can be
375 * allocated cheaply. Negative means no hint.
376 * Ignored if NULL.
377 *
378 * @return true if target should be flushed based on the input values.
379 */
380 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000381 int* vertexCount,
382 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
384 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000385 * Sets source of vertex data for the next draw. Array must contain
386 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 *
388 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000389 * @param size size of the vertex data.
390 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000392 void setVertexSourceToArray(GrVertexLayout vertexLayout,
393 const void* vertexArray,
394 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
396 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397 * Sets source of index data for the next indexed draw. Array must contain
398 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400 * @param array cpu array containing index data.
401 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000402 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000403 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000404
405 /**
406 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000407 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 *
409 * @param buffer vertex buffer containing vertex data. Must be
410 * unlocked before draw call.
411 * @param vertexLayout layout of the vertex data in the buffer.
412 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000413 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
414 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
416 /**
417 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000418 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000419 *
420 * @param buffer index buffer containing indices. Must be unlocked
421 * before indexed draw call.
422 */
423 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000424
425 /**
426 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
427 * source to reserved, array, or buffer before next draw. May be able to free
428 * up temporary storage allocated by setVertexSourceToArray or
429 * reserveVertexSpace.
430 */
431 void resetVertexSource();
432
433 /**
434 * Resets index source. Indexed Drawing from reset indices is illegal. Set
435 * index source to reserved, array, or buffer before next indexed draw. May
436 * be able to free up temporary storage allocated by setIndexSourceToArray
437 * or reserveIndexSpace.
438 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000439 void resetIndexSource();
440
441 /**
442 * Query to find out if the vertex or index source is reserved.
443 */
444 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000445 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000446 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
447 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000448
449 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000450 * Pushes and resets the vertex/index sources. Any reserved vertex / index
451 * data is finalized (i.e. cannot be updated after the matching pop but can
452 * be drawn from). Must be balanced by a pop.
453 */
454 void pushGeometrySource();
455
456 /**
457 * Pops the vertex / index sources from the matching push.
458 */
459 void popGeometrySource();
460
461 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000462 * Draws indexed geometry using the current state and current vertex / index
463 * sources.
464 *
465 * @param type The type of primitives to draw.
466 * @param startVertex the vertex in the vertex array/buffer corresponding
467 * to index 0
468 * @param startIndex first index to read from index src.
469 * @param vertexCount one greater than the max index.
470 * @param indexCount the number of index elements to read. The index count
471 * is effectively trimmed to the last completely
472 * specified primitive.
473 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 void drawIndexed(GrPrimitiveType type,
475 int startVertex,
476 int startIndex,
477 int vertexCount,
478 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
480 /**
481 * Draws non-indexed geometry using the current state and current vertex
482 * sources.
483 *
484 * @param type The type of primitives to draw.
485 * @param startVertex the vertex in the vertex array/buffer corresponding
486 * to index 0
487 * @param vertexCount one greater than the max index.
488 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000489 void drawNonIndexed(GrPrimitiveType type,
490 int startVertex,
491 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000493 /**
494 * Helper function for drawing rects. This does not use the current index
495 * and vertex sources. After returning, the vertex and index sources may
496 * have changed. They should be reestablished before the next drawIndexed
497 * or drawNonIndexed. This cannot be called between reserving and releasing
498 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000499 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000500 * drawNonIndexed.
501 * @param rect the rect to draw
502 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000503 * @param stageMask bitmask indicating which stages are enabled.
504 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000505 * @param srcRects specifies rects for stages enabled by stageEnableMask.
506 * if stageEnableMask bit i is 1, srcRects is not NULL,
507 * and srcRects[i] is not NULL, then srcRects[i] will be
508 * used as coordinates for stage i. Otherwise, if stage i
509 * is enabled then rect is used as the coordinates.
510 * @param srcMatrices optional matrices applied to srcRects. If
511 * srcRect[i] is non-NULL and srcMatrices[i] is
512 * non-NULL then srcRect[i] will be transformed by
513 * srcMatrix[i]. srcMatrices can be NULL when no
514 * srcMatrices are desired.
515 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000516 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000517 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000518 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000519 const GrRect* srcRects[],
520 const GrMatrix* srcMatrices[]);
521
522 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000523 * This call is used to draw multiple instances of some geometry with a
524 * given number of vertices (V) and indices (I) per-instance. The indices in
525 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
526 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
527 * concrete example, the following index buffer for drawing a series of
528 * quads each as two triangles each satisfies these conditions with V=4 and
529 * I=6:
530 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
531 *
532 * The call assumes that the pattern of indices fills the entire index
533 * source. The size of the index buffer limits the number of instances that
534 * can be drawn by the GPU in a single draw. However, the caller may specify
535 * any (positive) number for instanceCount and if necessary multiple GPU
536 * draws will be issued. Morever, when drawIndexedInstances is called
537 * multiple times it may be possible for GrDrawTarget to group them into a
538 * single GPU draw.
539 *
540 * @param type the type of primitives to draw
541 * @param instanceCount the number of instances to draw. Each instance
542 * consists of verticesPerInstance vertices indexed by
543 * indicesPerInstance indices drawn as the primitive
544 * type specified by type.
545 * @param verticesPerInstance The number of vertices in each instance (V
546 * in the above description).
547 * @param indicesPerInstance The number of indices in each instance (I
548 * in the above description).
549 */
550 virtual void drawIndexedInstances(GrPrimitiveType type,
551 int instanceCount,
552 int verticesPerInstance,
553 int indicesPerInstance);
554
555 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000556 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000557 * matrices.
558 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000559 void drawSimpleRect(const GrRect& rect,
560 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000561 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000562 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000563 }
564
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000565 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000566 * Clear the render target. Ignores the clip and all other draw state
567 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
568 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000569 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000570 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000571
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000572 /**
573 * Returns the maximum number of edges that may be specified in a single
574 * draw call when performing edge antialiasing. This is usually limited
575 * by the number of fragment uniforms which may be uploaded. Must be a
576 * minimum of six, since a triangle's vertices each belong to two boundary
577 * edges which may be distinct.
578 */
579 virtual int getMaxEdges() const { return 6; }
580
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000581 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
583 class AutoStateRestore : ::GrNoncopyable {
584 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000585 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 AutoStateRestore(GrDrawTarget* target);
587 ~AutoStateRestore();
588
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000589 /**
590 * if this object is already saving state for param target then
591 * this does nothing. Otherise, it restores previously saved state on
592 * previous target (if any) and saves current state on param target.
593 */
594 void set(GrDrawTarget* target);
595
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 private:
597 GrDrawTarget* fDrawTarget;
598 SavedDrawState fDrawState;
599 };
600
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000601 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000602
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000603 /**
604 * Sets the view matrix to I and preconcats all stage matrices enabled in
605 * mask by the view inverse. Destructor undoes these changes.
606 */
607 class AutoDeviceCoordDraw : ::GrNoncopyable {
608 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000609 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000610 ~AutoDeviceCoordDraw();
611 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000612 GrDrawTarget* fDrawTarget;
613 GrMatrix fViewMatrix;
614 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
615 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000616 };
617
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000618 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000619
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 class AutoReleaseGeometry : ::GrNoncopyable {
621 public:
622 AutoReleaseGeometry(GrDrawTarget* target,
623 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000624 int vertexCount,
625 int indexCount);
626 AutoReleaseGeometry();
627 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000628 bool set(GrDrawTarget* target,
629 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000630 int vertexCount,
631 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000632 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000633 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
634 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000635 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000636 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 }
638
639 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000640 void reset();
641
reed@google.comac10a2d2010-12-22 21:39:39 +0000642 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 void* fVertices;
644 void* fIndices;
645 };
646
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000647 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000648
649 class AutoClipRestore : ::GrNoncopyable {
650 public:
651 AutoClipRestore(GrDrawTarget* target) {
652 fTarget = target;
653 fClip = fTarget->getClip();
654 }
655
656 ~AutoClipRestore() {
657 fTarget->setClip(fClip);
658 }
659 private:
660 GrDrawTarget* fTarget;
661 GrClip fClip;
662 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000663
664 ////////////////////////////////////////////////////////////////////////////
665
666 class AutoGeometryPush : ::GrNoncopyable {
667 public:
668 AutoGeometryPush(GrDrawTarget* target) {
669 GrAssert(NULL != target);
670 fTarget = target;
671 target->pushGeometrySource();
672 }
673 ~AutoGeometryPush() {
674 fTarget->popGeometrySource();
675 }
676 private:
677 GrDrawTarget* fTarget;
678 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000679
680 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000681 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000682
reed@google.comac10a2d2010-12-22 21:39:39 +0000683 /**
684 * Helper function to compute the size of a vertex from a vertex layout
685 * @return size of a single vertex.
686 */
687 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000688
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000689 /**
690 * Helper function for determining the index of texture coordinates that
691 * is input for a texture stage. Note that a stage may instead use positions
692 * as texture coordinates, in which case the result of the function is
693 * indistinguishable from the case when the stage is disabled.
694 *
695 * @param stage the stage to query
696 * @param vertexLayout layout to query
697 *
698 * @return the texture coordinate index or -1 if the stage doesn't use
699 * separate (non-position) texture coordinates.
700 */
701 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000702
703 /**
704 * Helper function to compute the offset of texture coordinates in a vertex
705 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000706 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000707 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000708 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000709 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000710
711 /**
712 * Helper function to compute the offset of the color in a vertex
713 * @return offset of color in vertex layout or -1 if the
714 * layout has no color.
715 */
716 static int VertexColorOffset(GrVertexLayout vertexLayout);
717
bsalomon@google.coma3108262011-10-10 14:08:47 +0000718 /**
719 * Helper function to compute the offset of the coverage in a vertex
720 * @return offset of coverage in vertex layout or -1 if the
721 * layout has no coverage.
722 */
723 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
724
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000725 /**
726 * Helper function to compute the offset of the edge pts in a vertex
727 * @return offset of edge in vertex layout or -1 if the
728 * layout has no edge.
729 */
730 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
731
reed@google.comac10a2d2010-12-22 21:39:39 +0000732 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000733 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000734 * coordinates of some index.
735 *
736 * @param coordIndex the tex coord index to query
737 * @param vertexLayout layout to query
738 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000739 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000740 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000741 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000742 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000743 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000744
reed@google.comac10a2d2010-12-22 21:39:39 +0000745 /**
746 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000747 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000748 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000749 * @param stage the stage to query
750 * @param vertexLayout layout to query
751 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000752 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000753 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000754 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000755 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000756
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000757 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000758 * Helper function to compute the size of each vertex and the offsets of
759 * texture coordinates and color. Determines tex coord offsets by tex coord
760 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000761 * by StageTexCoordVertexLayoutBit.)
762 *
763 * @param vertexLayout the layout to query
764 * @param texCoordOffsetsByIdx after return it is the offset of each
765 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000766 * index isn't used. (optional)
767 * @param colorOffset after return it is the offset of the
768 * color field in each vertex, or -1 if
769 * there aren't per-vertex colors. (optional)
770 * @param coverageOffset after return it is the offset of the
771 * coverage field in each vertex, or -1 if
772 * there aren't per-vertex coeverages.
773 * (optional)
774 * @param edgeOffset after return it is the offset of the
775 * edge eq field in each vertex, or -1 if
776 * there aren't per-vertex edge equations.
777 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000778 * @return size of a single vertex
779 */
780 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000781 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
782 int *colorOffset,
783 int *coverageOffset,
784 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000785
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000786 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000787 * Helper function to compute the size of each vertex and the offsets of
788 * texture coordinates and color. Determines tex coord offsets by stage
789 * rather than by index. (Each stage can be mapped to any t.c. index
790 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000791 * tex coords then that stage's offset will be 0 (positions are always at 0).
792 *
793 * @param vertexLayout the layout to query
794 * @param texCoordOffsetsByStage after return it is the offset of each
795 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000796 * index isn't used. (optional)
797 * @param colorOffset after return it is the offset of the
798 * color field in each vertex, or -1 if
799 * there aren't per-vertex colors.
800 * (optional)
801 * @param coverageOffset after return it is the offset of the
802 * coverage field in each vertex, or -1 if
803 * there aren't per-vertex coeverages.
804 * (optional)
805 * @param edgeOffset after return it is the offset of the
806 * edge eq field in each vertex, or -1 if
807 * there aren't per-vertex edge equations.
808 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000809 * @return size of a single vertex
810 */
811 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000812 int texCoordOffsetsByStage[GrDrawState::kNumStages],
813 int* colorOffset,
814 int* coverageOffset,
815 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000816
817 /**
818 * Accessing positions, texture coords, or colors, of a vertex within an
819 * array is a hassle involving casts and simple math. These helpers exist
820 * to keep GrDrawTarget clients' code a bit nicer looking.
821 */
822
823 /**
824 * Gets a pointer to a GrPoint of a vertex's position or texture
825 * coordinate.
826 * @param vertices the vetex array
827 * @param vertexIndex the index of the vertex in the array
828 * @param vertexSize the size of each vertex in the array
829 * @param offset the offset in bytes of the vertex component.
830 * Defaults to zero (corresponding to vertex position)
831 * @return pointer to the vertex component as a GrPoint
832 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000833 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000834 int vertexIndex,
835 int vertexSize,
836 int offset = 0) {
837 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000838 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000839 vertexIndex * vertexSize);
840 }
841 static const GrPoint* GetVertexPoint(const void* vertices,
842 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000843 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000844 int offset = 0) {
845 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000846 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000847 vertexIndex * vertexSize);
848 }
849
850 /**
851 * Gets a pointer to a GrColor inside a vertex within a vertex array.
852 * @param vertices the vetex array
853 * @param vertexIndex the index of the vertex in the array
854 * @param vertexSize the size of each vertex in the array
855 * @param offset the offset in bytes of the vertex color
856 * @return pointer to the vertex component as a GrColor
857 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000858 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000859 int vertexIndex,
860 int vertexSize,
861 int offset) {
862 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000863 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000864 vertexIndex * vertexSize);
865 }
866 static const GrColor* GetVertexColor(const void* vertices,
867 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000868 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000869 int offset) {
870 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000871 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000872 vertexIndex * vertexSize);
873 }
874
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000875 static void VertexLayoutUnitTest();
876
reed@google.comac10a2d2010-12-22 21:39:39 +0000877protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000878
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000879 /**
880 * Optimizations for blending / coverage to be applied based on the current
881 * state.
882 * Subclasses that actually draw (as opposed to those that just buffer for
883 * playback) must implement the flags that replace the output color.
884 */
885 enum BlendOptFlags {
886 /**
887 * No optimization
888 */
889 kNone_BlendOpt = 0,
890 /**
891 * Don't draw at all
892 */
893 kSkipDraw_BlendOptFlag = 0x2,
894 /**
895 * Emit the src color, disable HW blending (replace dst with src)
896 */
897 kDisableBlend_BlendOptFlag = 0x4,
898 /**
899 * The coverage value does not have to be computed separately from
900 * alpha, the the output color can be the modulation of the two.
901 */
902 kCoverageAsAlpha_BlendOptFlag = 0x1,
903 /**
904 * Instead of emitting a src color, emit coverage in the alpha channel
905 * and r,g,b are "don't cares".
906 */
907 kEmitCoverage_BlendOptFlag = 0x10,
908 /**
909 * Emit transparent black instead of the src color, no need to compute
910 * coverage.
911 */
912 kEmitTransBlack_BlendOptFlag = 0x8,
913 };
914 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000915
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000916 // Determines what optimizations can be applied based on the blend.
917 // The coeffecients may have to be tweaked in order for the optimization
918 // to work. srcCoeff and dstCoeff are optional params that receive the
919 // tweaked coeffecients.
920 // Normally the function looks at the current state to see if coverage
921 // is enabled. By setting forceCoverage the caller can speculatively
922 // determine the blend optimizations that would be used if there was
923 // partial pixel coverage
924 BlendOptFlags getBlendOpts(bool forceCoverage = false,
925 GrBlendCoeff* srcCoeff = NULL,
926 GrBlendCoeff* dstCoeff = NULL) const;
927
928 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000929 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000930
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000931 enum GeometrySrcType {
932 kNone_GeometrySrcType, //<! src has not been specified
933 kReserved_GeometrySrcType, //<! src was set using reserve*Space
934 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
935 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
936 };
937
938 struct GeometrySrcState {
939 GeometrySrcType fVertexSrc;
940 union {
941 // valid if src type is buffer
942 const GrVertexBuffer* fVertexBuffer;
943 // valid if src type is reserved or array
944 int fVertexCount;
945 };
946
947 GeometrySrcType fIndexSrc;
948 union {
949 // valid if src type is buffer
950 const GrIndexBuffer* fIndexBuffer;
951 // valid if src type is reserved or array
952 int fIndexCount;
953 };
954
955 GrVertexLayout fVertexLayout;
956 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000957
958 int indexCountInCurrentSource() const {
959 const GeometrySrcState& src = this->getGeomSrc();
960 switch (src.fIndexSrc) {
961 case kNone_GeometrySrcType:
962 return 0;
963 case kReserved_GeometrySrcType:
964 case kArray_GeometrySrcType:
965 return src.fIndexCount;
966 case kBuffer_GeometrySrcType:
967 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
968 default:
969 GrCrash("Unexpected Index Source.");
970 return 0;
971 }
972 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000973 // given a vertex layout and a draw state, will a stage be used?
974 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000975 const GrDrawState& state) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000976 return NULL != state.getTexture(stage) &&
977 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000978 }
979
980 bool isStageEnabled(int stage) const {
bsalomon@google.come79c8152012-03-29 19:07:12 +0000981 return StageWillBeUsed(stage, this->getVertexLayout(),
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000982 this->getDrawState());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000983 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000984
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000985 StageMask enabledStages() const {
986 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000987 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000988 mask |= this->isStageEnabled(s) ? 1 : 0;
989 }
990 return mask;
991 }
992
reed@google.comac10a2d2010-12-22 21:39:39 +0000993 // Helpers for GrDrawTarget subclasses that won't have private access to
994 // SavedDrawState but need to peek at the state values.
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000995 static GrDrawState& accessSavedDrawState(SavedDrawState& sds) {
996 return *sds.fState.get();
997 }
998 static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds){
999 return *sds.fState.get();
1000 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001001
bsalomon@google.com97805382012-03-13 14:32:07 +00001002 // A sublcass can optionally overload this function to be notified before
1003 // vertex and index space is reserved.
1004 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
1005 int vertexCount,
1006 int indexCount) {}
1007
1008
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001009 // implemented by subclass to allocate space for reserved geom
1010 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1011 int vertexCount,
1012 void** vertices) = 0;
1013 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1014 // implemented by subclass to handle release of reserved geom space
1015 virtual void releaseReservedVertexSpace() = 0;
1016 virtual void releaseReservedIndexSpace() = 0;
1017 // subclass must consume array contents when set
1018 virtual void onSetVertexSourceToArray(const void* vertexArray,
1019 int vertexCount) = 0;
1020 virtual void onSetIndexSourceToArray(const void* indexArray,
1021 int indexCount) = 0;
1022 // subclass is notified that geom source will be set away from an array
1023 virtual void releaseVertexArray() = 0;
1024 virtual void releaseIndexArray() = 0;
1025 // subclass overrides to be notified just before geo src state
1026 // is pushed/popped.
1027 virtual void geometrySourceWillPush() = 0;
1028 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1029 // subclass called to perform drawing
1030 virtual void onDrawIndexed(GrPrimitiveType type,
1031 int startVertex,
1032 int startIndex,
1033 int vertexCount,
1034 int indexCount) = 0;
1035 virtual void onDrawNonIndexed(GrPrimitiveType type,
1036 int startVertex,
1037 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001038 // subclass overrides to be notified when clip is set. Must call
1039 // INHERITED::clipwillBeSet
1040 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001041
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001042 // Helpers for drawRect, protected so subclasses that override drawRect
1043 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001044 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001045 const GrRect* srcRects[]);
1046
1047 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001048 const GrMatrix* matrix,
1049 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001050 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001051 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001052 void* vertices);
1053
bsalomon@google.come79c8152012-03-29 19:07:12 +00001054 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001055 const GeometrySrcState& getGeomSrc() const {
1056 return fGeoSrcStateStack.back();
1057 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001058 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1059 // because of the assert.
1060 GrVertexLayout getVertexLayout() const {
1061 // the vertex layout is only valid if a vertex source has been
1062 // specified.
1063 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1064 return this->getGeomSrc().fVertexLayout;
1065 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001066
1067 GrClip fClip;
1068
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001069 GrDrawState* fDrawState;
1070 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001071
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001072 Caps fCaps;
1073
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001074 // subclasses must call this in their destructors to ensure all vertex
1075 // and index sources have been released (including those held by
1076 // pushGeometrySource())
1077 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001078
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001079private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001080 // helpers for reserving vertex and index space.
1081 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1082 int vertexCount,
1083 void** vertices);
1084 bool reserveIndexSpace(int indexCount, void** indices);
1085
bsalomon@google.come8262622011-11-07 02:30:51 +00001086 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1087 // indicate non-indexed drawing.
1088 bool checkDraw(GrPrimitiveType type, int startVertex,
1089 int startIndex, int vertexCount,
1090 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001091 // called when setting a new vert/idx source to unref prev vb/ib
1092 void releasePreviousVertexSource();
1093 void releasePreviousIndexSource();
1094
1095 enum {
1096 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001098 SkSTArray<kPreallocGeoSrcStateStackCnt,
1099 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001100
reed@google.comac10a2d2010-12-22 21:39:39 +00001101};
1102
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001103GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1104
reed@google.comac10a2d2010-12-22 21:39:39 +00001105#endif