blob: bf6e41bb2e309bd4c52d4fdbe7c85a3cebec089a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrDrawTarget_DEFINED
12#define GrDrawTarget_DEFINED
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrClip.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrColor.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000016#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000017#include "GrIndexBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018#include "GrMatrix.h"
19#include "GrRefCnt.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000020#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000021#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000022#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
Scroggo97c88c22011-05-11 14:05:25 +000024#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000025#include "SkTLazy.h"
Scroggo97c88c22011-05-11 14:05:25 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000028class GrClipIterator;
29class GrVertexBuffer;
30class GrIndexBuffer;
31
32class GrDrawTarget : public GrRefCnt {
33public:
34 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000035 * Represents the draw target capabilities.
36 */
37 struct Caps {
38 Caps() { memset(this, 0, sizeof(Caps)); }
39 Caps(const Caps& c) { *this = c; }
40 Caps& operator= (const Caps& c) {
41 memcpy(this, &c, sizeof(Caps));
42 return *this;
43 }
44 void print() const;
45 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000047 bool fTwoSidedStencilSupport : 1;
48 bool fStencilWrapOpsSupport : 1;
49 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000050 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000051 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000052 bool fFSAASupport : 1;
53 bool fDualSourceBlendingSupport : 1;
54 bool fBufferLockSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000055 int fMaxRenderTargetSize;
56 int fMaxTextureSize;
57 };
58
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000059 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000060 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000061
reed@google.comac10a2d2010-12-22 21:39:39 +000062 ///////////////////////////////////////////////////////////////////////////
63
64 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000065 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000068 * Gets the capabilities of the draw target.
69 */
70 const Caps& getCaps() const { return fCaps; }
71
72 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000073 * Sets the current clip to the region specified by clip. All draws will be
74 * clipped against this clip if kClip_StateBit is enabled.
75 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000076 * Setting the clip may (or may not) zero out the client's stencil bits.
77 *
reed@google.comac10a2d2010-12-22 21:39:39 +000078 * @param description of the clipping region
79 */
80 void setClip(const GrClip& clip);
81
82 /**
83 * Gets the current clip.
84 *
85 * @return the clip.
86 */
87 const GrClip& getClip() const;
88
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000089 /**
90 * Sets the draw state object for the draw target. Note that this does not
91 * make a copy. The GrDrawTarget will take a reference to passed object.
92 * Passing NULL will cause the GrDrawTarget to use its own internal draw
93 * state object rather than an externally provided one.
94 */
95 void setDrawState(GrDrawState* drawState);
96
97 /**
98 * Read-only access to the GrDrawTarget's current draw state.
99 */
100 const GrDrawState& getDrawState() const { return *fDrawState; }
101
102 /**
103 * Read-write access to the GrDrawTarget's current draw state. Note that
104 * this doesn't ref.
105 */
106 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000107
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000108 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000109 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
110 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000111 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000112 * @param matrix the matrix to concat
113 */
114 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
115 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000116 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000117 }
118
119 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000120 * Color alpha and coverage are two inputs to the drawing pipeline. For some
121 * blend modes it is safe to fold the coverage into constant or per-vertex
122 * color alpha value. For other blend modes they must be handled separately.
123 * Depending on features available in the underlying 3D API this may or may
124 * not be possible.
125 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000126 * This function considers the current draw state and the draw target's
127 * capabilities to determine whether coverage can be handled correctly. The
128 * following assumptions are made:
129 * 1. The caller intends to somehow specify coverage. This can be
130 * specified either by enabling a coverage stage on the GrDrawState or
131 * via the vertex layout.
132 * 2. Other than enabling coverage stages, the current configuration of
133 * the target's GrDrawState is as it will be at draw time.
134 * 3. If a vertex source has not yet been specified then all stages with
135 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000136 */
137 bool canApplyCoverage() const;
138
139 /**
140 * Determines whether incorporating partial pixel coverage into the constant
141 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000142 * blending result. If a vertex source has not yet been specified then
143 * the function assumes that all stages with non-NULL textures will be
144 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000145 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000146 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000147
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000148 /**
bsalomon@google.come79c8152012-03-29 19:07:12 +0000149 * Given the current draw state and hw support, will HW AA lines be used
150 * (if line primitive type is drawn)? If a vertex source has not yet been
151 * specified then the function assumes that all stages with non-NULL
152 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000153 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000154 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000155
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000156 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000157 * The format of vertices is represented as a bitfield of flags.
158 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000159 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
160 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
161 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000162 * use any of the texture coordinates as its input texture coordinates or it
163 * may use the positions as texture coordinates.
164 *
165 * If no texture coordinates are specified for a stage then the stage is
166 * disabled.
167 *
168 * Only one type of texture coord can be specified per stage. For
169 * example StageTexCoordVertexLayoutBit(0, 2) and
170 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
171 *
172 * The order in memory is always (position, texture coord 0, ..., color,
173 * coverage) with any unused fields omitted. Note that this means that if
174 * only texture coordinates 1 is referenced then there is no texture
175 * coordinates 0 and the order would be (position, texture coordinate 1
176 * [, color][, coverage]).
177 */
178
179 /**
180 * Generates a bit indicating that a texture stage uses texture coordinates
181 *
182 * @param stage the stage that will use texture coordinates.
183 * @param texCoordIdx the index of the texture coordinates to use
184 *
185 * @return the bit to add to a GrVertexLayout bitfield.
186 */
187 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000188 GrAssert(stage < GrDrawState::kNumStages);
189 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
190 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000191 }
192
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000193private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000194 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
195 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000196
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000197public:
198 /**
199 * Generates a bit indicating that a texture stage uses the position
200 * as its texture coordinate.
201 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000202 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000203 * coordinates.
204 *
205 * @return the bit to add to a GrVertexLayout bitfield.
206 */
207 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000208 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000209 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000210 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000211
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000212 /**
213 * Modify the existing vertex layout. Realistically the only thing that
214 * can be added w/o recomputing the vertex layout is one of the
215 * StagePosAsTexCoordVertexLayoutBit flags
216 */
217 void addToVertexLayout(int flag) {
218 GrAssert((1 << TEX_COORD_BIT_CNT) == flag ||
219 (1 << (TEX_COORD_BIT_CNT + 1)) == flag ||
220 (1 << (TEX_COORD_BIT_CNT + 2)) == flag ||
221 (1 << (TEX_COORD_BIT_CNT + 3)) == flag);
222 fGeoSrcStateStack.back().fVertexLayout |= flag;
223 }
224
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000225private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000226 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
227 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000228
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000229public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000230
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000231 /**
232 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 */
234 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000235 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000236 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000237 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000238 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000239 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000241 * text [GrGpuTextVertex vs GrPoint].)
242 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000243 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000244
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000245 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000246 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000247 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000248 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000249 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000250 kDummyVertexLayoutBit,
251 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000253 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000254 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000255
256 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000257 * There are three types of "sources" of geometry (vertices and indices) for
258 * draw calls made on the target. When performing an indexed draw, the
259 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000260 * specified it can be used for multiple draws. However, the time at which
261 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000262 *
263 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000264 * already specified geometry that it isn't finished with. So there are push
265 * and pop methods. This allows the client to push the sources, draw
266 * something using alternate sources, and then pop to restore the original
267 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000268 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000269 * Aside from pushes and pops, a source remains valid until another source
270 * is set or resetVertexSource / resetIndexSource is called. Drawing from
271 * a reset source is an error.
272 *
273 * The three types of sources are:
274 *
275 * 1. A cpu array (set*SourceToArray). This is useful when the caller
276 * already provided vertex data in a format compatible with a
277 * GrVertexLayout. The data in the array is consumed at the time that
278 * set*SourceToArray is called and subsequent edits to the array will not
279 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000280 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000281 * 2. Reserve. This is most useful when the caller has data it must
282 * transform before drawing and is not long-lived. The caller requests
283 * that the draw target make room for some amount of vertex and/or index
284 * data. The target provides ptrs to hold the vertex and/or index data.
285 *
286 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000287 * drawIndexedInstances, or pushGeometrySource. At this point the data is
288 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000290 * Where the space is allocated and how it is uploaded to the GPU is
291 * subclass-dependent.
292 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000294 * is long-lived. When the data in the buffer is consumed depends on the
295 * GrDrawTarget subclass. For deferred subclasses the caller has to
296 * guarantee that the data is still available in the buffers at playback.
297 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000298 */
299
300 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000301 * Reserves space for vertices and/or indices. Zero can be specifed as
302 * either the vertex or index count if the caller desires to only reserve
303 * space for only indices or only vertices. If zero is specifed for
304 * vertexCount then the vertex source will be unmodified and likewise for
305 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000306 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000307 * If the function returns true then the reserve suceeded and the vertices
308 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000310 * If the target cannot make space for the request then this function will
311 * return false. If vertexCount was non-zero then upon failure the vertex
312 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000314 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000315 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
316 * popGeomtrySource is called. At that point logically a snapshot of the
317 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000318 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000320 * @param vertexCount the number of vertices to reserve space for. Can be
321 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000322 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000323 * @param vertices will point to reserved vertex space if vertexCount is
324 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000325 * @param indices will point to reserved index space if indexCount is
326 * non-zero. Illegal to pass NULL if indexCount > 0.
327 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000328 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
329 int vertexCount,
330 int indexCount,
331 void** vertices,
332 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000333
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 /**
335 * Provides hints to caller about the number of vertices and indices
336 * that can be allocated cheaply. This can be useful if caller is reserving
337 * space but doesn't know exactly how much geometry is needed.
338 *
339 * Also may hint whether the draw target should be flushed first. This is
340 * useful for deferred targets.
341 *
342 * @param vertexLayout layout of vertices caller would like to reserve
343 * @param vertexCount in: hint about how many vertices the caller would
344 * like to allocate.
345 * out: a hint about the number of vertices that can be
346 * allocated cheaply. Negative means no hint.
347 * Ignored if NULL.
348 * @param indexCount in: hint about how many indices the caller would
349 * like to allocate.
350 * out: a hint about the number of indices that can be
351 * allocated cheaply. Negative means no hint.
352 * Ignored if NULL.
353 *
354 * @return true if target should be flushed based on the input values.
355 */
356 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000357 int* vertexCount,
358 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000359
360 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000361 * Sets source of vertex data for the next draw. Array must contain
362 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 *
364 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000365 * @param size size of the vertex data.
366 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000368 void setVertexSourceToArray(GrVertexLayout vertexLayout,
369 const void* vertexArray,
370 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000373 * Sets source of index data for the next indexed draw. Array must contain
374 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000376 * @param array cpu array containing index data.
377 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000379 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380
381 /**
382 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000383 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 *
385 * @param buffer vertex buffer containing vertex data. Must be
386 * unlocked before draw call.
387 * @param vertexLayout layout of the vertex data in the buffer.
388 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000389 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
390 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
392 /**
393 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000394 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 *
396 * @param buffer index buffer containing indices. Must be unlocked
397 * before indexed draw call.
398 */
399 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000400
401 /**
402 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
403 * source to reserved, array, or buffer before next draw. May be able to free
404 * up temporary storage allocated by setVertexSourceToArray or
405 * reserveVertexSpace.
406 */
407 void resetVertexSource();
408
409 /**
410 * Resets index source. Indexed Drawing from reset indices is illegal. Set
411 * index source to reserved, array, or buffer before next indexed draw. May
412 * be able to free up temporary storage allocated by setIndexSourceToArray
413 * or reserveIndexSpace.
414 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000415 void resetIndexSource();
416
417 /**
418 * Query to find out if the vertex or index source is reserved.
419 */
420 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000421 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000422 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
423 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000424
425 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000426 * Pushes and resets the vertex/index sources. Any reserved vertex / index
427 * data is finalized (i.e. cannot be updated after the matching pop but can
428 * be drawn from). Must be balanced by a pop.
429 */
430 void pushGeometrySource();
431
432 /**
433 * Pops the vertex / index sources from the matching push.
434 */
435 void popGeometrySource();
436
437 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 * Draws indexed geometry using the current state and current vertex / index
439 * sources.
440 *
441 * @param type The type of primitives to draw.
442 * @param startVertex the vertex in the vertex array/buffer corresponding
443 * to index 0
444 * @param startIndex first index to read from index src.
445 * @param vertexCount one greater than the max index.
446 * @param indexCount the number of index elements to read. The index count
447 * is effectively trimmed to the last completely
448 * specified primitive.
449 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000450 void drawIndexed(GrPrimitiveType type,
451 int startVertex,
452 int startIndex,
453 int vertexCount,
454 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
456 /**
457 * Draws non-indexed geometry using the current state and current vertex
458 * sources.
459 *
460 * @param type The type of primitives to draw.
461 * @param startVertex the vertex in the vertex array/buffer corresponding
462 * to index 0
463 * @param vertexCount one greater than the max index.
464 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000465 void drawNonIndexed(GrPrimitiveType type,
466 int startVertex,
467 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000469 /**
470 * Helper function for drawing rects. This does not use the current index
471 * and vertex sources. After returning, the vertex and index sources may
472 * have changed. They should be reestablished before the next drawIndexed
473 * or drawNonIndexed. This cannot be called between reserving and releasing
474 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000475 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000476 * drawNonIndexed.
477 * @param rect the rect to draw
478 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000479 * @param stageMask bitmask indicating which stages are enabled.
480 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000481 * @param srcRects specifies rects for stages enabled by stageEnableMask.
482 * if stageEnableMask bit i is 1, srcRects is not NULL,
483 * and srcRects[i] is not NULL, then srcRects[i] will be
484 * used as coordinates for stage i. Otherwise, if stage i
485 * is enabled then rect is used as the coordinates.
486 * @param srcMatrices optional matrices applied to srcRects. If
487 * srcRect[i] is non-NULL and srcMatrices[i] is
488 * non-NULL then srcRect[i] will be transformed by
489 * srcMatrix[i]. srcMatrices can be NULL when no
490 * srcMatrices are desired.
491 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000492 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000493 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000494 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000495 const GrRect* srcRects[],
496 const GrMatrix* srcMatrices[]);
497
498 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000499 * This call is used to draw multiple instances of some geometry with a
500 * given number of vertices (V) and indices (I) per-instance. The indices in
501 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
502 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
503 * concrete example, the following index buffer for drawing a series of
504 * quads each as two triangles each satisfies these conditions with V=4 and
505 * I=6:
506 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
507 *
508 * The call assumes that the pattern of indices fills the entire index
509 * source. The size of the index buffer limits the number of instances that
510 * can be drawn by the GPU in a single draw. However, the caller may specify
511 * any (positive) number for instanceCount and if necessary multiple GPU
512 * draws will be issued. Morever, when drawIndexedInstances is called
513 * multiple times it may be possible for GrDrawTarget to group them into a
514 * single GPU draw.
515 *
516 * @param type the type of primitives to draw
517 * @param instanceCount the number of instances to draw. Each instance
518 * consists of verticesPerInstance vertices indexed by
519 * indicesPerInstance indices drawn as the primitive
520 * type specified by type.
521 * @param verticesPerInstance The number of vertices in each instance (V
522 * in the above description).
523 * @param indicesPerInstance The number of indices in each instance (I
524 * in the above description).
525 */
526 virtual void drawIndexedInstances(GrPrimitiveType type,
527 int instanceCount,
528 int verticesPerInstance,
529 int indicesPerInstance);
530
531 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000532 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000533 * matrices.
534 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000535 void drawSimpleRect(const GrRect& rect,
536 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000537 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000538 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000539 }
540
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000541 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000542 * Clear the render target. Ignores the clip and all other draw state
543 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
544 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000545 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000546 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000547
robertphillips@google.comff175842012-05-14 19:31:39 +0000548 /**
549 * Release any resources that are cached but not currently in use. This
550 * is intended to give an application some recourse when resources are low.
551 */
552 virtual void purgeResources() {};
553
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000554 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000556 /**
557 * See AutoStateRestore below.
558 */
559 enum ASRInit {
560 kPreserve_ASRInit,
561 kReset_ASRInit
562 };
563
564 /**
565 * Saves off the current state and restores it in the destructor. It will
566 * install a new GrDrawState object on the target (setDrawState) and restore
567 * the previous one in the destructor. The caller should call drawState() to
568 * get the new draw state after the ASR is installed.
569 *
570 * GrDrawState* state = target->drawState();
571 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
572 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
573 * // target before asr was initialized.
574 * // Therefore, rt is set on the GrDrawState
575 * // that will be restored after asr's
576 * // destructor rather than target's current
577 * // GrDrawState.
578 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 class AutoStateRestore : ::GrNoncopyable {
580 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000581 /**
582 * Default ASR will have no effect unless set() is subsequently called.
583 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000584 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000585
586 /**
587 * Saves the state on target. The state will be restored when the ASR
588 * is destroyed. If this constructor is used do not call set().
589 *
590 * @param init Should the newly installed GrDrawState be a copy of the
591 * previous state or a default-initialized GrDrawState.
592 */
593 AutoStateRestore(GrDrawTarget* target, ASRInit init);
594
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 ~AutoStateRestore();
596
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000597 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000598 * Saves the state on target. The state will be restored when the ASR
599 * is destroyed. This should only be called once per ASR object and only
600 * when the default constructor was used. For nested saves use multiple
601 * ASR objects.
602 *
603 * @param init Should the newly installed GrDrawState be a copy of the
604 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000605 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000606 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000607
reed@google.comac10a2d2010-12-22 21:39:39 +0000608 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000609 GrDrawTarget* fDrawTarget;
610 SkTLazy<GrDrawState> fTempState;
611 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 };
613
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000614 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000615
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000616 /**
617 * Sets the view matrix to I and preconcats all stage matrices enabled in
618 * mask by the view inverse. Destructor undoes these changes.
619 */
620 class AutoDeviceCoordDraw : ::GrNoncopyable {
621 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000622 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000623 ~AutoDeviceCoordDraw();
624 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000625 GrDrawTarget* fDrawTarget;
626 GrMatrix fViewMatrix;
627 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
628 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000629 };
630
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000631 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000632
reed@google.comac10a2d2010-12-22 21:39:39 +0000633 class AutoReleaseGeometry : ::GrNoncopyable {
634 public:
635 AutoReleaseGeometry(GrDrawTarget* target,
636 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 int vertexCount,
638 int indexCount);
639 AutoReleaseGeometry();
640 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000641 bool set(GrDrawTarget* target,
642 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643 int vertexCount,
644 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000645 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000646 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
647 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000649 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000650 }
651
652 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000653 void reset();
654
reed@google.comac10a2d2010-12-22 21:39:39 +0000655 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 void* fVertices;
657 void* fIndices;
658 };
659
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000660 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000661
662 class AutoClipRestore : ::GrNoncopyable {
663 public:
664 AutoClipRestore(GrDrawTarget* target) {
665 fTarget = target;
666 fClip = fTarget->getClip();
667 }
668
669 ~AutoClipRestore() {
670 fTarget->setClip(fClip);
671 }
672 private:
673 GrDrawTarget* fTarget;
674 GrClip fClip;
675 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000676
677 ////////////////////////////////////////////////////////////////////////////
678
679 class AutoGeometryPush : ::GrNoncopyable {
680 public:
681 AutoGeometryPush(GrDrawTarget* target) {
682 GrAssert(NULL != target);
683 fTarget = target;
684 target->pushGeometrySource();
685 }
686 ~AutoGeometryPush() {
687 fTarget->popGeometrySource();
688 }
689 private:
690 GrDrawTarget* fTarget;
691 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000692
693 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000694 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000695
reed@google.comac10a2d2010-12-22 21:39:39 +0000696 /**
697 * Helper function to compute the size of a vertex from a vertex layout
698 * @return size of a single vertex.
699 */
700 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000701
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000702 /**
703 * Helper function for determining the index of texture coordinates that
704 * is input for a texture stage. Note that a stage may instead use positions
705 * as texture coordinates, in which case the result of the function is
706 * indistinguishable from the case when the stage is disabled.
707 *
708 * @param stage the stage to query
709 * @param vertexLayout layout to query
710 *
711 * @return the texture coordinate index or -1 if the stage doesn't use
712 * separate (non-position) texture coordinates.
713 */
714 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000715
716 /**
717 * Helper function to compute the offset of texture coordinates in a vertex
718 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000719 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000720 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000722 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000723
724 /**
725 * Helper function to compute the offset of the color in a vertex
726 * @return offset of color in vertex layout or -1 if the
727 * layout has no color.
728 */
729 static int VertexColorOffset(GrVertexLayout vertexLayout);
730
bsalomon@google.coma3108262011-10-10 14:08:47 +0000731 /**
732 * Helper function to compute the offset of the coverage in a vertex
733 * @return offset of coverage in vertex layout or -1 if the
734 * layout has no coverage.
735 */
736 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
737
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000738 /**
739 * Helper function to compute the offset of the edge pts in a vertex
740 * @return offset of edge in vertex layout or -1 if the
741 * layout has no edge.
742 */
743 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
744
reed@google.comac10a2d2010-12-22 21:39:39 +0000745 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000746 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000747 * coordinates of some index.
748 *
749 * @param coordIndex the tex coord index 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 index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000753 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000754 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000755 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000756 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000757
reed@google.comac10a2d2010-12-22 21:39:39 +0000758 /**
759 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000760 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000761 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000762 * @param stage the stage to query
763 * @param vertexLayout layout to query
764 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000765 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000766 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000767 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000768 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000769
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000770 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000771 * Helper function to compute the size of each vertex and the offsets of
772 * texture coordinates and color. Determines tex coord offsets by tex coord
773 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000774 * by StageTexCoordVertexLayoutBit.)
775 *
776 * @param vertexLayout the layout to query
777 * @param texCoordOffsetsByIdx 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. (optional)
783 * @param coverageOffset after return it is the offset of the
784 * coverage field in each vertex, or -1 if
785 * there aren't per-vertex coeverages.
786 * (optional)
787 * @param edgeOffset after return it is the offset of the
788 * edge eq field in each vertex, or -1 if
789 * there aren't per-vertex edge equations.
790 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000791 * @return size of a single vertex
792 */
793 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000794 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
795 int *colorOffset,
796 int *coverageOffset,
797 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000798
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000799 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000800 * Helper function to compute the size of each vertex and the offsets of
801 * texture coordinates and color. Determines tex coord offsets by stage
802 * rather than by index. (Each stage can be mapped to any t.c. index
803 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000804 * tex coords then that stage's offset will be 0 (positions are always at 0).
805 *
806 * @param vertexLayout the layout to query
807 * @param texCoordOffsetsByStage after return it is the offset of each
808 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000809 * index isn't used. (optional)
810 * @param colorOffset after return it is the offset of the
811 * color field in each vertex, or -1 if
812 * there aren't per-vertex colors.
813 * (optional)
814 * @param coverageOffset after return it is the offset of the
815 * coverage field in each vertex, or -1 if
816 * there aren't per-vertex coeverages.
817 * (optional)
818 * @param edgeOffset after return it is the offset of the
819 * edge eq field in each vertex, or -1 if
820 * there aren't per-vertex edge equations.
821 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000822 * @return size of a single vertex
823 */
824 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000825 int texCoordOffsetsByStage[GrDrawState::kNumStages],
826 int* colorOffset,
827 int* coverageOffset,
828 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000829
830 /**
831 * Accessing positions, texture coords, or colors, of a vertex within an
832 * array is a hassle involving casts and simple math. These helpers exist
833 * to keep GrDrawTarget clients' code a bit nicer looking.
834 */
835
836 /**
837 * Gets a pointer to a GrPoint of a vertex's position or texture
838 * coordinate.
839 * @param vertices the vetex array
840 * @param vertexIndex the index of the vertex in the array
841 * @param vertexSize the size of each vertex in the array
842 * @param offset the offset in bytes of the vertex component.
843 * Defaults to zero (corresponding to vertex position)
844 * @return pointer to the vertex component as a GrPoint
845 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000846 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000847 int vertexIndex,
848 int vertexSize,
849 int offset = 0) {
850 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000851 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000852 vertexIndex * vertexSize);
853 }
854 static const GrPoint* GetVertexPoint(const void* vertices,
855 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000856 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000857 int offset = 0) {
858 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000859 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000860 vertexIndex * vertexSize);
861 }
862
863 /**
864 * Gets a pointer to a GrColor inside a vertex within a vertex array.
865 * @param vertices the vetex array
866 * @param vertexIndex the index of the vertex in the array
867 * @param vertexSize the size of each vertex in the array
868 * @param offset the offset in bytes of the vertex color
869 * @return pointer to the vertex component as a GrColor
870 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000871 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000872 int vertexIndex,
873 int vertexSize,
874 int offset) {
875 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000876 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000877 vertexIndex * vertexSize);
878 }
879 static const GrColor* GetVertexColor(const void* vertices,
880 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000881 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000882 int offset) {
883 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000884 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000885 vertexIndex * vertexSize);
886 }
887
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000888 static void VertexLayoutUnitTest();
889
reed@google.comac10a2d2010-12-22 21:39:39 +0000890protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000891
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000892 /**
893 * Optimizations for blending / coverage to be applied based on the current
894 * state.
895 * Subclasses that actually draw (as opposed to those that just buffer for
896 * playback) must implement the flags that replace the output color.
897 */
898 enum BlendOptFlags {
899 /**
900 * No optimization
901 */
902 kNone_BlendOpt = 0,
903 /**
904 * Don't draw at all
905 */
906 kSkipDraw_BlendOptFlag = 0x2,
907 /**
908 * Emit the src color, disable HW blending (replace dst with src)
909 */
910 kDisableBlend_BlendOptFlag = 0x4,
911 /**
912 * The coverage value does not have to be computed separately from
913 * alpha, the the output color can be the modulation of the two.
914 */
915 kCoverageAsAlpha_BlendOptFlag = 0x1,
916 /**
917 * Instead of emitting a src color, emit coverage in the alpha channel
918 * and r,g,b are "don't cares".
919 */
920 kEmitCoverage_BlendOptFlag = 0x10,
921 /**
922 * Emit transparent black instead of the src color, no need to compute
923 * coverage.
924 */
925 kEmitTransBlack_BlendOptFlag = 0x8,
926 };
927 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000928
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000929 // Determines what optimizations can be applied based on the blend.
930 // The coeffecients may have to be tweaked in order for the optimization
931 // to work. srcCoeff and dstCoeff are optional params that receive the
932 // tweaked coeffecients.
933 // Normally the function looks at the current state to see if coverage
934 // is enabled. By setting forceCoverage the caller can speculatively
935 // determine the blend optimizations that would be used if there was
936 // partial pixel coverage
937 BlendOptFlags getBlendOpts(bool forceCoverage = false,
938 GrBlendCoeff* srcCoeff = NULL,
939 GrBlendCoeff* dstCoeff = NULL) const;
940
941 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000942 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000943
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000944 enum GeometrySrcType {
945 kNone_GeometrySrcType, //<! src has not been specified
946 kReserved_GeometrySrcType, //<! src was set using reserve*Space
947 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
948 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
949 };
950
951 struct GeometrySrcState {
952 GeometrySrcType fVertexSrc;
953 union {
954 // valid if src type is buffer
955 const GrVertexBuffer* fVertexBuffer;
956 // valid if src type is reserved or array
957 int fVertexCount;
958 };
959
960 GeometrySrcType fIndexSrc;
961 union {
962 // valid if src type is buffer
963 const GrIndexBuffer* fIndexBuffer;
964 // valid if src type is reserved or array
965 int fIndexCount;
966 };
967
968 GrVertexLayout fVertexLayout;
969 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000970
971 int indexCountInCurrentSource() const {
972 const GeometrySrcState& src = this->getGeomSrc();
973 switch (src.fIndexSrc) {
974 case kNone_GeometrySrcType:
975 return 0;
976 case kReserved_GeometrySrcType:
977 case kArray_GeometrySrcType:
978 return src.fIndexCount;
979 case kBuffer_GeometrySrcType:
980 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
981 default:
982 GrCrash("Unexpected Index Source.");
983 return 0;
984 }
985 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000986 // given a vertex layout and a draw state, will a stage be used?
987 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000988 const GrDrawState& state) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000989 return NULL != state.getTexture(stage) &&
990 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000991 }
992
993 bool isStageEnabled(int stage) const {
bsalomon@google.come79c8152012-03-29 19:07:12 +0000994 return StageWillBeUsed(stage, this->getVertexLayout(),
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000995 this->getDrawState());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000996 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000997
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000998 StageMask enabledStages() const {
999 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +00001000 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001001 mask |= this->isStageEnabled(s) ? 1 : 0;
1002 }
1003 return mask;
1004 }
1005
bsalomon@google.com97805382012-03-13 14:32:07 +00001006 // A sublcass can optionally overload this function to be notified before
1007 // vertex and index space is reserved.
1008 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
1009 int vertexCount,
1010 int indexCount) {}
1011
1012
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001013 // implemented by subclass to allocate space for reserved geom
1014 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1015 int vertexCount,
1016 void** vertices) = 0;
1017 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1018 // implemented by subclass to handle release of reserved geom space
1019 virtual void releaseReservedVertexSpace() = 0;
1020 virtual void releaseReservedIndexSpace() = 0;
1021 // subclass must consume array contents when set
1022 virtual void onSetVertexSourceToArray(const void* vertexArray,
1023 int vertexCount) = 0;
1024 virtual void onSetIndexSourceToArray(const void* indexArray,
1025 int indexCount) = 0;
1026 // subclass is notified that geom source will be set away from an array
1027 virtual void releaseVertexArray() = 0;
1028 virtual void releaseIndexArray() = 0;
1029 // subclass overrides to be notified just before geo src state
1030 // is pushed/popped.
1031 virtual void geometrySourceWillPush() = 0;
1032 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1033 // subclass called to perform drawing
1034 virtual void onDrawIndexed(GrPrimitiveType type,
1035 int startVertex,
1036 int startIndex,
1037 int vertexCount,
1038 int indexCount) = 0;
1039 virtual void onDrawNonIndexed(GrPrimitiveType type,
1040 int startVertex,
1041 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001042 // subclass overrides to be notified when clip is set. Must call
1043 // INHERITED::clipwillBeSet
1044 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001045
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001046 // Helpers for drawRect, protected so subclasses that override drawRect
1047 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001048 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001049 const GrRect* srcRects[]);
1050
1051 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001052 const GrMatrix* matrix,
1053 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001054 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001055 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001056 void* vertices);
1057
bsalomon@google.come79c8152012-03-29 19:07:12 +00001058 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001059 const GeometrySrcState& getGeomSrc() const {
1060 return fGeoSrcStateStack.back();
1061 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001062 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1063 // because of the assert.
1064 GrVertexLayout getVertexLayout() const {
1065 // the vertex layout is only valid if a vertex source has been
1066 // specified.
1067 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1068 return this->getGeomSrc().fVertexLayout;
1069 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001070
1071 GrClip fClip;
1072
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001073 GrDrawState* fDrawState;
1074 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001076 Caps fCaps;
1077
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001078 // subclasses must call this in their destructors to ensure all vertex
1079 // and index sources have been released (including those held by
1080 // pushGeometrySource())
1081 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001082
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001083private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001084 // helpers for reserving vertex and index space.
1085 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1086 int vertexCount,
1087 void** vertices);
1088 bool reserveIndexSpace(int indexCount, void** indices);
1089
bsalomon@google.come8262622011-11-07 02:30:51 +00001090 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1091 // indicate non-indexed drawing.
1092 bool checkDraw(GrPrimitiveType type, int startVertex,
1093 int startIndex, int vertexCount,
1094 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001095 // called when setting a new vert/idx source to unref prev vb/ib
1096 void releasePreviousVertexSource();
1097 void releasePreviousIndexSource();
1098
1099 enum {
1100 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001101 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001102 SkSTArray<kPreallocGeoSrcStateStackCnt,
1103 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001104
reed@google.comac10a2d2010-12-22 21:39:39 +00001105};
1106
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001107GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1108
reed@google.comac10a2d2010-12-22 21:39:39 +00001109#endif