blob: 6e0af4330c43591188e8fd51d165ffc0d092955f [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.comb9086a02012-11-01 18:02:54 +000016#include "SkMatrix.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000017#include "GrRefCnt.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000018#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
sugoi@google.com12b4e272012-12-06 20:13:11 +000020#include "SkPath.h"
Scroggo97c88c22011-05-11 14:05:25 +000021#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000022#include "SkTLazy.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000023#include "SkTArray.h"
Scroggo97c88c22011-05-11 14:05:25 +000024
robertphillips@google.coma2d71482012-08-01 20:08:47 +000025class GrClipData;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000026class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000027class GrVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
sugoi@google.com12b4e272012-12-06 20:13:11 +000029class SkStroke;
30
reed@google.comac10a2d2010-12-22 21:39:39 +000031class GrDrawTarget : public GrRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000032protected:
33 /** This helper class allows GrDrawTarget subclasses to set the caps values without having to be
34 made a friend of GrDrawTarget::Caps. */
35 class CapsInternals {
36 public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000037 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000038 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000039 bool fTwoSidedStencilSupport : 1;
40 bool fStencilWrapOpsSupport : 1;
41 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000042 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000043 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000044 bool fFSAASupport : 1;
45 bool fDualSourceBlendingSupport : 1;
46 bool fBufferLockSupport : 1;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000047 bool fPathStencilingSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000048 int fMaxRenderTargetSize;
49 int fMaxTextureSize;
50 };
51
bsalomon@google.comf6601872012-08-28 21:11:35 +000052public:
53 SK_DECLARE_INST_COUNT(GrDrawTarget)
54
55 /**
56 * Represents the draw target capabilities.
57 */
58 class Caps {
59 public:
60 Caps() { memset(this, 0, sizeof(Caps)); }
61 Caps(const Caps& c) { *this = c; }
62 Caps& operator= (const Caps& c) {
63 memcpy(this, &c, sizeof(Caps));
64 return *this;
65 }
66 void print() const;
67
68 bool eightBitPaletteSupport() const { return fInternals.f8BitPaletteSupport; }
69 bool npotTextureTileSupport() const { return fInternals.fNPOTTextureTileSupport; }
70 bool twoSidedStencilSupport() const { return fInternals.fTwoSidedStencilSupport; }
71 bool stencilWrapOpsSupport() const { return fInternals.fStencilWrapOpsSupport; }
72 bool hwAALineSupport() const { return fInternals.fHWAALineSupport; }
73 bool shaderDerivativeSupport() const { return fInternals.fShaderDerivativeSupport; }
74 bool geometryShaderSupport() const { return fInternals.fGeometryShaderSupport; }
75 bool fsaaSupport() const { return fInternals.fFSAASupport; }
76 bool dualSourceBlendingSupport() const { return fInternals.fDualSourceBlendingSupport; }
77 bool bufferLockSupport() const { return fInternals.fBufferLockSupport; }
78 bool pathStencilingSupport() const { return fInternals.fPathStencilingSupport; }
79
80 int maxRenderTargetSize() const { return fInternals.fMaxRenderTargetSize; }
81 int maxTextureSize() const { return fInternals.fMaxTextureSize; }
82 private:
83 CapsInternals fInternals;
bsalomon@google.com0d82fe52012-08-28 21:39:15 +000084 friend class GrDrawTarget; // to set values of fInternals
bsalomon@google.comf6601872012-08-28 21:11:35 +000085 };
86
reed@google.comac10a2d2010-12-22 21:39:39 +000087 ///////////////////////////////////////////////////////////////////////////
88
89 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000090 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000091
92 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000093 * Gets the capabilities of the draw target.
94 */
95 const Caps& getCaps() const { return fCaps; }
96
97 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000098 * Sets the current clip to the region specified by clip. All draws will be
99 * clipped against this clip if kClip_StateBit is enabled.
100 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000101 * Setting the clip may (or may not) zero out the client's stencil bits.
102 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 * @param description of the clipping region
104 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000105 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
107 /**
108 * Gets the current clip.
109 *
110 * @return the clip.
111 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000112 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000113
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000114 /**
115 * Sets the draw state object for the draw target. Note that this does not
116 * make a copy. The GrDrawTarget will take a reference to passed object.
117 * Passing NULL will cause the GrDrawTarget to use its own internal draw
118 * state object rather than an externally provided one.
119 */
120 void setDrawState(GrDrawState* drawState);
121
122 /**
123 * Read-only access to the GrDrawTarget's current draw state.
124 */
125 const GrDrawState& getDrawState() const { return *fDrawState; }
126
127 /**
128 * Read-write access to the GrDrawTarget's current draw state. Note that
129 * this doesn't ref.
130 */
131 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000132
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000133 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000134 * Color alpha and coverage are two inputs to the drawing pipeline. For some
135 * blend modes it is safe to fold the coverage into constant or per-vertex
136 * color alpha value. For other blend modes they must be handled separately.
137 * Depending on features available in the underlying 3D API this may or may
138 * not be possible.
139 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000140 * This function considers the current draw state and the draw target's
141 * capabilities to determine whether coverage can be handled correctly. The
142 * following assumptions are made:
143 * 1. The caller intends to somehow specify coverage. This can be
144 * specified either by enabling a coverage stage on the GrDrawState or
145 * via the vertex layout.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146 * 2. Other than enabling coverage stages, the current configuration of
bsalomon@google.come79c8152012-03-29 19:07:12 +0000147 * the target's GrDrawState is as it will be at draw time.
148 * 3. If a vertex source has not yet been specified then all stages with
149 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000150 */
151 bool canApplyCoverage() const;
152
153 /**
154 * Determines whether incorporating partial pixel coverage into the constant
155 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000156 * blending result. If a vertex source has not yet been specified then
157 * the function assumes that all stages with non-NULL textures will be
158 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000159 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000160 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000161
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000162 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163 * Given the current draw state and hw support, will HW AA lines be used
bsalomon@google.come79c8152012-03-29 19:07:12 +0000164 * (if line primitive type is drawn)? If a vertex source has not yet been
165 * specified then the function assumes that all stages with non-NULL
166 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000167 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000168 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000169
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000170 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000171 * The format of vertices is represented as a bitfield of flags.
172 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000173 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000174 * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000176 * use any of the texture coordinates as its input texture coordinates or it
177 * may use the positions as texture coordinates.
178 *
179 * If no texture coordinates are specified for a stage then the stage is
180 * disabled.
181 *
182 * Only one type of texture coord can be specified per stage. For
183 * example StageTexCoordVertexLayoutBit(0, 2) and
184 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
185 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186 * The order in memory is always (position, texture coord 0, ..., color,
187 * coverage) with any unused fields omitted. Note that this means that if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000188 * only texture coordinates 1 is referenced then there is no texture
189 * coordinates 0 and the order would be (position, texture coordinate 1
190 * [, color][, coverage]).
191 */
192
193 /**
194 * Generates a bit indicating that a texture stage uses texture coordinates
195 *
bsalomon@google.com08283af2012-10-26 13:01:20 +0000196 * @param stageIdx the stage that will use texture coordinates.
bsalomon@google.coma3108262011-10-10 14:08:47 +0000197 * @param texCoordIdx the index of the texture coordinates to use
198 *
199 * @return the bit to add to a GrVertexLayout bitfield.
200 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000201 static int StageTexCoordVertexLayoutBit(int stageIdx, int texCoordIdx) {
202 GrAssert(stageIdx < GrDrawState::kNumStages);
tomhudson@google.com93813632011-10-27 20:21:16 +0000203 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000204 return 1 << (stageIdx + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000205 }
206
bsalomon@google.com08283af2012-10-26 13:01:20 +0000207 static bool StageUsesTexCoords(GrVertexLayout layout, int stageIdx);
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000208
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000209private:
bsalomon@google.com52544c72012-07-10 15:06:59 +0000210 // non-stage bits start at this index.
211 static const int STAGE_BIT_CNT = GrDrawState::kNumStages *
212 GrDrawState::kMaxTexCoords;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000215 /**
216 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 */
218 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000219 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000220 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000221 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000222 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000223 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000225 * text [GrGpuTextVertex vs GrPoint].)
226 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000227 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000229 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000230 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000231 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000232 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000234 kDummyVertexLayoutBit,
235 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000236 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000237 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000238 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
240 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000241 * There are three types of "sources" of geometry (vertices and indices) for
242 * draw calls made on the target. When performing an indexed draw, the
243 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000244 * specified it can be used for multiple draws. However, the time at which
245 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000246 *
247 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000248 * already specified geometry that it isn't finished with. So there are push
249 * and pop methods. This allows the client to push the sources, draw
250 * something using alternate sources, and then pop to restore the original
251 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000252 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000253 * Aside from pushes and pops, a source remains valid until another source
254 * is set or resetVertexSource / resetIndexSource is called. Drawing from
255 * a reset source is an error.
256 *
257 * The three types of sources are:
258 *
259 * 1. A cpu array (set*SourceToArray). This is useful when the caller
260 * already provided vertex data in a format compatible with a
261 * GrVertexLayout. The data in the array is consumed at the time that
262 * set*SourceToArray is called and subsequent edits to the array will not
263 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000264 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000265 * 2. Reserve. This is most useful when the caller has data it must
266 * transform before drawing and is not long-lived. The caller requests
267 * that the draw target make room for some amount of vertex and/or index
268 * data. The target provides ptrs to hold the vertex and/or index data.
269 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000270 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000271 * drawIndexedInstances, or pushGeometrySource. At this point the data is
272 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000274 * Where the space is allocated and how it is uploaded to the GPU is
275 * subclass-dependent.
276 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000278 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000279 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000280 * guarantee that the data is still available in the buffers at playback.
281 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000282 */
283
284 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000285 * Reserves space for vertices and/or indices. Zero can be specifed as
286 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000287 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000288 * vertexCount then the vertex source will be unmodified and likewise for
289 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000291 * If the function returns true then the reserve suceeded and the vertices
292 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000294 * If the target cannot make space for the request then this function will
295 * return false. If vertexCount was non-zero then upon failure the vertex
296 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000298 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000299 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
300 * popGeomtrySource is called. At that point logically a snapshot of the
301 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000302 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000303 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000304 * @param vertexCount the number of vertices to reserve space for. Can be
305 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000306 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000307 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000309 * @param indices will point to reserved index space if indexCount is
310 * non-zero. Illegal to pass NULL if indexCount > 0.
311 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000312 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
313 int vertexCount,
314 int indexCount,
315 void** vertices,
316 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000317
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 /**
319 * Provides hints to caller about the number of vertices and indices
320 * that can be allocated cheaply. This can be useful if caller is reserving
321 * space but doesn't know exactly how much geometry is needed.
322 *
323 * Also may hint whether the draw target should be flushed first. This is
324 * useful for deferred targets.
325 *
326 * @param vertexLayout layout of vertices caller would like to reserve
327 * @param vertexCount in: hint about how many vertices the caller would
328 * like to allocate.
329 * out: a hint about the number of vertices that can be
330 * allocated cheaply. Negative means no hint.
331 * Ignored if NULL.
332 * @param indexCount in: hint about how many indices the caller would
333 * like to allocate.
334 * out: a hint about the number of indices that can be
335 * allocated cheaply. Negative means no hint.
336 * Ignored if NULL.
337 *
338 * @return true if target should be flushed based on the input values.
339 */
340 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000341 int* vertexCount,
342 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
344 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000345 * Sets source of vertex data for the next draw. Array must contain
346 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 *
348 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000349 * @param size size of the vertex data.
350 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000352 void setVertexSourceToArray(GrVertexLayout vertexLayout,
353 const void* vertexArray,
354 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
356 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000357 * Sets source of index data for the next indexed draw. Array must contain
358 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000360 * @param array cpu array containing index data.
361 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000363 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
365 /**
366 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000367 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000368 *
369 * @param buffer vertex buffer containing vertex data. Must be
370 * unlocked before draw call.
371 * @param vertexLayout layout of the vertex data in the buffer.
372 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000373 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
374 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000375
376 /**
377 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000378 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000379 *
380 * @param buffer index buffer containing indices. Must be unlocked
381 * before indexed draw call.
382 */
383 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000384
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000385 /**
386 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
387 * source to reserved, array, or buffer before next draw. May be able to free
388 * up temporary storage allocated by setVertexSourceToArray or
389 * reserveVertexSpace.
390 */
391 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000393 /**
394 * Resets index source. Indexed Drawing from reset indices is illegal. Set
395 * index source to reserved, array, or buffer before next indexed draw. May
396 * be able to free up temporary storage allocated by setIndexSourceToArray
397 * or reserveIndexSpace.
398 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000399 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000400
bsalomon@google.com97805382012-03-13 14:32:07 +0000401 /**
402 * Query to find out if the vertex or index source is reserved.
403 */
404 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000405 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000406 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
407 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
409 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000410 * Pushes and resets the vertex/index sources. Any reserved vertex / index
411 * data is finalized (i.e. cannot be updated after the matching pop but can
412 * be drawn from). Must be balanced by a pop.
413 */
414 void pushGeometrySource();
415
416 /**
417 * Pops the vertex / index sources from the matching push.
418 */
419 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000420
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000421 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 * Draws indexed geometry using the current state and current vertex / index
423 * sources.
424 *
425 * @param type The type of primitives to draw.
426 * @param startVertex the vertex in the vertex array/buffer corresponding
427 * to index 0
428 * @param startIndex first index to read from index src.
429 * @param vertexCount one greater than the max index.
430 * @param indexCount the number of index elements to read. The index count
431 * is effectively trimmed to the last completely
432 * specified primitive.
433 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000434 void drawIndexed(GrPrimitiveType type,
435 int startVertex,
436 int startIndex,
437 int vertexCount,
438 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000439
440 /**
441 * Draws non-indexed geometry using the current state and current vertex
442 * sources.
443 *
444 * @param type The type of primitives to draw.
445 * @param startVertex the vertex in the vertex array/buffer corresponding
446 * to index 0
447 * @param vertexCount one greater than the max index.
448 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000449 void drawNonIndexed(GrPrimitiveType type,
450 int startVertex,
451 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000452
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000453 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000454 * Draws path into the stencil buffer. The fill must be either even/odd or
455 * winding (not inverse or hairline). It will respect the HW antialias flag
456 * on the draw state (if possible in the 3D API).
457 */
sugoi@google.com12b4e272012-12-06 20:13:11 +0000458 void stencilPath(const GrPath*, const SkStroke& stroke, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000459
460 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000461 * Helper function for drawing rects. This does not use the current index
462 * and vertex sources. After returning, the vertex and index sources may
463 * have changed. They should be reestablished before the next drawIndexed
464 * or drawNonIndexed. This cannot be called between reserving and releasing
465 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000466 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000467 * drawNonIndexed.
468 * @param rect the rect to draw
469 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000470 * @param srcRects specifies rects for stages enabled by stageEnableMask.
471 * if stageEnableMask bit i is 1, srcRects is not NULL,
472 * and srcRects[i] is not NULL, then srcRects[i] will be
473 * used as coordinates for stage i. Otherwise, if stage i
474 * is enabled then rect is used as the coordinates.
475 * @param srcMatrices optional matrices applied to srcRects. If
476 * srcRect[i] is non-NULL and srcMatrices[i] is
477 * non-NULL then srcRect[i] will be transformed by
478 * srcMatrix[i]. srcMatrices can be NULL when no
479 * srcMatrices are desired.
480 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000481 virtual void drawRect(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000482 const SkMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000483 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000484 const SkMatrix* srcMatrices[]);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000485
486 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000487 * This call is used to draw multiple instances of some geometry with a
488 * given number of vertices (V) and indices (I) per-instance. The indices in
489 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
490 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
491 * concrete example, the following index buffer for drawing a series of
492 * quads each as two triangles each satisfies these conditions with V=4 and
493 * I=6:
494 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
495 *
496 * The call assumes that the pattern of indices fills the entire index
497 * source. The size of the index buffer limits the number of instances that
498 * can be drawn by the GPU in a single draw. However, the caller may specify
499 * any (positive) number for instanceCount and if necessary multiple GPU
500 * draws will be issued. Morever, when drawIndexedInstances is called
501 * multiple times it may be possible for GrDrawTarget to group them into a
502 * single GPU draw.
503 *
504 * @param type the type of primitives to draw
505 * @param instanceCount the number of instances to draw. Each instance
506 * consists of verticesPerInstance vertices indexed by
507 * indicesPerInstance indices drawn as the primitive
508 * type specified by type.
509 * @param verticesPerInstance The number of vertices in each instance (V
510 * in the above description).
511 * @param indicesPerInstance The number of indices in each instance (I
512 * in the above description).
513 */
514 virtual void drawIndexedInstances(GrPrimitiveType type,
515 int instanceCount,
516 int verticesPerInstance,
517 int indicesPerInstance);
518
519 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000520 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000521 * matrices.
522 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000523 void drawSimpleRect(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000524 const SkMatrix* matrix) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000525 drawRect(rect, matrix, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000526 }
527
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000528 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000529 * Clear the current render target if one isn't passed in. Ignores the
530 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000531 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000532 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000533 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000534 GrColor color,
535 GrRenderTarget* renderTarget = NULL) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000536
robertphillips@google.comff175842012-05-14 19:31:39 +0000537 /**
538 * Release any resources that are cached but not currently in use. This
539 * is intended to give an application some recourse when resources are low.
540 */
541 virtual void purgeResources() {};
542
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000543 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000545 /**
546 * See AutoStateRestore below.
547 */
548 enum ASRInit {
549 kPreserve_ASRInit,
550 kReset_ASRInit
551 };
552
553 /**
554 * Saves off the current state and restores it in the destructor. It will
555 * install a new GrDrawState object on the target (setDrawState) and restore
556 * the previous one in the destructor. The caller should call drawState() to
557 * get the new draw state after the ASR is installed.
558 *
559 * GrDrawState* state = target->drawState();
560 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
561 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
562 * // target before asr was initialized.
563 * // Therefore, rt is set on the GrDrawState
564 * // that will be restored after asr's
565 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000566 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000567 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 class AutoStateRestore : ::GrNoncopyable {
569 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000570 /**
571 * Default ASR will have no effect unless set() is subsequently called.
572 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000573 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000574
575 /**
576 * Saves the state on target. The state will be restored when the ASR
577 * is destroyed. If this constructor is used do not call set().
578 *
579 * @param init Should the newly installed GrDrawState be a copy of the
580 * previous state or a default-initialized GrDrawState.
581 */
582 AutoStateRestore(GrDrawTarget* target, ASRInit init);
583
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 ~AutoStateRestore();
585
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000586 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000587 * Saves the state on target. The state will be restored when the ASR
588 * is destroyed. This should only be called once per ASR object and only
589 * when the default constructor was used. For nested saves use multiple
590 * ASR objects.
591 *
592 * @param init Should the newly installed GrDrawState be a copy of the
593 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000594 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000595 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000596
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000598 GrDrawTarget* fDrawTarget;
599 SkTLazy<GrDrawState> fTempState;
600 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000601 };
602
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000603 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000604
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 class AutoReleaseGeometry : ::GrNoncopyable {
606 public:
607 AutoReleaseGeometry(GrDrawTarget* target,
608 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000609 int vertexCount,
610 int indexCount);
611 AutoReleaseGeometry();
612 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000613 bool set(GrDrawTarget* target,
614 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000615 int vertexCount,
616 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000617 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000618 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
619 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000621 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 }
623
624 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000625 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000626
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 void* fVertices;
629 void* fIndices;
630 };
631
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000632 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000633
634 class AutoClipRestore : ::GrNoncopyable {
635 public:
636 AutoClipRestore(GrDrawTarget* target) {
637 fTarget = target;
638 fClip = fTarget->getClip();
639 }
640
641 ~AutoClipRestore() {
642 fTarget->setClip(fClip);
643 }
644 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000645 GrDrawTarget* fTarget;
646 const GrClipData* fClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000647 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000648
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000649 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000650
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000651 class AutoGeometryPush : ::GrNoncopyable {
652 public:
653 AutoGeometryPush(GrDrawTarget* target) {
654 GrAssert(NULL != target);
655 fTarget = target;
656 target->pushGeometrySource();
657 }
658 ~AutoGeometryPush() {
659 fTarget->popGeometrySource();
660 }
661 private:
662 GrDrawTarget* fTarget;
663 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000664
665 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000666 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000667
reed@google.comac10a2d2010-12-22 21:39:39 +0000668 /**
669 * Helper function to compute the size of a vertex from a vertex layout
670 * @return size of a single vertex.
671 */
672 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000673
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000674 /**
675 * Helper function for determining the index of texture coordinates that
676 * is input for a texture stage. Note that a stage may instead use positions
677 * as texture coordinates, in which case the result of the function is
678 * indistinguishable from the case when the stage is disabled.
679 *
bsalomon@google.com08283af2012-10-26 13:01:20 +0000680 * @param stageIdx the stage to query
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000681 * @param vertexLayout layout to query
682 *
683 * @return the texture coordinate index or -1 if the stage doesn't use
684 * separate (non-position) texture coordinates.
685 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000686 static int VertexTexCoordsForStage(int stageIdx, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000687
688 /**
689 * Helper function to compute the offset of texture coordinates in a vertex
690 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000691 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000692 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000693 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000694 static int VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000695
696 /**
697 * Helper function to compute the offset of the color in a vertex
698 * @return offset of color in vertex layout or -1 if the
699 * layout has no color.
700 */
701 static int VertexColorOffset(GrVertexLayout vertexLayout);
702
bsalomon@google.coma3108262011-10-10 14:08:47 +0000703 /**
704 * Helper function to compute the offset of the coverage in a vertex
705 * @return offset of coverage in vertex layout or -1 if the
706 * layout has no coverage.
707 */
708 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
709
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000710 /**
711 * Helper function to compute the offset of the edge pts in a vertex
712 * @return offset of edge in vertex layout or -1 if the
713 * layout has no edge.
714 */
715 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
716
reed@google.comac10a2d2010-12-22 21:39:39 +0000717 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000718 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000719 * coordinates of some index.
720 *
721 * @param coordIndex the tex coord index to query
722 * @param vertexLayout layout to query
723 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000724 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000725 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000726 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000728 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000729
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000731 * Helper function to compute the size of each vertex and the offsets of
732 * texture coordinates and color. Determines tex coord offsets by tex coord
733 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000734 * by StageTexCoordVertexLayoutBit.)
735 *
736 * @param vertexLayout the layout to query
737 * @param texCoordOffsetsByIdx after return it is the offset of each
738 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000739 * index isn't used. (optional)
740 * @param colorOffset after return it is the offset of the
741 * color field in each vertex, or -1 if
742 * there aren't per-vertex colors. (optional)
743 * @param coverageOffset after return it is the offset of the
744 * coverage field in each vertex, or -1 if
745 * there aren't per-vertex coeverages.
746 * (optional)
747 * @param edgeOffset after return it is the offset of the
748 * edge eq field in each vertex, or -1 if
749 * there aren't per-vertex edge equations.
750 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000751 * @return size of a single vertex
752 */
753 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000754 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
755 int *colorOffset,
756 int *coverageOffset,
757 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000758
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000759 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000760 * Helper function to compute the size of each vertex and the offsets of
761 * texture coordinates and color. Determines tex coord offsets by stage
762 * rather than by index. (Each stage can be mapped to any t.c. index
763 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000764 * tex coords then that stage's offset will be 0 (positions are always at 0).
765 *
766 * @param vertexLayout the layout to query
767 * @param texCoordOffsetsByStage after return it is the offset of each
768 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000769 * index isn't used. (optional)
770 * @param colorOffset after return it is the offset of the
771 * color field in each vertex, or -1 if
772 * there aren't per-vertex colors.
773 * (optional)
774 * @param coverageOffset after return it is the offset of the
775 * coverage field in each vertex, or -1 if
776 * there aren't per-vertex coeverages.
777 * (optional)
778 * @param edgeOffset after return it is the offset of the
779 * edge eq field in each vertex, or -1 if
780 * there aren't per-vertex edge equations.
781 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000782 * @return size of a single vertex
783 */
784 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000785 int texCoordOffsetsByStage[GrDrawState::kNumStages],
786 int* colorOffset,
787 int* coverageOffset,
788 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000789
790 /**
791 * Accessing positions, texture coords, or colors, of a vertex within an
792 * array is a hassle involving casts and simple math. These helpers exist
793 * to keep GrDrawTarget clients' code a bit nicer looking.
794 */
795
796 /**
797 * Gets a pointer to a GrPoint of a vertex's position or texture
798 * coordinate.
799 * @param vertices the vetex array
800 * @param vertexIndex the index of the vertex in the array
801 * @param vertexSize the size of each vertex in the array
802 * @param offset the offset in bytes of the vertex component.
803 * Defaults to zero (corresponding to vertex position)
804 * @return pointer to the vertex component as a GrPoint
805 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000806 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000807 int vertexIndex,
808 int vertexSize,
809 int offset = 0) {
810 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000811 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000812 vertexIndex * vertexSize);
813 }
814 static const GrPoint* GetVertexPoint(const void* vertices,
815 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000816 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000817 int offset = 0) {
818 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000819 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000820 vertexIndex * vertexSize);
821 }
822
823 /**
824 * Gets a pointer to a GrColor inside a vertex within a vertex array.
825 * @param vertices the vetex array
826 * @param vertexIndex the index of the vertex in the array
827 * @param vertexSize the size of each vertex in the array
828 * @param offset the offset in bytes of the vertex color
829 * @return pointer to the vertex component as a GrColor
830 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000831 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000832 int vertexIndex,
833 int vertexSize,
834 int offset) {
835 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000836 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000837 vertexIndex * vertexSize);
838 }
839 static const GrColor* GetVertexColor(const void* vertices,
840 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000841 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000842 int offset) {
843 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000844 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000845 vertexIndex * vertexSize);
846 }
847
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000848 static void VertexLayoutUnitTest();
849
reed@google.comac10a2d2010-12-22 21:39:39 +0000850protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000851
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000852 /**
853 * Optimizations for blending / coverage to be applied based on the current
854 * state.
855 * Subclasses that actually draw (as opposed to those that just buffer for
856 * playback) must implement the flags that replace the output color.
857 */
858 enum BlendOptFlags {
859 /**
860 * No optimization
861 */
862 kNone_BlendOpt = 0,
863 /**
864 * Don't draw at all
865 */
866 kSkipDraw_BlendOptFlag = 0x2,
867 /**
868 * Emit the src color, disable HW blending (replace dst with src)
869 */
870 kDisableBlend_BlendOptFlag = 0x4,
871 /**
872 * The coverage value does not have to be computed separately from
873 * alpha, the the output color can be the modulation of the two.
874 */
875 kCoverageAsAlpha_BlendOptFlag = 0x1,
876 /**
877 * Instead of emitting a src color, emit coverage in the alpha channel
878 * and r,g,b are "don't cares".
879 */
880 kEmitCoverage_BlendOptFlag = 0x10,
881 /**
882 * Emit transparent black instead of the src color, no need to compute
883 * coverage.
884 */
885 kEmitTransBlack_BlendOptFlag = 0x8,
886 };
887 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000888
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000889 // Determines what optimizations can be applied based on the blend.
890 // The coeffecients may have to be tweaked in order for the optimization
891 // to work. srcCoeff and dstCoeff are optional params that receive the
892 // tweaked coeffecients.
893 // Normally the function looks at the current state to see if coverage
894 // is enabled. By setting forceCoverage the caller can speculatively
895 // determine the blend optimizations that would be used if there was
896 // partial pixel coverage
897 BlendOptFlags getBlendOpts(bool forceCoverage = false,
898 GrBlendCoeff* srcCoeff = NULL,
899 GrBlendCoeff* dstCoeff = NULL) const;
900
901 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000902 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000903
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000904 enum GeometrySrcType {
905 kNone_GeometrySrcType, //<! src has not been specified
906 kReserved_GeometrySrcType, //<! src was set using reserve*Space
907 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
908 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
909 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000910
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000911 struct GeometrySrcState {
912 GeometrySrcType fVertexSrc;
913 union {
914 // valid if src type is buffer
915 const GrVertexBuffer* fVertexBuffer;
916 // valid if src type is reserved or array
917 int fVertexCount;
918 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000919
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000920 GeometrySrcType fIndexSrc;
921 union {
922 // valid if src type is buffer
923 const GrIndexBuffer* fIndexBuffer;
924 // valid if src type is reserved or array
925 int fIndexCount;
926 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000927
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000928 GrVertexLayout fVertexLayout;
929 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000930
931 int indexCountInCurrentSource() const {
932 const GeometrySrcState& src = this->getGeomSrc();
933 switch (src.fIndexSrc) {
934 case kNone_GeometrySrcType:
935 return 0;
936 case kReserved_GeometrySrcType:
937 case kArray_GeometrySrcType:
938 return src.fIndexCount;
939 case kBuffer_GeometrySrcType:
940 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
941 default:
942 GrCrash("Unexpected Index Source.");
943 return 0;
944 }
945 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000946
bsalomon@google.com08283af2012-10-26 13:01:20 +0000947 bool isStageEnabled(int stageIdx) const {
948 return this->getDrawState().isStageEnabled(stageIdx);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000949 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000950
bsalomon@google.com97805382012-03-13 14:32:07 +0000951 // A sublcass can optionally overload this function to be notified before
952 // vertex and index space is reserved.
953 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
954 int vertexCount,
955 int indexCount) {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000956
bsalomon@google.com97805382012-03-13 14:32:07 +0000957
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000958 // implemented by subclass to allocate space for reserved geom
959 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
960 int vertexCount,
961 void** vertices) = 0;
962 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
963 // implemented by subclass to handle release of reserved geom space
964 virtual void releaseReservedVertexSpace() = 0;
965 virtual void releaseReservedIndexSpace() = 0;
966 // subclass must consume array contents when set
967 virtual void onSetVertexSourceToArray(const void* vertexArray,
968 int vertexCount) = 0;
969 virtual void onSetIndexSourceToArray(const void* indexArray,
970 int indexCount) = 0;
971 // subclass is notified that geom source will be set away from an array
972 virtual void releaseVertexArray() = 0;
973 virtual void releaseIndexArray() = 0;
974 // subclass overrides to be notified just before geo src state
975 // is pushed/popped.
976 virtual void geometrySourceWillPush() = 0;
977 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
978 // subclass called to perform drawing
979 virtual void onDrawIndexed(GrPrimitiveType type,
980 int startVertex,
981 int startIndex,
982 int vertexCount,
983 int indexCount) = 0;
984 virtual void onDrawNonIndexed(GrPrimitiveType type,
985 int startVertex,
986 int vertexCount) = 0;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000987 virtual void onStencilPath(const GrPath*, const SkStroke& stroke, SkPath::FillType fill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000988
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000989 // subclass overrides to be notified when clip is set. Must call
990 // INHERITED::clipwillBeSet
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000991 virtual void clipWillBeSet(const GrClipData* clipData) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000992
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000993 // Helpers for drawRect, protected so subclasses that override drawRect
994 // can use them.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000995 static GrVertexLayout GetRectVertexLayout(const GrRect* srcRects[]);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000996
997 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000998 const SkMatrix* matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000999 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001000 const SkMatrix* srcMatrices[],
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001001 GrColor color,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001002 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001003 void* vertices);
1004
bsalomon@google.come79c8152012-03-29 19:07:12 +00001005 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001006 const GeometrySrcState& getGeomSrc() const {
1007 return fGeoSrcStateStack.back();
1008 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001009 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1010 // because of the assert.
1011 GrVertexLayout getVertexLayout() const {
1012 // the vertex layout is only valid if a vertex source has been
1013 // specified.
1014 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1015 return this->getGeomSrc().fVertexLayout;
1016 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
bsalomon@google.comf6601872012-08-28 21:11:35 +00001018 // allows derived class to set the caps
1019 CapsInternals* capsInternals() { return &fCaps.fInternals; }
1020
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001021 const GrClipData* fClip;
reed@google.comac10a2d2010-12-22 21:39:39 +00001022
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001023 GrDrawState* fDrawState;
1024 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001025
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001026 Caps fCaps;
1027
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001028 // subclasses must call this in their destructors to ensure all vertex
rmistry@google.comd6176b02012-08-23 18:14:13 +00001029 // and index sources have been released (including those held by
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001030 // pushGeometrySource())
1031 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001032
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001033private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001034 // helpers for reserving vertex and index space.
1035 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1036 int vertexCount,
1037 void** vertices);
1038 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001039
bsalomon@google.come8262622011-11-07 02:30:51 +00001040 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1041 // indicate non-indexed drawing.
1042 bool checkDraw(GrPrimitiveType type, int startVertex,
1043 int startIndex, int vertexCount,
1044 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001045 // called when setting a new vert/idx source to unref prev vb/ib
1046 void releasePreviousVertexSource();
1047 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001048
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001049 enum {
1050 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001052 SkSTArray<kPreallocGeoSrcStateStackCnt,
bsalomon@google.com92669012011-09-27 19:10:05 +00001053 GeometrySrcState, true> fGeoSrcStateStack;
reed@google.comfa35e3d2012-06-26 20:16:17 +00001054
1055 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056};
1057
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001058GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1059
reed@google.comac10a2d2010-12-22 21:39:39 +00001060#endif