blob: 3a857670386db6e75e7e65958b59e081a2a7aae1 [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.com290e5362012-03-27 14:34:08 +000089 const GrDrawState& getDrawState() const { return fCurrDrawState; }
90 GrDrawState* drawState() { return &fCurrDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000091
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000092 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000093 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
94 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000095 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000096 * @param matrix the matrix to concat
97 */
98 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
99 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000100 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000101 }
102
103 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000104 * Determines if blending will require a read of a dst given the current
105 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000106 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000107 * @return true if the dst surface will be read at each pixel hit by the
108 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000109 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000110 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000111
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000112 /**
113 * Color alpha and coverage are two inputs to the drawing pipeline. For some
114 * blend modes it is safe to fold the coverage into constant or per-vertex
115 * color alpha value. For other blend modes they must be handled separately.
116 * Depending on features available in the underlying 3D API this may or may
117 * not be possible.
118 *
119 * This function looks at the current blend on the draw target and the draw
120 * target's capabilities to determine whether coverage can be handled
121 * correctly.
122 */
123 bool canApplyCoverage() const;
124
125 /**
126 * Determines whether incorporating partial pixel coverage into the constant
127 * color specified by setColor or per-vertex colors will give the right
128 * blending result.
129 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000130 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000131
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000132 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000133 * Given the current draw state, vertex layout, and hw support, will HW AA
134 * lines be used (if line primitive type is drawn)? (Note that lines are
135 * always 1 pixel wide)
136 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000137 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000138
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000139 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000140 * Used to save and restore the GrGpu's drawing state
141 */
142 struct SavedDrawState {
143 private:
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000144 SkTLazy<GrDrawState> fState;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000145 friend class GrDrawTarget;
146 };
147
148 /**
149 * Saves the current draw state. The state can be restored at a later time
150 * with restoreDrawState.
151 *
152 * See also AutoStateRestore class.
153 *
154 * @param state will hold the state after the function returns.
155 */
156 void saveCurrentDrawState(SavedDrawState* state) const;
157
158 /**
159 * Restores previously saved draw state. The client guarantees that state
160 * was previously passed to saveCurrentDrawState and that the rendertarget
161 * and texture set at save are still valid.
162 *
163 * See also AutoStateRestore class.
164 *
165 * @param state the previously saved state to restore.
166 */
167 void restoreDrawState(const SavedDrawState& state);
168
169 /**
170 * Copies the draw state from another target to this target.
171 *
172 * @param srcTarget draw target used as src of the draw state.
173 */
bsalomon@google.com7c3578a2012-03-19 21:18:04 +0000174 void copyDrawState(const GrDrawTarget& srcTarget);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000175
176 /**
177 * The format of vertices is represented as a bitfield of flags.
178 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000179 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
180 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
181 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000182 * use any of the texture coordinates as its input texture coordinates or it
183 * may use the positions as texture coordinates.
184 *
185 * If no texture coordinates are specified for a stage then the stage is
186 * disabled.
187 *
188 * Only one type of texture coord can be specified per stage. For
189 * example StageTexCoordVertexLayoutBit(0, 2) and
190 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
191 *
192 * The order in memory is always (position, texture coord 0, ..., color,
193 * coverage) with any unused fields omitted. Note that this means that if
194 * only texture coordinates 1 is referenced then there is no texture
195 * coordinates 0 and the order would be (position, texture coordinate 1
196 * [, color][, coverage]).
197 */
198
199 /**
200 * Generates a bit indicating that a texture stage uses texture coordinates
201 *
202 * @param stage the stage that will use texture coordinates.
203 * @param texCoordIdx the index of the texture coordinates to use
204 *
205 * @return the bit to add to a GrVertexLayout bitfield.
206 */
207 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000208 GrAssert(stage < GrDrawState::kNumStages);
209 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
210 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000211 }
212
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000214 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
215 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000216
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000217public:
218 /**
219 * Generates a bit indicating that a texture stage uses the position
220 * as its texture coordinate.
221 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000222 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000223 * coordinates.
224 *
225 * @return the bit to add to a GrVertexLayout bitfield.
226 */
227 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000228 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000229 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000230 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000231
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000232private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000233 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
234 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000235
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000236public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000237
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000238 /**
239 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 */
241 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000242 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000243 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000244 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000245 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000246 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000247 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000248 * text [GrGpuTextVertex vs GrPoint].)
249 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000250 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000251
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000252 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000253 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000254 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000255 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000257 kDummyVertexLayoutBit,
258 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000260 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000261 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000262
263 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000264 * There are three types of "sources" of geometry (vertices and indices) for
265 * draw calls made on the target. When performing an indexed draw, the
266 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000267 * specified it can be used for multiple draws. However, the time at which
268 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000269 *
270 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000271 * already specified geometry that it isn't finished with. So there are push
272 * and pop methods. This allows the client to push the sources, draw
273 * something using alternate sources, and then pop to restore the original
274 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000275 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000276 * Aside from pushes and pops, a source remains valid until another source
277 * is set or resetVertexSource / resetIndexSource is called. Drawing from
278 * a reset source is an error.
279 *
280 * The three types of sources are:
281 *
282 * 1. A cpu array (set*SourceToArray). This is useful when the caller
283 * already provided vertex data in a format compatible with a
284 * GrVertexLayout. The data in the array is consumed at the time that
285 * set*SourceToArray is called and subsequent edits to the array will not
286 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000287 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000288 * 2. Reserve. This is most useful when the caller has data it must
289 * transform before drawing and is not long-lived. The caller requests
290 * that the draw target make room for some amount of vertex and/or index
291 * data. The target provides ptrs to hold the vertex and/or index data.
292 *
293 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000294 * drawIndexedInstances, or pushGeometrySource. At this point the data is
295 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000297 * Where the space is allocated and how it is uploaded to the GPU is
298 * subclass-dependent.
299 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000300 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000301 * is long-lived. When the data in the buffer is consumed depends on the
302 * GrDrawTarget subclass. For deferred subclasses the caller has to
303 * guarantee that the data is still available in the buffers at playback.
304 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000305 */
306
307 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000308 * Reserves space for vertices and/or indices. Zero can be specifed as
309 * either the vertex or index count if the caller desires to only reserve
310 * space for only indices or only vertices. If zero is specifed for
311 * vertexCount then the vertex source will be unmodified and likewise for
312 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000314 * If the function returns true then the reserve suceeded and the vertices
315 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000317 * If the target cannot make space for the request then this function will
318 * return false. If vertexCount was non-zero then upon failure the vertex
319 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000321 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000322 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
323 * popGeomtrySource is called. At that point logically a snapshot of the
324 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000325 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000326 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000327 * @param vertexCount the number of vertices to reserve space for. Can be
328 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000329 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000330 * @param vertices will point to reserved vertex space if vertexCount is
331 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000332 * @param indices will point to reserved index space if indexCount is
333 * non-zero. Illegal to pass NULL if indexCount > 0.
334 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000335 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
336 int vertexCount,
337 int indexCount,
338 void** vertices,
339 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000340
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 /**
342 * Provides hints to caller about the number of vertices and indices
343 * that can be allocated cheaply. This can be useful if caller is reserving
344 * space but doesn't know exactly how much geometry is needed.
345 *
346 * Also may hint whether the draw target should be flushed first. This is
347 * useful for deferred targets.
348 *
349 * @param vertexLayout layout of vertices caller would like to reserve
350 * @param vertexCount in: hint about how many vertices the caller would
351 * like to allocate.
352 * out: a hint about the number of vertices that can be
353 * allocated cheaply. Negative means no hint.
354 * Ignored if NULL.
355 * @param indexCount in: hint about how many indices the caller would
356 * like to allocate.
357 * out: a hint about the number of indices that can be
358 * allocated cheaply. Negative means no hint.
359 * Ignored if NULL.
360 *
361 * @return true if target should be flushed based on the input values.
362 */
363 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000364 int* vertexCount,
365 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000366
367 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000368 * Sets source of vertex data for the next draw. Array must contain
369 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 *
371 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000372 * @param size size of the vertex data.
373 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000374 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000375 void setVertexSourceToArray(GrVertexLayout vertexLayout,
376 const void* vertexArray,
377 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378
379 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000380 * Sets source of index data for the next indexed draw. Array must contain
381 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000383 * @param array cpu array containing index data.
384 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000385 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000386 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000387
388 /**
389 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000390 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 *
392 * @param buffer vertex buffer containing vertex data. Must be
393 * unlocked before draw call.
394 * @param vertexLayout layout of the vertex data in the buffer.
395 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000396 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
397 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000398
399 /**
400 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000401 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000402 *
403 * @param buffer index buffer containing indices. Must be unlocked
404 * before indexed draw call.
405 */
406 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000407
408 /**
409 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
410 * source to reserved, array, or buffer before next draw. May be able to free
411 * up temporary storage allocated by setVertexSourceToArray or
412 * reserveVertexSpace.
413 */
414 void resetVertexSource();
415
416 /**
417 * Resets index source. Indexed Drawing from reset indices is illegal. Set
418 * index source to reserved, array, or buffer before next indexed draw. May
419 * be able to free up temporary storage allocated by setIndexSourceToArray
420 * or reserveIndexSpace.
421 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000422 void resetIndexSource();
423
424 /**
425 * Query to find out if the vertex or index source is reserved.
426 */
427 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000428 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000429 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
430 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000431
432 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000433 * Pushes and resets the vertex/index sources. Any reserved vertex / index
434 * data is finalized (i.e. cannot be updated after the matching pop but can
435 * be drawn from). Must be balanced by a pop.
436 */
437 void pushGeometrySource();
438
439 /**
440 * Pops the vertex / index sources from the matching push.
441 */
442 void popGeometrySource();
443
444 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000445 * Draws indexed geometry using the current state and current vertex / index
446 * sources.
447 *
448 * @param type The type of primitives to draw.
449 * @param startVertex the vertex in the vertex array/buffer corresponding
450 * to index 0
451 * @param startIndex first index to read from index src.
452 * @param vertexCount one greater than the max index.
453 * @param indexCount the number of index elements to read. The index count
454 * is effectively trimmed to the last completely
455 * specified primitive.
456 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000457 void drawIndexed(GrPrimitiveType type,
458 int startVertex,
459 int startIndex,
460 int vertexCount,
461 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
463 /**
464 * Draws non-indexed geometry using the current state and current vertex
465 * sources.
466 *
467 * @param type The type of primitives to draw.
468 * @param startVertex the vertex in the vertex array/buffer corresponding
469 * to index 0
470 * @param vertexCount one greater than the max index.
471 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000472 void drawNonIndexed(GrPrimitiveType type,
473 int startVertex,
474 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000476 /**
477 * Helper function for drawing rects. This does not use the current index
478 * and vertex sources. After returning, the vertex and index sources may
479 * have changed. They should be reestablished before the next drawIndexed
480 * or drawNonIndexed. This cannot be called between reserving and releasing
481 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000482 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000483 * drawNonIndexed.
484 * @param rect the rect to draw
485 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000486 * @param stageMask bitmask indicating which stages are enabled.
487 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000488 * @param srcRects specifies rects for stages enabled by stageEnableMask.
489 * if stageEnableMask bit i is 1, srcRects is not NULL,
490 * and srcRects[i] is not NULL, then srcRects[i] will be
491 * used as coordinates for stage i. Otherwise, if stage i
492 * is enabled then rect is used as the coordinates.
493 * @param srcMatrices optional matrices applied to srcRects. If
494 * srcRect[i] is non-NULL and srcMatrices[i] is
495 * non-NULL then srcRect[i] will be transformed by
496 * srcMatrix[i]. srcMatrices can be NULL when no
497 * srcMatrices are desired.
498 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000499 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000500 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000501 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000502 const GrRect* srcRects[],
503 const GrMatrix* srcMatrices[]);
504
505 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000506 * This call is used to draw multiple instances of some geometry with a
507 * given number of vertices (V) and indices (I) per-instance. The indices in
508 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
509 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
510 * concrete example, the following index buffer for drawing a series of
511 * quads each as two triangles each satisfies these conditions with V=4 and
512 * I=6:
513 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
514 *
515 * The call assumes that the pattern of indices fills the entire index
516 * source. The size of the index buffer limits the number of instances that
517 * can be drawn by the GPU in a single draw. However, the caller may specify
518 * any (positive) number for instanceCount and if necessary multiple GPU
519 * draws will be issued. Morever, when drawIndexedInstances is called
520 * multiple times it may be possible for GrDrawTarget to group them into a
521 * single GPU draw.
522 *
523 * @param type the type of primitives to draw
524 * @param instanceCount the number of instances to draw. Each instance
525 * consists of verticesPerInstance vertices indexed by
526 * indicesPerInstance indices drawn as the primitive
527 * type specified by type.
528 * @param verticesPerInstance The number of vertices in each instance (V
529 * in the above description).
530 * @param indicesPerInstance The number of indices in each instance (I
531 * in the above description).
532 */
533 virtual void drawIndexedInstances(GrPrimitiveType type,
534 int instanceCount,
535 int verticesPerInstance,
536 int indicesPerInstance);
537
538 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000539 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000540 * matrices.
541 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000542 void drawSimpleRect(const GrRect& rect,
543 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000544 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000545 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000546 }
547
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000548 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000549 * Clear the render target. Ignores the clip and all other draw state
550 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
551 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000552 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000553 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000554
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000555 /**
556 * Returns the maximum number of edges that may be specified in a single
557 * draw call when performing edge antialiasing. This is usually limited
558 * by the number of fragment uniforms which may be uploaded. Must be a
559 * minimum of six, since a triangle's vertices each belong to two boundary
560 * edges which may be distinct.
561 */
562 virtual int getMaxEdges() const { return 6; }
563
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000564 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000565
566 class AutoStateRestore : ::GrNoncopyable {
567 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000568 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 AutoStateRestore(GrDrawTarget* target);
570 ~AutoStateRestore();
571
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000572 /**
573 * if this object is already saving state for param target then
574 * this does nothing. Otherise, it restores previously saved state on
575 * previous target (if any) and saves current state on param target.
576 */
577 void set(GrDrawTarget* target);
578
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 private:
580 GrDrawTarget* fDrawTarget;
581 SavedDrawState fDrawState;
582 };
583
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000584 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000585
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000586 /**
587 * Sets the view matrix to I and preconcats all stage matrices enabled in
588 * mask by the view inverse. Destructor undoes these changes.
589 */
590 class AutoDeviceCoordDraw : ::GrNoncopyable {
591 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000592 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000593 ~AutoDeviceCoordDraw();
594 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000595 GrDrawTarget* fDrawTarget;
596 GrMatrix fViewMatrix;
597 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
598 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000599 };
600
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000601 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 class AutoReleaseGeometry : ::GrNoncopyable {
604 public:
605 AutoReleaseGeometry(GrDrawTarget* target,
606 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000607 int vertexCount,
608 int indexCount);
609 AutoReleaseGeometry();
610 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000611 bool set(GrDrawTarget* target,
612 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000613 int vertexCount,
614 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000615 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000616 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
617 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000619 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 }
621
622 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000623 void reset();
624
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 void* fVertices;
627 void* fIndices;
628 };
629
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000630 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000631
632 class AutoClipRestore : ::GrNoncopyable {
633 public:
634 AutoClipRestore(GrDrawTarget* target) {
635 fTarget = target;
636 fClip = fTarget->getClip();
637 }
638
639 ~AutoClipRestore() {
640 fTarget->setClip(fClip);
641 }
642 private:
643 GrDrawTarget* fTarget;
644 GrClip fClip;
645 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000646
647 ////////////////////////////////////////////////////////////////////////////
648
649 class AutoGeometryPush : ::GrNoncopyable {
650 public:
651 AutoGeometryPush(GrDrawTarget* target) {
652 GrAssert(NULL != target);
653 fTarget = target;
654 target->pushGeometrySource();
655 }
656 ~AutoGeometryPush() {
657 fTarget->popGeometrySource();
658 }
659 private:
660 GrDrawTarget* fTarget;
661 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000662
663 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000664 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000665
reed@google.comac10a2d2010-12-22 21:39:39 +0000666 /**
667 * Helper function to compute the size of a vertex from a vertex layout
668 * @return size of a single vertex.
669 */
670 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000671
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000672 /**
673 * Helper function for determining the index of texture coordinates that
674 * is input for a texture stage. Note that a stage may instead use positions
675 * as texture coordinates, in which case the result of the function is
676 * indistinguishable from the case when the stage is disabled.
677 *
678 * @param stage the stage to query
679 * @param vertexLayout layout to query
680 *
681 * @return the texture coordinate index or -1 if the stage doesn't use
682 * separate (non-position) texture coordinates.
683 */
684 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000685
686 /**
687 * Helper function to compute the offset of texture coordinates in a vertex
688 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000689 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000690 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000691 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000692 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000693
694 /**
695 * Helper function to compute the offset of the color in a vertex
696 * @return offset of color in vertex layout or -1 if the
697 * layout has no color.
698 */
699 static int VertexColorOffset(GrVertexLayout vertexLayout);
700
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 /**
702 * Helper function to compute the offset of the coverage in a vertex
703 * @return offset of coverage in vertex layout or -1 if the
704 * layout has no coverage.
705 */
706 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
707
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000708 /**
709 * Helper function to compute the offset of the edge pts in a vertex
710 * @return offset of edge in vertex layout or -1 if the
711 * layout has no edge.
712 */
713 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
714
reed@google.comac10a2d2010-12-22 21:39:39 +0000715 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000716 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000717 * coordinates of some index.
718 *
719 * @param coordIndex the tex coord index to query
720 * @param vertexLayout layout to query
721 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000722 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000723 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000725 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000726 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727
reed@google.comac10a2d2010-12-22 21:39:39 +0000728 /**
729 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000730 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000732 * @param stage the stage to query
733 * @param vertexLayout layout to query
734 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000735 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000736 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000737 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000738 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000739
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000740 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000741 * Helper function to compute the size of each vertex and the offsets of
742 * texture coordinates and color. Determines tex coord offsets by tex coord
743 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000744 * by StageTexCoordVertexLayoutBit.)
745 *
746 * @param vertexLayout the layout to query
747 * @param texCoordOffsetsByIdx after return it is the offset of each
748 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000749 * index isn't used. (optional)
750 * @param colorOffset after return it is the offset of the
751 * color field in each vertex, or -1 if
752 * there aren't per-vertex colors. (optional)
753 * @param coverageOffset after return it is the offset of the
754 * coverage field in each vertex, or -1 if
755 * there aren't per-vertex coeverages.
756 * (optional)
757 * @param edgeOffset after return it is the offset of the
758 * edge eq field in each vertex, or -1 if
759 * there aren't per-vertex edge equations.
760 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000761 * @return size of a single vertex
762 */
763 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000764 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
765 int *colorOffset,
766 int *coverageOffset,
767 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000768
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000769 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000770 * Helper function to compute the size of each vertex and the offsets of
771 * texture coordinates and color. Determines tex coord offsets by stage
772 * rather than by index. (Each stage can be mapped to any t.c. index
773 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000774 * tex coords then that stage's offset will be 0 (positions are always at 0).
775 *
776 * @param vertexLayout the layout to query
777 * @param texCoordOffsetsByStage after return it is the offset of each
778 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000779 * index isn't used. (optional)
780 * @param colorOffset after return it is the offset of the
781 * color field in each vertex, or -1 if
782 * there aren't per-vertex colors.
783 * (optional)
784 * @param coverageOffset after return it is the offset of the
785 * coverage field in each vertex, or -1 if
786 * there aren't per-vertex coeverages.
787 * (optional)
788 * @param edgeOffset after return it is the offset of the
789 * edge eq field in each vertex, or -1 if
790 * there aren't per-vertex edge equations.
791 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000792 * @return size of a single vertex
793 */
794 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000795 int texCoordOffsetsByStage[GrDrawState::kNumStages],
796 int* colorOffset,
797 int* coverageOffset,
798 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000799
800 /**
801 * Accessing positions, texture coords, or colors, of a vertex within an
802 * array is a hassle involving casts and simple math. These helpers exist
803 * to keep GrDrawTarget clients' code a bit nicer looking.
804 */
805
806 /**
807 * Gets a pointer to a GrPoint of a vertex's position or texture
808 * coordinate.
809 * @param vertices the vetex array
810 * @param vertexIndex the index of the vertex in the array
811 * @param vertexSize the size of each vertex in the array
812 * @param offset the offset in bytes of the vertex component.
813 * Defaults to zero (corresponding to vertex position)
814 * @return pointer to the vertex component as a GrPoint
815 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000816 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000817 int vertexIndex,
818 int vertexSize,
819 int offset = 0) {
820 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000821 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000822 vertexIndex * vertexSize);
823 }
824 static const GrPoint* GetVertexPoint(const void* vertices,
825 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000826 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000827 int offset = 0) {
828 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000829 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000830 vertexIndex * vertexSize);
831 }
832
833 /**
834 * Gets a pointer to a GrColor inside a vertex within a vertex array.
835 * @param vertices the vetex array
836 * @param vertexIndex the index of the vertex in the array
837 * @param vertexSize the size of each vertex in the array
838 * @param offset the offset in bytes of the vertex color
839 * @return pointer to the vertex component as a GrColor
840 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000841 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000842 int vertexIndex,
843 int vertexSize,
844 int offset) {
845 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000846 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000847 vertexIndex * vertexSize);
848 }
849 static const GrColor* GetVertexColor(const void* vertices,
850 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000851 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000852 int offset) {
853 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000854 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000855 vertexIndex * vertexSize);
856 }
857
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000858 static void VertexLayoutUnitTest();
859
reed@google.comac10a2d2010-12-22 21:39:39 +0000860protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000861
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000862 /**
863 * Optimizations for blending / coverage to be applied based on the current
864 * state.
865 * Subclasses that actually draw (as opposed to those that just buffer for
866 * playback) must implement the flags that replace the output color.
867 */
868 enum BlendOptFlags {
869 /**
870 * No optimization
871 */
872 kNone_BlendOpt = 0,
873 /**
874 * Don't draw at all
875 */
876 kSkipDraw_BlendOptFlag = 0x2,
877 /**
878 * Emit the src color, disable HW blending (replace dst with src)
879 */
880 kDisableBlend_BlendOptFlag = 0x4,
881 /**
882 * The coverage value does not have to be computed separately from
883 * alpha, the the output color can be the modulation of the two.
884 */
885 kCoverageAsAlpha_BlendOptFlag = 0x1,
886 /**
887 * Instead of emitting a src color, emit coverage in the alpha channel
888 * and r,g,b are "don't cares".
889 */
890 kEmitCoverage_BlendOptFlag = 0x10,
891 /**
892 * Emit transparent black instead of the src color, no need to compute
893 * coverage.
894 */
895 kEmitTransBlack_BlendOptFlag = 0x8,
896 };
897 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000898
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000899 // Determines what optimizations can be applied based on the blend.
900 // The coeffecients may have to be tweaked in order for the optimization
901 // to work. srcCoeff and dstCoeff are optional params that receive the
902 // tweaked coeffecients.
903 // Normally the function looks at the current state to see if coverage
904 // is enabled. By setting forceCoverage the caller can speculatively
905 // determine the blend optimizations that would be used if there was
906 // partial pixel coverage
907 BlendOptFlags getBlendOpts(bool forceCoverage = false,
908 GrBlendCoeff* srcCoeff = NULL,
909 GrBlendCoeff* dstCoeff = NULL) const;
910
911 // determine if src alpha is guaranteed to be one for all src pixels
912 bool srcAlphaWillBeOne() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000913
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000914 enum GeometrySrcType {
915 kNone_GeometrySrcType, //<! src has not been specified
916 kReserved_GeometrySrcType, //<! src was set using reserve*Space
917 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
918 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
919 };
920
921 struct GeometrySrcState {
922 GeometrySrcType fVertexSrc;
923 union {
924 // valid if src type is buffer
925 const GrVertexBuffer* fVertexBuffer;
926 // valid if src type is reserved or array
927 int fVertexCount;
928 };
929
930 GeometrySrcType fIndexSrc;
931 union {
932 // valid if src type is buffer
933 const GrIndexBuffer* fIndexBuffer;
934 // valid if src type is reserved or array
935 int fIndexCount;
936 };
937
938 GrVertexLayout fVertexLayout;
939 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000940
941 int indexCountInCurrentSource() const {
942 const GeometrySrcState& src = this->getGeomSrc();
943 switch (src.fIndexSrc) {
944 case kNone_GeometrySrcType:
945 return 0;
946 case kReserved_GeometrySrcType:
947 case kArray_GeometrySrcType:
948 return src.fIndexCount;
949 case kBuffer_GeometrySrcType:
950 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
951 default:
952 GrCrash("Unexpected Index Source.");
953 return 0;
954 }
955 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000956 // given a vertex layout and a draw state, will a stage be used?
957 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000958 const GrDrawState& state) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000959 return NULL != state.getTexture(stage) &&
960 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000961 }
962
963 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000964 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
bsalomon@google.com290e5362012-03-27 14:34:08 +0000965 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000966 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000967
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000968 StageMask enabledStages() const {
969 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000970 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000971 mask |= this->isStageEnabled(s) ? 1 : 0;
972 }
973 return mask;
974 }
975
reed@google.comac10a2d2010-12-22 21:39:39 +0000976 // Helpers for GrDrawTarget subclasses that won't have private access to
977 // SavedDrawState but need to peek at the state values.
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000978 static GrDrawState& accessSavedDrawState(SavedDrawState& sds) {
979 return *sds.fState.get();
980 }
981 static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds){
982 return *sds.fState.get();
983 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com97805382012-03-13 14:32:07 +0000985 // A sublcass can optionally overload this function to be notified before
986 // vertex and index space is reserved.
987 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
988 int vertexCount,
989 int indexCount) {}
990
991
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000992 // implemented by subclass to allocate space for reserved geom
993 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
994 int vertexCount,
995 void** vertices) = 0;
996 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
997 // implemented by subclass to handle release of reserved geom space
998 virtual void releaseReservedVertexSpace() = 0;
999 virtual void releaseReservedIndexSpace() = 0;
1000 // subclass must consume array contents when set
1001 virtual void onSetVertexSourceToArray(const void* vertexArray,
1002 int vertexCount) = 0;
1003 virtual void onSetIndexSourceToArray(const void* indexArray,
1004 int indexCount) = 0;
1005 // subclass is notified that geom source will be set away from an array
1006 virtual void releaseVertexArray() = 0;
1007 virtual void releaseIndexArray() = 0;
1008 // subclass overrides to be notified just before geo src state
1009 // is pushed/popped.
1010 virtual void geometrySourceWillPush() = 0;
1011 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1012 // subclass called to perform drawing
1013 virtual void onDrawIndexed(GrPrimitiveType type,
1014 int startVertex,
1015 int startIndex,
1016 int vertexCount,
1017 int indexCount) = 0;
1018 virtual void onDrawNonIndexed(GrPrimitiveType type,
1019 int startVertex,
1020 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001021 // subclass overrides to be notified when clip is set. Must call
1022 // INHERITED::clipwillBeSet
1023 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001024
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001025 // Helpers for drawRect, protected so subclasses that override drawRect
1026 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001027 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001028 const GrRect* srcRects[]);
1029
1030 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001031 const GrMatrix* matrix,
1032 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001033 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001034 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001035 void* vertices);
1036
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001037 // accessor for derived classes
1038 const GeometrySrcState& getGeomSrc() const {
1039 return fGeoSrcStateStack.back();
1040 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001041
1042 GrClip fClip;
1043
bsalomon@google.com290e5362012-03-27 14:34:08 +00001044 GrDrawState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001045
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001046 Caps fCaps;
1047
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001048 // subclasses must call this in their destructors to ensure all vertex
1049 // and index sources have been released (including those held by
1050 // pushGeometrySource())
1051 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001052
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001053private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001054 // helpers for reserving vertex and index space.
1055 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1056 int vertexCount,
1057 void** vertices);
1058 bool reserveIndexSpace(int indexCount, void** indices);
1059
bsalomon@google.come8262622011-11-07 02:30:51 +00001060 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1061 // indicate non-indexed drawing.
1062 bool checkDraw(GrPrimitiveType type, int startVertex,
1063 int startIndex, int vertexCount,
1064 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001065 // called when setting a new vert/idx source to unref prev vb/ib
1066 void releasePreviousVertexSource();
1067 void releasePreviousIndexSource();
1068
1069 enum {
1070 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001072 SkSTArray<kPreallocGeoSrcStateStackCnt,
1073 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001074
reed@google.comac10a2d2010-12-22 21:39:39 +00001075};
1076
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001077GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1078
reed@google.comac10a2d2010-12-22 21:39:39 +00001079#endif