blob: 76aa4118016d4941dd36b085fb41d487e7149832 [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"
24
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrClipIterator;
27class GrVertexBuffer;
28class GrIndexBuffer;
29
30class GrDrawTarget : public GrRefCnt {
31public:
32 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033 * Represents the draw target capabilities.
34 */
35 struct Caps {
36 Caps() { memset(this, 0, sizeof(Caps)); }
37 Caps(const Caps& c) { *this = c; }
38 Caps& operator= (const Caps& c) {
39 memcpy(this, &c, sizeof(Caps));
40 return *this;
41 }
42 void print() const;
43 bool f8BitPaletteSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000044 bool fNPOTTextureTileSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000045 bool fTwoSidedStencilSupport : 1;
46 bool fStencilWrapOpsSupport : 1;
47 bool fHWAALineSupport : 1;
48 bool fShaderSupport : 1;
49 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.coma3108262011-10-10 14:08:47 +000054 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000055 int fMaxRenderTargetSize;
56 int fMaxTextureSize;
57 };
58
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000059 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000060 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000061
reed@google.comac10a2d2010-12-22 21:39:39 +000062 ///////////////////////////////////////////////////////////////////////////
63
64 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000065 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000068 * Gets the capabilities of the draw target.
69 */
70 const Caps& getCaps() const { return fCaps; }
71
72 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000073 * Sets the current clip to the region specified by clip. All draws will be
74 * clipped against this clip if kClip_StateBit is enabled.
75 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000076 * Setting the clip may (or may not) zero out the client's stencil bits.
77 *
reed@google.comac10a2d2010-12-22 21:39:39 +000078 * @param description of the clipping region
79 */
80 void setClip(const GrClip& clip);
81
82 /**
83 * Gets the current clip.
84 *
85 * @return the clip.
86 */
87 const GrClip& getClip() const;
88
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000089 const GrDrawState& getDrawState() const { return fCurrDrawState; }
90 GrDrawState* drawState() { return &fCurrDrawState; }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000091
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000092 /**
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000093 * Shortcut for drawState()->preConcatSamplerMatrices() on all enabled
94 * stages
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000095 *
bsalomon@google.com3d0835b2011-12-08 16:12:03 +000096 * @param matrix the matrix to concat
97 */
98 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
99 StageMask stageMask = this->enabledStages();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000100 this->drawState()->preConcatSamplerMatrices(stageMask, matrix);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000101 }
102
103 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000104 * Determines if blending will require a read of a dst given the current
105 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000106 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000107 * @return true if the dst surface will be read at each pixel hit by the
108 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000109 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000110 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000111
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000112 /**
113 * Color alpha and coverage are two inputs to the drawing pipeline. For some
114 * blend modes it is safe to fold the coverage into constant or per-vertex
115 * color alpha value. For other blend modes they must be handled separately.
116 * Depending on features available in the underlying 3D API this may or may
117 * not be possible.
118 *
119 * This function looks at the current blend on the draw target and the draw
120 * target's capabilities to determine whether coverage can be handled
121 * correctly.
122 */
123 bool canApplyCoverage() const;
124
125 /**
126 * Determines whether incorporating partial pixel coverage into the constant
127 * color specified by setColor or per-vertex colors will give the right
128 * blending result.
129 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000130 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000131
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000132 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000133 * Given the current draw state, vertex layout, and hw support, will HW AA
134 * lines be used (if line primitive type is drawn)? (Note that lines are
135 * always 1 pixel wide)
136 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000137 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000138
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000139 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000140 * Used to save and restore the GrGpu's drawing state
141 */
142 struct SavedDrawState {
143 private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000144 GrDrawState fState;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000145 friend class GrDrawTarget;
146 };
147
148 /**
149 * Saves the current draw state. The state can be restored at a later time
150 * with restoreDrawState.
151 *
152 * See also AutoStateRestore class.
153 *
154 * @param state will hold the state after the function returns.
155 */
156 void saveCurrentDrawState(SavedDrawState* state) const;
157
158 /**
159 * Restores previously saved draw state. The client guarantees that state
160 * was previously passed to saveCurrentDrawState and that the rendertarget
161 * and texture set at save are still valid.
162 *
163 * See also AutoStateRestore class.
164 *
165 * @param state the previously saved state to restore.
166 */
167 void restoreDrawState(const SavedDrawState& state);
168
169 /**
170 * Copies the draw state from another target to this target.
171 *
172 * @param srcTarget draw target used as src of the draw state.
173 */
174 void copyDrawState(const GrDrawTarget& srcTarget);
175
176 /**
177 * The format of vertices is represented as a bitfield of flags.
178 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000179 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
180 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
181 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000182 * use any of the texture coordinates as its input texture coordinates or it
183 * may use the positions as texture coordinates.
184 *
185 * If no texture coordinates are specified for a stage then the stage is
186 * disabled.
187 *
188 * Only one type of texture coord can be specified per stage. For
189 * example StageTexCoordVertexLayoutBit(0, 2) and
190 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
191 *
192 * The order in memory is always (position, texture coord 0, ..., color,
193 * coverage) with any unused fields omitted. Note that this means that if
194 * only texture coordinates 1 is referenced then there is no texture
195 * coordinates 0 and the order would be (position, texture coordinate 1
196 * [, color][, coverage]).
197 */
198
199 /**
200 * Generates a bit indicating that a texture stage uses texture coordinates
201 *
202 * @param stage the stage that will use texture coordinates.
203 * @param texCoordIdx the index of the texture coordinates to use
204 *
205 * @return the bit to add to a GrVertexLayout bitfield.
206 */
207 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000208 GrAssert(stage < GrDrawState::kNumStages);
209 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
210 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000211 }
212
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000214 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
215 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000216
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000217public:
218 /**
219 * Generates a bit indicating that a texture stage uses the position
220 * as its texture coordinate.
221 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000222 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000223 * coordinates.
224 *
225 * @return the bit to add to a GrVertexLayout bitfield.
226 */
227 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000228 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000229 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000230 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000231
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000232private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000233 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
234 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000235
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000236public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000237
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000238 /**
239 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 */
241 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000242 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000243 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000244 /* vertices have coverage (GrColor where all channels should have the
245 * same value)
246 */
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.com25fb21f2011-06-21 18:17:25 +0000265 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000266 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000267 * can use a different method. Once geometry is specified it can be used for
268 * multiple drawIndexed and drawNonIndexed calls.
269 *
270 * Sometimes it is necessary to perform a draw while upstack code has
271 * already specified geometry that it isn't finished with. There are push
272 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273 *
274 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
275 * caller's client has already provided vertex data in a format
276 * the time compatible with a GrVertexLayout. The array must contain the
277 * data at set*SourceToArray is called. The source stays in effect for
278 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
279 * again or one of the other two paths is chosen.
280 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000281 * 2. Reserve. This is most useful when the caller has data it must
282 * transform before drawing and is not long-lived. The caller requests
283 * that the draw target make room for some amount of vertex and/or index
284 * data. The target provides ptrs to hold the vertex and/or index data.
285 *
286 * The data is writable up until the next drawIndexed, drawNonIndexed,
287 * or pushGeometrySource At this point the data is frozen and the ptrs
288 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289 *
290 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000291 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
292 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
293 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000294 */
295
296 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000297 * Reserves space for vertices. Draw target will use reserved vertices at
298 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 *
300 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000301 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 * of vertices to be filled by caller. The next draw will read
303 * these vertices.
304 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000305 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000306 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000307 * GrDrawTarget to decide whether to put data in buffers, to group vertex
308 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000310 * After the next draw or pushGeometrySource the vertices ptr is no longer
311 * valid and the geometry data cannot be further modified. The contents
312 * that were put in the reserved space can be drawn by multiple draws,
313 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000314 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000315 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000316 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 * @param vertices will point to reserved vertex space if vertexCount is
318 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 *
320 * @return true if succeeded in allocating space for the vertices and false
321 * if not.
322 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000323 bool reserveVertexSpace(GrVertexLayout vertexLayout,
324 int vertexCount,
325 void** vertices);
326 /**
327 * Reserves space for indices. Draw target will use the reserved indices at
328 * the next indexed draw.
329 *
330 * If succeeds:
331 * if indexCount > 0, *indices will be the array
332 * of indices to be filled by caller. The next draw will read
333 * these indices.
334 *
335 * If a client does not already have a index buffer then this is the
336 * preferred way to allocate index data. It allows the subclass of
337 * GrDrawTarget to decide whether to put data in buffers, to group index
338 * data that uses the same state (e.g. for deferred rendering), etc.
339 *
340 * After the next indexed draw or pushGeometrySource the indices ptr is no
341 * longer valid and the geometry data cannot be further modified. The
342 * contents that were put in the reserved space can be drawn by multiple
343 * draws, however.
344 *
345 * @param indexCount the number of indices to reserve space for. Can be 0.
346 * @param indices will point to reserved index space if indexCount is
347 * non-zero. Illegal to pass NULL if indexCount > 0.
348 */
349
350 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 /**
352 * Provides hints to caller about the number of vertices and indices
353 * that can be allocated cheaply. This can be useful if caller is reserving
354 * space but doesn't know exactly how much geometry is needed.
355 *
356 * Also may hint whether the draw target should be flushed first. This is
357 * useful for deferred targets.
358 *
359 * @param vertexLayout layout of vertices caller would like to reserve
360 * @param vertexCount in: hint about how many vertices the caller would
361 * like to allocate.
362 * out: a hint about the number of vertices that can be
363 * allocated cheaply. Negative means no hint.
364 * Ignored if NULL.
365 * @param indexCount in: hint about how many indices the caller would
366 * like to allocate.
367 * out: a hint about the number of indices that can be
368 * allocated cheaply. Negative means no hint.
369 * Ignored if NULL.
370 *
371 * @return true if target should be flushed based on the input values.
372 */
373 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000374 int* vertexCount,
375 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000376
377 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000378 * Sets source of vertex data for the next draw. Array must contain
379 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 *
381 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000382 * @param size size of the vertex data.
383 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000385 void setVertexSourceToArray(GrVertexLayout vertexLayout,
386 const void* vertexArray,
387 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000388
389 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000390 * Sets source of index data for the next indexed draw. Array must contain
391 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000393 * @param array cpu array containing index data.
394 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000396 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000397
398 /**
399 * Sets source of vertex data for the next draw. Data does not have to be
400 * in the buffer until drawIndexed or drawNonIndexed.
401 *
402 * @param buffer vertex buffer containing vertex data. Must be
403 * unlocked before draw call.
404 * @param vertexLayout layout of the vertex data in the buffer.
405 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000406 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
407 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
409 /**
410 * Sets source of index data for the next indexed draw. Data does not have
411 * to be in the buffer until drawIndexed or drawNonIndexed.
412 *
413 * @param buffer index buffer containing indices. Must be unlocked
414 * before indexed draw call.
415 */
416 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000417
418 /**
419 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
420 * source to reserved, array, or buffer before next draw. May be able to free
421 * up temporary storage allocated by setVertexSourceToArray or
422 * reserveVertexSpace.
423 */
424 void resetVertexSource();
425
426 /**
427 * Resets index source. Indexed Drawing from reset indices is illegal. Set
428 * index source to reserved, array, or buffer before next indexed draw. May
429 * be able to free up temporary storage allocated by setIndexSourceToArray
430 * or reserveIndexSpace.
431 */
432 void resetIndexSource();
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.
tomhudson@google.com93813632011-10-27 20:21:16 +0000932 static GrDrawState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000933 { return sds.fState; }
tomhudson@google.com93813632011-10-27 20:21:16 +0000934 static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000935 { return sds.fState; }
936
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000937 // implemented by subclass to allocate space for reserved geom
938 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
939 int vertexCount,
940 void** vertices) = 0;
941 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
942 // implemented by subclass to handle release of reserved geom space
943 virtual void releaseReservedVertexSpace() = 0;
944 virtual void releaseReservedIndexSpace() = 0;
945 // subclass must consume array contents when set
946 virtual void onSetVertexSourceToArray(const void* vertexArray,
947 int vertexCount) = 0;
948 virtual void onSetIndexSourceToArray(const void* indexArray,
949 int indexCount) = 0;
950 // subclass is notified that geom source will be set away from an array
951 virtual void releaseVertexArray() = 0;
952 virtual void releaseIndexArray() = 0;
953 // subclass overrides to be notified just before geo src state
954 // is pushed/popped.
955 virtual void geometrySourceWillPush() = 0;
956 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
957 // subclass called to perform drawing
958 virtual void onDrawIndexed(GrPrimitiveType type,
959 int startVertex,
960 int startIndex,
961 int vertexCount,
962 int indexCount) = 0;
963 virtual void onDrawNonIndexed(GrPrimitiveType type,
964 int startVertex,
965 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000966 // subclass overrides to be notified when clip is set. Must call
967 // INHERITED::clipwillBeSet
968 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000969
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000970 // Helpers for drawRect, protected so subclasses that override drawRect
971 // can use them.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000972 static GrVertexLayout GetRectVertexLayout(StageMask stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000973 const GrRect* srcRects[]);
974
975 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000976 const GrMatrix* matrix,
977 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000978 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +0000979 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000980 void* vertices);
981
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000982 // accessor for derived classes
983 const GeometrySrcState& getGeomSrc() const {
984 return fGeoSrcStateStack.back();
985 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000986
987 GrClip fClip;
988
tomhudson@google.com93813632011-10-27 20:21:16 +0000989 GrDrawState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000990
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000991 Caps fCaps;
992
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000993 // subclasses must call this in their destructors to ensure all vertex
994 // and index sources have been released (including those held by
995 // pushGeometrySource())
996 void releaseGeometry();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000997private:
bsalomon@google.come8262622011-11-07 02:30:51 +0000998 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
999 // indicate non-indexed drawing.
1000 bool checkDraw(GrPrimitiveType type, int startVertex,
1001 int startIndex, int vertexCount,
1002 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001003 // called when setting a new vert/idx source to unref prev vb/ib
1004 void releasePreviousVertexSource();
1005 void releasePreviousIndexSource();
1006
1007 enum {
1008 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001010 SkSTArray<kPreallocGeoSrcStateStackCnt,
1011 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001012
reed@google.comac10a2d2010-12-22 21:39:39 +00001013};
1014
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001015GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1016
reed@google.comac10a2d2010-12-22 21:39:39 +00001017#endif