blob: b72fb3a8e873ba9a9a2d415525e5afe7d2f88261 [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"
19#include "GrRenderTarget.h"
20#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000021#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000022#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
Scroggo97c88c22011-05-11 14:05:25 +000024#include "SkXfermode.h"
25
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;
49 bool fShaderSupport : 1;
50 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000051 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000052 bool fFSAASupport : 1;
53 bool fDualSourceBlendingSupport : 1;
54 bool fBufferLockSupport : 1;
bsalomon@google.coma3108262011-10-10 14:08:47 +000055 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000056 int fMaxRenderTargetSize;
57 int fMaxTextureSize;
58 };
59
bsalomon@google.com0fec61d2011-12-08 15:53:53 +000060 // for convenience
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000061 typedef GrDrawState::StageMask StageMask;
reed@google.comac10a2d2010-12-22 21:39:39 +000062
reed@google.comac10a2d2010-12-22 21:39:39 +000063 ///////////////////////////////////////////////////////////////////////////
64
65 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000066 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000067
68 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000069 * Gets the capabilities of the draw target.
70 */
71 const Caps& getCaps() const { return fCaps; }
72
73 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000074 * Sets the current clip to the region specified by clip. All draws will be
75 * clipped against this clip if kClip_StateBit is enabled.
76 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000077 * Setting the clip may (or may not) zero out the client's stencil bits.
78 *
reed@google.comac10a2d2010-12-22 21:39:39 +000079 * @param description of the clipping region
80 */
81 void setClip(const GrClip& clip);
82
83 /**
84 * Gets the current clip.
85 *
86 * @return the clip.
87 */
88 const GrClip& getClip() const;
89
90 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +000091 * Determines if blending will require a read of a dst given the current
92 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000093 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +000094 * @return true if the dst surface will be read at each pixel hit by the
95 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000096 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +000097 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000098
bsalomon@google.comd46e2422011-09-23 17:40:07 +000099 /**
100 * Color alpha and coverage are two inputs to the drawing pipeline. For some
101 * blend modes it is safe to fold the coverage into constant or per-vertex
102 * color alpha value. For other blend modes they must be handled separately.
103 * Depending on features available in the underlying 3D API this may or may
104 * not be possible.
105 *
106 * This function looks at the current blend on the draw target and the draw
107 * target's capabilities to determine whether coverage can be handled
108 * correctly.
109 */
110 bool canApplyCoverage() const;
111
112 /**
113 * Determines whether incorporating partial pixel coverage into the constant
114 * color specified by setColor or per-vertex colors will give the right
115 * blending result.
116 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000117 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000118
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000119 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000120 * Given the current draw state, vertex layout, and hw support, will HW AA
121 * lines be used (if line primitive type is drawn)? (Note that lines are
122 * always 1 pixel wide)
123 */
tomhudson@google.com93813632011-10-27 20:21:16 +0000124 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000125
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000126 const GrDrawState& getDrawState() const { return fCurrDrawState; }
127 GrDrawState* drawState() { return &fCurrDrawState; }
128
129 // Convenience Pass-thrus to GrDrawState. These are likely candidates for
130 // removal.
131 void setViewMatrix(const GrMatrix& m) {
132 this->drawState()->setViewMatrix(m);
133 }
134 GrMatrix* viewMatrix() {
135 return this->drawState()->viewMatrix();
136 }
137 const GrMatrix& getViewMatrix() const {
138 return this->getDrawState().getViewMatrix();
139 }
140 bool getViewInverse(GrMatrix* inv) const {
141 return this->getDrawState().getViewInverse(inv);
142 }
143 void setRenderTarget(GrRenderTarget* renderTarget) {
144 this->drawState()->setRenderTarget(renderTarget);
145 }
146 const GrRenderTarget* getRenderTarget() const {
147 return this->getDrawState().getRenderTarget();
148 }
149 GrRenderTarget* getRenderTarget() {
150 return this->drawState()->getRenderTarget();
151 }
152 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
153 this->drawState()->setBlendFunc(srcCoeff, dstCoeff);
154 }
155 void setTexture(int stage, GrTexture* texture) {
156 this->drawState()->setTexture(stage, texture);
157 }
158 const GrTexture* getTexture(int stage) const {
159 return this->getDrawState().getTexture(stage);
160 }
161 GrTexture* getTexture(int stage) {
162 return this->drawState()->getTexture(stage);
163 }
164 // THIS WILL BE REMOVED AND REPLACED WITH DIRECT ACCESS TO SAMPLER
165 // IN A COMING REVISION.
166 void setSamplerState(int stage, const GrSamplerState& sampler) {
167 *(this->drawState()->sampler(stage)) = sampler;
168 }
169 const GrSamplerState& getSampler(int stage) const {
170 return this->getDrawState().getSampler(stage);
171 }
172 void preConcatSamplerMatrices(StageMask stageMask,
173 const GrMatrix& m) {
174 this->drawState()->preConcatSamplerMatrices(stageMask, m);
175 }
176 GrColor getColor() const { return this->getDrawState().getColor(); }
177 void setColor(GrColor color) { this->drawState()->setColor(color); }
178 void setFirstCoverageStage(int firstCoverageStage) {
179 this->drawState()->setFirstCoverageStage(firstCoverageStage);
180 }
181 int getFirstCoverageStage() const {
182 return this->getDrawState().getFirstCoverageStage();
183 }
184 void setDrawFace(const GrDrawState::DrawFace face) {
185 this->drawState()->setDrawFace(face);
186 }
187 GrDrawState::DrawFace getDrawFace() const {
188 return this->getDrawState().getDrawFace();
189 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000190
bsalomon@google.coma3108262011-10-10 14:08:47 +0000191 /**
192 * Used to save and restore the GrGpu's drawing state
193 */
194 struct SavedDrawState {
195 private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000196 GrDrawState fState;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000197 friend class GrDrawTarget;
198 };
199
200 /**
201 * Saves the current draw state. The state can be restored at a later time
202 * with restoreDrawState.
203 *
204 * See also AutoStateRestore class.
205 *
206 * @param state will hold the state after the function returns.
207 */
208 void saveCurrentDrawState(SavedDrawState* state) const;
209
210 /**
211 * Restores previously saved draw state. The client guarantees that state
212 * was previously passed to saveCurrentDrawState and that the rendertarget
213 * and texture set at save are still valid.
214 *
215 * See also AutoStateRestore class.
216 *
217 * @param state the previously saved state to restore.
218 */
219 void restoreDrawState(const SavedDrawState& state);
220
221 /**
222 * Copies the draw state from another target to this target.
223 *
224 * @param srcTarget draw target used as src of the draw state.
225 */
226 void copyDrawState(const GrDrawTarget& srcTarget);
227
228 /**
229 * The format of vertices is represented as a bitfield of flags.
230 * Flags that indicate the layout of vertex data. Vertices always contain
tomhudson@google.com93813632011-10-27 20:21:16 +0000231 * positions and may also contain up to GrDrawState::kMaxTexCoords sets
232 * of 2D texture * coordinates, per-vertex colors, and per-vertex coverage.
233 * Each stage can
bsalomon@google.coma3108262011-10-10 14:08:47 +0000234 * use any of the texture coordinates as its input texture coordinates or it
235 * may use the positions as texture coordinates.
236 *
237 * If no texture coordinates are specified for a stage then the stage is
238 * disabled.
239 *
240 * Only one type of texture coord can be specified per stage. For
241 * example StageTexCoordVertexLayoutBit(0, 2) and
242 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
243 *
244 * The order in memory is always (position, texture coord 0, ..., color,
245 * coverage) with any unused fields omitted. Note that this means that if
246 * only texture coordinates 1 is referenced then there is no texture
247 * coordinates 0 and the order would be (position, texture coordinate 1
248 * [, color][, coverage]).
249 */
250
251 /**
252 * Generates a bit indicating that a texture stage uses texture coordinates
253 *
254 * @param stage the stage that will use texture coordinates.
255 * @param texCoordIdx the index of the texture coordinates to use
256 *
257 * @return the bit to add to a GrVertexLayout bitfield.
258 */
259 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000260 GrAssert(stage < GrDrawState::kNumStages);
261 GrAssert(texCoordIdx < GrDrawState::kMaxTexCoords);
262 return 1 << (stage + (texCoordIdx * GrDrawState::kNumStages));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000263 }
264
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000265private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000266 static const int TEX_COORD_BIT_CNT = GrDrawState::kNumStages *
267 GrDrawState::kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000268
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000269public:
270 /**
271 * Generates a bit indicating that a texture stage uses the position
272 * as its texture coordinate.
273 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000275 * coordinates.
276 *
277 * @return the bit to add to a GrVertexLayout bitfield.
278 */
279 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000280 GrAssert(stage < GrDrawState::kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000281 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000282 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000283
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000284private:
tomhudson@google.com93813632011-10-27 20:21:16 +0000285 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT +
286 GrDrawState::kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000287
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000288public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000289
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000290 /**
291 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000292 */
293 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000294 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000296 /* vertices have coverage (GrColor where all channels should have the
297 * same value)
298 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000299 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000300 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000301 * text [GrGpuTextVertex vs GrPoint].)
302 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000303 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000304
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000305 /* Each vertex specificies an edge. Distance to the edge is used to
306 * compute a coverage. See setVertexEdgeType().
307 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000308 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000310 kDummyVertexLayoutBit,
311 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000313 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000314 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
316 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000317 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000318 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000319 * can use a different method. Once geometry is specified it can be used for
320 * multiple drawIndexed and drawNonIndexed calls.
321 *
322 * Sometimes it is necessary to perform a draw while upstack code has
323 * already specified geometry that it isn't finished with. There are push
324 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000325 *
326 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
327 * caller's client has already provided vertex data in a format
328 * the time compatible with a GrVertexLayout. The array must contain the
329 * data at set*SourceToArray is called. The source stays in effect for
330 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
331 * again or one of the other two paths is chosen.
332 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000333 * 2. Reserve. This is most useful when the caller has data it must
334 * transform before drawing and is not long-lived. The caller requests
335 * that the draw target make room for some amount of vertex and/or index
336 * data. The target provides ptrs to hold the vertex and/or index data.
337 *
338 * The data is writable up until the next drawIndexed, drawNonIndexed,
339 * or pushGeometrySource At this point the data is frozen and the ptrs
340 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000341 *
342 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000343 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
344 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
345 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000346 */
347
348 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000349 * Reserves space for vertices. Draw target will use reserved vertices at
350 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 *
352 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000353 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 * of vertices to be filled by caller. The next draw will read
355 * these vertices.
356 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000357 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000358 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000359 * GrDrawTarget to decide whether to put data in buffers, to group vertex
360 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000361 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000362 * After the next draw or pushGeometrySource the vertices ptr is no longer
363 * valid and the geometry data cannot be further modified. The contents
364 * that were put in the reserved space can be drawn by multiple draws,
365 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000366 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000368 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 * @param vertices will point to reserved vertex space if vertexCount is
370 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 *
372 * @return true if succeeded in allocating space for the vertices and false
373 * if not.
374 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000375 bool reserveVertexSpace(GrVertexLayout vertexLayout,
376 int vertexCount,
377 void** vertices);
378 /**
379 * Reserves space for indices. Draw target will use the reserved indices at
380 * the next indexed draw.
381 *
382 * If succeeds:
383 * if indexCount > 0, *indices will be the array
384 * of indices to be filled by caller. The next draw will read
385 * these indices.
386 *
387 * If a client does not already have a index buffer then this is the
388 * preferred way to allocate index data. It allows the subclass of
389 * GrDrawTarget to decide whether to put data in buffers, to group index
390 * data that uses the same state (e.g. for deferred rendering), etc.
391 *
392 * After the next indexed draw or pushGeometrySource the indices ptr is no
393 * longer valid and the geometry data cannot be further modified. The
394 * contents that were put in the reserved space can be drawn by multiple
395 * draws, however.
396 *
397 * @param indexCount the number of indices to reserve space for. Can be 0.
398 * @param indices will point to reserved index space if indexCount is
399 * non-zero. Illegal to pass NULL if indexCount > 0.
400 */
401
402 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000403 /**
404 * Provides hints to caller about the number of vertices and indices
405 * that can be allocated cheaply. This can be useful if caller is reserving
406 * space but doesn't know exactly how much geometry is needed.
407 *
408 * Also may hint whether the draw target should be flushed first. This is
409 * useful for deferred targets.
410 *
411 * @param vertexLayout layout of vertices caller would like to reserve
412 * @param vertexCount in: hint about how many vertices the caller would
413 * like to allocate.
414 * out: a hint about the number of vertices that can be
415 * allocated cheaply. Negative means no hint.
416 * Ignored if NULL.
417 * @param indexCount in: hint about how many indices the caller would
418 * like to allocate.
419 * out: a hint about the number of indices that can be
420 * allocated cheaply. Negative means no hint.
421 * Ignored if NULL.
422 *
423 * @return true if target should be flushed based on the input values.
424 */
425 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000426 int* vertexCount,
427 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000428
429 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000430 * Sets source of vertex data for the next draw. Array must contain
431 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 *
433 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000434 * @param size size of the vertex data.
435 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000437 void setVertexSourceToArray(GrVertexLayout vertexLayout,
438 const void* vertexArray,
439 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
441 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000442 * Sets source of index data for the next indexed draw. Array must contain
443 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000444 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000445 * @param array cpu array containing index data.
446 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000448 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
450 /**
451 * Sets source of vertex data for the next draw. Data does not have to be
452 * in the buffer until drawIndexed or drawNonIndexed.
453 *
454 * @param buffer vertex buffer containing vertex data. Must be
455 * unlocked before draw call.
456 * @param vertexLayout layout of the vertex data in the buffer.
457 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000458 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
459 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
461 /**
462 * Sets source of index data for the next indexed draw. Data does not have
463 * to be in the buffer until drawIndexed or drawNonIndexed.
464 *
465 * @param buffer index buffer containing indices. Must be unlocked
466 * before indexed draw call.
467 */
468 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000469
470 /**
471 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
472 * source to reserved, array, or buffer before next draw. May be able to free
473 * up temporary storage allocated by setVertexSourceToArray or
474 * reserveVertexSpace.
475 */
476 void resetVertexSource();
477
478 /**
479 * Resets index source. Indexed Drawing from reset indices is illegal. Set
480 * index source to reserved, array, or buffer before next indexed draw. May
481 * be able to free up temporary storage allocated by setIndexSourceToArray
482 * or reserveIndexSpace.
483 */
484 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
486 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000487 * Pushes and resets the vertex/index sources. Any reserved vertex / index
488 * data is finalized (i.e. cannot be updated after the matching pop but can
489 * be drawn from). Must be balanced by a pop.
490 */
491 void pushGeometrySource();
492
493 /**
494 * Pops the vertex / index sources from the matching push.
495 */
496 void popGeometrySource();
497
498 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 * Draws indexed geometry using the current state and current vertex / index
500 * sources.
501 *
502 * @param type The type of primitives to draw.
503 * @param startVertex the vertex in the vertex array/buffer corresponding
504 * to index 0
505 * @param startIndex first index to read from index src.
506 * @param vertexCount one greater than the max index.
507 * @param indexCount the number of index elements to read. The index count
508 * is effectively trimmed to the last completely
509 * specified primitive.
510 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000511 void drawIndexed(GrPrimitiveType type,
512 int startVertex,
513 int startIndex,
514 int vertexCount,
515 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000516
517 /**
518 * Draws non-indexed geometry using the current state and current vertex
519 * sources.
520 *
521 * @param type The type of primitives to draw.
522 * @param startVertex the vertex in the vertex array/buffer corresponding
523 * to index 0
524 * @param vertexCount one greater than the max index.
525 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000526 void drawNonIndexed(GrPrimitiveType type,
527 int startVertex,
528 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000529
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000530 /**
531 * Helper function for drawing rects. This does not use the current index
532 * and vertex sources. After returning, the vertex and index sources may
533 * have changed. They should be reestablished before the next drawIndexed
534 * or drawNonIndexed. This cannot be called between reserving and releasing
535 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000536 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000537 * drawNonIndexed.
538 * @param rect the rect to draw
539 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000540 * @param stageMask bitmask indicating which stages are enabled.
541 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000542 * @param srcRects specifies rects for stages enabled by stageEnableMask.
543 * if stageEnableMask bit i is 1, srcRects is not NULL,
544 * and srcRects[i] is not NULL, then srcRects[i] will be
545 * used as coordinates for stage i. Otherwise, if stage i
546 * is enabled then rect is used as the coordinates.
547 * @param srcMatrices optional matrices applied to srcRects. If
548 * srcRect[i] is non-NULL and srcMatrices[i] is
549 * non-NULL then srcRect[i] will be transformed by
550 * srcMatrix[i]. srcMatrices can be NULL when no
551 * srcMatrices are desired.
552 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000553 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000554 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000555 StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000556 const GrRect* srcRects[],
557 const GrMatrix* srcMatrices[]);
558
559 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000560 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000561 * matrices.
562 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000563 void drawSimpleRect(const GrRect& rect,
564 const GrMatrix* matrix,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000565 StageMask stageEnableBitfield) {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000566 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000567 }
568
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000569 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000570 * Clear the render target. Ignores the clip and all other draw state
571 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
572 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000573 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000574 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000575
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000576 /**
577 * Returns the maximum number of edges that may be specified in a single
578 * draw call when performing edge antialiasing. This is usually limited
579 * by the number of fragment uniforms which may be uploaded. Must be a
580 * minimum of six, since a triangle's vertices each belong to two boundary
581 * edges which may be distinct.
582 */
583 virtual int getMaxEdges() const { return 6; }
584
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000585 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
587 class AutoStateRestore : ::GrNoncopyable {
588 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000589 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 AutoStateRestore(GrDrawTarget* target);
591 ~AutoStateRestore();
592
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000593 /**
594 * if this object is already saving state for param target then
595 * this does nothing. Otherise, it restores previously saved state on
596 * previous target (if any) and saves current state on param target.
597 */
598 void set(GrDrawTarget* target);
599
reed@google.comac10a2d2010-12-22 21:39:39 +0000600 private:
601 GrDrawTarget* fDrawTarget;
602 SavedDrawState fDrawState;
603 };
604
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000605 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000606
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000607 class AutoViewMatrixRestore : ::GrNoncopyable {
608 public:
609 AutoViewMatrixRestore() {
610 fDrawTarget = NULL;
611 }
612
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000613 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000614 : fDrawTarget(target), fMatrix(target->getViewMatrix()) {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000615 GrAssert(NULL != target);
616 }
617
618 void set(GrDrawTarget* target) {
619 GrAssert(NULL != target);
620 if (NULL != fDrawTarget) {
621 fDrawTarget->setViewMatrix(fMatrix);
622 }
623 fDrawTarget = target;
624 fMatrix = target->getViewMatrix();
625 }
626
627 ~AutoViewMatrixRestore() {
628 if (NULL != fDrawTarget) {
629 fDrawTarget->setViewMatrix(fMatrix);
630 }
631 }
632
633 private:
634 GrDrawTarget* fDrawTarget;
635 GrMatrix fMatrix;
636 };
637
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000638 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000639
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000640 /**
641 * Sets the view matrix to I and preconcats all stage matrices enabled in
642 * mask by the view inverse. Destructor undoes these changes.
643 */
644 class AutoDeviceCoordDraw : ::GrNoncopyable {
645 public:
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000646 AutoDeviceCoordDraw(GrDrawTarget* target,
647 GrDrawState::StageMask stageMask);
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000648 ~AutoDeviceCoordDraw();
649 private:
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000650 GrDrawTarget* fDrawTarget;
651 GrMatrix fViewMatrix;
652 GrMatrix fSamplerMatrices[GrDrawState::kNumStages];
653 GrDrawState::StageMask fStageMask;
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000654 };
655
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000656 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000657
reed@google.comac10a2d2010-12-22 21:39:39 +0000658 class AutoReleaseGeometry : ::GrNoncopyable {
659 public:
660 AutoReleaseGeometry(GrDrawTarget* target,
661 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000662 int vertexCount,
663 int indexCount);
664 AutoReleaseGeometry();
665 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000666 bool set(GrDrawTarget* target,
667 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000668 int vertexCount,
669 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000670 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000671 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
672 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000673 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000674 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +0000675 }
676
677 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000678 void reset();
679
reed@google.comac10a2d2010-12-22 21:39:39 +0000680 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000681 void* fVertices;
682 void* fIndices;
683 };
684
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000685 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000686
687 class AutoClipRestore : ::GrNoncopyable {
688 public:
689 AutoClipRestore(GrDrawTarget* target) {
690 fTarget = target;
691 fClip = fTarget->getClip();
692 }
693
694 ~AutoClipRestore() {
695 fTarget->setClip(fClip);
696 }
697 private:
698 GrDrawTarget* fTarget;
699 GrClip fClip;
700 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000701
702 ////////////////////////////////////////////////////////////////////////////
703
704 class AutoGeometryPush : ::GrNoncopyable {
705 public:
706 AutoGeometryPush(GrDrawTarget* target) {
707 GrAssert(NULL != target);
708 fTarget = target;
709 target->pushGeometrySource();
710 }
711 ~AutoGeometryPush() {
712 fTarget->popGeometrySource();
713 }
714 private:
715 GrDrawTarget* fTarget;
716 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000717
718 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000719 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000720
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 /**
722 * Helper function to compute the size of a vertex from a vertex layout
723 * @return size of a single vertex.
724 */
725 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000726
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000727 /**
728 * Helper function for determining the index of texture coordinates that
729 * is input for a texture stage. Note that a stage may instead use positions
730 * as texture coordinates, in which case the result of the function is
731 * indistinguishable from the case when the stage is disabled.
732 *
733 * @param stage the stage to query
734 * @param vertexLayout layout to query
735 *
736 * @return the texture coordinate index or -1 if the stage doesn't use
737 * separate (non-position) texture coordinates.
738 */
739 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000740
741 /**
742 * Helper function to compute the offset of texture coordinates in a vertex
743 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000744 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000745 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000746 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000747 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000748
749 /**
750 * Helper function to compute the offset of the color in a vertex
751 * @return offset of color in vertex layout or -1 if the
752 * layout has no color.
753 */
754 static int VertexColorOffset(GrVertexLayout vertexLayout);
755
bsalomon@google.coma3108262011-10-10 14:08:47 +0000756 /**
757 * Helper function to compute the offset of the coverage in a vertex
758 * @return offset of coverage in vertex layout or -1 if the
759 * layout has no coverage.
760 */
761 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
762
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000763 /**
764 * Helper function to compute the offset of the edge pts in a vertex
765 * @return offset of edge in vertex layout or -1 if the
766 * layout has no edge.
767 */
768 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
769
reed@google.comac10a2d2010-12-22 21:39:39 +0000770 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000771 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000772 * coordinates of some index.
773 *
774 * @param coordIndex the tex coord index to query
775 * @param vertexLayout layout to query
776 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000777 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000778 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000779 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000780 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000781 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000782
reed@google.comac10a2d2010-12-22 21:39:39 +0000783 /**
784 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000785 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000786 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000787 * @param stage the stage to query
788 * @param vertexLayout layout to query
789 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000790 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000791 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000792 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000793 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000794
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000795 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000796 * Helper function to compute the size of each vertex and the offsets of
797 * texture coordinates and color. Determines tex coord offsets by tex coord
798 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000799 * by StageTexCoordVertexLayoutBit.)
800 *
801 * @param vertexLayout the layout to query
802 * @param texCoordOffsetsByIdx after return it is the offset of each
803 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000804 * index isn't used. (optional)
805 * @param colorOffset after return it is the offset of the
806 * color field in each vertex, or -1 if
807 * there aren't per-vertex colors. (optional)
808 * @param coverageOffset after return it is the offset of the
809 * coverage field in each vertex, or -1 if
810 * there aren't per-vertex coeverages.
811 * (optional)
812 * @param edgeOffset after return it is the offset of the
813 * edge eq field in each vertex, or -1 if
814 * there aren't per-vertex edge equations.
815 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000816 * @return size of a single vertex
817 */
818 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000819 int texCoordOffsetsByIdx[GrDrawState::kMaxTexCoords],
820 int *colorOffset,
821 int *coverageOffset,
822 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000823
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000824 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000825 * Helper function to compute the size of each vertex and the offsets of
826 * texture coordinates and color. Determines tex coord offsets by stage
827 * rather than by index. (Each stage can be mapped to any t.c. index
828 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000829 * tex coords then that stage's offset will be 0 (positions are always at 0).
830 *
831 * @param vertexLayout the layout to query
832 * @param texCoordOffsetsByStage after return it is the offset of each
833 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +0000834 * index isn't used. (optional)
835 * @param colorOffset after return it is the offset of the
836 * color field in each vertex, or -1 if
837 * there aren't per-vertex colors.
838 * (optional)
839 * @param coverageOffset after return it is the offset of the
840 * coverage field in each vertex, or -1 if
841 * there aren't per-vertex coeverages.
842 * (optional)
843 * @param edgeOffset after return it is the offset of the
844 * edge eq field in each vertex, or -1 if
845 * there aren't per-vertex edge equations.
846 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000847 * @return size of a single vertex
848 */
849 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000850 int texCoordOffsetsByStage[GrDrawState::kNumStages],
851 int* colorOffset,
852 int* coverageOffset,
853 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000854
855 /**
856 * Accessing positions, texture coords, or colors, of a vertex within an
857 * array is a hassle involving casts and simple math. These helpers exist
858 * to keep GrDrawTarget clients' code a bit nicer looking.
859 */
860
861 /**
862 * Gets a pointer to a GrPoint of a vertex's position or texture
863 * coordinate.
864 * @param vertices the vetex array
865 * @param vertexIndex the index of the vertex in the array
866 * @param vertexSize the size of each vertex in the array
867 * @param offset the offset in bytes of the vertex component.
868 * Defaults to zero (corresponding to vertex position)
869 * @return pointer to the vertex component as a GrPoint
870 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000871 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000872 int vertexIndex,
873 int vertexSize,
874 int offset = 0) {
875 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000876 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000877 vertexIndex * vertexSize);
878 }
879 static const GrPoint* GetVertexPoint(const void* vertices,
880 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000881 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000882 int offset = 0) {
883 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000884 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000885 vertexIndex * vertexSize);
886 }
887
888 /**
889 * Gets a pointer to a GrColor inside a vertex within a vertex array.
890 * @param vertices the vetex array
891 * @param vertexIndex the index of the vertex in the array
892 * @param vertexSize the size of each vertex in the array
893 * @param offset the offset in bytes of the vertex color
894 * @return pointer to the vertex component as a GrColor
895 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000896 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000897 int vertexIndex,
898 int vertexSize,
899 int offset) {
900 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000901 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000902 vertexIndex * vertexSize);
903 }
904 static const GrColor* GetVertexColor(const void* vertices,
905 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000906 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000907 int offset) {
908 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000909 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000910 vertexIndex * vertexSize);
911 }
912
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000913 static void VertexLayoutUnitTest();
914
reed@google.comac10a2d2010-12-22 21:39:39 +0000915protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +0000916
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000917 /**
918 * Optimizations for blending / coverage to be applied based on the current
919 * state.
920 * Subclasses that actually draw (as opposed to those that just buffer for
921 * playback) must implement the flags that replace the output color.
922 */
923 enum BlendOptFlags {
924 /**
925 * No optimization
926 */
927 kNone_BlendOpt = 0,
928 /**
929 * Don't draw at all
930 */
931 kSkipDraw_BlendOptFlag = 0x2,
932 /**
933 * Emit the src color, disable HW blending (replace dst with src)
934 */
935 kDisableBlend_BlendOptFlag = 0x4,
936 /**
937 * The coverage value does not have to be computed separately from
938 * alpha, the the output color can be the modulation of the two.
939 */
940 kCoverageAsAlpha_BlendOptFlag = 0x1,
941 /**
942 * Instead of emitting a src color, emit coverage in the alpha channel
943 * and r,g,b are "don't cares".
944 */
945 kEmitCoverage_BlendOptFlag = 0x10,
946 /**
947 * Emit transparent black instead of the src color, no need to compute
948 * coverage.
949 */
950 kEmitTransBlack_BlendOptFlag = 0x8,
951 };
952 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000953
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000954 // Determines what optimizations can be applied based on the blend.
955 // The coeffecients may have to be tweaked in order for the optimization
956 // to work. srcCoeff and dstCoeff are optional params that receive the
957 // tweaked coeffecients.
958 // Normally the function looks at the current state to see if coverage
959 // is enabled. By setting forceCoverage the caller can speculatively
960 // determine the blend optimizations that would be used if there was
961 // partial pixel coverage
962 BlendOptFlags getBlendOpts(bool forceCoverage = false,
963 GrBlendCoeff* srcCoeff = NULL,
964 GrBlendCoeff* dstCoeff = NULL) const;
965
966 // determine if src alpha is guaranteed to be one for all src pixels
967 bool srcAlphaWillBeOne() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000968
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000969 enum GeometrySrcType {
970 kNone_GeometrySrcType, //<! src has not been specified
971 kReserved_GeometrySrcType, //<! src was set using reserve*Space
972 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
973 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
974 };
975
976 struct GeometrySrcState {
977 GeometrySrcType fVertexSrc;
978 union {
979 // valid if src type is buffer
980 const GrVertexBuffer* fVertexBuffer;
981 // valid if src type is reserved or array
982 int fVertexCount;
983 };
984
985 GeometrySrcType fIndexSrc;
986 union {
987 // valid if src type is buffer
988 const GrIndexBuffer* fIndexBuffer;
989 // valid if src type is reserved or array
990 int fIndexCount;
991 };
992
993 GrVertexLayout fVertexLayout;
994 };
995
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000996 // given a vertex layout and a draw state, will a stage be used?
997 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
tomhudson@google.com93813632011-10-27 20:21:16 +0000998 const GrDrawState& state) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000999 return NULL != state.getTexture(stage) &&
1000 VertexUsesStage(stage, layout);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001001 }
1002
1003 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001004 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1005 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001006 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001007
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001008 StageMask enabledStages() const {
1009 StageMask mask = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +00001010 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001011 mask |= this->isStageEnabled(s) ? 1 : 0;
1012 }
1013 return mask;
1014 }
1015
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 // Helpers for GrDrawTarget subclasses that won't have private access to
1017 // SavedDrawState but need to peek at the state values.
tomhudson@google.com93813632011-10-27 20:21:16 +00001018 static GrDrawState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 { return sds.fState; }
tomhudson@google.com93813632011-10-27 20:21:16 +00001020 static const GrDrawState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 { return sds.fState; }
1022
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001023 // implemented by subclass to allocate space for reserved geom
1024 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1025 int vertexCount,
1026 void** vertices) = 0;
1027 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1028 // implemented by subclass to handle release of reserved geom space
1029 virtual void releaseReservedVertexSpace() = 0;
1030 virtual void releaseReservedIndexSpace() = 0;
1031 // subclass must consume array contents when set
1032 virtual void onSetVertexSourceToArray(const void* vertexArray,
1033 int vertexCount) = 0;
1034 virtual void onSetIndexSourceToArray(const void* indexArray,
1035 int indexCount) = 0;
1036 // subclass is notified that geom source will be set away from an array
1037 virtual void releaseVertexArray() = 0;
1038 virtual void releaseIndexArray() = 0;
1039 // subclass overrides to be notified just before geo src state
1040 // is pushed/popped.
1041 virtual void geometrySourceWillPush() = 0;
1042 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1043 // subclass called to perform drawing
1044 virtual void onDrawIndexed(GrPrimitiveType type,
1045 int startVertex,
1046 int startIndex,
1047 int vertexCount,
1048 int indexCount) = 0;
1049 virtual void onDrawNonIndexed(GrPrimitiveType type,
1050 int startVertex,
1051 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001052 // subclass overrides to be notified when clip is set. Must call
1053 // INHERITED::clipwillBeSet
1054 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001055
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001056 // Helpers for drawRect, protected so subclasses that override drawRect
1057 // can use them.
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001058 static GrVertexLayout GetRectVertexLayout(StageMask stageMask,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001059 const GrRect* srcRects[]);
1060
1061 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001062 const GrMatrix* matrix,
1063 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001064 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001065 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001066 void* vertices);
1067
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001068 // accessor for derived classes
1069 const GeometrySrcState& getGeomSrc() const {
1070 return fGeoSrcStateStack.back();
1071 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001072
1073 GrClip fClip;
1074
tomhudson@google.com93813632011-10-27 20:21:16 +00001075 GrDrawState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001077 Caps fCaps;
1078
bsalomon@google.com4a018bb2011-10-28 19:50:21 +00001079 // subclasses must call this in their destructors to ensure all vertex
1080 // and index sources have been released (including those held by
1081 // pushGeometrySource())
1082 void releaseGeometry();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001083private:
bsalomon@google.come8262622011-11-07 02:30:51 +00001084 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to
1085 // indicate non-indexed drawing.
1086 bool checkDraw(GrPrimitiveType type, int startVertex,
1087 int startIndex, int vertexCount,
1088 int indexCount) const;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001089 // called when setting a new vert/idx source to unref prev vb/ib
1090 void releasePreviousVertexSource();
1091 void releasePreviousIndexSource();
1092
1093 enum {
1094 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001096 SkSTArray<kPreallocGeoSrcStateStackCnt,
1097 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001098
reed@google.comac10a2d2010-12-22 21:39:39 +00001099};
1100
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001101GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1102
reed@google.comac10a2d2010-12-22 21:39:39 +00001103#endif