blob: 9c78902f799076b4e0f685d4d3bbf760bff4a9a9 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrDrawTarget_DEFINED
19#define GrDrawTarget_DEFINED
20
reed@google.comac10a2d2010-12-22 21:39:39 +000021#include "GrMatrix.h"
22#include "GrColor.h"
23#include "GrRefCnt.h"
24#include "GrSamplerState.h"
25#include "GrClip.h"
26
27class GrTexture;
28class GrRenderTarget;
29class GrClipIterator;
30class GrVertexBuffer;
31class GrIndexBuffer;
32
33class GrDrawTarget : public GrRefCnt {
34public:
35 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000036 * Number of texture stages. Each stage takes as input a color and
37 * 2D texture coordinates. The color input to the first enabled stage is the
38 * per-vertex color or the constant color (setColor/setAlpha) if there are
39 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000040 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000041 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000042 * lookups are specified by a texture a sampler (setSamplerState). Texture
43 * coordinates for each stage come from the vertices based on a
44 * GrVertexLayout bitfield. The output fragment color is the output color of
45 * the last enabled stage. The presence or absence of texture coordinates
46 * for each stage in the vertex layout indicates whether a stage is enabled
47 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000048 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000049 enum {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000050 kNumStages = 2,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000051 kMaxTexCoords = kNumStages
52 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000053
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000054 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000055 * Geometric primitives used for drawing.
56 */
57 enum PrimitiveType {
58 kTriangles_PrimitiveType,
59 kTriangleStrip_PrimitiveType,
60 kTriangleFan_PrimitiveType,
61 kPoints_PrimitiveType,
62 kLines_PrimitiveType,
63 kLineStrip_PrimitiveType
64 };
65
66 /**
67 * Flags that affect rendering. Controlled using enable/disableState(). All
68 * default to disabled.
69 */
70 enum StateBits {
71 kDither_StateBit = 0x1,//<! Perform color dithering
72 kAntialias_StateBit = 0x2,//<! Perform anti-aliasing. The render-
73 // target must support some form of AA
74 // (msaa, coverage sampling, etc). For
75 // GrGpu-created rendertarget/textures
76 // this is controlled by parameters
77 // passed to createTexture.
78 kClip_StateBit = 0x4,//<! Controls whether drawing is clipped
79 // against the region specified by
80 // setClip.
81 };
82
83 /**
84 * Coeffecients for alpha-blending.
85 */
86 enum BlendCoeff {
87 kZero_BlendCoeff, //<! 0
88 kOne_BlendCoeff, //<! 1
89 kSC_BlendCoeff, //<! src color
90 kISC_BlendCoeff, //<! one minus src color
91 kDC_BlendCoeff, //<! dst color
92 kIDC_BlendCoeff, //<! one minus dst color
93 kSA_BlendCoeff, //<! src alpha
94 kISA_BlendCoeff, //<! one minus src alpha
95 kDA_BlendCoeff, //<! dst alpha
96 kIDA_BlendCoeff, //<! one minus dst alpha
97 };
98
99 /**
100 * StencilPass
101 *
102 * Sets the stencil state for subsequent draw calls. Used to fill paths.
103 *
104 * Winding requires two passes when the GPU/API doesn't support separate
105 * stencil.
106 *
107 * The color pass for path fill is used to zero out stencil bits used for
108 * path filling. Every pixel covere by a winding/EO stencil pass must get
109 * covered by the color pass in order to leave stencil buffer in the correct
110 * state for the next path draw.
111 *
112 * NOTE: Stencil-based Winding fill has alias-to-zero problems. (e.g. A
113 * winding count of 128,256,512,etc with a 8 bit stencil buffer
114 * will be unfilled)
115 */
116 enum StencilPass {
117 kNone_StencilPass, //<! Not drawing a path or clip.
118 kEvenOddStencil_StencilPass, //<! records in/out in stencil buffer
119 // using the Even/Odd fill rule.
120 kEvenOddColor_StencilPass, //<! writes colors to color target in
121 // pixels marked inside the fill by
122 // kEOFillStencil_StencilPass. Clears
123 // stencil in pixels covered by
124 // geometry.
125 kWindingStencil1_StencilPass, //<! records in/out in stencil buffer
126 // using the Winding fill rule.
127 kWindingStencil2_StencilPass, //<! records in/out in stencil buffer
128 // using the Winding fill rule.
129 // Run when single-stencil-pass winding
130 // not supported (i.e. no separate
131 // stencil support)
132 kWindingColor_StencilPass, //<! writes colors to color target in
133 // pixels marked inside the fill by
134 // kWindFillStencil_StencilPass. Clears
135 // stencil in pixels covered by
136 // geometry.
137 kDrawTargetCount_StencilPass //<! Subclass may extend this enum to use
138 // the stencil for other purposes (e.g.
139 // to do stencil-based clipping)
140 // This value is provided as basis for
141 // defining these extended enum values.
142 };
143
144protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000145
reed@google.com8195f672011-01-12 18:14:28 +0000146 struct DrState {
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 uint32_t fFlagBits;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000148 BlendCoeff fSrcBlend;
149 BlendCoeff fDstBlend;
150 GrTexture* fTextures[kNumStages];
151 GrSamplerState fSamplerStates[kNumStages];
152 GrRenderTarget* fRenderTarget;
153 GrColor fColor;
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 StencilPass fStencilPass;
155 bool fReverseFill;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000156 GrMatrix fViewMatrix;
reed@google.com8195f672011-01-12 18:14:28 +0000157 bool operator ==(const DrState& s) const {
158 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 }
reed@google.com8195f672011-01-12 18:14:28 +0000160 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 };
162
163public:
164 ///////////////////////////////////////////////////////////////////////////
165
166 GrDrawTarget();
167
168 /**
169 * Sets the current clip to the region specified by clip. All draws will be
170 * clipped against this clip if kClip_StateBit is enabled.
171 *
172 * @param description of the clipping region
173 */
174 void setClip(const GrClip& clip);
175
176 /**
177 * Gets the current clip.
178 *
179 * @return the clip.
180 */
181 const GrClip& getClip() const;
182
183 /**
184 * Sets the texture used at the next drawing call
185 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000186 * @param stage The texture stage for which the texture will be set
187 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 * @param texture The texture to set. Can be NULL though there is no advantage
189 * to settings a NULL texture if doing non-textured drawing
190 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000191 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
193 /**
194 * Retrieves the currently set texture.
195 *
196 * @return The currently set texture. The return value will be NULL if no
197 * texture has been set, NULL was most recently passed to
198 * setTexture, or the last setTexture was destroyed.
199 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000200 const GrTexture* getTexture(int stage) const;
201 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
203 /**
204 * Sets the rendertarget used at the next drawing call
205 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000206 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 */
208 void setRenderTarget(GrRenderTarget* target);
209
210 /**
211 * Retrieves the currently set rendertarget.
212 *
213 * @return The currently set render target.
214 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000215 const GrRenderTarget* getRenderTarget() const;
216 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
218 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000219 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000221 * The sampler state determines how texture coordinates are
222 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000224 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 * @param samplerState Specifies the sampler state.
226 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000227 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
229 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000230 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000232 * @param stage the stage of the sampler to set
233 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000235 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
236 GrAssert(stage >= 0 && stage < kNumStages);
237 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
238 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
240 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000241 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000242 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000243 * @param stage the stage to of sampler to get
244 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000245 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000246 const GrMatrix& getSamplerMatrix(int stage) const {
247 return fCurrDrawState.fSamplerStates[stage].getMatrix();
248 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000249
250 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000251 * Sets the matrix of a stage's sampler
252 *
253 * @param stage the stage of sampler set
254 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000255 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000256 const void setSamplerMatrix(int stage, const GrMatrix& matrix) {
257 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
258 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000259
260 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 * Sets the matrix applied to veretx positions.
262 *
263 * In the post-view-matrix space the rectangle [0,w]x[0,h]
264 * fully covers the render target. (w and h are the width and height of the
265 * the rendertarget.)
266 *
267 * @param m the matrix used to transform the vertex positions.
268 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000269 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 /**
272 * Multiplies the current view matrix by a matrix
273 *
274 * After this call V' = V*m where V is the old view matrix,
275 * m is the parameter to this function, and V' is the new view matrix.
276 * (We consider positions to be column vectors so position vector p is
277 * transformed by matrix X as p' = X*p.)
278 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000279 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000281 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000282
283 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000284 * Retrieves the current view matrix
285 * @return the current view matrix.
286 */
287 const GrMatrix& getViewMatrix() const;
288
289 /**
290 * Retrieves the inverse of the current view matrix.
291 *
292 * If the current view matrix is invertible, return true, and if matrix
293 * is non-null, copy the inverse into it. If the current view matrix is
294 * non-invertible, return false and ignore the matrix parameter.
295 *
296 * @param matrix if not null, will receive a copy of the current inverse.
297 */
298 bool getViewInverse(GrMatrix* matrix) const;
299
300 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 * Sets color for next draw to a premultiplied-alpha color.
302 *
303 * @param the color to set.
304 */
305 void setColor(GrColor);
306
307 /**
308 * Sets the color to be used for the next draw to be
309 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
310 *
311 * @param alpha The alpha value to set as the color.
312 */
313 void setAlpha(uint8_t alpha);
314
315 /**
316 * Sets pass for path rendering
317 *
318 * @param pass of path rendering
319 */
320 void setStencilPass(StencilPass pass);
321
322 /**
323 * Reveses the in/out decision of the fill rule for path rendering.
324 * Only affects kEOFillColor_StencilPass and kWindingFillColor_StencilPass
325 *
326 * @param reverse true to reverse, false otherwise
327 */
328 void setReverseFill(bool reverse);
329
330 /**
331 * Enable render state settings.
332 *
333 * @param flags bitfield of StateBits specifing the states to enable
334 */
335 void enableState(uint32_t stateBits);
336
337 /**
338 * Disable render state settings.
339 *
340 * @param flags bitfield of StateBits specifing the states to disable
341 */
342 void disableState(uint32_t stateBits);
343
344 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000345 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
346 }
347
348 bool isClipState() const {
349 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 }
351
352 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 * Sets the blending function coeffecients.
354 *
355 * The blend function will be:
356 * D' = sat(S*srcCoef + D*dstCoef)
357 *
358 * where D is the existing destination color, S is the incoming source
359 * color, and D' is the new destination color that will be written. sat()
360 * is the saturation function.
361 *
362 * @param srcCoef coeffecient applied to the src color.
363 * @param dstCoef coeffecient applied to the dst color.
364 */
365 void setBlendFunc(BlendCoeff srcCoef, BlendCoeff dstCoef);
366
367 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000368 * Used to save and restore the GrGpu's drawing state
369 */
370 struct SavedDrawState {
371 private:
reed@google.com8195f672011-01-12 18:14:28 +0000372 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 friend class GrDrawTarget;
374 };
375
376 /**
377 * Saves the current draw state. The state can be restored at a later time
378 * with restoreDrawState.
379 *
380 * See also AutoStateRestore class.
381 *
382 * @param state will hold the state after the function returns.
383 */
384 void saveCurrentDrawState(SavedDrawState* state) const;
385
386 /**
387 * Restores previously saved draw state. The client guarantees that state
388 * was previously passed to saveCurrentDrawState and that the rendertarget
389 * and texture set at save are still valid.
390 *
391 * See also AutoStateRestore class.
392 *
393 * @param state the previously saved state to restore.
394 */
395 void restoreDrawState(const SavedDrawState& state);
396
397 /**
398 * Copies the draw state from another target to this target.
399 *
400 * @param srcTarget draw target used as src of the draw state.
401 */
402 void copyDrawState(const GrDrawTarget& srcTarget);
403
404 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000405 * The format of vertices is represented as a bitfield of flags.
406 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 * coordinates and per-vertex colors. Each stage can use any of the texture
409 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000411 * If no texture coordinates are specified for a stage then the stage is
412 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000414 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000415 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000416 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000417 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000419 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000420 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000421 * the order would be (position, texture coordinate 1[, color]).
422 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000423
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000424 /**
425 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000426 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000427 * @param stage the stage that will use texture coordinates.
428 * @param texCoordIdx the index of the texture coordinates to use
429 *
430 * @return the bit to add to a GrVertexLayout bitfield.
431 */
432 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
433 GrAssert(stage < kNumStages);
434 GrAssert(texCoordIdx < kMaxTexCoords);
435 return 1 << (stage + (texCoordIdx * kNumStages));
436 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000437
438 /**
439 * Determines if blend is effectively disabled.
440 *
441 * @return true if blend can be disabled without changing the rendering
442 * result given the current state including the vertex layout specified
443 * with the vertex source.
444 */
445 bool canDisableBlend() const;
446
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000447private:
448 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
449public:
450 /**
451 * Generates a bit indicating that a texture stage uses the position
452 * as its texture coordinate.
453 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000455 * coordinates.
456 *
457 * @return the bit to add to a GrVertexLayout bitfield.
458 */
459 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
460 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000461 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000462 }
463private:
464 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000466public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000468 /**
469 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 */
471 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000473 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
474 //<! vertices have colors
475 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
476 //<! use text vertices. (Pos
477 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000479 // text [GrGpuTextVertex vs
480 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 // for below assert
482 kDummy,
483 kHighVertexLayoutBit = kDummy - 1
484 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000485 // make sure we haven't exceeded the number of bits in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout)));
487
488 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000489 * There are three paths for specifying geometry (vertices and optionally
490 * indices) to the draw target. When indexed drawing the indices and vertices
491 * can be each use a different path.
492 *
493 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
494 * caller's client has already provided vertex data in a format
495 * the time compatible with a GrVertexLayout. The array must contain the
496 * data at set*SourceToArray is called. The source stays in effect for
497 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
498 * again or one of the other two paths is chosen.
499 *
500 * 2. Reserve and Lock. This is most useful when the caller has data it must
501 * transform before drawing and will not likely render it again. The
502 * caller requests that the draw target make room for some amount of
503 * vertex and/or index data. The target provides ptrs to hold the data
504 * data. The caller can write the data into the pts up until the first
505 * drawIndexed or drawNonIndexed call. At this point the data is frozen
506 * and the ptrs are no longer guaranteed to be valid. All subsequent
507 * drawIndexed & drawNonIndexed calls will use this data until
508 * releaseReserved geometry is called. This must be called before another
509 * source is set.
510 *
511 * 3. Vertex and Index Buffers. This is most useful for geometry that will
512 * be rendered multiple times. SetVertexSourceToBuffer &
513 * SetIndexSourceToBuffer are used to set the buffer and subsequent
514 * drawIndexed and drawNonIndexed calls use this source until another
515 * source is set.
516 */
517
518 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 * Reserves space for vertices and/or indices. Draw target will use
520 * reserved vertices / indices at next draw.
521 *
522 * If succeeds:
523 * if vertexCount is nonzero, *vertices will be the array
524 * of vertices to be filled by caller. The next draw will read
525 * these vertices.
526 *
527 * if indecCount is nonzero, *indices will be the array of indices
528 * to be filled by caller. The next indexed draw will read from
529 * these indices.
530 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000531 * If a client does not already have a vertex buffer then this is the
532 * preferred way to allocate vertex/index array. It allows the subclass of
533 * GrDrawTarget to decide whether to put data in buffers, to group vertex
534 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000536 * Following the first draw after reserveAndLockGeometry the ptrs returned
537 * by releaseReservedGeometry are no longer valid and the geometry data
538 * cannot be further modified. The contents that were put in the reserved
539 * space can be drawn by multiple draws, however.
540 *
541 * reserveAndLockGeometry must be matched with a releaseReservedGeometry
542 * call after all draws that reference the reserved geometry data have
543 * been called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 *
545 * AutoGeometryRelease can be used to automatically call the release.
546 *
547 * @param vertexCount the number of vertices to reserve space for. Can be 0.
548 * @param indexCount the number of indices to reserve space for. Can be 0.
549 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
550 * @param vertices will point to reserved vertex space if vertexCount is
551 * non-zero. Illegal to pass NULL if vertexCount > 0.
552 * @param indices will point to reserved index space if indexCount is
553 * non-zero. Illegal to pass NULL if indexCount > 0.
554 *
555 * @return true if succeeded in allocating space for the vertices and false
556 * if not.
557 */
558 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
559 uint32_t vertexCount,
560 uint32_t indexCount,
561 void** vertices,
562 void** indices);
563 /**
564 * Provides hints to caller about the number of vertices and indices
565 * that can be allocated cheaply. This can be useful if caller is reserving
566 * space but doesn't know exactly how much geometry is needed.
567 *
568 * Also may hint whether the draw target should be flushed first. This is
569 * useful for deferred targets.
570 *
571 * @param vertexLayout layout of vertices caller would like to reserve
572 * @param vertexCount in: hint about how many vertices the caller would
573 * like to allocate.
574 * out: a hint about the number of vertices that can be
575 * allocated cheaply. Negative means no hint.
576 * Ignored if NULL.
577 * @param indexCount in: hint about how many indices the caller would
578 * like to allocate.
579 * out: a hint about the number of indices that can be
580 * allocated cheaply. Negative means no hint.
581 * Ignored if NULL.
582 *
583 * @return true if target should be flushed based on the input values.
584 */
585 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000586 int* vertexCount,
587 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
589 /**
590 * Releases reserved vertex/index data from reserveAndLockGeometry().
591 */
592 void releaseReservedGeometry();
593
594 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000595 * Sets source of vertex data for the next draw. Array must contain
596 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 *
598 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000599 * @param size size of the vertex data.
600 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000601 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000602 void setVertexSourceToArray(GrVertexLayout vertexLayout,
603 const void* vertexArray,
604 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
606 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000607 * Sets source of index data for the next indexed draw. Array must contain
608 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000610 * @param array cpu array containing index data.
611 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000613 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000614
615 /**
616 * Sets source of vertex data for the next draw. Data does not have to be
617 * in the buffer until drawIndexed or drawNonIndexed.
618 *
619 * @param buffer vertex buffer containing vertex data. Must be
620 * unlocked before draw call.
621 * @param vertexLayout layout of the vertex data in the buffer.
622 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000623 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
624 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000625
626 /**
627 * Sets source of index data for the next indexed draw. Data does not have
628 * to be in the buffer until drawIndexed or drawNonIndexed.
629 *
630 * @param buffer index buffer containing indices. Must be unlocked
631 * before indexed draw call.
632 */
633 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
634
635 /**
636 * Draws indexed geometry using the current state and current vertex / index
637 * sources.
638 *
639 * @param type The type of primitives to draw.
640 * @param startVertex the vertex in the vertex array/buffer corresponding
641 * to index 0
642 * @param startIndex first index to read from index src.
643 * @param vertexCount one greater than the max index.
644 * @param indexCount the number of index elements to read. The index count
645 * is effectively trimmed to the last completely
646 * specified primitive.
647 */
648 virtual void drawIndexed(PrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000649 int startVertex,
650 int startIndex,
651 int vertexCount,
652 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000653
654 /**
655 * Draws non-indexed geometry using the current state and current vertex
656 * sources.
657 *
658 * @param type The type of primitives to draw.
659 * @param startVertex the vertex in the vertex array/buffer corresponding
660 * to index 0
661 * @param vertexCount one greater than the max index.
662 */
663 virtual void drawNonIndexed(PrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000664 int startVertex,
665 int vertexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000666
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000667 /**
668 * Helper function for drawing rects. This does not use the current index
669 * and vertex sources. After returning, the vertex and index sources may
670 * have changed. They should be reestablished before the next drawIndexed
671 * or drawNonIndexed. This cannot be called between reserving and releasing
672 * geometry. The GrDrawTarget subclass may be able to perform additional
673 * optimizations if drawRect is used rather than drawIndexed or
674 * drawNonIndexed.
675 * @param rect the rect to draw
676 * @param matrix optional matrix applied to rect (before viewMatrix)
677 * @param stageEnableMask bitmask indicating which stages are enabled.
678 * Bit i indicates whether stage i is enabled.
679 * @param srcRects specifies rects for stages enabled by stageEnableMask.
680 * if stageEnableMask bit i is 1, srcRects is not NULL,
681 * and srcRects[i] is not NULL, then srcRects[i] will be
682 * used as coordinates for stage i. Otherwise, if stage i
683 * is enabled then rect is used as the coordinates.
684 * @param srcMatrices optional matrices applied to srcRects. If
685 * srcRect[i] is non-NULL and srcMatrices[i] is
686 * non-NULL then srcRect[i] will be transformed by
687 * srcMatrix[i]. srcMatrices can be NULL when no
688 * srcMatrices are desired.
689 */
690 virtual void drawRect(const GrRect& rect,
691 const GrMatrix* matrix,
692 int stageEnableMask,
693 const GrRect* srcRects[],
694 const GrMatrix* srcMatrices[]);
695
696 /**
697 * Helper for drawRect when the caller doesn't need separate src rects or
698 * matrices.
699 */
700 void drawSimpleRect(const GrRect& rect,
701 const GrMatrix* matrix,
702 int stageEnableMask) {
703 drawRect(rect, matrix, stageEnableMask, NULL, NULL);
704 }
705
reed@google.comac10a2d2010-12-22 21:39:39 +0000706 ///////////////////////////////////////////////////////////////////////////
707
708 class AutoStateRestore : ::GrNoncopyable {
709 public:
710 AutoStateRestore(GrDrawTarget* target);
711 ~AutoStateRestore();
712
713 private:
714 GrDrawTarget* fDrawTarget;
715 SavedDrawState fDrawState;
716 };
717
718 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000719
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000720 class AutoViewMatrixRestore : ::GrNoncopyable {
721 public:
722 AutoViewMatrixRestore() {
723 fDrawTarget = NULL;
724 }
725
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000726 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000727 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
728 GrAssert(NULL != target);
729 }
730
731 void set(GrDrawTarget* target) {
732 GrAssert(NULL != target);
733 if (NULL != fDrawTarget) {
734 fDrawTarget->setViewMatrix(fMatrix);
735 }
736 fDrawTarget = target;
737 fMatrix = target->getViewMatrix();
738 }
739
740 ~AutoViewMatrixRestore() {
741 if (NULL != fDrawTarget) {
742 fDrawTarget->setViewMatrix(fMatrix);
743 }
744 }
745
746 private:
747 GrDrawTarget* fDrawTarget;
748 GrMatrix fMatrix;
749 };
750
751 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000752
753 class AutoReleaseGeometry : ::GrNoncopyable {
754 public:
755 AutoReleaseGeometry(GrDrawTarget* target,
756 GrVertexLayout vertexLayout,
757 uint32_t vertexCount,
758 uint32_t indexCount) {
759 fTarget = target;
760 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
761 vertexCount,
762 indexCount,
763 &fVertices,
764 &fIndices);
765 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000766
767 AutoReleaseGeometry() {
768 fSuccess = false;
769 }
770
reed@google.comac10a2d2010-12-22 21:39:39 +0000771 ~AutoReleaseGeometry() {
772 if (fSuccess) {
773 fTarget->releaseReservedGeometry();
774 }
775 }
776
bsalomon@google.com5782d712011-01-21 21:03:59 +0000777 bool set(GrDrawTarget* target,
778 GrVertexLayout vertexLayout,
779 uint32_t vertexCount,
780 uint32_t indexCount) {
781 if (fSuccess) {
782 fTarget->releaseReservedGeometry();
783 }
784 fTarget = target;
785 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
786 vertexCount,
787 indexCount,
788 &fVertices,
789 &fIndices);
790 return fSuccess;
791 }
792
reed@google.comac10a2d2010-12-22 21:39:39 +0000793 bool succeeded() const { return fSuccess; }
794 void* vertices() const { return fVertices; }
795 void* indices() const { return fIndices; }
796
797 GrPoint* positions() const {
798 return static_cast<GrPoint*>(fVertices);
799 }
800
801 private:
802 GrDrawTarget* fTarget;
803 bool fSuccess;
804 void* fVertices;
805 void* fIndices;
806 };
807
808 ///////////////////////////////////////////////////////////////////////////
809
810 class AutoClipRestore : ::GrNoncopyable {
811 public:
812 AutoClipRestore(GrDrawTarget* target) {
813 fTarget = target;
814 fClip = fTarget->getClip();
815 }
816
817 ~AutoClipRestore() {
818 fTarget->setClip(fClip);
819 }
820 private:
821 GrDrawTarget* fTarget;
822 GrClip fClip;
823 };
824
825 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000826 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000827
reed@google.comac10a2d2010-12-22 21:39:39 +0000828 /**
829 * Helper function to compute the size of a vertex from a vertex layout
830 * @return size of a single vertex.
831 */
832 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000833
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000834 /**
835 * Helper function for determining the index of texture coordinates that
836 * is input for a texture stage. Note that a stage may instead use positions
837 * as texture coordinates, in which case the result of the function is
838 * indistinguishable from the case when the stage is disabled.
839 *
840 * @param stage the stage to query
841 * @param vertexLayout layout to query
842 *
843 * @return the texture coordinate index or -1 if the stage doesn't use
844 * separate (non-position) texture coordinates.
845 */
846 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000847
848 /**
849 * Helper function to compute the offset of texture coordinates in a vertex
850 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000851 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000852 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000853 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000854 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000855
856 /**
857 * Helper function to compute the offset of the color in a vertex
858 * @return offset of color in vertex layout or -1 if the
859 * layout has no color.
860 */
861 static int VertexColorOffset(GrVertexLayout vertexLayout);
862
863 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000864 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000865 * coordinates of some index.
866 *
867 * @param coordIndex the tex coord index to query
868 * @param vertexLayout layout to query
869 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000870 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000871 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000872 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000873 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000874 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000875
reed@google.comac10a2d2010-12-22 21:39:39 +0000876 /**
877 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000878 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000879 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000880 * @param stage the stage to query
881 * @param vertexLayout layout to query
882 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000883 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000884 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000885 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000886 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000887
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000888 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000889 * Helper function to compute the size of each vertex and the offsets of
890 * texture coordinates and color. Determines tex coord offsets by tex coord
891 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000892 * by StageTexCoordVertexLayoutBit.)
893 *
894 * @param vertexLayout the layout to query
895 * @param texCoordOffsetsByIdx after return it is the offset of each
896 * tex coord index in the vertex or -1 if
897 * index isn't used.
898 * @return size of a single vertex
899 */
900 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
901 int texCoordOffsetsByIdx[kMaxTexCoords],
902 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000903
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000904 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000905 * Helper function to compute the size of each vertex and the offsets of
906 * texture coordinates and color. Determines tex coord offsets by stage
907 * rather than by index. (Each stage can be mapped to any t.c. index
908 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000909 * tex coords then that stage's offset will be 0 (positions are always at 0).
910 *
911 * @param vertexLayout the layout to query
912 * @param texCoordOffsetsByStage after return it is the offset of each
913 * tex coord index in the vertex or -1 if
914 * index isn't used.
915 * @return size of a single vertex
916 */
917 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
918 int texCoordOffsetsByStage[kNumStages],
919 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000920
921 /**
922 * Accessing positions, texture coords, or colors, of a vertex within an
923 * array is a hassle involving casts and simple math. These helpers exist
924 * to keep GrDrawTarget clients' code a bit nicer looking.
925 */
926
927 /**
928 * Gets a pointer to a GrPoint of a vertex's position or texture
929 * coordinate.
930 * @param vertices the vetex array
931 * @param vertexIndex the index of the vertex in the array
932 * @param vertexSize the size of each vertex in the array
933 * @param offset the offset in bytes of the vertex component.
934 * Defaults to zero (corresponding to vertex position)
935 * @return pointer to the vertex component as a GrPoint
936 */
937 static GrPoint* GetVertexPoint(void* vertices,
938 int vertexIndex,
939 int vertexSize,
940 int offset = 0) {
941 intptr_t start = GrTCast<intptr_t>(vertices);
942 return GrTCast<GrPoint*>(start + offset +
943 vertexIndex * vertexSize);
944 }
945 static const GrPoint* GetVertexPoint(const void* vertices,
946 int vertexIndex,
947 int vertexSize,
948 int offset = 0) {
949 intptr_t start = GrTCast<intptr_t>(vertices);
950 return GrTCast<const GrPoint*>(start + offset +
951 vertexIndex * vertexSize);
952 }
953
954 /**
955 * Gets a pointer to a GrColor inside a vertex within a vertex array.
956 * @param vertices the vetex array
957 * @param vertexIndex the index of the vertex in the array
958 * @param vertexSize the size of each vertex in the array
959 * @param offset the offset in bytes of the vertex color
960 * @return pointer to the vertex component as a GrColor
961 */
962 static GrColor* GetVertexColor(void* vertices,
963 int vertexIndex,
964 int vertexSize,
965 int offset) {
966 intptr_t start = GrTCast<intptr_t>(vertices);
967 return GrTCast<GrColor*>(start + offset +
968 vertexIndex * vertexSize);
969 }
970 static const GrColor* GetVertexColor(const void* vertices,
971 int vertexIndex,
972 int vertexSize,
973 int offset) {
974 const intptr_t start = GrTCast<intptr_t>(vertices);
975 return GrTCast<const GrColor*>(start + offset +
976 vertexIndex * vertexSize);
977 }
978
reed@google.comac10a2d2010-12-22 21:39:39 +0000979protected:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000980
reed@google.comac10a2d2010-12-22 21:39:39 +0000981 // Helpers for GrDrawTarget subclasses that won't have private access to
982 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +0000983 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +0000985 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000986 { return sds.fState; }
987
988 // implemented by subclass
989 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
990 void** vertices,
991 void** indices) = 0;
992
993 virtual void releaseGeometryHelper() = 0;
994
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000995 // subclass overrides to be notified when clip is set.
996 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000997
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000998 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
999 int vertexCount) = 0;
1000
1001 virtual void setIndexSourceToArrayHelper(const void* indexArray,
1002 int indexCount) = 0;
1003
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001004 // Helpers for drawRect, protected so subclasses that override drawRect
1005 // can use them.
1006 static GrVertexLayout GetRectVertexLayout(int stageEnableMask,
1007 const GrRect* srcRects[]);
1008
1009 static void SetRectVertices(const GrRect& rect,
1010 const GrMatrix* matrix,
1011 const GrRect* srcRects[],
1012 const GrMatrix* srcMatrices[],
1013 GrVertexLayout layout,
1014 void* vertices);
1015
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 enum GeometrySrcType {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001017 kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry
1018 kArray_GeometrySrcType, // src was set using set*SourceToArray
1019 kBuffer_GeometrySrcType // src was set using set*SourceToBuffer
reed@google.comac10a2d2010-12-22 21:39:39 +00001020 };
1021
1022 struct {
1023 bool fLocked;
1024 uint32_t fVertexCount;
1025 uint32_t fIndexCount;
1026 } fReservedGeometry;
1027
1028 struct GeometrySrc {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001029 GeometrySrcType fVertexSrc;
1030 const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer
1031 GeometrySrcType fIndexSrc;
1032 const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer
1033 GrVertexLayout fVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +00001034 } fGeometrySrc;
1035
1036 GrClip fClip;
1037
reed@google.com8195f672011-01-12 18:14:28 +00001038 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001039
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001040 // Not meant for external use. Only setVertexSourceToBuffer and
1041 // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't
1042 // support nested reserveAndLockGeometry (and cpu arrays internally use the
1043 // same path).
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 class AutoGeometrySrcRestore {
1045 public:
1046 AutoGeometrySrcRestore(GrDrawTarget* target) {
1047 fTarget = target;
1048 fGeometrySrc = fTarget->fGeometrySrc;
1049 }
1050 ~AutoGeometrySrcRestore() {
1051 fTarget->fGeometrySrc = fGeometrySrc;
1052 }
1053 private:
1054 GrDrawTarget *fTarget;
1055 GeometrySrc fGeometrySrc;
1056
1057 AutoGeometrySrcRestore();
1058 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
1059 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
1060 };
1061
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001062private:
1063 void VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +00001064};
1065
1066#endif