blob: 1628ae3f30a8c1f7db70ae74680973b32495765f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrDrawTarget_DEFINED
12#define GrDrawTarget_DEFINED
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrClip.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrColor.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000016#include "GrDrawState.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000017#include "GrMatrix.h"
18#include "GrRefCnt.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000019#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000020#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
Scroggo97c88c22011-05-11 14:05:25 +000023#include "SkXfermode.h"
bsalomon@google.com46f7afb2012-01-18 19:51:55 +000024#include "SkTLazy.h"
Scroggo97c88c22011-05-11 14:05:25 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000027class GrClipIterator;
28class GrVertexBuffer;
29class GrIndexBuffer;
30
31class GrDrawTarget : public GrRefCnt {
32public:
33 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000034 * Represents the draw target capabilities.
35 */
36 struct Caps {
37 Caps() { memset(this, 0, sizeof(Caps)); }
38 Caps(const Caps& c) { *this = c; }
39 Caps& operator= (const Caps& c) {
40 memcpy(this, &c, sizeof(Caps));
41 return *this;
42 }
43 void print() const;
44 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000045 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000046 bool fTwoSidedStencilSupport : 1;
47 bool fStencilWrapOpsSupport : 1;
48 bool fHWAALineSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000049 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000050 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000051 bool fFSAASupport : 1;
52 bool fDualSourceBlendingSupport : 1;
53 bool fBufferLockSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000054 int fMaxRenderTargetSize;
55 int fMaxTextureSize;
56 };
57
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000058 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000059 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000060
reed@google.comac10a2d2010-12-22 21:39:39 +000061 ///////////////////////////////////////////////////////////////////////////
62
63 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000064 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000065
66 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000067 * Gets the capabilities of the draw target.
68 */
69 const Caps& getCaps() const { return fCaps; }
70
71 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000072 * Sets the current clip to the region specified by clip. All draws will be
73 * clipped against this clip if kClip_StateBit is enabled.
74 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000075 * Setting the clip may (or may not) zero out the client's stencil bits.
76 *
reed@google.comac10a2d2010-12-22 21:39:39 +000077 * @param description of the clipping region
78 */
79 void setClip(const GrClip& clip);
80
81 /**
82 * Gets the current clip.
83 *
84 * @return the clip.
85 */
86 const GrClip& getClip() const;
87
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000088 const GrDrawState& getDrawState() const { return fCurrDrawState; }
89 GrDrawState* drawState() { return &fCurrDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000090
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000091 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000092 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
93 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000094 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000095 * @param matrix the matrix to concat
96 */
97 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
98 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000099 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000100 }
101
102 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000103 * Determines if blending will require a read of a dst given the current
104 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000105 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000106 * @return true if the dst surface will be read at each pixel hit by the
107 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000108 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000109 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000110
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000111 /**
112 * Color alpha and coverage are two inputs to the drawing pipeline. For some
113 * blend modes it is safe to fold the coverage into constant or per-vertex
114 * color alpha value. For other blend modes they must be handled separately.
115 * Depending on features available in the underlying 3D API this may or may
116 * not be possible.
117 *
118 * This function looks at the current blend on the draw target and the draw
119 * target's capabilities to determine whether coverage can be handled
120 * correctly.
121 */
122 bool canApplyCoverage() const;
123
124 /**
125 * Determines whether incorporating partial pixel coverage into the constant
126 * color specified by setColor or per-vertex colors will give the right
127 * blending result.
128 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000129 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000130
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000131 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000132 * Given the current draw state, vertex layout, and hw support, will HW AA
133 * lines be used (if line primitive type is drawn)? (Note that lines are
134 * always 1 pixel wide)
135 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000136 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000137
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000138 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000139 * Used to save and restore the GrGpu's drawing state
140 */
141 struct SavedDrawState {
142 private:
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000143 SkTLazy<GrDrawState> fState;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000144 friend class GrDrawTarget;
145 };
146
147 /**
148 * Saves the current draw state. The state can be restored at a later time
149 * with restoreDrawState.
150 *
151 * See also AutoStateRestore class.
152 *
153 * @param state will hold the state after the function returns.
154 */
155 void saveCurrentDrawState(SavedDrawState* state) const;
156
157 /**
158 * Restores previously saved draw state. The client guarantees that state
159 * was previously passed to saveCurrentDrawState and that the rendertarget
160 * and texture set at save are still valid.
161 *
162 * See also AutoStateRestore class.
163 *
164 * @param state the previously saved state to restore.
165 */
166 void restoreDrawState(const SavedDrawState& state);
167
168 /**
169 * Copies the draw state from another target to this target.
170 *
171 * @param srcTarget draw target used as src of the draw state.
172 */
bsalomon@google.com4a44b4b2012-03-19 21:09:08 +0000173 void copyDrawState(const GrDrawTarget& srcTarget) {
174 fCurrDrawState = srcTarget.fCurrDrawState;
175 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000176
177 /**
178 * The format of vertices is represented as a bitfield of flags.
179 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000180 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
181 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
182 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000183 * use any of the texture coordinates as its input texture coordinates or it
184 * may use the positions as texture coordinates.
185 *
186 * If no texture coordinates are specified for a stage then the stage is
187 * disabled.
188 *
189 * Only one type of texture coord can be specified per stage. For
190 * example StageTexCoordVertexLayoutBit(0, 2) and
191 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
192 *
193 * The order in memory is always (position, texture coord 0, ..., color,
194 * coverage) with any unused fields omitted. Note that this means that if
195 * only texture coordinates 1 is referenced then there is no texture
196 * coordinates 0 and the order would be (position, texture coordinate 1
197 * [, color][, coverage]).
198 */
199
200 /**
201 * Generates a bit indicating that a texture stage uses texture coordinates
202 *
203 * @param stage the stage that will use texture coordinates.
204 * @param texCoordIdx the index of the texture coordinates to use
205 *
206 * @return the bit to add to a GrVertexLayout bitfield.
207 */
208 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000209 GrAssert(stage < GrDrawState::kNumStages);
210 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
211 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000212 }
213
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000214private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000215 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
216 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000217
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000218public:
219 /**
220 * Generates a bit indicating that a texture stage uses the position
221 * as its texture coordinate.
222 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000223 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000224 * coordinates.
225 *
226 * @return the bit to add to a GrVertexLayout bitfield.
227 */
228 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000229 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000230 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000231 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000232
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000233private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000234 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
235 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000236
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000237public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000238
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000239 /**
240 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 */
242 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000243 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000244 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000245 /* vertices have coverage (GrColor)
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000246 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000247 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000248 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000249 * text [GrGpuTextVertex vs GrPoint].)
250 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000251 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000252
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000253 /* Each vertex specificies an edge. Distance to the edge is used to
bsalomon@google.comcff56082012-01-11 15:33:20 +0000254 * compute a coverage. See GrDrawState::setVertexEdgeType().
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000255 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000256 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000258 kDummyVertexLayoutBit,
259 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000260 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000261 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000262 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000263
264 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000265 * There are three types of "sources" of geometry (vertices and indices) for
266 * draw calls made on the target. When performing an indexed draw, the
267 * indices and vertices can use different source types. Once a source is
268 * specified it can be used for multiple drawIndexed and drawNonIndexed
269 * calls. However, the time at which the geometry data is no longer editable
270 * depends on the source type.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000271 *
272 * Sometimes it is necessary to perform a draw while upstack code has
bsalomon@google.come3d70952012-03-13 12:40:53 +0000273 * already specified geometry that it isn't finished with. So there are push
274 * and pop methods. This allows the client to push the sources, draw
275 * something using alternate sources, and then pop to restore the original
276 * sources.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000278 * Aside from pushes and pops, a source remains valid until another source
279 * is set or resetVertexSource / resetIndexSource is called. Drawing from
280 * a reset source is an error.
281 *
282 * The three types of sources are:
283 *
284 * 1. A cpu array (set*SourceToArray). This is useful when the caller
285 * already provided vertex data in a format compatible with a
286 * GrVertexLayout. The data in the array is consumed at the time that
287 * set*SourceToArray is called and subsequent edits to the array will not
288 * be reflected in draws.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000290 * 2. Reserve. This is most useful when the caller has data it must
291 * transform before drawing and is not long-lived. The caller requests
292 * that the draw target make room for some amount of vertex and/or index
293 * data. The target provides ptrs to hold the vertex and/or index data.
294 *
295 * The data is writable up until the next drawIndexed, drawNonIndexed,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000296 * or pushGeometrySource. At this point the data is frozen and the ptrs
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000297 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000298 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000299 * Where the space is allocated and how it is uploaded to the GPU is
300 * subclass-dependent.
301 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000302 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.come3d70952012-03-13 12:40:53 +0000303 * is long-lived. When the data in the buffer is consumed depends on the
304 * GrDrawTarget subclass. For deferred subclasses the caller has to
305 * guarantee that the data is still available in the buffers at playback.
306 * (TODO: Make this more automatic as we have done for read/write pixels)
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000307 */
308
309 /**
bsalomon@google.come3d70952012-03-13 12:40:53 +0000310 * Reserves space for vertices and/or indices. Zero can be specifed as
311 * either the vertex or index count if the caller desires to only reserve
312 * space for only indices or only vertices. If zero is specifed for
313 * vertexCount then the vertex source will be unmodified and likewise for
314 * indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000315 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000316 * If the function returns true then the reserve suceeded and the vertices
317 * and indices pointers will point to the space created.
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000319 * If the target cannot make space for the request then this function will
320 * return false. If vertexCount was non-zero then upon failure the vertex
321 * source is reset and likewise for indexCount.
reed@google.comac10a2d2010-12-22 21:39:39 +0000322 *
bsalomon@google.come3d70952012-03-13 12:40:53 +0000323 * The pointers to the space allocated for vertices and indices remain valid
324 * until a drawIndexed, drawNonIndexed, or push/popGeomtrySource is called.
325 * At that point logically a snapshot of the data is made and the pointers
326 * are invalid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000327 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000328 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.come3d70952012-03-13 12:40:53 +0000329 * @param vertexCount the number of vertices to reserve space for. Can be
330 * 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000331 * @param indexCount the number of indices to reserve space for. Can be 0.
bsalomon@google.come3d70952012-03-13 12:40:53 +0000332 * @param vertices will point to reserved vertex space if vertexCount is
333 * non-zero. Illegal to pass NULL if vertexCount > 0.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000334 * @param indices will point to reserved index space if indexCount is
335 * non-zero. Illegal to pass NULL if indexCount > 0.
336 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000337 bool reserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
338 int vertexCount,
339 int indexCount,
340 void** vertices,
341 void** indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000342
reed@google.comac10a2d2010-12-22 21:39:39 +0000343 /**
344 * Provides hints to caller about the number of vertices and indices
345 * that can be allocated cheaply. This can be useful if caller is reserving
346 * space but doesn't know exactly how much geometry is needed.
347 *
348 * Also may hint whether the draw target should be flushed first. This is
349 * useful for deferred targets.
350 *
351 * @param vertexLayout layout of vertices caller would like to reserve
352 * @param vertexCount in: hint about how many vertices the caller would
353 * like to allocate.
354 * out: a hint about the number of vertices that can be
355 * allocated cheaply. Negative means no hint.
356 * Ignored if NULL.
357 * @param indexCount in: hint about how many indices the caller would
358 * like to allocate.
359 * out: a hint about the number of indices that can be
360 * allocated cheaply. Negative means no hint.
361 * Ignored if NULL.
362 *
363 * @return true if target should be flushed based on the input values.
364 */
365 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000366 int* vertexCount,
367 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
369 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000370 * Sets source of vertex data for the next draw. Array must contain
371 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 *
373 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000374 * @param size size of the vertex data.
375 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000377 void setVertexSourceToArray(GrVertexLayout vertexLayout,
378 const void* vertexArray,
379 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380
381 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000382 * Sets source of index data for the next indexed draw. Array must contain
383 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000385 * @param array cpu array containing index data.
386 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000388 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
390 /**
391 * Sets source of vertex data for the next draw. Data does not have to be
392 * in the buffer until drawIndexed or drawNonIndexed.
393 *
394 * @param buffer vertex buffer containing vertex data. Must be
395 * unlocked before draw call.
396 * @param vertexLayout layout of the vertex data in the buffer.
397 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000398 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
399 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000400
401 /**
402 * Sets source of index data for the next indexed draw. Data does not have
403 * to be in the buffer until drawIndexed or drawNonIndexed.
404 *
405 * @param buffer index buffer containing indices. Must be unlocked
406 * before indexed draw call.
407 */
408 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000409
410 /**
411 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
412 * source to reserved, array, or buffer before next draw. May be able to free
413 * up temporary storage allocated by setVertexSourceToArray or
414 * reserveVertexSpace.
415 */
416 void resetVertexSource();
417
418 /**
419 * Resets index source. Indexed Drawing from reset indices is illegal. Set
420 * index source to reserved, array, or buffer before next indexed draw. May
421 * be able to free up temporary storage allocated by setIndexSourceToArray
422 * or reserveIndexSpace.
423 */
bsalomon@google.com97805382012-03-13 14:32:07 +0000424 void resetIndexSource();
425
426 /**
427 * Query to find out if the vertex or index source is reserved.
428 */
429 bool hasReservedVerticesOrIndices() const {
bsalomon@google.com73d98aa2012-03-13 14:41:19 +0000430 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
bsalomon@google.com97805382012-03-13 14:32:07 +0000431 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
432 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000433
434 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000435 * Pushes and resets the vertex/index sources. Any reserved vertex / index
436 * data is finalized (i.e. cannot be updated after the matching pop but can
437 * be drawn from). Must be balanced by a pop.
438 */
439 void pushGeometrySource();
440
441 /**
442 * Pops the vertex / index sources from the matching push.
443 */
444 void popGeometrySource();
445
446 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 * Draws indexed geometry using the current state and current vertex / index
448 * sources.
449 *
450 * @param type The type of primitives to draw.
451 * @param startVertex the vertex in the vertex array/buffer corresponding
452 * to index 0
453 * @param startIndex first index to read from index src.
454 * @param vertexCount one greater than the max index.
455 * @param indexCount the number of index elements to read. The index count
456 * is effectively trimmed to the last completely
457 * specified primitive.
458 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000459 void drawIndexed(GrPrimitiveType type,
460 int startVertex,
461 int startIndex,
462 int vertexCount,
463 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
465 /**
466 * Draws non-indexed geometry using the current state and current vertex
467 * sources.
468 *
469 * @param type The type of primitives to draw.
470 * @param startVertex the vertex in the vertex array/buffer corresponding
471 * to index 0
472 * @param vertexCount one greater than the max index.
473 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 void drawNonIndexed(GrPrimitiveType type,
475 int startVertex,
476 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000478 /**
479 * Helper function for drawing rects. This does not use the current index
480 * and vertex sources. After returning, the vertex and index sources may
481 * have changed. They should be reestablished before the next drawIndexed
482 * or drawNonIndexed. This cannot be called between reserving and releasing
483 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000484 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000485 * drawNonIndexed.
486 * @param rect the rect to draw
487 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000488 * @param stageMask bitmask indicating which stages are enabled.
489 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000490 * @param srcRects specifies rects for stages enabled by stageEnableMask.
491 * if stageEnableMask bit i is 1, srcRects is not NULL,
492 * and srcRects[i] is not NULL, then srcRects[i] will be
493 * used as coordinates for stage i. Otherwise, if stage i
494 * is enabled then rect is used as the coordinates.
495 * @param srcMatrices optional matrices applied to srcRects. If
496 * srcRect[i] is non-NULL and srcMatrices[i] is
497 * non-NULL then srcRect[i] will be transformed by
498 * srcMatrix[i]. srcMatrices can be NULL when no
499 * srcMatrices are desired.
500 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000501 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000502 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000503 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000504 const GrRect* srcRects[],
505 const GrMatrix* srcMatrices[]);
506
507 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000508 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000509 * matrices.
510 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000511 void drawSimpleRect(const GrRect& rect,
512 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000513 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000514 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000515 }
516
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000517 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000518 * Clear the render target. Ignores the clip and all other draw state
519 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
520 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000521 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000522 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000523
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000524 /**
525 * Returns the maximum number of edges that may be specified in a single
526 * draw call when performing edge antialiasing. This is usually limited
527 * by the number of fragment uniforms which may be uploaded. Must be a
528 * minimum of six, since a triangle's vertices each belong to two boundary
529 * edges which may be distinct.
530 */
531 virtual int getMaxEdges() const { return 6; }
532
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000534
535 class AutoStateRestore : ::GrNoncopyable {
536 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000537 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 AutoStateRestore(GrDrawTarget* target);
539 ~AutoStateRestore();
540
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000541 /**
542 * if this object is already saving state for param target then
543 * this does nothing. Otherise, it restores previously saved state on
544 * previous target (if any) and saves current state on param target.
545 */
546 void set(GrDrawTarget* target);
547
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 private:
549 GrDrawTarget* fDrawTarget;
550 SavedDrawState fDrawState;
551 };
552
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000553 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000554
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000555 /**
556 * Sets the view matrix to I and preconcats all stage matrices enabled in
557 * mask by the view inverse. Destructor undoes these changes.
558 */
559 class AutoDeviceCoordDraw : ::GrNoncopyable {
560 public:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000561 AutoDeviceCoordDraw(GrDrawTarget* target, StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000562 ~AutoDeviceCoordDraw();
563 private:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000564 GrDrawTarget* fDrawTarget;
565 GrMatrix fViewMatrix;
566 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
567 int fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000568 };
569
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000570 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000571
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 class AutoReleaseGeometry : ::GrNoncopyable {
573 public:
574 AutoReleaseGeometry(GrDrawTarget* target,
575 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000576 int vertexCount,
577 int indexCount);
578 AutoReleaseGeometry();
579 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000580 bool set(GrDrawTarget* target,
581 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000582 int vertexCount,
583 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000584 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000585 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
586 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000588 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 }
590
591 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000592 void reset();
593
reed@google.comac10a2d2010-12-22 21:39:39 +0000594 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 void* fVertices;
596 void* fIndices;
597 };
598
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000599 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000600
601 class AutoClipRestore : ::GrNoncopyable {
602 public:
603 AutoClipRestore(GrDrawTarget* target) {
604 fTarget = target;
605 fClip = fTarget->getClip();
606 }
607
608 ~AutoClipRestore() {
609 fTarget->setClip(fClip);
610 }
611 private:
612 GrDrawTarget* fTarget;
613 GrClip fClip;
614 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000615
616 ////////////////////////////////////////////////////////////////////////////
617
618 class AutoGeometryPush : ::GrNoncopyable {
619 public:
620 AutoGeometryPush(GrDrawTarget* target) {
621 GrAssert(NULL != target);
622 fTarget = target;
623 target->pushGeometrySource();
624 }
625 ~AutoGeometryPush() {
626 fTarget->popGeometrySource();
627 }
628 private:
629 GrDrawTarget* fTarget;
630 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000631
632 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000633 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000634
reed@google.comac10a2d2010-12-22 21:39:39 +0000635 /**
636 * Helper function to compute the size of a vertex from a vertex layout
637 * @return size of a single vertex.
638 */
639 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000640
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000641 /**
642 * Helper function for determining the index of texture coordinates that
643 * is input for a texture stage. Note that a stage may instead use positions
644 * as texture coordinates, in which case the result of the function is
645 * indistinguishable from the case when the stage is disabled.
646 *
647 * @param stage the stage to query
648 * @param vertexLayout layout to query
649 *
650 * @return the texture coordinate index or -1 if the stage doesn't use
651 * separate (non-position) texture coordinates.
652 */
653 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000654
655 /**
656 * Helper function to compute the offset of texture coordinates in a vertex
657 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000658 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000659 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000661 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000662
663 /**
664 * Helper function to compute the offset of the color in a vertex
665 * @return offset of color in vertex layout or -1 if the
666 * layout has no color.
667 */
668 static int VertexColorOffset(GrVertexLayout vertexLayout);
669
bsalomon@google.coma3108262011-10-10 14:08:47 +0000670 /**
671 * Helper function to compute the offset of the coverage in a vertex
672 * @return offset of coverage in vertex layout or -1 if the
673 * layout has no coverage.
674 */
675 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
676
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000677 /**
678 * Helper function to compute the offset of the edge pts in a vertex
679 * @return offset of edge in vertex layout or -1 if the
680 * layout has no edge.
681 */
682 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
683
reed@google.comac10a2d2010-12-22 21:39:39 +0000684 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000685 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000686 * coordinates of some index.
687 *
688 * @param coordIndex the tex coord index to query
689 * @param vertexLayout layout to query
690 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000691 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000692 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000693 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000694 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000695 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000696
reed@google.comac10a2d2010-12-22 21:39:39 +0000697 /**
698 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000699 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000700 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000701 * @param stage the stage to query
702 * @param vertexLayout layout to query
703 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000704 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000705 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000706 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000707 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000708
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000709 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000710 * Helper function to compute the size of each vertex and the offsets of
711 * texture coordinates and color. Determines tex coord offsets by tex coord
712 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000713 * by StageTexCoordVertexLayoutBit.)
714 *
715 * @param vertexLayout the layout to query
716 * @param texCoordOffsetsByIdx after return it is the offset of each
717 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000718 * index isn't used. (optional)
719 * @param colorOffset after return it is the offset of the
720 * color field in each vertex, or -1 if
721 * there aren't per-vertex colors. (optional)
722 * @param coverageOffset after return it is the offset of the
723 * coverage field in each vertex, or -1 if
724 * there aren't per-vertex coeverages.
725 * (optional)
726 * @param edgeOffset after return it is the offset of the
727 * edge eq field in each vertex, or -1 if
728 * there aren't per-vertex edge equations.
729 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000730 * @return size of a single vertex
731 */
732 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000733 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
734 int *colorOffset,
735 int *coverageOffset,
736 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000737
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000738 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000739 * Helper function to compute the size of each vertex and the offsets of
740 * texture coordinates and color. Determines tex coord offsets by stage
741 * rather than by index. (Each stage can be mapped to any t.c. index
742 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000743 * tex coords then that stage's offset will be 0 (positions are always at 0).
744 *
745 * @param vertexLayout the layout to query
746 * @param texCoordOffsetsByStage 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.
752 * (optional)
753 * @param coverageOffset after return it is the offset of the
754 * coverage field in each vertex, or -1 if
755 * there aren't per-vertex coeverages.
756 * (optional)
757 * @param edgeOffset after return it is the offset of the
758 * edge eq field in each vertex, or -1 if
759 * there aren't per-vertex edge equations.
760 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000761 * @return size of a single vertex
762 */
763 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000764 int texCoordOffsetsByStage[GrDrawState::kNumStages],
765 int* colorOffset,
766 int* coverageOffset,
767 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000768
769 /**
770 * Accessing positions, texture coords, or colors, of a vertex within an
771 * array is a hassle involving casts and simple math. These helpers exist
772 * to keep GrDrawTarget clients' code a bit nicer looking.
773 */
774
775 /**
776 * Gets a pointer to a GrPoint of a vertex's position or texture
777 * coordinate.
778 * @param vertices the vetex array
779 * @param vertexIndex the index of the vertex in the array
780 * @param vertexSize the size of each vertex in the array
781 * @param offset the offset in bytes of the vertex component.
782 * Defaults to zero (corresponding to vertex position)
783 * @return pointer to the vertex component as a GrPoint
784 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000785 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000786 int vertexIndex,
787 int vertexSize,
788 int offset = 0) {
789 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000790 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000791 vertexIndex * vertexSize);
792 }
793 static const GrPoint* GetVertexPoint(const void* vertices,
794 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000795 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000796 int offset = 0) {
797 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000798 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000799 vertexIndex * vertexSize);
800 }
801
802 /**
803 * Gets a pointer to a GrColor inside a vertex within a vertex array.
804 * @param vertices the vetex array
805 * @param vertexIndex the index of the vertex in the array
806 * @param vertexSize the size of each vertex in the array
807 * @param offset the offset in bytes of the vertex color
808 * @return pointer to the vertex component as a GrColor
809 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000810 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000811 int vertexIndex,
812 int vertexSize,
813 int offset) {
814 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000815 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000816 vertexIndex * vertexSize);
817 }
818 static const GrColor* GetVertexColor(const void* vertices,
819 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000820 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000821 int offset) {
822 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000823 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000824 vertexIndex * vertexSize);
825 }
826
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000827 static void VertexLayoutUnitTest();
828
reed@google.comac10a2d2010-12-22 21:39:39 +0000829protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000830
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000831 /**
832 * Optimizations for blending / coverage to be applied based on the current
833 * state.
834 * Subclasses that actually draw (as opposed to those that just buffer for
835 * playback) must implement the flags that replace the output color.
836 */
837 enum BlendOptFlags {
838 /**
839 * No optimization
840 */
841 kNone_BlendOpt = 0,
842 /**
843 * Don't draw at all
844 */
845 kSkipDraw_BlendOptFlag = 0x2,
846 /**
847 * Emit the src color, disable HW blending (replace dst with src)
848 */
849 kDisableBlend_BlendOptFlag = 0x4,
850 /**
851 * The coverage value does not have to be computed separately from
852 * alpha, the the output color can be the modulation of the two.
853 */
854 kCoverageAsAlpha_BlendOptFlag = 0x1,
855 /**
856 * Instead of emitting a src color, emit coverage in the alpha channel
857 * and r,g,b are "don't cares".
858 */
859 kEmitCoverage_BlendOptFlag = 0x10,
860 /**
861 * Emit transparent black instead of the src color, no need to compute
862 * coverage.
863 */
864 kEmitTransBlack_BlendOptFlag = 0x8,
865 };
866 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000867
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000868 // Determines what optimizations can be applied based on the blend.
869 // The coeffecients may have to be tweaked in order for the optimization
870 // to work. srcCoeff and dstCoeff are optional params that receive the
871 // tweaked coeffecients.
872 // Normally the function looks at the current state to see if coverage
873 // is enabled. By setting forceCoverage the caller can speculatively
874 // determine the blend optimizations that would be used if there was
875 // partial pixel coverage
876 BlendOptFlags getBlendOpts(bool forceCoverage = false,
877 GrBlendCoeff* srcCoeff = NULL,
878 GrBlendCoeff* dstCoeff = NULL) const;
879
880 // determine if src alpha is guaranteed to be one for all src pixels
881 bool srcAlphaWillBeOne() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000882
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000883 enum GeometrySrcType {
884 kNone_GeometrySrcType, //<! src has not been specified
885 kReserved_GeometrySrcType, //<! src was set using reserve*Space
886 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
887 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
888 };
889
890 struct GeometrySrcState {
891 GeometrySrcType fVertexSrc;
892 union {
893 // valid if src type is buffer
894 const GrVertexBuffer* fVertexBuffer;
895 // valid if src type is reserved or array
896 int fVertexCount;
897 };
898
899 GeometrySrcType fIndexSrc;
900 union {
901 // valid if src type is buffer
902 const GrIndexBuffer* fIndexBuffer;
903 // valid if src type is reserved or array
904 int fIndexCount;
905 };
906
907 GrVertexLayout fVertexLayout;
908 };
909
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000910 // given a vertex layout and a draw state, will a stage be used?
911 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000912 const GrDrawState& state) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000913 return NULL != state.getTexture(stage) &&
914 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000915 }
916
917 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000918 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
919 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000920 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000921
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000922 StageMask enabledStages() const {
923 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000924 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000925 mask |= this->isStageEnabled(s) ? 1 : 0;
926 }
927 return mask;
928 }
929
reed@google.comac10a2d2010-12-22 21:39:39 +0000930 // Helpers for GrDrawTarget subclasses that won't have private access to
931 // SavedDrawState but need to peek at the state values.
bsalomon@google.com46f7afb2012-01-18 19:51:55 +0000932 static GrDrawState& accessSavedDrawState(SavedDrawState& sds) {
933 return *sds.fState.get();
934 }
935 static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds){
936 return *sds.fState.get();
937 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000938
bsalomon@google.com97805382012-03-13 14:32:07 +0000939 // A sublcass can optionally overload this function to be notified before
940 // vertex and index space is reserved.
941 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
942 int vertexCount,
943 int indexCount) {}
944
945
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000946 // implemented by subclass to allocate space for reserved geom
947 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
948 int vertexCount,
949 void** vertices) = 0;
950 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
951 // implemented by subclass to handle release of reserved geom space
952 virtual void releaseReservedVertexSpace() = 0;
953 virtual void releaseReservedIndexSpace() = 0;
954 // subclass must consume array contents when set
955 virtual void onSetVertexSourceToArray(const void* vertexArray,
956 int vertexCount) = 0;
957 virtual void onSetIndexSourceToArray(const void* indexArray,
958 int indexCount) = 0;
959 // subclass is notified that geom source will be set away from an array
960 virtual void releaseVertexArray() = 0;
961 virtual void releaseIndexArray() = 0;
962 // subclass overrides to be notified just before geo src state
963 // is pushed/popped.
964 virtual void geometrySourceWillPush() = 0;
965 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
966 // subclass called to perform drawing
967 virtual void onDrawIndexed(GrPrimitiveType type,
968 int startVertex,
969 int startIndex,
970 int vertexCount,
971 int indexCount) = 0;
972 virtual void onDrawNonIndexed(GrPrimitiveType type,
973 int startVertex,
974 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000975 // subclass overrides to be notified when clip is set. Must call
976 // INHERITED::clipwillBeSet
977 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000978
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000979 // Helpers for drawRect, protected so subclasses that override drawRect
980 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000981 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000982 const GrRect* srcRects[]);
983
984 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000985 const GrMatrix* matrix,
986 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000987 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +0000988 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000989 void* vertices);
990
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000991 // accessor for derived classes
992 const GeometrySrcState& getGeomSrc() const {
993 return fGeoSrcStateStack.back();
994 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
996 GrClip fClip;
997
tomhudson@google.com93813632011-10-27 20:21:16 +0000998 GrDrawState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000999
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001000 Caps fCaps;
1001
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001002 // subclasses must call this in their destructors to ensure all vertex
1003 // and index sources have been released (including those held by
1004 // pushGeometrySource())
1005 void releaseGeometry();
bsalomon@google.come3d70952012-03-13 12:40:53 +00001006
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001007private:
bsalomon@google.come3d70952012-03-13 12:40:53 +00001008 // helpers for reserving vertex and index space.
1009 bool reserveVertexSpace(GrVertexLayout vertexLayout,
1010 int vertexCount,
1011 void** vertices);
1012 bool reserveIndexSpace(int indexCount, void** indices);
1013
bsalomon@google.come8262622011-11-07 02:30:51 +00001014 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1015 // indicate non-indexed drawing.
1016 bool checkDraw(GrPrimitiveType type, int startVertex,
1017 int startIndex, int vertexCount,
1018 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001019 // called when setting a new vert/idx source to unref prev vb/ib
1020 void releasePreviousVertexSource();
1021 void releasePreviousIndexSource();
1022
1023 enum {
1024 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001026 SkSTArray<kPreallocGeoSrcStateStackCnt,
1027 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001028
reed@google.comac10a2d2010-12-22 21:39:39 +00001029};
1030
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001031GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1032
reed@google.comac10a2d2010-12-22 21:39:39 +00001033#endif