blob: a54dd0f1524961d32930ed7373fe08915cf2a23c [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
bsalomon@google.com8d67c072012-12-13 20:38:14 +000014#include "GrClipData.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000015#include "GrDrawState.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000016#include "GrIndexBuffer.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000017#include "SkMatrix.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018#include "GrRefCnt.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000019#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com8d67c072012-12-13 20:38:14 +000021#include "SkClipStack.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000022#include "SkPath.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000023#include "SkTLazy.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000024#include "SkTArray.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000025#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000026
robertphillips@google.coma2d71482012-08-01 20:08:47 +000027class GrClipData;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000028class GrPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000029class GrVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000030
sugoi@google.com5f74cf82012-12-17 21:16:45 +000031class SkStrokeRec;
sugoi@google.com12b4e272012-12-06 20:13:11 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033class GrDrawTarget : public GrRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000034protected:
35 /** This helper class allows GrDrawTarget subclasses to set the caps values without having to be
36 made a friend of GrDrawTarget::Caps. */
37 class CapsInternals {
38 public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000039 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000040 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000041 bool fTwoSidedStencilSupport : 1;
42 bool fStencilWrapOpsSupport : 1;
43 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000044 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000045 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046 bool fFSAASupport : 1;
47 bool fDualSourceBlendingSupport : 1;
48 bool fBufferLockSupport : 1;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000049 bool fPathStencilingSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000050 int fMaxRenderTargetSize;
51 int fMaxTextureSize;
52 };
53
bsalomon@google.comf6601872012-08-28 21:11:35 +000054public:
55 SK_DECLARE_INST_COUNT(GrDrawTarget)
56
57 /**
58 * Represents the draw target capabilities.
59 */
60 class Caps {
61 public:
62 Caps() { memset(this, 0, sizeof(Caps)); }
63 Caps(const Caps& c) { *this = c; }
64 Caps& operator= (const Caps& c) {
65 memcpy(this, &c, sizeof(Caps));
66 return *this;
67 }
68 void print() const;
69
70 bool eightBitPaletteSupport() const { return fInternals.f8BitPaletteSupport; }
71 bool npotTextureTileSupport() const { return fInternals.fNPOTTextureTileSupport; }
72 bool twoSidedStencilSupport() const { return fInternals.fTwoSidedStencilSupport; }
73 bool stencilWrapOpsSupport() const { return fInternals.fStencilWrapOpsSupport; }
74 bool hwAALineSupport() const { return fInternals.fHWAALineSupport; }
75 bool shaderDerivativeSupport() const { return fInternals.fShaderDerivativeSupport; }
76 bool geometryShaderSupport() const { return fInternals.fGeometryShaderSupport; }
77 bool fsaaSupport() const { return fInternals.fFSAASupport; }
78 bool dualSourceBlendingSupport() const { return fInternals.fDualSourceBlendingSupport; }
79 bool bufferLockSupport() const { return fInternals.fBufferLockSupport; }
80 bool pathStencilingSupport() const { return fInternals.fPathStencilingSupport; }
81
82 int maxRenderTargetSize() const { return fInternals.fMaxRenderTargetSize; }
83 int maxTextureSize() const { return fInternals.fMaxTextureSize; }
84 private:
85 CapsInternals fInternals;
bsalomon@google.com0d82fe52012-08-28 21:39:15 +000086 friend class GrDrawTarget; // to set values of fInternals
bsalomon@google.comf6601872012-08-28 21:11:35 +000087 };
88
reed@google.comac10a2d2010-12-22 21:39:39 +000089 ///////////////////////////////////////////////////////////////////////////
90
91 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000092 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000093
94 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000095 * Gets the capabilities of the draw target.
96 */
97 const Caps& getCaps() const { return fCaps; }
98
99 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 * Sets the current clip to the region specified by clip. All draws will be
101 * clipped against this clip if kClip_StateBit is enabled.
102 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000103 * Setting the clip may (or may not) zero out the client's stencil bits.
104 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 * @param description of the clipping region
106 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000107 void setClip(const GrClipData* clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
109 /**
110 * Gets the current clip.
111 *
112 * @return the clip.
113 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000114 const GrClipData* getClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000116 /**
117 * Sets the draw state object for the draw target. Note that this does not
118 * make a copy. The GrDrawTarget will take a reference to passed object.
119 * Passing NULL will cause the GrDrawTarget to use its own internal draw
120 * state object rather than an externally provided one.
121 */
122 void setDrawState(GrDrawState* drawState);
123
124 /**
125 * Read-only access to the GrDrawTarget's current draw state.
126 */
127 const GrDrawState& getDrawState() const { return *fDrawState; }
128
129 /**
130 * Read-write access to the GrDrawTarget's current draw state. Note that
131 * this doesn't ref.
132 */
133 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000134
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000135 /**
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000136 * Color alpha and coverage are two inputs to the drawing pipeline. For some
137 * blend modes it is safe to fold the coverage into constant or per-vertex
138 * color alpha value. For other blend modes they must be handled separately.
139 * Depending on features available in the underlying 3D API this may or may
140 * not be possible.
141 *
bsalomon@google.come79c8152012-03-29 19:07:12 +0000142 * This function considers the current draw state and the draw target's
143 * capabilities to determine whether coverage can be handled correctly. The
144 * following assumptions are made:
145 * 1. The caller intends to somehow specify coverage. This can be
146 * specified either by enabling a coverage stage on the GrDrawState or
147 * via the vertex layout.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000148 * 2. Other than enabling coverage stages, the current configuration of
bsalomon@google.come79c8152012-03-29 19:07:12 +0000149 * the target's GrDrawState is as it will be at draw time.
150 * 3. If a vertex source has not yet been specified then all stages with
151 * non-NULL textures will be referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000152 */
153 bool canApplyCoverage() const;
154
155 /**
156 * Determines whether incorporating partial pixel coverage into the constant
157 * color specified by setColor or per-vertex colors will give the right
bsalomon@google.come79c8152012-03-29 19:07:12 +0000158 * blending result. If a vertex source has not yet been specified then
159 * the function assumes that all stages with non-NULL textures will be
160 * referenced by the vertex layout.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000161 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000162 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000163
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000164 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165 * Given the current draw state and hw support, will HW AA lines be used
bsalomon@google.come79c8152012-03-29 19:07:12 +0000166 * (if line primitive type is drawn)? If a vertex source has not yet been
167 * specified then the function assumes that all stages with non-NULL
168 * textures will be referenced by the vertex layout.
bsalomon@google.com471d4712011-08-23 15:45:25 +0000169 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000170 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000171
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000172 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000173 * The format of vertices is represented as a bitfield of flags.
174 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000175 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000176 * of 2D texture coordinates, per-vertex colors, and per-vertex coverage.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000178 * use any of the texture coordinates as its input texture coordinates or it
179 * may use the positions as texture coordinates.
180 *
181 * If no texture coordinates are specified for a stage then the stage is
182 * disabled.
183 *
184 * Only one type of texture coord can be specified per stage. For
185 * example StageTexCoordVertexLayoutBit(0, 2) and
186 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
187 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188 * The order in memory is always (position, texture coord 0, ..., color,
189 * coverage) with any unused fields omitted. Note that this means that if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000190 * only texture coordinates 1 is referenced then there is no texture
191 * coordinates 0 and the order would be (position, texture coordinate 1
192 * [, color][, coverage]).
193 */
194
195 /**
196 * Generates a bit indicating that a texture stage uses texture coordinates
197 *
bsalomon@google.com08283af2012-10-26 13:01:20 +0000198 * @param stageIdx the stage that will use texture coordinates.
bsalomon@google.coma3108262011-10-10 14:08:47 +0000199 * @param texCoordIdx the index of the texture coordinates to use
200 *
201 * @return the bit to add to a GrVertexLayout bitfield.
202 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000203 static int StageTexCoordVertexLayoutBit(int stageIdx, int texCoordIdx) {
204 GrAssert(stageIdx < GrDrawState::kNumStages);
tomhudson@google.com93813632011-10-27 20:21:16 +0000205 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000206 return 1 << (stageIdx + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000207 }
208
bsalomon@google.com08283af2012-10-26 13:01:20 +0000209 static bool StageUsesTexCoords(GrVertexLayout layout, int stageIdx);
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000210
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000211private:
bsalomon@google.com52544c72012-07-10 15:06:59 +0000212 // non-stage bits start at this index.
213 static const int STAGE_BIT_CNT = GrDrawState::kNumStages *
214 GrDrawState::kMaxTexCoords;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000215public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000216
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000217 /**
218 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 */
220 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000221 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000222 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000223 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000224 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000225 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000227 * text [GrGpuTextVertex vs GrPoint].)
228 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000229 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000231 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000232 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000233 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000234 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000236 kDummyVertexLayoutBit,
237 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000239 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000240 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
242 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000243 * There are three types of "sources" of geometry (vertices and indices) for
244 * draw calls made on the target. When performing an indexed draw, the
245 * indices and vertices can use different source types. Once a source is
bsalomon@google.com934c5702012-03-20 21:17:58 +0000246 * specified it can be used for multiple draws. However, the time at which
247 * the geometry data is no longer editable depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000248 *
249 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000250 * already specified geometry that it isn't finished with. So there are push
251 * and pop methods. This allows the client to push the sources, draw
252 * something using alternate sources, and then pop to restore the original
253 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000254 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000255 * Aside from pushes and pops, a source remains valid until another source
256 * is set or resetVertexSource / resetIndexSource is called. Drawing from
257 * a reset source is an error.
258 *
259 * The three types of sources are:
260 *
261 * 1. A cpu array (set*SourceToArray). This is useful when the caller
262 * already provided vertex data in a format compatible with a
263 * GrVertexLayout. The data in the array is consumed at the time that
264 * set*SourceToArray is called and subsequent edits to the array will not
265 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000266 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000267 * 2. Reserve. This is most useful when the caller has data it must
268 * transform before drawing and is not long-lived. The caller requests
269 * that the draw target make room for some amount of vertex and/or index
270 * data. The target provides ptrs to hold the vertex and/or index data.
271 *
rmistry@google.comd6176b02012-08-23 18:14:13 +0000272 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000273 * drawIndexedInstances, or pushGeometrySource. At this point the data is
274 * frozen and the ptrs are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000275 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000276 * Where the space is allocated and how it is uploaded to the GPU is
277 * subclass-dependent.
278 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000279 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000280 * is long-lived. When the data in the buffer is consumed depends on the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000281 * GrDrawTarget subclass. For deferred subclasses the caller has to
bsalomon@google.come3d70952012-03-13 12:40:53 +0000282 * guarantee that the data is still available in the buffers at playback.
283 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000284 */
285
286 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000287 * Reserves space for vertices and/or indices. Zero can be specifed as
288 * either the vertex or index count if the caller desires to only reserve
rmistry@google.comd6176b02012-08-23 18:14:13 +0000289 * space for only indices or only vertices. If zero is specifed for
bsalomon@google.come3d70952012-03-13 12:40:53 +0000290 * vertexCount then the vertex source will be unmodified and likewise for
291 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000292 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000293 * If the function returns true then the reserve suceeded and the vertices
294 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000296 * If the target cannot make space for the request then this function will
297 * return false. If vertexCount was non-zero then upon failure the vertex
298 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000300 * The pointers to the space allocated for vertices and indices remain valid
bsalomon@google.com934c5702012-03-20 21:17:58 +0000301 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, or push/
302 * popGeomtrySource is called. At that point logically a snapshot of the
303 * data is made and the pointers are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000304 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000306 * @param vertexCount the number of vertices to reserve space for. Can be
307 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000308 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000309 * @param vertices will point to reserved vertex space if vertexCount is
rmistry@google.comd6176b02012-08-23 18:14:13 +0000310 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000311 * @param indices will point to reserved index space if indexCount is
312 * non-zero. Illegal to pass NULL if indexCount > 0.
313 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000314 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
315 int vertexCount,
316 int indexCount,
317 void** vertices,
318 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000319
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 /**
321 * Provides hints to caller about the number of vertices and indices
322 * that can be allocated cheaply. This can be useful if caller is reserving
323 * space but doesn't know exactly how much geometry is needed.
324 *
325 * Also may hint whether the draw target should be flushed first. This is
326 * useful for deferred targets.
327 *
328 * @param vertexLayout layout of vertices caller would like to reserve
329 * @param vertexCount in: hint about how many vertices the caller would
330 * like to allocate.
331 * out: a hint about the number of vertices that can be
332 * allocated cheaply. Negative means no hint.
333 * Ignored if NULL.
334 * @param indexCount in: hint about how many indices the caller would
335 * like to allocate.
336 * out: a hint about the number of indices that can be
337 * allocated cheaply. Negative means no hint.
338 * Ignored if NULL.
339 *
340 * @return true if target should be flushed based on the input values.
341 */
342 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000343 int* vertexCount,
344 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
346 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000347 * Sets source of vertex data for the next draw. Array must contain
348 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 *
350 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000351 * @param size size of the vertex data.
352 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000354 void setVertexSourceToArray(GrVertexLayout vertexLayout,
355 const void* vertexArray,
356 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
358 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000359 * Sets source of index data for the next indexed draw. Array must contain
360 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000361 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000362 * @param array cpu array containing index data.
363 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000365 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366
367 /**
368 * Sets source of vertex data for the next draw. Data does not have to be
bsalomon@google.com934c5702012-03-20 21:17:58 +0000369 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 *
371 * @param buffer vertex buffer containing vertex data. Must be
372 * unlocked before draw call.
373 * @param vertexLayout layout of the vertex data in the buffer.
374 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000375 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
376 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000377
378 /**
379 * Sets source of index data for the next indexed draw. Data does not have
bsalomon@google.com934c5702012-03-20 21:17:58 +0000380 * to be in the buffer until drawIndexed.
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 *
382 * @param buffer index buffer containing indices. Must be unlocked
383 * before indexed draw call.
384 */
385 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000386
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387 /**
388 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
389 * source to reserved, array, or buffer before next draw. May be able to free
390 * up temporary storage allocated by setVertexSourceToArray or
391 * reserveVertexSpace.
392 */
393 void resetVertexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000394
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000395 /**
396 * Resets index source. Indexed Drawing from reset indices is illegal. Set
397 * index source to reserved, array, or buffer before next indexed draw. May
398 * be able to free up temporary storage allocated by setIndexSourceToArray
399 * or reserveIndexSpace.
400 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000401 void resetIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402
bsalomon@google.com97805382012-03-13 14:32:07 +0000403 /**
404 * Query to find out if the vertex or index source is reserved.
405 */
406 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000407 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000408 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
409 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
411 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000412 * Pushes and resets the vertex/index sources. Any reserved vertex / index
413 * data is finalized (i.e. cannot be updated after the matching pop but can
414 * be drawn from). Must be balanced by a pop.
415 */
416 void pushGeometrySource();
417
418 /**
419 * Pops the vertex / index sources from the matching push.
420 */
421 void popGeometrySource();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000422
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000423 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 * Draws indexed geometry using the current state and current vertex / index
425 * sources.
426 *
427 * @param type The type of primitives to draw.
428 * @param startVertex the vertex in the vertex array/buffer corresponding
429 * to index 0
430 * @param startIndex first index to read from index src.
431 * @param vertexCount one greater than the max index.
432 * @param indexCount the number of index elements to read. The index count
433 * is effectively trimmed to the last completely
434 * specified primitive.
435 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000436 void drawIndexed(GrPrimitiveType type,
437 int startVertex,
438 int startIndex,
439 int vertexCount,
440 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
442 /**
443 * Draws non-indexed geometry using the current state and current vertex
444 * sources.
445 *
446 * @param type The type of primitives to draw.
447 * @param startVertex the vertex in the vertex array/buffer corresponding
448 * to index 0
449 * @param vertexCount one greater than the max index.
450 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000451 void drawNonIndexed(GrPrimitiveType type,
452 int startVertex,
453 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000455 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000456 * Draws path into the stencil buffer. The fill must be either even/odd or
457 * winding (not inverse or hairline). It will respect the HW antialias flag
458 * on the draw state (if possible in the 3D API).
459 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000460 void stencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000461
462 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000463 * Helper function for drawing rects. This does not use the current index
464 * and vertex sources. After returning, the vertex and index sources may
465 * have changed. They should be reestablished before the next drawIndexed
466 * or drawNonIndexed. This cannot be called between reserving and releasing
467 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000468 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000469 * drawNonIndexed.
470 * @param rect the rect to draw
471 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000472 * @param srcRects specifies rects for stages enabled by stageEnableMask.
473 * if stageEnableMask bit i is 1, srcRects is not NULL,
474 * and srcRects[i] is not NULL, then srcRects[i] will be
475 * used as coordinates for stage i. Otherwise, if stage i
476 * is enabled then rect is used as the coordinates.
477 * @param srcMatrices optional matrices applied to srcRects. If
478 * srcRect[i] is non-NULL and srcMatrices[i] is
479 * non-NULL then srcRect[i] will be transformed by
480 * srcMatrix[i]. srcMatrices can be NULL when no
481 * srcMatrices are desired.
482 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000483 virtual void drawRect(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000484 const SkMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000485 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000486 const SkMatrix* srcMatrices[]);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000487 /**
488 * Helper for drawRect when the caller doesn't need separate src rects or
489 * matrices.
490 */
491 void drawSimpleRect(const GrRect& rect, const SkMatrix* matrix = NULL) {
492 drawRect(rect, matrix, NULL, NULL);
493 }
494 void drawSimpleRect(const GrIRect& irect, const SkMatrix* matrix = NULL) {
495 SkRect rect = SkRect::MakeFromIRect(irect);
496 this->drawRect(rect, matrix, NULL, NULL);
497 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000498
skia.committer@gmail.com61b05dc2012-12-14 02:02:06 +0000499
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000500 /**
bsalomon@google.com934c5702012-03-20 21:17:58 +0000501 * This call is used to draw multiple instances of some geometry with a
502 * given number of vertices (V) and indices (I) per-instance. The indices in
503 * the index source must have the form i[k+I] == i[k] + V. Also, all indices
504 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a
505 * concrete example, the following index buffer for drawing a series of
506 * quads each as two triangles each satisfies these conditions with V=4 and
507 * I=6:
508 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...)
509 *
510 * The call assumes that the pattern of indices fills the entire index
511 * source. The size of the index buffer limits the number of instances that
512 * can be drawn by the GPU in a single draw. However, the caller may specify
513 * any (positive) number for instanceCount and if necessary multiple GPU
514 * draws will be issued. Morever, when drawIndexedInstances is called
515 * multiple times it may be possible for GrDrawTarget to group them into a
516 * single GPU draw.
517 *
518 * @param type the type of primitives to draw
519 * @param instanceCount the number of instances to draw. Each instance
520 * consists of verticesPerInstance vertices indexed by
521 * indicesPerInstance indices drawn as the primitive
522 * type specified by type.
523 * @param verticesPerInstance The number of vertices in each instance (V
524 * in the above description).
525 * @param indicesPerInstance The number of indices in each instance (I
526 * in the above description).
527 */
528 virtual void drawIndexedInstances(GrPrimitiveType type,
529 int instanceCount,
530 int verticesPerInstance,
531 int indicesPerInstance);
532
533 /**
rmistry@google.comd6176b02012-08-23 18:14:13 +0000534 * Clear the current render target if one isn't passed in. Ignores the
535 * clip and all other draw state (blend mode, stages, etc). Clears the
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000536 * whole thing if rect is NULL, otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000537 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000538 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000539 GrColor color,
540 GrRenderTarget* renderTarget = NULL) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000541
robertphillips@google.comff175842012-05-14 19:31:39 +0000542 /**
543 * Release any resources that are cached but not currently in use. This
544 * is intended to give an application some recourse when resources are low.
545 */
546 virtual void purgeResources() {};
547
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000548 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000549
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000550 /**
551 * See AutoStateRestore below.
552 */
553 enum ASRInit {
554 kPreserve_ASRInit,
555 kReset_ASRInit
556 };
557
558 /**
559 * Saves off the current state and restores it in the destructor. It will
560 * install a new GrDrawState object on the target (setDrawState) and restore
561 * the previous one in the destructor. The caller should call drawState() to
562 * get the new draw state after the ASR is installed.
563 *
564 * GrDrawState* state = target->drawState();
565 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit).
566 * state->setRenderTarget(rt); // state refers to the GrDrawState set on
567 * // target before asr was initialized.
568 * // Therefore, rt is set on the GrDrawState
569 * // that will be restored after asr's
570 * // destructor rather than target's current
rmistry@google.comd6176b02012-08-23 18:14:13 +0000571 * // GrDrawState.
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000572 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 class AutoStateRestore : ::GrNoncopyable {
574 public:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000575 /**
576 * Default ASR will have no effect unless set() is subsequently called.
577 */
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000578 AutoStateRestore();
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000579
580 /**
581 * Saves the state on target. The state will be restored when the ASR
582 * is destroyed. If this constructor is used do not call set().
583 *
584 * @param init Should the newly installed GrDrawState be a copy of the
585 * previous state or a default-initialized GrDrawState.
586 */
587 AutoStateRestore(GrDrawTarget* target, ASRInit init);
588
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 ~AutoStateRestore();
590
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000591 /**
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000592 * Saves the state on target. The state will be restored when the ASR
593 * is destroyed. This should only be called once per ASR object and only
594 * when the default constructor was used. For nested saves use multiple
595 * ASR objects.
596 *
597 * @param init Should the newly installed GrDrawState be a copy of the
598 * previous state or a default-initialized GrDrawState.
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000599 */
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000600 void set(GrDrawTarget* target, ASRInit init);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000601
reed@google.comac10a2d2010-12-22 21:39:39 +0000602 private:
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000603 GrDrawTarget* fDrawTarget;
604 SkTLazy<GrDrawState> fTempState;
605 GrDrawState* fSavedState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 };
607
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000608 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000609
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 class AutoReleaseGeometry : ::GrNoncopyable {
611 public:
612 AutoReleaseGeometry(GrDrawTarget* target,
613 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000614 int vertexCount,
615 int indexCount);
616 AutoReleaseGeometry();
617 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000618 bool set(GrDrawTarget* target,
619 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000620 int vertexCount,
621 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000622 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000623 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
624 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000626 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 }
628
629 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000630 void reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000631
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000633 void* fVertices;
634 void* fIndices;
635 };
636
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000638
639 class AutoClipRestore : ::GrNoncopyable {
640 public:
641 AutoClipRestore(GrDrawTarget* target) {
642 fTarget = target;
643 fClip = fTarget->getClip();
644 }
645
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000646 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
647
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 ~AutoClipRestore() {
649 fTarget->setClip(fClip);
650 }
651 private:
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000652 GrDrawTarget* fTarget;
653 const GrClipData* fClip;
654 SkTLazy<SkClipStack> fStack;
655 GrClipData fReplacementClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000657
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000658 ////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000659
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000660 class AutoGeometryPush : ::GrNoncopyable {
661 public:
662 AutoGeometryPush(GrDrawTarget* target) {
663 GrAssert(NULL != target);
664 fTarget = target;
665 target->pushGeometrySource();
666 }
667 ~AutoGeometryPush() {
668 fTarget->popGeometrySource();
669 }
670 private:
671 GrDrawTarget* fTarget;
672 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000673
674 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000675 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000676
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 /**
678 * Helper function to compute the size of a vertex from a vertex layout
679 * @return size of a single vertex.
680 */
681 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000682
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000683 /**
684 * Helper function for determining the index of texture coordinates that
685 * is input for a texture stage. Note that a stage may instead use positions
686 * as texture coordinates, in which case the result of the function is
687 * indistinguishable from the case when the stage is disabled.
688 *
bsalomon@google.com08283af2012-10-26 13:01:20 +0000689 * @param stageIdx the stage to query
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000690 * @param vertexLayout layout to query
691 *
692 * @return the texture coordinate index or -1 if the stage doesn't use
693 * separate (non-position) texture coordinates.
694 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000695 static int VertexTexCoordsForStage(int stageIdx, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000696
697 /**
698 * Helper function to compute the offset of texture coordinates in a vertex
699 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000700 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000701 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000702 */
bsalomon@google.com08283af2012-10-26 13:01:20 +0000703 static int VertexStageCoordOffset(int stageIdx, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000704
705 /**
706 * Helper function to compute the offset of the color in a vertex
707 * @return offset of color in vertex layout or -1 if the
708 * layout has no color.
709 */
710 static int VertexColorOffset(GrVertexLayout vertexLayout);
711
bsalomon@google.coma3108262011-10-10 14:08:47 +0000712 /**
713 * Helper function to compute the offset of the coverage in a vertex
714 * @return offset of coverage in vertex layout or -1 if the
715 * layout has no coverage.
716 */
717 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
718
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000719 /**
720 * Helper function to compute the offset of the edge pts in a vertex
721 * @return offset of edge in vertex layout or -1 if the
722 * layout has no edge.
723 */
724 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
725
reed@google.comac10a2d2010-12-22 21:39:39 +0000726 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000728 * coordinates of some index.
729 *
730 * @param coordIndex the tex coord index to query
731 * @param vertexLayout layout to query
732 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000733 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000734 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000736 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000737 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000738
reed@google.comac10a2d2010-12-22 21:39:39 +0000739 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000740 * Helper function to compute the size of each vertex and the offsets of
741 * texture coordinates and color. Determines tex coord offsets by tex coord
742 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000743 * by StageTexCoordVertexLayoutBit.)
744 *
745 * @param vertexLayout the layout to query
746 * @param texCoordOffsetsByIdx after return it is the offset of each
747 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000748 * index isn't used. (optional)
749 * @param colorOffset after return it is the offset of the
750 * color field in each vertex, or -1 if
751 * there aren't per-vertex colors. (optional)
752 * @param coverageOffset after return it is the offset of the
753 * coverage field in each vertex, or -1 if
754 * there aren't per-vertex coeverages.
755 * (optional)
756 * @param edgeOffset after return it is the offset of the
757 * edge eq field in each vertex, or -1 if
758 * there aren't per-vertex edge equations.
759 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000760 * @return size of a single vertex
761 */
762 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000763 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
764 int *colorOffset,
765 int *coverageOffset,
766 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000767
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000768 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000769 * Helper function to compute the size of each vertex and the offsets of
770 * texture coordinates and color. Determines tex coord offsets by stage
771 * rather than by index. (Each stage can be mapped to any t.c. index
772 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000773 * tex coords then that stage's offset will be 0 (positions are always at 0).
774 *
775 * @param vertexLayout the layout to query
776 * @param texCoordOffsetsByStage after return it is the offset of each
777 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000778 * index isn't used. (optional)
779 * @param colorOffset after return it is the offset of the
780 * color field in each vertex, or -1 if
781 * there aren't per-vertex colors.
782 * (optional)
783 * @param coverageOffset after return it is the offset of the
784 * coverage field in each vertex, or -1 if
785 * there aren't per-vertex coeverages.
786 * (optional)
787 * @param edgeOffset after return it is the offset of the
788 * edge eq field in each vertex, or -1 if
789 * there aren't per-vertex edge equations.
790 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000791 * @return size of a single vertex
792 */
793 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000794 int texCoordOffsetsByStage[GrDrawState::kNumStages],
795 int* colorOffset,
796 int* coverageOffset,
797 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000798
799 /**
800 * Accessing positions, texture coords, or colors, of a vertex within an
801 * array is a hassle involving casts and simple math. These helpers exist
802 * to keep GrDrawTarget clients' code a bit nicer looking.
803 */
804
805 /**
806 * Gets a pointer to a GrPoint of a vertex's position or texture
807 * coordinate.
808 * @param vertices the vetex array
809 * @param vertexIndex the index of the vertex in the array
810 * @param vertexSize the size of each vertex in the array
811 * @param offset the offset in bytes of the vertex component.
812 * Defaults to zero (corresponding to vertex position)
813 * @return pointer to the vertex component as a GrPoint
814 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000815 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000816 int vertexIndex,
817 int vertexSize,
818 int offset = 0) {
819 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000820 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000821 vertexIndex * vertexSize);
822 }
823 static const GrPoint* GetVertexPoint(const void* vertices,
824 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000825 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000826 int offset = 0) {
827 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000828 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000829 vertexIndex * vertexSize);
830 }
831
832 /**
833 * Gets a pointer to a GrColor inside a vertex within a vertex array.
834 * @param vertices the vetex array
835 * @param vertexIndex the index of the vertex in the array
836 * @param vertexSize the size of each vertex in the array
837 * @param offset the offset in bytes of the vertex color
838 * @return pointer to the vertex component as a GrColor
839 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000840 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000841 int vertexIndex,
842 int vertexSize,
843 int offset) {
844 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000845 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000846 vertexIndex * vertexSize);
847 }
848 static const GrColor* GetVertexColor(const void* vertices,
849 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000850 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000851 int offset) {
852 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000853 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000854 vertexIndex * vertexSize);
855 }
856
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000857 static void VertexLayoutUnitTest();
858
reed@google.comac10a2d2010-12-22 21:39:39 +0000859protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000860
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000861 /**
862 * Optimizations for blending / coverage to be applied based on the current
863 * state.
864 * Subclasses that actually draw (as opposed to those that just buffer for
865 * playback) must implement the flags that replace the output color.
866 */
867 enum BlendOptFlags {
868 /**
869 * No optimization
870 */
871 kNone_BlendOpt = 0,
872 /**
873 * Don't draw at all
874 */
875 kSkipDraw_BlendOptFlag = 0x2,
876 /**
877 * Emit the src color, disable HW blending (replace dst with src)
878 */
879 kDisableBlend_BlendOptFlag = 0x4,
880 /**
881 * The coverage value does not have to be computed separately from
882 * alpha, the the output color can be the modulation of the two.
883 */
884 kCoverageAsAlpha_BlendOptFlag = 0x1,
885 /**
886 * Instead of emitting a src color, emit coverage in the alpha channel
887 * and r,g,b are "don't cares".
888 */
889 kEmitCoverage_BlendOptFlag = 0x10,
890 /**
891 * Emit transparent black instead of the src color, no need to compute
892 * coverage.
893 */
894 kEmitTransBlack_BlendOptFlag = 0x8,
895 };
896 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000897
skia.committer@gmail.com61b05dc2012-12-14 02:02:06 +0000898 /**
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000899 * Determines what optimizations can be applied based on the blend. The coefficients may have
900 * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
901 * params that receive the tweaked coefficients. Normally the function looks at the current
902 * state to see if coverage is enabled. By setting forceCoverage the caller can speculatively
903 * determine the blend optimizations that would be used if there was partial pixel coverage.
904 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000905 BlendOptFlags getBlendOpts(bool forceCoverage = false,
906 GrBlendCoeff* srcCoeff = NULL,
907 GrBlendCoeff* dstCoeff = NULL) const;
908
909 // determine if src alpha is guaranteed to be one for all src pixels
bsalomon@google.come79c8152012-03-29 19:07:12 +0000910 bool srcAlphaWillBeOne(GrVertexLayout vertexLayout) const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000911
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000912 enum GeometrySrcType {
913 kNone_GeometrySrcType, //<! src has not been specified
914 kReserved_GeometrySrcType, //<! src was set using reserve*Space
915 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
916 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
917 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000919 struct GeometrySrcState {
920 GeometrySrcType fVertexSrc;
921 union {
922 // valid if src type is buffer
923 const GrVertexBuffer* fVertexBuffer;
924 // valid if src type is reserved or array
925 int fVertexCount;
926 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000927
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000928 GeometrySrcType fIndexSrc;
929 union {
930 // valid if src type is buffer
931 const GrIndexBuffer* fIndexBuffer;
932 // valid if src type is reserved or array
933 int fIndexCount;
934 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000935
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000936 GrVertexLayout fVertexLayout;
937 };
bsalomon@google.com934c5702012-03-20 21:17:58 +0000938
939 int indexCountInCurrentSource() const {
940 const GeometrySrcState& src = this->getGeomSrc();
941 switch (src.fIndexSrc) {
942 case kNone_GeometrySrcType:
943 return 0;
944 case kReserved_GeometrySrcType:
945 case kArray_GeometrySrcType:
946 return src.fIndexCount;
947 case kBuffer_GeometrySrcType:
948 return src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
949 default:
950 GrCrash("Unexpected Index Source.");
951 return 0;
952 }
953 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000954
bsalomon@google.com08283af2012-10-26 13:01:20 +0000955 bool isStageEnabled(int stageIdx) const {
956 return this->getDrawState().isStageEnabled(stageIdx);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000957 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000958
bsalomon@google.com97805382012-03-13 14:32:07 +0000959 // A sublcass can optionally overload this function to be notified before
960 // vertex and index space is reserved.
961 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
962 int vertexCount,
963 int indexCount) {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000964
bsalomon@google.com97805382012-03-13 14:32:07 +0000965
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000966 // implemented by subclass to allocate space for reserved geom
967 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
968 int vertexCount,
969 void** vertices) = 0;
970 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
971 // implemented by subclass to handle release of reserved geom space
972 virtual void releaseReservedVertexSpace() = 0;
973 virtual void releaseReservedIndexSpace() = 0;
974 // subclass must consume array contents when set
975 virtual void onSetVertexSourceToArray(const void* vertexArray,
976 int vertexCount) = 0;
977 virtual void onSetIndexSourceToArray(const void* indexArray,
978 int indexCount) = 0;
979 // subclass is notified that geom source will be set away from an array
980 virtual void releaseVertexArray() = 0;
981 virtual void releaseIndexArray() = 0;
982 // subclass overrides to be notified just before geo src state
983 // is pushed/popped.
984 virtual void geometrySourceWillPush() = 0;
985 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
986 // subclass called to perform drawing
987 virtual void onDrawIndexed(GrPrimitiveType type,
988 int startVertex,
989 int startIndex,
990 int vertexCount,
991 int indexCount) = 0;
992 virtual void onDrawNonIndexed(GrPrimitiveType type,
993 int startVertex,
994 int vertexCount) = 0;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000995 virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType fill) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000996
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000997 // subclass overrides to be notified when clip is set. Must call
998 // INHERITED::clipwillBeSet
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000999 virtual void clipWillBeSet(const GrClipData* clipData) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001000
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001001 // Helpers for drawRect, protected so subclasses that override drawRect
1002 // can use them.
bsalomon@google.come3d32162012-07-20 13:37:06 +00001003 static GrVertexLayout GetRectVertexLayout(const GrRect* srcRects[]);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001004
1005 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001006 const SkMatrix* matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001007 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001008 const SkMatrix* srcMatrices[],
robertphillips@google.com8b129aa2012-10-05 15:37:00 +00001009 GrColor color,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001010 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001011 void* vertices);
1012
bsalomon@google.come79c8152012-03-29 19:07:12 +00001013 // accessors for derived classes
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001014 const GeometrySrcState& getGeomSrc() const {
1015 return fGeoSrcStateStack.back();
1016 }
bsalomon@google.come79c8152012-03-29 19:07:12 +00001017 // it is prefereable to call this rather than getGeomSrc()->fVertexLayout
1018 // because of the assert.
1019 GrVertexLayout getVertexLayout() const {
1020 // the vertex layout is only valid if a vertex source has been
1021 // specified.
1022 GrAssert(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType);
1023 return this->getGeomSrc().fVertexLayout;
1024 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001025
bsalomon@google.comf6601872012-08-28 21:11:35 +00001026 // allows derived class to set the caps
1027 CapsInternals* capsInternals() { return &fCaps.fInternals; }
1028
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001029 const GrClipData* fClip;
reed@google.comac10a2d2010-12-22 21:39:39 +00001030
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001031 GrDrawState* fDrawState;
1032 GrDrawState fDefaultDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001033
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001034 Caps fCaps;
1035
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001036 // subclasses must call this in their destructors to ensure all vertex
rmistry@google.comd6176b02012-08-23 18:14:13 +00001037 // and index sources have been released (including those held by
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001038 // pushGeometrySource())
1039 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001040
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001041private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001042 // helpers for reserving vertex and index space.
1043 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1044 int vertexCount,
1045 void** vertices);
1046 bool reserveIndexSpace(int indexCount, void** indices);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001047
bsalomon@google.come8262622011-11-07 02:30:51 +00001048 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1049 // indicate non-indexed drawing.
1050 bool checkDraw(GrPrimitiveType type, int startVertex,
1051 int startIndex, int vertexCount,
1052 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001053 // called when setting a new vert/idx source to unref prev vb/ib
1054 void releasePreviousVertexSource();
1055 void releasePreviousIndexSource();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001056
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001057 enum {
1058 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001060 SkSTArray<kPreallocGeoSrcStateStackCnt,
bsalomon@google.com92669012011-09-27 19:10:05 +00001061 GeometrySrcState, true> fGeoSrcStateStack;
reed@google.comfa35e3d2012-06-26 20:16:17 +00001062
1063 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064};
1065
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001066GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1067
reed@google.comac10a2d2010-12-22 21:39:39 +00001068#endif