blob: 10633f5ed3bf4fcf4a110ecf8f503e615f7ab5b6 [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"
16#include "GrMatrix.h"
17#include "GrRefCnt.h"
18#include "GrRenderTarget.h"
19#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;
44 bool fNPOTTextureSupport : 1;
45 bool fNPOTTextureTileSupport : 1;
46 bool fNPOTRenderTargetSupport : 1;
47 bool fTwoSidedStencilSupport : 1;
48 bool fStencilWrapOpsSupport : 1;
49 bool fHWAALineSupport : 1;
50 bool fShaderSupport : 1;
51 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000052 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000053 bool fFSAASupport : 1;
54 bool fDualSourceBlendingSupport : 1;
55 bool fBufferLockSupport : 1;
bsalomon@google.coma3108262011-10-10 14:08:47 +000056 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000057 int fMinRenderTargetWidth;
58 int fMinRenderTargetHeight;
59 int fMaxRenderTargetSize;
60 int fMaxTextureSize;
61 };
62
63 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000064 * Number of texture stages. Each stage takes as input a color and
65 * 2D texture coordinates. The color input to the first enabled stage is the
66 * per-vertex color or the constant color (setColor/setAlpha) if there are
67 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000068 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000069 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000070 * lookups are specified by a texture a sampler (setSamplerState). Texture
71 * coordinates for each stage come from the vertices based on a
72 * GrVertexLayout bitfield. The output fragment color is the output color of
73 * the last enabled stage. The presence or absence of texture coordinates
74 * for each stage in the vertex layout indicates whether a stage is enabled
75 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000076 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000077 enum {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000078 kNumStages = 3,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000079 kMaxTexCoords = kNumStages
80 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000082 /**
83 * The absolute maximum number of edges that may be specified for
84 * a single draw call when performing edge antialiasing. This is used for
85 * the size of several static buffers, so implementations of getMaxEdges()
86 * (below) should clamp to this value.
87 */
88 enum {
89 kMaxEdges = 32
90 };
91
bsalomon@google.comaeb21602011-08-30 18:13:44 +000092 /**
93 * When specifying edges as vertex data this enum specifies what type of
94 * edges are in use. The edges are always 4 GrScalars in memory, even when
95 * the edge type requires fewer than 4.
96 */
97 enum VertexEdgeType {
98 /* 1-pixel wide line
99 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
100 kHairLine_EdgeType,
101 /* 1-pixel wide quadratic
102 u^2-v canonical coords (only 2 components used) */
103 kHairQuad_EdgeType
104 };
105
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000106 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000107 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000109 typedef int StageBitfield;
110 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 /**
113 * Flags that affect rendering. Controlled using enable/disableState(). All
114 * default to disabled.
115 */
116 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000117 kDither_StateBit = 0x01, //<! Perform color dithering
118 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 // target must support some form of AA
120 // (msaa, coverage sampling, etc). For
121 // GrGpu-created rendertarget/textures
122 // this is controlled by parameters
123 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000124 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 // against the region specified by
126 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000127 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
128 // Useful while performing stencil
129 // ops.
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000130 kEdgeAAConcave_StateBit = 0x10,//<! If set, edge AA will test edge
131 // pairs for convexity while
132 // rasterizing. Set this if the
133 // source polygon is non-convex.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134 // subclass may use additional bits internally
135 kDummyStateBit,
136 kLastPublicStateBit = kDummyStateBit-1
137 };
138
139 enum DrawFace {
140 kBoth_DrawFace,
141 kCCW_DrawFace,
142 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 };
144
145 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000146 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000147 * Changing the clip has the side-effect of possibly zeroing
148 * out the client settable stencil bits. So multipass algorithms
149 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000150 * @param settings the stencil settings to use.
151 */
152 void setStencil(const GrStencilSettings& settings) {
153 fCurrDrawState.fStencilSettings = settings;
154 }
155
156 /**
157 * Shortcut to disable stencil testing and ops.
158 */
159 void disableStencil() {
160 fCurrDrawState.fStencilSettings.setDisabled();
161 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000163 class Edge {
164 public:
165 Edge() {}
166 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
167 GrPoint intersect(const Edge& other) {
168 return GrPoint::Make(
169 (fY * other.fZ - other.fY * fZ) /
170 (fX * other.fY - other.fX * fY),
171 (fX * other.fZ - other.fX * fZ) /
172 (other.fX * fY - fX * other.fY));
173 }
174 float fX, fY, fZ;
175 };
176
reed@google.comac10a2d2010-12-22 21:39:39 +0000177protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
reed@google.com8195f672011-01-12 18:14:28 +0000179 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000180 DrState() {
181 // make sure any pad is zero for memcmp
182 // all DrState members should default to something
183 // valid by the memset
184 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000185
186 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000187 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000188 fFirstCoverageStage = kNumStages;
189
190 // pedantic assertion that our ptrs will
191 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000192 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000193
194 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000195 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000196 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000197 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000199 GrBlendCoeff fSrcBlend;
200 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000201 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000202 GrTexture* fTextures[kNumStages];
203 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000204 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000205 GrRenderTarget* fRenderTarget;
206 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000207 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000208 GrColor fColorFilterColor;
209 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000210
211 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000212 GrMatrix fViewMatrix;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 VertexEdgeType fVertexEdgeType;
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000214 Edge fEdgeAAEdges[kMaxEdges];
215 int fEdgeAANumEdges;
reed@google.com8195f672011-01-12 18:14:28 +0000216 bool operator ==(const DrState& s) const {
217 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 }
reed@google.com8195f672011-01-12 18:14:28 +0000219 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 };
221
222public:
223 ///////////////////////////////////////////////////////////////////////////
224
225 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000226 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000227
228 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000229 * Gets the capabilities of the draw target.
230 */
231 const Caps& getCaps() const { return fCaps; }
232
233 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 * Sets the current clip to the region specified by clip. All draws will be
235 * clipped against this clip if kClip_StateBit is enabled.
236 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000237 * Setting the clip may (or may not) zero out the client's stencil bits.
238 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 * @param description of the clipping region
240 */
241 void setClip(const GrClip& clip);
242
243 /**
244 * Gets the current clip.
245 *
246 * @return the clip.
247 */
248 const GrClip& getClip() const;
249
250 /**
251 * Sets the texture used at the next drawing call
252 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000253 * @param stage The texture stage for which the texture will be set
254 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 * @param texture The texture to set. Can be NULL though there is no advantage
256 * to settings a NULL texture if doing non-textured drawing
257 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000258 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000259
260 /**
261 * Retrieves the currently set texture.
262 *
263 * @return The currently set texture. The return value will be NULL if no
264 * texture has been set, NULL was most recently passed to
265 * setTexture, or the last setTexture was destroyed.
266 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267 const GrTexture* getTexture(int stage) const;
268 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
270 /**
271 * Sets the rendertarget used at the next drawing call
272 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000273 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 */
275 void setRenderTarget(GrRenderTarget* target);
276
277 /**
278 * Retrieves the currently set rendertarget.
279 *
280 * @return The currently set render target.
281 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000282 const GrRenderTarget* getRenderTarget() const;
283 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
285 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000286 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000288 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000289 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000291 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000292 * @param samplerState Specifies the sampler state.
293 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000294 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000295
296 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000297 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000299 * @param stage the stage of the sampler to set
300 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000302 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
303 GrAssert(stage >= 0 && stage < kNumStages);
304 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000305 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000306
307 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000308 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000309 * matrix
310 */
311 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
312 for (int i = 0; i < kNumStages; ++i) {
313 if ((1 << i) & stageMask) {
314 this->preConcatSamplerMatrix(i, matrix);
315 }
316 }
317 }
318
319 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000320 * Shortcut for preConcatSamplerMatrix on all enabled stages in mask with
321 * same matrix
322 *
323 * @param stage the stage of the sampler to set
324 * @param matrix the matrix to concat
325 */
326 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
327 StageBitfield stageMask = this->enabledStages();
328 this->preConcatSamplerMatrices(stageMask, matrix);
329 }
330
331 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000332 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000333 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000334 * @param stage the stage to of sampler to get
335 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000336 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000337 const GrMatrix& getSamplerMatrix(int stage) const {
338 return fCurrDrawState.fSamplerStates[stage].getMatrix();
339 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000340
341 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000342 * Sets the matrix of a stage's sampler
343 *
344 * @param stage the stage of sampler set
345 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000346 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000347 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000348 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
349 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000350
351 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 * Sets the matrix applied to veretx positions.
353 *
354 * In the post-view-matrix space the rectangle [0,w]x[0,h]
355 * fully covers the render target. (w and h are the width and height of the
356 * the rendertarget.)
357 *
358 * @param m the matrix used to transform the vertex positions.
359 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000360 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000361
362 /**
363 * Multiplies the current view matrix by a matrix
364 *
365 * After this call V' = V*m where V is the old view matrix,
366 * m is the parameter to this function, and V' is the new view matrix.
367 * (We consider positions to be column vectors so position vector p is
368 * transformed by matrix X as p' = X*p.)
369 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000370 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000372 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000373
374 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000375 * Multiplies the current view matrix by a matrix
376 *
377 * After this call V' = m*V where V is the old view matrix,
378 * m is the parameter to this function, and V' is the new view matrix.
379 * (We consider positions to be column vectors so position vector p is
380 * transformed by matrix X as p' = X*p.)
381 *
382 * @param m the matrix used to modify the view matrix.
383 */
384 void postConcatViewMatrix(const GrMatrix& m);
385
386 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000387 * Retrieves the current view matrix
388 * @return the current view matrix.
389 */
390 const GrMatrix& getViewMatrix() const;
391
392 /**
393 * Retrieves the inverse of the current view matrix.
394 *
395 * If the current view matrix is invertible, return true, and if matrix
396 * is non-null, copy the inverse into it. If the current view matrix is
397 * non-invertible, return false and ignore the matrix parameter.
398 *
399 * @param matrix if not null, will receive a copy of the current inverse.
400 */
401 bool getViewInverse(GrMatrix* matrix) const;
402
403 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 * Sets color for next draw to a premultiplied-alpha color.
405 *
406 * @param the color to set.
407 */
408 void setColor(GrColor);
409
410 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000411 * Gets the currently set color.
412 * @return the current color.
413 */
414 GrColor getColor() const { return fCurrDrawState.fColor; }
415
416 /**
Scroggo97c88c22011-05-11 14:05:25 +0000417 * Add a color filter that can be represented by a color and a mode.
418 */
419 void setColorFilter(GrColor, SkXfermode::Mode);
420
421 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 * Sets the color to be used for the next draw to be
423 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
424 *
425 * @param alpha The alpha value to set as the color.
426 */
427 void setAlpha(uint8_t alpha);
428
429 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000430 * Controls whether clockwise, counterclockwise, or both faces are drawn.
431 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000433 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000434
435 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000436 * A common pattern is to compute a color with the initial stages and then
437 * modulate that color by a coverage value in later stage(s) (AA, mask-
438 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
439 * computed based on the pre-coverage-modulated color. The division of
440 * stages between color-computing and coverage-computing is specified by
441 * this method. Initially this is kNumStages (all stages are color-
442 * computing).
443 */
444 void setFirstCoverageStage(int firstCoverageStage) {
445 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
446 }
447
448 /**
449 * Gets the index of the first coverage-computing stage.
450 */
451 int getFirstCoverageStage() const {
452 return fCurrDrawState.fFirstCoverageStage;
453 }
454
455 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000456 * Gets whether the target is drawing clockwise, counterclockwise,
457 * or both faces.
458 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000460 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
462 /**
463 * Enable render state settings.
464 *
465 * @param flags bitfield of StateBits specifing the states to enable
466 */
467 void enableState(uint32_t stateBits);
468
469 /**
470 * Disable render state settings.
471 *
472 * @param flags bitfield of StateBits specifing the states to disable
473 */
474 void disableState(uint32_t stateBits);
475
476 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000477 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
478 }
479
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000480 bool isAntialiasState() const {
481 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
482 }
483
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000484 bool isClipState() const {
485 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 }
487
bsalomon@google.comd302f142011-03-03 13:54:13 +0000488 bool isColorWriteDisabled() const {
489 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
490 }
491
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 * Sets the blending function coeffecients.
494 *
495 * The blend function will be:
496 * D' = sat(S*srcCoef + D*dstCoef)
497 *
498 * where D is the existing destination color, S is the incoming source
499 * color, and D' is the new destination color that will be written. sat()
500 * is the saturation function.
501 *
502 * @param srcCoef coeffecient applied to the src color.
503 * @param dstCoef coeffecient applied to the dst color.
504 */
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000505 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
reed@google.comac10a2d2010-12-22 21:39:39 +0000506
507 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000508 * Sets the blending function constant referenced by the following blending
509 * coeffecients:
510 * kConstC_BlendCoeff
511 * kIConstC_BlendCoeff
512 * kConstA_BlendCoeff
513 * kIConstA_BlendCoeff
514 *
515 * @param constant the constant to set
516 */
517 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
518
519 /**
520 * Retrieves the last value set by setBlendConstant()
521 * @return the blending constant value
522 */
523 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
524
525 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000526 * Determines if blending will require a read of a dst given the current
527 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000528 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000529 * @return true if the dst surface will be read at each pixel hit by the
530 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000531 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000532 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000533
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000534 /**
535 * Color alpha and coverage are two inputs to the drawing pipeline. For some
536 * blend modes it is safe to fold the coverage into constant or per-vertex
537 * color alpha value. For other blend modes they must be handled separately.
538 * Depending on features available in the underlying 3D API this may or may
539 * not be possible.
540 *
541 * This function looks at the current blend on the draw target and the draw
542 * target's capabilities to determine whether coverage can be handled
543 * correctly.
544 */
545 bool canApplyCoverage() const;
546
547 /**
548 * Determines whether incorporating partial pixel coverage into the constant
549 * color specified by setColor or per-vertex colors will give the right
550 * blending result.
551 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000552 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000553
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000554 /**
555 * Determines the interpretation per-vertex edge data when the
556 * kEdge_VertexLayoutBit is set (see below). When per-vertex edges are not
557 * specified the value of this setting has no effect.
558 */
559 void setVertexEdgeType(VertexEdgeType type) {
560 fCurrDrawState.fVertexEdgeType = type;
561 }
562
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000563 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000564 * Given the current draw state, vertex layout, and hw support, will HW AA
565 * lines be used (if line primitive type is drawn)? (Note that lines are
566 * always 1 pixel wide)
567 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000568 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000569
570 /**
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000571 * Sets the edge data required for edge antialiasing.
572 *
573 * @param edges 3 * 6 float values, representing the edge
574 * equations in Ax + By + C form
575 */
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000576 void setEdgeAAData(const Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000577
bsalomon@google.coma3108262011-10-10 14:08:47 +0000578 /**
579 * Used to save and restore the GrGpu's drawing state
580 */
581 struct SavedDrawState {
582 private:
583 DrState fState;
584 friend class GrDrawTarget;
585 };
586
587 /**
588 * Saves the current draw state. The state can be restored at a later time
589 * with restoreDrawState.
590 *
591 * See also AutoStateRestore class.
592 *
593 * @param state will hold the state after the function returns.
594 */
595 void saveCurrentDrawState(SavedDrawState* state) const;
596
597 /**
598 * Restores previously saved draw state. The client guarantees that state
599 * was previously passed to saveCurrentDrawState and that the rendertarget
600 * and texture set at save are still valid.
601 *
602 * See also AutoStateRestore class.
603 *
604 * @param state the previously saved state to restore.
605 */
606 void restoreDrawState(const SavedDrawState& state);
607
608 /**
609 * Copies the draw state from another target to this target.
610 *
611 * @param srcTarget draw target used as src of the draw state.
612 */
613 void copyDrawState(const GrDrawTarget& srcTarget);
614
615 /**
616 * The format of vertices is represented as a bitfield of flags.
617 * Flags that indicate the layout of vertex data. Vertices always contain
618 * positions and may also contain up to kMaxTexCoords sets of 2D texture
619 * coordinates, per-vertex colors, and per-vertex coverage. Each stage can
620 * use any of the texture coordinates as its input texture coordinates or it
621 * may use the positions as texture coordinates.
622 *
623 * If no texture coordinates are specified for a stage then the stage is
624 * disabled.
625 *
626 * Only one type of texture coord can be specified per stage. For
627 * example StageTexCoordVertexLayoutBit(0, 2) and
628 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
629 *
630 * The order in memory is always (position, texture coord 0, ..., color,
631 * coverage) with any unused fields omitted. Note that this means that if
632 * only texture coordinates 1 is referenced then there is no texture
633 * coordinates 0 and the order would be (position, texture coordinate 1
634 * [, color][, coverage]).
635 */
636
637 /**
638 * Generates a bit indicating that a texture stage uses texture coordinates
639 *
640 * @param stage the stage that will use texture coordinates.
641 * @param texCoordIdx the index of the texture coordinates to use
642 *
643 * @return the bit to add to a GrVertexLayout bitfield.
644 */
645 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
646 GrAssert(stage < kNumStages);
647 GrAssert(texCoordIdx < kMaxTexCoords);
648 return 1 << (stage + (texCoordIdx * kNumStages));
649 }
650
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000651private:
652 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000653
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000654public:
655 /**
656 * Generates a bit indicating that a texture stage uses the position
657 * as its texture coordinate.
658 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000659 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000660 * coordinates.
661 *
662 * @return the bit to add to a GrVertexLayout bitfield.
663 */
664 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
665 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000666 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000667 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000668
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000669private:
670 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000671
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000672public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000673
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000674 /**
675 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000676 */
677 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000678 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000679 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000680 /* vertices have coverage (GrColor where all channels should have the
681 * same value)
682 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000683 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000684 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000685 * text [GrGpuTextVertex vs GrPoint].)
686 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000687 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000688
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000689 /* Each vertex specificies an edge. Distance to the edge is used to
690 * compute a coverage. See setVertexEdgeType().
691 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000692 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000693 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000694 kDummyVertexLayoutBit,
695 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000696 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000697 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000698 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000699
700 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000701 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000702 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000703 * can use a different method. Once geometry is specified it can be used for
704 * multiple drawIndexed and drawNonIndexed calls.
705 *
706 * Sometimes it is necessary to perform a draw while upstack code has
707 * already specified geometry that it isn't finished with. There are push
708 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000709 *
710 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
711 * caller's client has already provided vertex data in a format
712 * the time compatible with a GrVertexLayout. The array must contain the
713 * data at set*SourceToArray is called. The source stays in effect for
714 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
715 * again or one of the other two paths is chosen.
716 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000717 * 2. Reserve. This is most useful when the caller has data it must
718 * transform before drawing and is not long-lived. The caller requests
719 * that the draw target make room for some amount of vertex and/or index
720 * data. The target provides ptrs to hold the vertex and/or index data.
721 *
722 * The data is writable up until the next drawIndexed, drawNonIndexed,
723 * or pushGeometrySource At this point the data is frozen and the ptrs
724 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000725 *
726 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000727 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
728 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
729 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000730 */
731
732 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733 * Reserves space for vertices. Draw target will use reserved vertices at
734 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 *
736 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000737 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000738 * of vertices to be filled by caller. The next draw will read
739 * these vertices.
740 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000741 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000742 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000743 * GrDrawTarget to decide whether to put data in buffers, to group vertex
744 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000745 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000746 * After the next draw or pushGeometrySource the vertices ptr is no longer
747 * valid and the geometry data cannot be further modified. The contents
748 * that were put in the reserved space can be drawn by multiple draws,
749 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000750 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000751 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 * @param vertices will point to reserved vertex space if vertexCount is
754 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000755 *
756 * @return true if succeeded in allocating space for the vertices and false
757 * if not.
758 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000759 bool reserveVertexSpace(GrVertexLayout vertexLayout,
760 int vertexCount,
761 void** vertices);
762 /**
763 * Reserves space for indices. Draw target will use the reserved indices at
764 * the next indexed draw.
765 *
766 * If succeeds:
767 * if indexCount > 0, *indices will be the array
768 * of indices to be filled by caller. The next draw will read
769 * these indices.
770 *
771 * If a client does not already have a index buffer then this is the
772 * preferred way to allocate index data. It allows the subclass of
773 * GrDrawTarget to decide whether to put data in buffers, to group index
774 * data that uses the same state (e.g. for deferred rendering), etc.
775 *
776 * After the next indexed draw or pushGeometrySource the indices ptr is no
777 * longer valid and the geometry data cannot be further modified. The
778 * contents that were put in the reserved space can be drawn by multiple
779 * draws, however.
780 *
781 * @param indexCount the number of indices to reserve space for. Can be 0.
782 * @param indices will point to reserved index space if indexCount is
783 * non-zero. Illegal to pass NULL if indexCount > 0.
784 */
785
786 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000787 /**
788 * Provides hints to caller about the number of vertices and indices
789 * that can be allocated cheaply. This can be useful if caller is reserving
790 * space but doesn't know exactly how much geometry is needed.
791 *
792 * Also may hint whether the draw target should be flushed first. This is
793 * useful for deferred targets.
794 *
795 * @param vertexLayout layout of vertices caller would like to reserve
796 * @param vertexCount in: hint about how many vertices the caller would
797 * like to allocate.
798 * out: a hint about the number of vertices that can be
799 * allocated cheaply. Negative means no hint.
800 * Ignored if NULL.
801 * @param indexCount in: hint about how many indices the caller would
802 * like to allocate.
803 * out: a hint about the number of indices that can be
804 * allocated cheaply. Negative means no hint.
805 * Ignored if NULL.
806 *
807 * @return true if target should be flushed based on the input values.
808 */
809 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000810 int* vertexCount,
811 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000812
813 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000814 * Sets source of vertex data for the next draw. Array must contain
815 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000816 *
817 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000818 * @param size size of the vertex data.
819 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000820 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000821 void setVertexSourceToArray(GrVertexLayout vertexLayout,
822 const void* vertexArray,
823 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000824
825 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000826 * Sets source of index data for the next indexed draw. Array must contain
827 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000828 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000829 * @param array cpu array containing index data.
830 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000831 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000832 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000833
834 /**
835 * Sets source of vertex data for the next draw. Data does not have to be
836 * in the buffer until drawIndexed or drawNonIndexed.
837 *
838 * @param buffer vertex buffer containing vertex data. Must be
839 * unlocked before draw call.
840 * @param vertexLayout layout of the vertex data in the buffer.
841 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000842 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
843 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000844
845 /**
846 * Sets source of index data for the next indexed draw. Data does not have
847 * to be in the buffer until drawIndexed or drawNonIndexed.
848 *
849 * @param buffer index buffer containing indices. Must be unlocked
850 * before indexed draw call.
851 */
852 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000853
854 /**
855 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
856 * source to reserved, array, or buffer before next draw. May be able to free
857 * up temporary storage allocated by setVertexSourceToArray or
858 * reserveVertexSpace.
859 */
860 void resetVertexSource();
861
862 /**
863 * Resets index source. Indexed Drawing from reset indices is illegal. Set
864 * index source to reserved, array, or buffer before next indexed draw. May
865 * be able to free up temporary storage allocated by setIndexSourceToArray
866 * or reserveIndexSpace.
867 */
868 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000869
870 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000871 * Pushes and resets the vertex/index sources. Any reserved vertex / index
872 * data is finalized (i.e. cannot be updated after the matching pop but can
873 * be drawn from). Must be balanced by a pop.
874 */
875 void pushGeometrySource();
876
877 /**
878 * Pops the vertex / index sources from the matching push.
879 */
880 void popGeometrySource();
881
882 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000883 * Draws indexed geometry using the current state and current vertex / index
884 * sources.
885 *
886 * @param type The type of primitives to draw.
887 * @param startVertex the vertex in the vertex array/buffer corresponding
888 * to index 0
889 * @param startIndex first index to read from index src.
890 * @param vertexCount one greater than the max index.
891 * @param indexCount the number of index elements to read. The index count
892 * is effectively trimmed to the last completely
893 * specified primitive.
894 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000895 void drawIndexed(GrPrimitiveType type,
896 int startVertex,
897 int startIndex,
898 int vertexCount,
899 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000900
901 /**
902 * Draws non-indexed geometry using the current state and current vertex
903 * sources.
904 *
905 * @param type The type of primitives to draw.
906 * @param startVertex the vertex in the vertex array/buffer corresponding
907 * to index 0
908 * @param vertexCount one greater than the max index.
909 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000910 void drawNonIndexed(GrPrimitiveType type,
911 int startVertex,
912 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000913
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000914 /**
915 * Helper function for drawing rects. This does not use the current index
916 * and vertex sources. After returning, the vertex and index sources may
917 * have changed. They should be reestablished before the next drawIndexed
918 * or drawNonIndexed. This cannot be called between reserving and releasing
919 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000920 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000921 * drawNonIndexed.
922 * @param rect the rect to draw
923 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000924 * @param stageEnableBitfield bitmask indicating which stages are enabled.
925 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000926 * @param srcRects specifies rects for stages enabled by stageEnableMask.
927 * if stageEnableMask bit i is 1, srcRects is not NULL,
928 * and srcRects[i] is not NULL, then srcRects[i] will be
929 * used as coordinates for stage i. Otherwise, if stage i
930 * is enabled then rect is used as the coordinates.
931 * @param srcMatrices optional matrices applied to srcRects. If
932 * srcRect[i] is non-NULL and srcMatrices[i] is
933 * non-NULL then srcRect[i] will be transformed by
934 * srcMatrix[i]. srcMatrices can be NULL when no
935 * srcMatrices are desired.
936 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000937 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000938 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000939 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000940 const GrRect* srcRects[],
941 const GrMatrix* srcMatrices[]);
942
943 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000944 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000945 * matrices.
946 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000947 void drawSimpleRect(const GrRect& rect,
948 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000949 StageBitfield stageEnableBitfield) {
950 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000951 }
952
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000953 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000954 * Clear the render target. Ignores the clip and all other draw state
955 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
956 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000957 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000958 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000959
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000960 /**
961 * Returns the maximum number of edges that may be specified in a single
962 * draw call when performing edge antialiasing. This is usually limited
963 * by the number of fragment uniforms which may be uploaded. Must be a
964 * minimum of six, since a triangle's vertices each belong to two boundary
965 * edges which may be distinct.
966 */
967 virtual int getMaxEdges() const { return 6; }
968
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000969 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000970
971 class AutoStateRestore : ::GrNoncopyable {
972 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000973 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000974 AutoStateRestore(GrDrawTarget* target);
975 ~AutoStateRestore();
976
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000977 /**
978 * if this object is already saving state for param target then
979 * this does nothing. Otherise, it restores previously saved state on
980 * previous target (if any) and saves current state on param target.
981 */
982 void set(GrDrawTarget* target);
983
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 private:
985 GrDrawTarget* fDrawTarget;
986 SavedDrawState fDrawState;
987 };
988
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000989 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000990
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000991 class AutoViewMatrixRestore : ::GrNoncopyable {
992 public:
993 AutoViewMatrixRestore() {
994 fDrawTarget = NULL;
995 }
996
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000997 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000998 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
999 GrAssert(NULL != target);
1000 }
1001
1002 void set(GrDrawTarget* target) {
1003 GrAssert(NULL != target);
1004 if (NULL != fDrawTarget) {
1005 fDrawTarget->setViewMatrix(fMatrix);
1006 }
1007 fDrawTarget = target;
1008 fMatrix = target->getViewMatrix();
1009 }
1010
1011 ~AutoViewMatrixRestore() {
1012 if (NULL != fDrawTarget) {
1013 fDrawTarget->setViewMatrix(fMatrix);
1014 }
1015 }
1016
1017 private:
1018 GrDrawTarget* fDrawTarget;
1019 GrMatrix fMatrix;
1020 };
1021
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001022 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001024 /**
1025 * Sets the view matrix to I and preconcats all stage matrices enabled in
1026 * mask by the view inverse. Destructor undoes these changes.
1027 */
1028 class AutoDeviceCoordDraw : ::GrNoncopyable {
1029 public:
1030 AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask);
1031 ~AutoDeviceCoordDraw();
1032 private:
1033 GrDrawTarget* fDrawTarget;
1034 GrMatrix fViewMatrix;
1035 GrMatrix fSamplerMatrices[kNumStages];
1036 int fStageMask;
1037 };
1038
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001039 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001040
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 class AutoReleaseGeometry : ::GrNoncopyable {
1042 public:
1043 AutoReleaseGeometry(GrDrawTarget* target,
1044 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001045 int vertexCount,
1046 int indexCount);
1047 AutoReleaseGeometry();
1048 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001049 bool set(GrDrawTarget* target,
1050 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001051 int vertexCount,
1052 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001053 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001054 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
1055 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001057 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 }
1059
1060 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001061 void reset();
1062
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 void* fVertices;
1065 void* fIndices;
1066 };
1067
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001068 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001069
1070 class AutoClipRestore : ::GrNoncopyable {
1071 public:
1072 AutoClipRestore(GrDrawTarget* target) {
1073 fTarget = target;
1074 fClip = fTarget->getClip();
1075 }
1076
1077 ~AutoClipRestore() {
1078 fTarget->setClip(fClip);
1079 }
1080 private:
1081 GrDrawTarget* fTarget;
1082 GrClip fClip;
1083 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001084
1085 ////////////////////////////////////////////////////////////////////////////
1086
1087 class AutoGeometryPush : ::GrNoncopyable {
1088 public:
1089 AutoGeometryPush(GrDrawTarget* target) {
1090 GrAssert(NULL != target);
1091 fTarget = target;
1092 target->pushGeometrySource();
1093 }
1094 ~AutoGeometryPush() {
1095 fTarget->popGeometrySource();
1096 }
1097 private:
1098 GrDrawTarget* fTarget;
1099 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001100
1101 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001102 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001103
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 /**
1105 * Helper function to compute the size of a vertex from a vertex layout
1106 * @return size of a single vertex.
1107 */
1108 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001109
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001110 /**
1111 * Helper function for determining the index of texture coordinates that
1112 * is input for a texture stage. Note that a stage may instead use positions
1113 * as texture coordinates, in which case the result of the function is
1114 * indistinguishable from the case when the stage is disabled.
1115 *
1116 * @param stage the stage to query
1117 * @param vertexLayout layout to query
1118 *
1119 * @return the texture coordinate index or -1 if the stage doesn't use
1120 * separate (non-position) texture coordinates.
1121 */
1122 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001123
1124 /**
1125 * Helper function to compute the offset of texture coordinates in a vertex
1126 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001127 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001128 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001130 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001131
1132 /**
1133 * Helper function to compute the offset of the color in a vertex
1134 * @return offset of color in vertex layout or -1 if the
1135 * layout has no color.
1136 */
1137 static int VertexColorOffset(GrVertexLayout vertexLayout);
1138
bsalomon@google.coma3108262011-10-10 14:08:47 +00001139 /**
1140 * Helper function to compute the offset of the coverage in a vertex
1141 * @return offset of coverage in vertex layout or -1 if the
1142 * layout has no coverage.
1143 */
1144 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
1145
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001146 /**
1147 * Helper function to compute the offset of the edge pts in a vertex
1148 * @return offset of edge in vertex layout or -1 if the
1149 * layout has no edge.
1150 */
1151 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
1152
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001154 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001155 * coordinates of some index.
1156 *
1157 * @param coordIndex the tex coord index to query
1158 * @param vertexLayout layout to query
1159 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001160 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001161 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001162 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001163 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001164 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001165
reed@google.comac10a2d2010-12-22 21:39:39 +00001166 /**
1167 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001168 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001170 * @param stage the stage to query
1171 * @param vertexLayout layout to query
1172 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001173 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001174 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001175 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001176 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001177
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001178 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001179 * Helper function to compute the size of each vertex and the offsets of
1180 * texture coordinates and color. Determines tex coord offsets by tex coord
1181 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001182 * by StageTexCoordVertexLayoutBit.)
1183 *
1184 * @param vertexLayout the layout to query
1185 * @param texCoordOffsetsByIdx after return it is the offset of each
1186 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001187 * index isn't used. (optional)
1188 * @param colorOffset after return it is the offset of the
1189 * color field in each vertex, or -1 if
1190 * there aren't per-vertex colors. (optional)
1191 * @param coverageOffset after return it is the offset of the
1192 * coverage field in each vertex, or -1 if
1193 * there aren't per-vertex coeverages.
1194 * (optional)
1195 * @param edgeOffset after return it is the offset of the
1196 * edge eq field in each vertex, or -1 if
1197 * there aren't per-vertex edge equations.
1198 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001199 * @return size of a single vertex
1200 */
1201 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1202 int texCoordOffsetsByIdx[kMaxTexCoords],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001203 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001204 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001205 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001206
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001207 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001208 * Helper function to compute the size of each vertex and the offsets of
1209 * texture coordinates and color. Determines tex coord offsets by stage
1210 * rather than by index. (Each stage can be mapped to any t.c. index
1211 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001212 * tex coords then that stage's offset will be 0 (positions are always at 0).
1213 *
1214 * @param vertexLayout the layout to query
1215 * @param texCoordOffsetsByStage after return it is the offset of each
1216 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001217 * index isn't used. (optional)
1218 * @param colorOffset after return it is the offset of the
1219 * color field in each vertex, or -1 if
1220 * there aren't per-vertex colors.
1221 * (optional)
1222 * @param coverageOffset after return it is the offset of the
1223 * coverage field in each vertex, or -1 if
1224 * there aren't per-vertex coeverages.
1225 * (optional)
1226 * @param edgeOffset after return it is the offset of the
1227 * edge eq field in each vertex, or -1 if
1228 * there aren't per-vertex edge equations.
1229 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001230 * @return size of a single vertex
1231 */
1232 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1233 int texCoordOffsetsByStage[kNumStages],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001234 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001235 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001236 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001237
1238 /**
1239 * Accessing positions, texture coords, or colors, of a vertex within an
1240 * array is a hassle involving casts and simple math. These helpers exist
1241 * to keep GrDrawTarget clients' code a bit nicer looking.
1242 */
1243
1244 /**
1245 * Gets a pointer to a GrPoint of a vertex's position or texture
1246 * coordinate.
1247 * @param vertices the vetex array
1248 * @param vertexIndex the index of the vertex in the array
1249 * @param vertexSize the size of each vertex in the array
1250 * @param offset the offset in bytes of the vertex component.
1251 * Defaults to zero (corresponding to vertex position)
1252 * @return pointer to the vertex component as a GrPoint
1253 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001254 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001255 int vertexIndex,
1256 int vertexSize,
1257 int offset = 0) {
1258 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001259 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001260 vertexIndex * vertexSize);
1261 }
1262 static const GrPoint* GetVertexPoint(const void* vertices,
1263 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001264 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001265 int offset = 0) {
1266 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001267 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001268 vertexIndex * vertexSize);
1269 }
1270
1271 /**
1272 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1273 * @param vertices the vetex array
1274 * @param vertexIndex the index of the vertex in the array
1275 * @param vertexSize the size of each vertex in the array
1276 * @param offset the offset in bytes of the vertex color
1277 * @return pointer to the vertex component as a GrColor
1278 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001279 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001280 int vertexIndex,
1281 int vertexSize,
1282 int offset) {
1283 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001284 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001285 vertexIndex * vertexSize);
1286 }
1287 static const GrColor* GetVertexColor(const void* vertices,
1288 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001289 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001290 int offset) {
1291 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001292 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001293 vertexIndex * vertexSize);
1294 }
1295
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001296 static void VertexLayoutUnitTest();
1297
reed@google.comac10a2d2010-12-22 21:39:39 +00001298protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +00001299
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001300 /**
1301 * Optimizations for blending / coverage to be applied based on the current
1302 * state.
1303 * Subclasses that actually draw (as opposed to those that just buffer for
1304 * playback) must implement the flags that replace the output color.
1305 */
1306 enum BlendOptFlags {
1307 /**
1308 * No optimization
1309 */
1310 kNone_BlendOpt = 0,
1311 /**
1312 * Don't draw at all
1313 */
1314 kSkipDraw_BlendOptFlag = 0x2,
1315 /**
1316 * Emit the src color, disable HW blending (replace dst with src)
1317 */
1318 kDisableBlend_BlendOptFlag = 0x4,
1319 /**
1320 * The coverage value does not have to be computed separately from
1321 * alpha, the the output color can be the modulation of the two.
1322 */
1323 kCoverageAsAlpha_BlendOptFlag = 0x1,
1324 /**
1325 * Instead of emitting a src color, emit coverage in the alpha channel
1326 * and r,g,b are "don't cares".
1327 */
1328 kEmitCoverage_BlendOptFlag = 0x10,
1329 /**
1330 * Emit transparent black instead of the src color, no need to compute
1331 * coverage.
1332 */
1333 kEmitTransBlack_BlendOptFlag = 0x8,
1334 };
1335 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +00001336
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001337 // Determines what optimizations can be applied based on the blend.
1338 // The coeffecients may have to be tweaked in order for the optimization
1339 // to work. srcCoeff and dstCoeff are optional params that receive the
1340 // tweaked coeffecients.
1341 // Normally the function looks at the current state to see if coverage
1342 // is enabled. By setting forceCoverage the caller can speculatively
1343 // determine the blend optimizations that would be used if there was
1344 // partial pixel coverage
1345 BlendOptFlags getBlendOpts(bool forceCoverage = false,
1346 GrBlendCoeff* srcCoeff = NULL,
1347 GrBlendCoeff* dstCoeff = NULL) const;
1348
1349 // determine if src alpha is guaranteed to be one for all src pixels
1350 bool srcAlphaWillBeOne() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +00001351
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001352 enum GeometrySrcType {
1353 kNone_GeometrySrcType, //<! src has not been specified
1354 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1355 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1356 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1357 };
1358
1359 struct GeometrySrcState {
1360 GeometrySrcType fVertexSrc;
1361 union {
1362 // valid if src type is buffer
1363 const GrVertexBuffer* fVertexBuffer;
1364 // valid if src type is reserved or array
1365 int fVertexCount;
1366 };
1367
1368 GeometrySrcType fIndexSrc;
1369 union {
1370 // valid if src type is buffer
1371 const GrIndexBuffer* fIndexBuffer;
1372 // valid if src type is reserved or array
1373 int fIndexCount;
1374 };
1375
1376 GrVertexLayout fVertexLayout;
1377 };
1378
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001379 // given a vertex layout and a draw state, will a stage be used?
1380 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001381 const DrState& state) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001382 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1383 }
1384
1385 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001386 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1387 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001388 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001389
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001390 StageBitfield enabledStages() const {
1391 StageBitfield mask = 0;
1392 for (int s = 0; s < kNumStages; ++s) {
1393 mask |= this->isStageEnabled(s) ? 1 : 0;
1394 }
1395 return mask;
1396 }
1397
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 // Helpers for GrDrawTarget subclasses that won't have private access to
1399 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001400 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001401 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001402 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 { return sds.fState; }
1404
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001405 // implemented by subclass to allocate space for reserved geom
1406 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1407 int vertexCount,
1408 void** vertices) = 0;
1409 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1410 // implemented by subclass to handle release of reserved geom space
1411 virtual void releaseReservedVertexSpace() = 0;
1412 virtual void releaseReservedIndexSpace() = 0;
1413 // subclass must consume array contents when set
1414 virtual void onSetVertexSourceToArray(const void* vertexArray,
1415 int vertexCount) = 0;
1416 virtual void onSetIndexSourceToArray(const void* indexArray,
1417 int indexCount) = 0;
1418 // subclass is notified that geom source will be set away from an array
1419 virtual void releaseVertexArray() = 0;
1420 virtual void releaseIndexArray() = 0;
1421 // subclass overrides to be notified just before geo src state
1422 // is pushed/popped.
1423 virtual void geometrySourceWillPush() = 0;
1424 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1425 // subclass called to perform drawing
1426 virtual void onDrawIndexed(GrPrimitiveType type,
1427 int startVertex,
1428 int startIndex,
1429 int vertexCount,
1430 int indexCount) = 0;
1431 virtual void onDrawNonIndexed(GrPrimitiveType type,
1432 int startVertex,
1433 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001434 // subclass overrides to be notified when clip is set. Must call
1435 // INHERITED::clipwillBeSet
1436 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001437
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001438 // Helpers for drawRect, protected so subclasses that override drawRect
1439 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001440 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001441 const GrRect* srcRects[]);
1442
1443 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001444 const GrMatrix* matrix,
1445 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001446 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001447 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001448 void* vertices);
1449
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001450 // accessor for derived classes
1451 const GeometrySrcState& getGeomSrc() const {
1452 return fGeoSrcStateStack.back();
1453 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001454
1455 GrClip fClip;
1456
reed@google.com8195f672011-01-12 18:14:28 +00001457 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001458
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001459 Caps fCaps;
1460
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001461private:
1462 // called when setting a new vert/idx source to unref prev vb/ib
1463 void releasePreviousVertexSource();
1464 void releasePreviousIndexSource();
1465
1466 enum {
1467 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001468 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001469 SkSTArray<kPreallocGeoSrcStateStackCnt,
1470 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001471
reed@google.comac10a2d2010-12-22 21:39:39 +00001472};
1473
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001474GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1475
reed@google.comac10a2d2010-12-22 21:39:39 +00001476#endif