blob: 5f241a6bff22e122282a95f0bb365bff4dc459a6 [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
tomhudson@google.com93813632011-10-27 20:21:16 +000014#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000015#include "GrIndexBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000016#include "GrMatrix.h"
17#include "GrRefCnt.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000018#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
Scroggo97c88c22011-05-11 14:05:25 +000020#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000021#include "SkTLazy.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000022#include "SkTArray.h"
Scroggo97c88c22011-05-11 14:05:25 +000023
robertphillips@google.coma2d71482012-08-01 20:08:47 +000024class GrClipData;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000025class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28class GrDrawTarget : public GrRefCnt {
29public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000030 SK_DECLARE_INST_COUNT(GrDrawTarget)
31
reed@google.comac10a2d2010-12-22 21:39:39 +000032 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033 * Represents the draw target capabilities.
34 */
35 struct Caps {
36 Caps() { memset(this, 0, sizeof(Caps)); }
37 Caps(const Caps& c) { *this = c; }
38 Caps& operator= (const Caps& c) {
39 memcpy(this, &c, sizeof(Caps));
40 return *this;
41 }
42 void print() const;
43 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000044 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000045 bool fTwoSidedStencilSupport : 1;
46 bool fStencilWrapOpsSupport : 1;
47 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000048 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000049 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000050 bool fFSAASupport : 1;
51 bool fDualSourceBlendingSupport : 1;
52 bool fBufferLockSupport : 1;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000053 bool fPathStencilingSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000054 int fMaxRenderTargetSize;
55 int fMaxTextureSize;
56 };
57
reed@google.comac10a2d2010-12-22 21:39:39 +000058 ///////////////////////////////////////////////////////////////////////////
59
60 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000061 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000062
63 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000064 * Gets the capabilities of the draw target.
65 */
66 const Caps& getCaps() const { return fCaps; }
67
68 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000069 * Sets the current clip to the region specified by clip. All draws will be
70 * clipped against this clip if kClip_StateBit is enabled.
71 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000072 * Setting the clip may (or may not) zero out the client's stencil bits.
73 *
reed@google.comac10a2d2010-12-22 21:39:39 +000074 * @param description of the clipping region
75 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000076 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +000077
78 /**
79 * Gets the current clip.
80 *
81 * @return the clip.
82 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000083 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000085 /**
86 * Sets the draw state object for the draw target. Note that this does not
87 * make a copy. The GrDrawTarget will take a reference to passed object.
88 * Passing NULL will cause the GrDrawTarget to use its own internal draw
89 * state object rather than an externally provided one.
90 */
91 void setDrawState(GrDrawState* drawState);
92
93 /**
94 * Read-only access to the GrDrawTarget's current draw state.
95 */
96 const GrDrawState& getDrawState() const { return *fDrawState; }
97
98 /**
99 * Read-write access to the GrDrawTarget's current draw state. Note that
100 * this doesn't ref.
101 */
102 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000103
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000104 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000105 * Color alpha and coverage are two inputs to the drawing pipeline. For some
106 * blend modes it is safe to fold the coverage into constant or per-vertex
107 * color alpha value. For other blend modes they must be handled separately.
108 * Depending on features available in the underlying 3D API this may or may
109 * not be possible.
110 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000111 * This function considers the current draw state and the draw target's
112 * capabilities to determine whether coverage can be handled correctly. The
113 * following assumptions are made:
114 * 1. The caller intends to somehow specify coverage. This can be
115 * specified either by enabling a coverage stage on the GrDrawState or
116 * via the vertex layout.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117 * 2. Other than enabling coverage stages, the current configuration of
bsalomon@google.come79c8152012-03-29 19:07:12 +0000118 * the target's GrDrawState is as it will be at draw time.
119 * 3. If a vertex source has not yet been specified then all stages with
120 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000121 */
122 bool canApplyCoverage() const;
123
124 /**
125 * Determines whether incorporating partial pixel coverage into the constant
126 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000127 * blending result. If a vertex source has not yet been specified then
128 * the function assumes that all stages with non-NULL textures will be
129 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000130 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000131 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000132
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000133 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134 * Given the current draw state and hw support, will HW AA lines be used
bsalomon@google.come79c8152012-03-29 19:07:12 +0000135 * (if line primitive type is drawn)? If a vertex source has not yet been
136 * specified then the function assumes that all stages with non-NULL
137 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000138 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000139 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000140
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000141 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000142 * The format of vertices is represented as a bitfield of flags.
143 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000144 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000145 * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000147 * use any of the texture coordinates as its input texture coordinates or it
148 * may use the positions as texture coordinates.
149 *
150 * If no texture coordinates are specified for a stage then the stage is
151 * disabled.
152 *
153 * Only one type of texture coord can be specified per stage. For
154 * example StageTexCoordVertexLayoutBit(0, 2) and
155 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
156 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157 * The order in memory is always (position, texture coord 0, ..., color,
158 * coverage) with any unused fields omitted. Note that this means that if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000159 * only texture coordinates 1 is referenced then there is no texture
160 * coordinates 0 and the order would be (position, texture coordinate 1
161 * [, color][, coverage]).
162 */
163
164 /**
165 * Generates a bit indicating that a texture stage uses texture coordinates
166 *
167 * @param stage the stage that will use texture coordinates.
168 * @param texCoordIdx the index of the texture coordinates to use
169 *
170 * @return the bit to add to a GrVertexLayout bitfield.
171 */
172 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000173 GrAssert(stage < GrDrawState::kNumStages);
174 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
175 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000176 }
177
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000178 static bool StageUsesTexCoords(GrVertexLayout layout, int stage);
179
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000180private:
bsalomon@google.com52544c72012-07-10 15:06:59 +0000181 // non-stage bits start at this index.
182 static const int STAGE_BIT_CNT = GrDrawState::kNumStages *
183 GrDrawState::kMaxTexCoords;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000184public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000185
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000186 /**
187 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 */
189 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000190 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000191 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000192 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000193 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000194 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000195 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000196 * text [GrGpuTextVertex vs GrPoint].)
197 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000198 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000199
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000200 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000201 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000202 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000203 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000205 kDummyVertexLayoutBit,
206 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000208 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000209 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
211 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000212 * There are three types of "sources" of geometry (vertices and indices) for
213 * draw calls made on the target. When performing an indexed draw, the
214 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000215 * specified it can be used for multiple draws. However, the time at which
216 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000217 *
218 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000219 * already specified geometry that it isn't finished with. So there are push
220 * and pop methods. This allows the client to push the sources, draw
221 * something using alternate sources, and then pop to restore the original
222 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000223 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000224 * Aside from pushes and pops, a source remains valid until another source
225 * is set or resetVertexSource / resetIndexSource is called. Drawing from
226 * a reset source is an error.
227 *
228 * The three types of sources are:
229 *
230 * 1. A cpu array (set*SourceToArray). This is useful when the caller
231 * already provided vertex data in a format compatible with a
232 * GrVertexLayout. The data in the array is consumed at the time that
233 * set*SourceToArray is called and subsequent edits to the array will not
234 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000235 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000236 * 2. Reserve. This is most useful when the caller has data it must
237 * transform before drawing and is not long-lived. The caller requests
238 * that the draw target make room for some amount of vertex and/or index
239 * data. The target provides ptrs to hold the vertex and/or index data.
240 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000241 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000242 * drawIndexedInstances, or pushGeometrySource. At this point the data is
243 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000245 * Where the space is allocated and how it is uploaded to the GPU is
246 * subclass-dependent.
247 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000248 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000249 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000250 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000251 * guarantee that the data is still available in the buffers at playback.
252 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000253 */
254
255 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000256 * Reserves space for vertices and/or indices. Zero can be specifed as
257 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000259 * vertexCount then the vertex source will be unmodified and likewise for
260 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000262 * If the function returns true then the reserve suceeded and the vertices
263 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000265 * If the target cannot make space for the request then this function will
266 * return false. If vertexCount was non-zero then upon failure the vertex
267 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000269 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000270 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
271 * popGeomtrySource is called. At that point logically a snapshot of the
272 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000275 * @param vertexCount the number of vertices to reserve space for. Can be
276 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000277 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000278 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000279 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000280 * @param indices will point to reserved index space if indexCount is
281 * non-zero. Illegal to pass NULL if indexCount > 0.
282 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000283 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
284 int vertexCount,
285 int indexCount,
286 void** vertices,
287 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000288
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 /**
290 * Provides hints to caller about the number of vertices and indices
291 * that can be allocated cheaply. This can be useful if caller is reserving
292 * space but doesn't know exactly how much geometry is needed.
293 *
294 * Also may hint whether the draw target should be flushed first. This is
295 * useful for deferred targets.
296 *
297 * @param vertexLayout layout of vertices caller would like to reserve
298 * @param vertexCount in: hint about how many vertices the caller would
299 * like to allocate.
300 * out: a hint about the number of vertices that can be
301 * allocated cheaply. Negative means no hint.
302 * Ignored if NULL.
303 * @param indexCount in: hint about how many indices the caller would
304 * like to allocate.
305 * out: a hint about the number of indices that can be
306 * allocated cheaply. Negative means no hint.
307 * Ignored if NULL.
308 *
309 * @return true if target should be flushed based on the input values.
310 */
311 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312 int* vertexCount,
313 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
315 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000316 * Sets source of vertex data for the next draw. Array must contain
317 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 *
319 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000320 * @param size size of the vertex data.
321 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000322 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000323 void setVertexSourceToArray(GrVertexLayout vertexLayout,
324 const void* vertexArray,
325 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000326
327 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000328 * Sets source of index data for the next indexed draw. Array must contain
329 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000330 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000331 * @param array cpu array containing index data.
332 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000334 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000335
336 /**
337 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000338 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000339 *
340 * @param buffer vertex buffer containing vertex data. Must be
341 * unlocked before draw call.
342 * @param vertexLayout layout of the vertex data in the buffer.
343 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000344 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
345 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 /**
348 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000349 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 *
351 * @param buffer index buffer containing indices. Must be unlocked
352 * before indexed draw call.
353 */
354 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000356 /**
357 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
358 * source to reserved, array, or buffer before next draw. May be able to free
359 * up temporary storage allocated by setVertexSourceToArray or
360 * reserveVertexSpace.
361 */
362 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000363
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000364 /**
365 * Resets index source. Indexed Drawing from reset indices is illegal. Set
366 * index source to reserved, array, or buffer before next indexed draw. May
367 * be able to free up temporary storage allocated by setIndexSourceToArray
368 * or reserveIndexSpace.
369 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000370 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000371
bsalomon@google.com97805382012-03-13 14:32:07 +0000372 /**
373 * Query to find out if the vertex or index source is reserved.
374 */
375 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000376 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000377 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
378 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
380 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000381 * Pushes and resets the vertex/index sources. Any reserved vertex / index
382 * data is finalized (i.e. cannot be updated after the matching pop but can
383 * be drawn from). Must be balanced by a pop.
384 */
385 void pushGeometrySource();
386
387 /**
388 * Pops the vertex / index sources from the matching push.
389 */
390 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000391
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000392 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 * Draws indexed geometry using the current state and current vertex / index
394 * sources.
395 *
396 * @param type The type of primitives to draw.
397 * @param startVertex the vertex in the vertex array/buffer corresponding
398 * to index 0
399 * @param startIndex first index to read from index src.
400 * @param vertexCount one greater than the max index.
401 * @param indexCount the number of index elements to read. The index count
402 * is effectively trimmed to the last completely
403 * specified primitive.
404 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000405 void drawIndexed(GrPrimitiveType type,
406 int startVertex,
407 int startIndex,
408 int vertexCount,
409 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
411 /**
412 * Draws non-indexed geometry using the current state and current vertex
413 * sources.
414 *
415 * @param type The type of primitives to draw.
416 * @param startVertex the vertex in the vertex array/buffer corresponding
417 * to index 0
418 * @param vertexCount one greater than the max index.
419 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000420 void drawNonIndexed(GrPrimitiveType type,
421 int startVertex,
422 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000424 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000425 * Draws path into the stencil buffer. The fill must be either even/odd or
426 * winding (not inverse or hairline). It will respect the HW antialias flag
427 * on the draw state (if possible in the 3D API).
428 */
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000429 void stencilPath(const GrPath*, GrPathFill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000430
431 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000432 * Helper function for drawing rects. This does not use the current index
433 * and vertex sources. After returning, the vertex and index sources may
434 * have changed. They should be reestablished before the next drawIndexed
435 * or drawNonIndexed. This cannot be called between reserving and releasing
436 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000437 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000438 * drawNonIndexed.
439 * @param rect the rect to draw
440 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000441 * @param srcRects specifies rects for stages enabled by stageEnableMask.
442 * if stageEnableMask bit i is 1, srcRects is not NULL,
443 * and srcRects[i] is not NULL, then srcRects[i] will be
444 * used as coordinates for stage i. Otherwise, if stage i
445 * is enabled then rect is used as the coordinates.
446 * @param srcMatrices optional matrices applied to srcRects. If
447 * srcRect[i] is non-NULL and srcMatrices[i] is
448 * non-NULL then srcRect[i] will be transformed by
449 * srcMatrix[i]. srcMatrices can be NULL when no
450 * srcMatrices are desired.
451 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000452 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000453 const GrMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000454 const GrRect* srcRects[],
455 const GrMatrix* srcMatrices[]);
456
457 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000458 * This call is used to draw multiple instances of some geometry with a
459 * given number of vertices (V) and indices (I) per-instance. The indices in
460 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
461 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
462 * concrete example, the following index buffer for drawing a series of
463 * quads each as two triangles each satisfies these conditions with V=4 and
464 * I=6:
465 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
466 *
467 * The call assumes that the pattern of indices fills the entire index
468 * source. The size of the index buffer limits the number of instances that
469 * can be drawn by the GPU in a single draw. However, the caller may specify
470 * any (positive) number for instanceCount and if necessary multiple GPU
471 * draws will be issued. Morever, when drawIndexedInstances is called
472 * multiple times it may be possible for GrDrawTarget to group them into a
473 * single GPU draw.
474 *
475 * @param type the type of primitives to draw
476 * @param instanceCount the number of instances to draw. Each instance
477 * consists of verticesPerInstance vertices indexed by
478 * indicesPerInstance indices drawn as the primitive
479 * type specified by type.
480 * @param verticesPerInstance The number of vertices in each instance (V
481 * in the above description).
482 * @param indicesPerInstance The number of indices in each instance (I
483 * in the above description).
484 */
485 virtual void drawIndexedInstances(GrPrimitiveType type,
486 int instanceCount,
487 int verticesPerInstance,
488 int indicesPerInstance);
489
490 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000491 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000492 * matrices.
493 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000494 void drawSimpleRect(const GrRect& rect,
bsalomon@google.come3d32162012-07-20 13:37:06 +0000495 const GrMatrix* matrix) {
496 drawRect(rect, matrix, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000497 }
498
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000499 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000500 * Clear the current render target if one isn't passed in. Ignores the
501 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000502 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000503 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000504 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000505 GrColor color,
506 GrRenderTarget* renderTarget = NULL) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000507
robertphillips@google.comff175842012-05-14 19:31:39 +0000508 /**
509 * Release any resources that are cached but not currently in use. This
510 * is intended to give an application some recourse when resources are low.
511 */
512 virtual void purgeResources() {};
513
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000514 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000515
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000516 /**
517 * See AutoStateRestore below.
518 */
519 enum ASRInit {
520 kPreserve_ASRInit,
521 kReset_ASRInit
522 };
523
524 /**
525 * Saves off the current state and restores it in the destructor. It will
526 * install a new GrDrawState object on the target (setDrawState) and restore
527 * the previous one in the destructor. The caller should call drawState() to
528 * get the new draw state after the ASR is installed.
529 *
530 * GrDrawState* state = target->drawState();
531 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
532 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
533 * // target before asr was initialized.
534 * // Therefore, rt is set on the GrDrawState
535 * // that will be restored after asr's
536 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000537 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000538 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000539 class AutoStateRestore : ::GrNoncopyable {
540 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000541 /**
542 * Default ASR will have no effect unless set() is subsequently called.
543 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000544 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000545
546 /**
547 * Saves the state on target. The state will be restored when the ASR
548 * is destroyed. If this constructor is used do not call set().
549 *
550 * @param init Should the newly installed GrDrawState be a copy of the
551 * previous state or a default-initialized GrDrawState.
552 */
553 AutoStateRestore(GrDrawTarget* target, ASRInit init);
554
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 ~AutoStateRestore();
556
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000557 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000558 * Saves the state on target. The state will be restored when the ASR
559 * is destroyed. This should only be called once per ASR object and only
560 * when the default constructor was used. For nested saves use multiple
561 * ASR objects.
562 *
563 * @param init Should the newly installed GrDrawState be a copy of the
564 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000565 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000566 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000567
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000569 GrDrawTarget* fDrawTarget;
570 SkTLazy<GrDrawState> fTempState;
571 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 };
573
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000574 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000575
rmistry@google.comd6176b02012-08-23 18:14:13 +0000576 /**
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000577 * Sets the view matrix to I and preconcats all stage matrices enabled in
578 * mask by the view inverse. Destructor undoes these changes.
579 */
580 class AutoDeviceCoordDraw : ::GrNoncopyable {
581 public:
bsalomon@google.come3d32162012-07-20 13:37:06 +0000582 /**
583 * If a stage's texture matrix is applied to explicit per-vertex coords,
584 * rather than to positions, then we don't want to modify its matrix.
585 * The explicitCoordStageMask is used to specify such stages.
586 *
587 * TODO: Remove this when custom stage's control their own texture
588 * matrix and there is a "view matrix has changed" notification to the
589 * custom stages.
590 */
591 AutoDeviceCoordDraw(GrDrawTarget* target,
592 uint32_t explicitCoordStageMask = 0);
593 bool succeeded() const { return NULL != fDrawTarget; }
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000594 ~AutoDeviceCoordDraw();
595 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000596 GrDrawTarget* fDrawTarget;
597 GrMatrix fViewMatrix;
598 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
bsalomon@google.come3d32162012-07-20 13:37:06 +0000599 int fRestoreMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000600 };
601
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000602 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000603
reed@google.comac10a2d2010-12-22 21:39:39 +0000604 class AutoReleaseGeometry : ::GrNoncopyable {
605 public:
606 AutoReleaseGeometry(GrDrawTarget* target,
607 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000608 int vertexCount,
609 int indexCount);
610 AutoReleaseGeometry();
611 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612 bool set(GrDrawTarget* target,
613 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000614 int vertexCount,
615 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000616 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000617 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
618 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000620 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000621 }
622
623 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000624 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000625
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 void* fVertices;
628 void* fIndices;
629 };
630
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000631 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000632
633 class AutoClipRestore : ::GrNoncopyable {
634 public:
635 AutoClipRestore(GrDrawTarget* target) {
636 fTarget = target;
637 fClip = fTarget->getClip();
638 }
639
640 ~AutoClipRestore() {
641 fTarget->setClip(fClip);
642 }
643 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000644 GrDrawTarget* fTarget;
645 const GrClipData* fClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000647
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000648 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000649
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000650 class AutoGeometryPush : ::GrNoncopyable {
651 public:
652 AutoGeometryPush(GrDrawTarget* target) {
653 GrAssert(NULL != target);
654 fTarget = target;
655 target->pushGeometrySource();
656 }
657 ~AutoGeometryPush() {
658 fTarget->popGeometrySource();
659 }
660 private:
661 GrDrawTarget* fTarget;
662 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000663
664 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000665 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000666
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 /**
668 * Helper function to compute the size of a vertex from a vertex layout
669 * @return size of a single vertex.
670 */
671 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000672
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000673 /**
674 * Helper function for determining the index of texture coordinates that
675 * is input for a texture stage. Note that a stage may instead use positions
676 * as texture coordinates, in which case the result of the function is
677 * indistinguishable from the case when the stage is disabled.
678 *
679 * @param stage the stage to query
680 * @param vertexLayout layout to query
681 *
682 * @return the texture coordinate index or -1 if the stage doesn't use
683 * separate (non-position) texture coordinates.
684 */
685 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000686
687 /**
688 * Helper function to compute the offset of texture coordinates in a vertex
689 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000691 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000692 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000693 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000694
695 /**
696 * Helper function to compute the offset of the color in a vertex
697 * @return offset of color in vertex layout or -1 if the
698 * layout has no color.
699 */
700 static int VertexColorOffset(GrVertexLayout vertexLayout);
701
bsalomon@google.coma3108262011-10-10 14:08:47 +0000702 /**
703 * Helper function to compute the offset of the coverage in a vertex
704 * @return offset of coverage in vertex layout or -1 if the
705 * layout has no coverage.
706 */
707 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
708
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000709 /**
710 * Helper function to compute the offset of the edge pts in a vertex
711 * @return offset of edge in vertex layout or -1 if the
712 * layout has no edge.
713 */
714 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
715
reed@google.comac10a2d2010-12-22 21:39:39 +0000716 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000717 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000718 * coordinates of some index.
719 *
720 * @param coordIndex the tex coord index to query
721 * @param vertexLayout layout to query
722 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000723 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000724 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000725 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000726 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000727 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000728
reed@google.comac10a2d2010-12-22 21:39:39 +0000729 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000730 * Helper function to compute the size of each vertex and the offsets of
731 * texture coordinates and color. Determines tex coord offsets by tex coord
732 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000733 * by StageTexCoordVertexLayoutBit.)
734 *
735 * @param vertexLayout the layout to query
736 * @param texCoordOffsetsByIdx after return it is the offset of each
737 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000738 * index isn't used. (optional)
739 * @param colorOffset after return it is the offset of the
740 * color field in each vertex, or -1 if
741 * there aren't per-vertex colors. (optional)
742 * @param coverageOffset after return it is the offset of the
743 * coverage field in each vertex, or -1 if
744 * there aren't per-vertex coeverages.
745 * (optional)
746 * @param edgeOffset after return it is the offset of the
747 * edge eq field in each vertex, or -1 if
748 * there aren't per-vertex edge equations.
749 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000750 * @return size of a single vertex
751 */
752 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000753 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
754 int *colorOffset,
755 int *coverageOffset,
756 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000757
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000758 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000759 * Helper function to compute the size of each vertex and the offsets of
760 * texture coordinates and color. Determines tex coord offsets by stage
761 * rather than by index. (Each stage can be mapped to any t.c. index
762 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000763 * tex coords then that stage's offset will be 0 (positions are always at 0).
764 *
765 * @param vertexLayout the layout to query
766 * @param texCoordOffsetsByStage after return it is the offset of each
767 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000768 * index isn't used. (optional)
769 * @param colorOffset after return it is the offset of the
770 * color field in each vertex, or -1 if
771 * there aren't per-vertex colors.
772 * (optional)
773 * @param coverageOffset after return it is the offset of the
774 * coverage field in each vertex, or -1 if
775 * there aren't per-vertex coeverages.
776 * (optional)
777 * @param edgeOffset after return it is the offset of the
778 * edge eq field in each vertex, or -1 if
779 * there aren't per-vertex edge equations.
780 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000781 * @return size of a single vertex
782 */
783 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000784 int texCoordOffsetsByStage[GrDrawState::kNumStages],
785 int* colorOffset,
786 int* coverageOffset,
787 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000788
789 /**
790 * Accessing positions, texture coords, or colors, of a vertex within an
791 * array is a hassle involving casts and simple math. These helpers exist
792 * to keep GrDrawTarget clients' code a bit nicer looking.
793 */
794
795 /**
796 * Gets a pointer to a GrPoint of a vertex's position or texture
797 * coordinate.
798 * @param vertices the vetex array
799 * @param vertexIndex the index of the vertex in the array
800 * @param vertexSize the size of each vertex in the array
801 * @param offset the offset in bytes of the vertex component.
802 * Defaults to zero (corresponding to vertex position)
803 * @return pointer to the vertex component as a GrPoint
804 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000805 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000806 int vertexIndex,
807 int vertexSize,
808 int offset = 0) {
809 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000810 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000811 vertexIndex * vertexSize);
812 }
813 static const GrPoint* GetVertexPoint(const void* vertices,
814 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000815 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000816 int offset = 0) {
817 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000818 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000819 vertexIndex * vertexSize);
820 }
821
822 /**
823 * Gets a pointer to a GrColor inside a vertex within a vertex array.
824 * @param vertices the vetex array
825 * @param vertexIndex the index of the vertex in the array
826 * @param vertexSize the size of each vertex in the array
827 * @param offset the offset in bytes of the vertex color
828 * @return pointer to the vertex component as a GrColor
829 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000830 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000831 int vertexIndex,
832 int vertexSize,
833 int offset) {
834 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000835 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000836 vertexIndex * vertexSize);
837 }
838 static const GrColor* GetVertexColor(const void* vertices,
839 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000840 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000841 int offset) {
842 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000843 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000844 vertexIndex * vertexSize);
845 }
846
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000847 static void VertexLayoutUnitTest();
848
reed@google.comac10a2d2010-12-22 21:39:39 +0000849protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000850
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000851 /**
852 * Optimizations for blending / coverage to be applied based on the current
853 * state.
854 * Subclasses that actually draw (as opposed to those that just buffer for
855 * playback) must implement the flags that replace the output color.
856 */
857 enum BlendOptFlags {
858 /**
859 * No optimization
860 */
861 kNone_BlendOpt = 0,
862 /**
863 * Don't draw at all
864 */
865 kSkipDraw_BlendOptFlag = 0x2,
866 /**
867 * Emit the src color, disable HW blending (replace dst with src)
868 */
869 kDisableBlend_BlendOptFlag = 0x4,
870 /**
871 * The coverage value does not have to be computed separately from
872 * alpha, the the output color can be the modulation of the two.
873 */
874 kCoverageAsAlpha_BlendOptFlag = 0x1,
875 /**
876 * Instead of emitting a src color, emit coverage in the alpha channel
877 * and r,g,b are "don't cares".
878 */
879 kEmitCoverage_BlendOptFlag = 0x10,
880 /**
881 * Emit transparent black instead of the src color, no need to compute
882 * coverage.
883 */
884 kEmitTransBlack_BlendOptFlag = 0x8,
885 };
886 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000887
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000888 // Determines what optimizations can be applied based on the blend.
889 // The coeffecients may have to be tweaked in order for the optimization
890 // to work. srcCoeff and dstCoeff are optional params that receive the
891 // tweaked coeffecients.
892 // Normally the function looks at the current state to see if coverage
893 // is enabled. By setting forceCoverage the caller can speculatively
894 // determine the blend optimizations that would be used if there was
895 // partial pixel coverage
896 BlendOptFlags getBlendOpts(bool forceCoverage = false,
897 GrBlendCoeff* srcCoeff = NULL,
898 GrBlendCoeff* dstCoeff = NULL) const;
899
900 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000901 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000902
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000903 enum GeometrySrcType {
904 kNone_GeometrySrcType, //<! src has not been specified
905 kReserved_GeometrySrcType, //<! src was set using reserve*Space
906 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
907 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
908 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000909
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000910 struct GeometrySrcState {
911 GeometrySrcType fVertexSrc;
912 union {
913 // valid if src type is buffer
914 const GrVertexBuffer* fVertexBuffer;
915 // valid if src type is reserved or array
916 int fVertexCount;
917 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000919 GeometrySrcType fIndexSrc;
920 union {
921 // valid if src type is buffer
922 const GrIndexBuffer* fIndexBuffer;
923 // valid if src type is reserved or array
924 int fIndexCount;
925 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000926
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000927 GrVertexLayout fVertexLayout;
928 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000929
930 int indexCountInCurrentSource() const {
931 const GeometrySrcState& src = this->getGeomSrc();
932 switch (src.fIndexSrc) {
933 case kNone_GeometrySrcType:
934 return 0;
935 case kReserved_GeometrySrcType:
936 case kArray_GeometrySrcType:
937 return src.fIndexCount;
938 case kBuffer_GeometrySrcType:
939 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
940 default:
941 GrCrash("Unexpected Index Source.");
942 return 0;
943 }
944 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000945
946 bool isStageEnabled(int stage) const {
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000947 return this->getDrawState().isStageEnabled(stage);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000948 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000949
bsalomon@google.com97805382012-03-13 14:32:07 +0000950 // A sublcass can optionally overload this function to be notified before
951 // vertex and index space is reserved.
952 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
953 int vertexCount,
954 int indexCount) {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000955
bsalomon@google.com97805382012-03-13 14:32:07 +0000956
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000957 // implemented by subclass to allocate space for reserved geom
958 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
959 int vertexCount,
960 void** vertices) = 0;
961 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
962 // implemented by subclass to handle release of reserved geom space
963 virtual void releaseReservedVertexSpace() = 0;
964 virtual void releaseReservedIndexSpace() = 0;
965 // subclass must consume array contents when set
966 virtual void onSetVertexSourceToArray(const void* vertexArray,
967 int vertexCount) = 0;
968 virtual void onSetIndexSourceToArray(const void* indexArray,
969 int indexCount) = 0;
970 // subclass is notified that geom source will be set away from an array
971 virtual void releaseVertexArray() = 0;
972 virtual void releaseIndexArray() = 0;
973 // subclass overrides to be notified just before geo src state
974 // is pushed/popped.
975 virtual void geometrySourceWillPush() = 0;
976 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
977 // subclass called to perform drawing
978 virtual void onDrawIndexed(GrPrimitiveType type,
979 int startVertex,
980 int startIndex,
981 int vertexCount,
982 int indexCount) = 0;
983 virtual void onDrawNonIndexed(GrPrimitiveType type,
984 int startVertex,
985 int vertexCount) = 0;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000986 virtual void onStencilPath(const GrPath*, GrPathFill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000987
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000988 // subclass overrides to be notified when clip is set. Must call
989 // INHERITED::clipwillBeSet
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000990 virtual void clipWillBeSet(const GrClipData* clipData) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000991
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000992 // Helpers for drawRect, protected so subclasses that override drawRect
993 // can use them.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000994 static GrVertexLayout GetRectVertexLayout(const GrRect* srcRects[]);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000995
996 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000997 const GrMatrix* matrix,
998 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000999 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001000 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001001 void* vertices);
1002
bsalomon@google.come79c8152012-03-29 19:07:12 +00001003 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001004 const GeometrySrcState& getGeomSrc() const {
1005 return fGeoSrcStateStack.back();
1006 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001007 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1008 // because of the assert.
1009 GrVertexLayout getVertexLayout() const {
1010 // the vertex layout is only valid if a vertex source has been
1011 // specified.
1012 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1013 return this->getGeomSrc().fVertexLayout;
1014 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001015
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001016 const GrClipData* fClip;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001018 GrDrawState* fDrawState;
1019 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001020
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001021 Caps fCaps;
1022
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001023 // subclasses must call this in their destructors to ensure all vertex
rmistry@google.comd6176b02012-08-23 18:14:13 +00001024 // and index sources have been released (including those held by
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001025 // pushGeometrySource())
1026 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001027
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001028private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001029 // helpers for reserving vertex and index space.
1030 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1031 int vertexCount,
1032 void** vertices);
1033 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001034
bsalomon@google.come8262622011-11-07 02:30:51 +00001035 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1036 // indicate non-indexed drawing.
1037 bool checkDraw(GrPrimitiveType type, int startVertex,
1038 int startIndex, int vertexCount,
1039 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001040 // called when setting a new vert/idx source to unref prev vb/ib
1041 void releasePreviousVertexSource();
1042 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001043
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001044 enum {
1045 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001047 SkSTArray<kPreallocGeoSrcStateStackCnt,
bsalomon@google.com92669012011-09-27 19:10:05 +00001048 GeometrySrcState, true> fGeoSrcStateStack;
reed@google.comfa35e3d2012-06-26 20:16:17 +00001049
1050 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +00001051};
1052
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001053GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1054
reed@google.comac10a2d2010-12-22 21:39:39 +00001055#endif