blob: ec84cdaa65a780081f9d540d85980b204c06bf34 [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
21#include "GrScalar.h"
22#include "GrMatrix.h"
23#include "GrColor.h"
24#include "GrRefCnt.h"
25#include "GrSamplerState.h"
26#include "GrClip.h"
27
28class GrTexture;
29class GrRenderTarget;
30class GrClipIterator;
31class GrVertexBuffer;
32class GrIndexBuffer;
33
34class GrDrawTarget : public GrRefCnt {
35public:
36 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000037 * Number of texture stages. Each stage takes as input a color and
38 * 2D texture coordinates. The color input to the first enabled stage is the
39 * per-vertex color or the constant color (setColor/setAlpha) if there are
40 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000041 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000042 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000043 * lookups are specified by a texture (setTexture), a texture matrix
bsalomon@google.com5782d712011-01-21 21:03:59 +000044 * (setTextureMatrix), and a sampler (setSamplerState). Texture coordinates
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000045 * for each stage come from the vertices based on a GrVertexLayout bitfield.
46 * The output fragment color is the output color of the last enabled stage.
bsalomon@google.com5782d712011-01-21 21:03:59 +000047 * The presence or absence of texture coordinates for each stage in the
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000048 * vertex layout indicates whether a stage is enabled or not.
49 */
bsalomon@google.com5782d712011-01-21 21:03:59 +000050
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000051 // Currently there is just one stage but this will be changed soon.
52 enum {
53 kNumStages = 1,
54 kMaxTexCoords = kNumStages
55 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000056
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000057 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000058 * Geometric primitives used for drawing.
59 */
60 enum PrimitiveType {
61 kTriangles_PrimitiveType,
62 kTriangleStrip_PrimitiveType,
63 kTriangleFan_PrimitiveType,
64 kPoints_PrimitiveType,
65 kLines_PrimitiveType,
66 kLineStrip_PrimitiveType
67 };
68
69 /**
70 * Flags that affect rendering. Controlled using enable/disableState(). All
71 * default to disabled.
72 */
73 enum StateBits {
74 kDither_StateBit = 0x1,//<! Perform color dithering
75 kAntialias_StateBit = 0x2,//<! Perform anti-aliasing. The render-
76 // target must support some form of AA
77 // (msaa, coverage sampling, etc). For
78 // GrGpu-created rendertarget/textures
79 // this is controlled by parameters
80 // passed to createTexture.
81 kClip_StateBit = 0x4,//<! Controls whether drawing is clipped
82 // against the region specified by
83 // setClip.
84 };
85
86 /**
87 * Coeffecients for alpha-blending.
88 */
89 enum BlendCoeff {
90 kZero_BlendCoeff, //<! 0
91 kOne_BlendCoeff, //<! 1
92 kSC_BlendCoeff, //<! src color
93 kISC_BlendCoeff, //<! one minus src color
94 kDC_BlendCoeff, //<! dst color
95 kIDC_BlendCoeff, //<! one minus dst color
96 kSA_BlendCoeff, //<! src alpha
97 kISA_BlendCoeff, //<! one minus src alpha
98 kDA_BlendCoeff, //<! dst alpha
99 kIDA_BlendCoeff, //<! one minus dst alpha
100 };
101
102 /**
103 * StencilPass
104 *
105 * Sets the stencil state for subsequent draw calls. Used to fill paths.
106 *
107 * Winding requires two passes when the GPU/API doesn't support separate
108 * stencil.
109 *
110 * The color pass for path fill is used to zero out stencil bits used for
111 * path filling. Every pixel covere by a winding/EO stencil pass must get
112 * covered by the color pass in order to leave stencil buffer in the correct
113 * state for the next path draw.
114 *
115 * NOTE: Stencil-based Winding fill has alias-to-zero problems. (e.g. A
116 * winding count of 128,256,512,etc with a 8 bit stencil buffer
117 * will be unfilled)
118 */
119 enum StencilPass {
120 kNone_StencilPass, //<! Not drawing a path or clip.
121 kEvenOddStencil_StencilPass, //<! records in/out in stencil buffer
122 // using the Even/Odd fill rule.
123 kEvenOddColor_StencilPass, //<! writes colors to color target in
124 // pixels marked inside the fill by
125 // kEOFillStencil_StencilPass. Clears
126 // stencil in pixels covered by
127 // geometry.
128 kWindingStencil1_StencilPass, //<! records in/out in stencil buffer
129 // using the Winding fill rule.
130 kWindingStencil2_StencilPass, //<! records in/out in stencil buffer
131 // using the Winding fill rule.
132 // Run when single-stencil-pass winding
133 // not supported (i.e. no separate
134 // stencil support)
135 kWindingColor_StencilPass, //<! writes colors to color target in
136 // pixels marked inside the fill by
137 // kWindFillStencil_StencilPass. Clears
138 // stencil in pixels covered by
139 // geometry.
140 kDrawTargetCount_StencilPass //<! Subclass may extend this enum to use
141 // the stencil for other purposes (e.g.
142 // to do stencil-based clipping)
143 // This value is provided as basis for
144 // defining these extended enum values.
145 };
146
147protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000148
reed@google.com8195f672011-01-12 18:14:28 +0000149 struct DrState {
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 uint32_t fFlagBits;
151 BlendCoeff fSrcBlend;
152 BlendCoeff fDstBlend;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000153 GrTexture* fTextures[kNumStages];
154 GrSamplerState fSamplerStates[kNumStages];
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 GrRenderTarget* fRenderTarget;
156 GrColor fColor;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 StencilPass fStencilPass;
158 bool fReverseFill;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000159 GrMatrix fViewMatrix;
160 GrMatrix fTextureMatrices[kNumStages];
reed@google.com8195f672011-01-12 18:14:28 +0000161 bool operator ==(const DrState& s) const {
162 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 }
reed@google.com8195f672011-01-12 18:14:28 +0000164 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 };
166
167public:
168 ///////////////////////////////////////////////////////////////////////////
169
170 GrDrawTarget();
171
172 /**
173 * Sets the current clip to the region specified by clip. All draws will be
174 * clipped against this clip if kClip_StateBit is enabled.
175 *
176 * @param description of the clipping region
177 */
178 void setClip(const GrClip& clip);
179
180 /**
181 * Gets the current clip.
182 *
183 * @return the clip.
184 */
185 const GrClip& getClip() const;
186
187 /**
188 * Sets the texture used at the next drawing call
189 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000190 * @param stage The texture stage for which the texture will be set
191 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 * @param texture The texture to set. Can be NULL though there is no advantage
193 * to settings a NULL texture if doing non-textured drawing
194 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000195 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000196
197 /**
198 * Retrieves the currently set texture.
199 *
200 * @return The currently set texture. The return value will be NULL if no
201 * texture has been set, NULL was most recently passed to
202 * setTexture, or the last setTexture was destroyed.
203 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000204 const GrTexture* getTexture(int stage) const;
205 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
207 /**
208 * Sets the rendertarget used at the next drawing call
209 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000210 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 */
212 void setRenderTarget(GrRenderTarget* target);
213
214 /**
215 * Retrieves the currently set rendertarget.
216 *
217 * @return The currently set render target.
218 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219 const GrRenderTarget* getRenderTarget() const;
220 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 /**
223 * Sets the sampler state for the next draw.
224 *
225 * The sampler state determines the address wrap modes and
226 * filtering
227 *
228 * @param samplerState Specifies the sampler state.
229 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000230 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
232 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000233 * Sets the matrix applied to texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 *
235 * The post-matrix texture coordinates in the square [0,1]^2 cover the
236 * entire area of the texture. This means the full POT width when a NPOT
237 * texture is embedded in a POT width texture to meet the 3D API
238 * requirements. The texture matrix is applied both when the texture
239 * coordinates are explicit and when vertex positions are used as texture
240 * coordinates. In the latter case the texture matrix is applied to the
241 * pre-modelview position values.
242 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000243 * @param stage the stage for which to set a matrix.
244 * @param m the matrix used to transform the texture coordinates.
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000246 void setTextureMatrix(int stage, const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
248 /**
249 * Sets the matrix applied to veretx positions.
250 *
251 * In the post-view-matrix space the rectangle [0,w]x[0,h]
252 * fully covers the render target. (w and h are the width and height of the
253 * the rendertarget.)
254 *
255 * @param m the matrix used to transform the vertex positions.
256 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000257 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000258
259 /**
260 * Multiplies the current view matrix by a matrix
261 *
262 * After this call V' = V*m where V is the old view matrix,
263 * m is the parameter to this function, and V' is the new view matrix.
264 * (We consider positions to be column vectors so position vector p is
265 * transformed by matrix X as p' = X*p.)
266 *
267 * @param m the matrix used to modify the modelview matrix.
268 */
269 void concatViewMatrix(const GrMatrix& m);
270
271 /**
272 * Sets color for next draw to a premultiplied-alpha color.
273 *
274 * @param the color to set.
275 */
276 void setColor(GrColor);
277
278 /**
279 * Sets the color to be used for the next draw to be
280 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
281 *
282 * @param alpha The alpha value to set as the color.
283 */
284 void setAlpha(uint8_t alpha);
285
286 /**
287 * Sets pass for path rendering
288 *
289 * @param pass of path rendering
290 */
291 void setStencilPass(StencilPass pass);
292
293 /**
294 * Reveses the in/out decision of the fill rule for path rendering.
295 * Only affects kEOFillColor_StencilPass and kWindingFillColor_StencilPass
296 *
297 * @param reverse true to reverse, false otherwise
298 */
299 void setReverseFill(bool reverse);
300
301 /**
302 * Enable render state settings.
303 *
304 * @param flags bitfield of StateBits specifing the states to enable
305 */
306 void enableState(uint32_t stateBits);
307
308 /**
309 * Disable render state settings.
310 *
311 * @param flags bitfield of StateBits specifing the states to disable
312 */
313 void disableState(uint32_t stateBits);
314
315 bool isDitherState() const {
316 return fCurrDrawState.fFlagBits & kDither_StateBit;
317 }
318
319 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 * Sets the blending function coeffecients.
321 *
322 * The blend function will be:
323 * D' = sat(S*srcCoef + D*dstCoef)
324 *
325 * where D is the existing destination color, S is the incoming source
326 * color, and D' is the new destination color that will be written. sat()
327 * is the saturation function.
328 *
329 * @param srcCoef coeffecient applied to the src color.
330 * @param dstCoef coeffecient applied to the dst color.
331 */
332 void setBlendFunc(BlendCoeff srcCoef, BlendCoeff dstCoef);
333
334 /**
335 * Retrieves the current view matrix
bsalomon@google.com5782d712011-01-21 21:03:59 +0000336 * return the current view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000338 const GrMatrix& getViewMatrix() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000339
340 /**
341 * Retrieves the inverse of the current view matrix.
342 *
343 * If the current view matrix is invertible, return true, and if matrix
344 * is non-null, copy the inverse into it. If the current view matrix is
345 * non-invertible, return false and ignore the matrix parameter.
346 *
347 * @param matrix if not null, will receive a copy of the current inverse.
348 */
349 bool getViewInverse(GrMatrix* matrix) const;
350
351 /**
352 * Used to save and restore the GrGpu's drawing state
353 */
354 struct SavedDrawState {
355 private:
reed@google.com8195f672011-01-12 18:14:28 +0000356 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000357 friend class GrDrawTarget;
358 };
359
360 /**
361 * Saves the current draw state. The state can be restored at a later time
362 * with restoreDrawState.
363 *
364 * See also AutoStateRestore class.
365 *
366 * @param state will hold the state after the function returns.
367 */
368 void saveCurrentDrawState(SavedDrawState* state) const;
369
370 /**
371 * Restores previously saved draw state. The client guarantees that state
372 * was previously passed to saveCurrentDrawState and that the rendertarget
373 * and texture set at save are still valid.
374 *
375 * See also AutoStateRestore class.
376 *
377 * @param state the previously saved state to restore.
378 */
379 void restoreDrawState(const SavedDrawState& state);
380
381 /**
382 * Copies the draw state from another target to this target.
383 *
384 * @param srcTarget draw target used as src of the draw state.
385 */
386 void copyDrawState(const GrDrawTarget& srcTarget);
387
388 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000389 * The format of vertices is represented as a bitfield of flags.
390 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000391 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000392 * coordinates and per-vertex colors. Each stage can use any of the texture
393 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000395 * If no texture coordinates are specified for a stage then the stage is
396 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000398 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000399 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000400 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000402 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000403 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000404 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000405 * the order would be (position, texture coordinate 1[, color]).
406 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 /**
409 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000410 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000411 * @param stage the stage that will use texture coordinates.
412 * @param texCoordIdx the index of the texture coordinates to use
413 *
414 * @return the bit to add to a GrVertexLayout bitfield.
415 */
416 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
417 GrAssert(stage < kNumStages);
418 GrAssert(texCoordIdx < kMaxTexCoords);
419 return 1 << (stage + (texCoordIdx * kNumStages));
420 }
421private:
422 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
423public:
424 /**
425 * Generates a bit indicating that a texture stage uses the position
426 * as its texture coordinate.
427 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000428 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000429 * coordinates.
430 *
431 * @return the bit to add to a GrVertexLayout bitfield.
432 */
433 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
434 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000436 }
437private:
438 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000439
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000440public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000442 /**
443 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000444 */
445 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000447 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
448 //<! vertices have colors
449 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
450 //<! use text vertices. (Pos
451 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000452 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000453 // text [GrGpuTextVertex vs
454 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000455 // for below assert
456 kDummy,
457 kHighVertexLayoutBit = kDummy - 1
458 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000459 // make sure we haven't exceeded the number of bits in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout)));
461
462 /**
463 * Reserves space for vertices and/or indices. Draw target will use
464 * reserved vertices / indices at next draw.
465 *
466 * If succeeds:
467 * if vertexCount is nonzero, *vertices will be the array
468 * of vertices to be filled by caller. The next draw will read
469 * these vertices.
470 *
471 * if indecCount is nonzero, *indices will be the array of indices
472 * to be filled by caller. The next indexed draw will read from
473 * these indices.
474 *
475 * If a client does not already have a vertex buffer or cpu arrays then this
476 * is the preferred way to allocate vertex/index array. It allows the
477 * subclass of GrDrawTarget to decide whether to put data in buffers, to
478 * group vertex data that uses the same state (e.g. for deferred rendering),
479 * etc.
480 *
481 * This must be matched with a releaseReservedGeometry call after all
482 * draws that reference the reserved geometry data have been called.
483 *
484 * AutoGeometryRelease can be used to automatically call the release.
485 *
486 * @param vertexCount the number of vertices to reserve space for. Can be 0.
487 * @param indexCount the number of indices to reserve space for. Can be 0.
488 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
489 * @param vertices will point to reserved vertex space if vertexCount is
490 * non-zero. Illegal to pass NULL if vertexCount > 0.
491 * @param indices will point to reserved index space if indexCount is
492 * non-zero. Illegal to pass NULL if indexCount > 0.
493 *
494 * @return true if succeeded in allocating space for the vertices and false
495 * if not.
496 */
497 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
498 uint32_t vertexCount,
499 uint32_t indexCount,
500 void** vertices,
501 void** indices);
502 /**
503 * Provides hints to caller about the number of vertices and indices
504 * that can be allocated cheaply. This can be useful if caller is reserving
505 * space but doesn't know exactly how much geometry is needed.
506 *
507 * Also may hint whether the draw target should be flushed first. This is
508 * useful for deferred targets.
509 *
510 * @param vertexLayout layout of vertices caller would like to reserve
511 * @param vertexCount in: hint about how many vertices the caller would
512 * like to allocate.
513 * out: a hint about the number of vertices that can be
514 * allocated cheaply. Negative means no hint.
515 * Ignored if NULL.
516 * @param indexCount in: hint about how many indices the caller would
517 * like to allocate.
518 * out: a hint about the number of indices that can be
519 * allocated cheaply. Negative means no hint.
520 * Ignored if NULL.
521 *
522 * @return true if target should be flushed based on the input values.
523 */
524 virtual bool geometryHints(GrVertexLayout vertexLayout,
525 int32_t* vertexCount,
526 int32_t* indexCount) const;
527
528 /**
529 * Releases reserved vertex/index data from reserveAndLockGeometry().
530 */
531 void releaseReservedGeometry();
532
533 /**
534 * Sets source of vertex data for the next draw. Data does not have to be
535 * in the array until drawIndexed or drawNonIndexed.
536 *
537 * @param array cpu array containing vertex data.
538 * @param vertexLayout layout of the vertex data in the array.
539 */
540 void setVertexSourceToArray(const void* array, GrVertexLayout vertexLayout);
541
542 /**
543 * Sets source of index data for the next indexed draw. Data does not have
544 * to be in the array until drawIndexed or drawNonIndexed.
545 *
546 * @param array cpu array containing index data.
547 */
548 void setIndexSourceToArray(const void* array);
549
550 /**
551 * Sets source of vertex data for the next draw. Data does not have to be
552 * in the buffer until drawIndexed or drawNonIndexed.
553 *
554 * @param buffer vertex buffer containing vertex data. Must be
555 * unlocked before draw call.
556 * @param vertexLayout layout of the vertex data in the buffer.
557 */
558 void setVertexSourceToBuffer(const GrVertexBuffer* buffer,
559 GrVertexLayout vertexLayout);
560
561 /**
562 * Sets source of index data for the next indexed draw. Data does not have
563 * to be in the buffer until drawIndexed or drawNonIndexed.
564 *
565 * @param buffer index buffer containing indices. Must be unlocked
566 * before indexed draw call.
567 */
568 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
569
570 /**
571 * Draws indexed geometry using the current state and current vertex / index
572 * sources.
573 *
574 * @param type The type of primitives to draw.
575 * @param startVertex the vertex in the vertex array/buffer corresponding
576 * to index 0
577 * @param startIndex first index to read from index src.
578 * @param vertexCount one greater than the max index.
579 * @param indexCount the number of index elements to read. The index count
580 * is effectively trimmed to the last completely
581 * specified primitive.
582 */
583 virtual void drawIndexed(PrimitiveType type,
584 uint32_t startVertex,
585 uint32_t startIndex,
586 uint32_t vertexCount,
587 uint32_t indexCount) = 0;
588
589 /**
590 * Draws non-indexed geometry using the current state and current vertex
591 * sources.
592 *
593 * @param type The type of primitives to draw.
594 * @param startVertex the vertex in the vertex array/buffer corresponding
595 * to index 0
596 * @param vertexCount one greater than the max index.
597 */
598 virtual void drawNonIndexed(PrimitiveType type,
599 uint32_t startVertex,
600 uint32_t vertexCount) = 0;
601
602 ///////////////////////////////////////////////////////////////////////////
603
604 class AutoStateRestore : ::GrNoncopyable {
605 public:
606 AutoStateRestore(GrDrawTarget* target);
607 ~AutoStateRestore();
608
609 private:
610 GrDrawTarget* fDrawTarget;
611 SavedDrawState fDrawState;
612 };
613
614 ///////////////////////////////////////////////////////////////////////////
615
616 class AutoReleaseGeometry : ::GrNoncopyable {
617 public:
618 AutoReleaseGeometry(GrDrawTarget* target,
619 GrVertexLayout vertexLayout,
620 uint32_t vertexCount,
621 uint32_t indexCount) {
622 fTarget = target;
623 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
624 vertexCount,
625 indexCount,
626 &fVertices,
627 &fIndices);
628 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000629
630 AutoReleaseGeometry() {
631 fSuccess = false;
632 }
633
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 ~AutoReleaseGeometry() {
635 if (fSuccess) {
636 fTarget->releaseReservedGeometry();
637 }
638 }
639
bsalomon@google.com5782d712011-01-21 21:03:59 +0000640 bool set(GrDrawTarget* target,
641 GrVertexLayout vertexLayout,
642 uint32_t vertexCount,
643 uint32_t indexCount) {
644 if (fSuccess) {
645 fTarget->releaseReservedGeometry();
646 }
647 fTarget = target;
648 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
649 vertexCount,
650 indexCount,
651 &fVertices,
652 &fIndices);
653 return fSuccess;
654 }
655
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 bool succeeded() const { return fSuccess; }
657 void* vertices() const { return fVertices; }
658 void* indices() const { return fIndices; }
659
660 GrPoint* positions() const {
661 return static_cast<GrPoint*>(fVertices);
662 }
663
664 private:
665 GrDrawTarget* fTarget;
666 bool fSuccess;
667 void* fVertices;
668 void* fIndices;
669 };
670
671 ///////////////////////////////////////////////////////////////////////////
672
673 class AutoClipRestore : ::GrNoncopyable {
674 public:
675 AutoClipRestore(GrDrawTarget* target) {
676 fTarget = target;
677 fClip = fTarget->getClip();
678 }
679
680 ~AutoClipRestore() {
681 fTarget->setClip(fClip);
682 }
683 private:
684 GrDrawTarget* fTarget;
685 GrClip fClip;
686 };
687
688 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000689 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690
reed@google.comac10a2d2010-12-22 21:39:39 +0000691 /**
692 * Helper function to compute the size of a vertex from a vertex layout
693 * @return size of a single vertex.
694 */
695 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000696
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000697 /**
698 * Helper function for determining the index of texture coordinates that
699 * is input for a texture stage. Note that a stage may instead use positions
700 * as texture coordinates, in which case the result of the function is
701 * indistinguishable from the case when the stage is disabled.
702 *
703 * @param stage the stage to query
704 * @param vertexLayout layout to query
705 *
706 * @return the texture coordinate index or -1 if the stage doesn't use
707 * separate (non-position) texture coordinates.
708 */
709 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000710
711 /**
712 * Helper function to compute the offset of texture coordinates in a vertex
713 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000714 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000715 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000716 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000717 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000718
719 /**
720 * Helper function to compute the offset of the color in a vertex
721 * @return offset of color in vertex layout or -1 if the
722 * layout has no color.
723 */
724 static int VertexColorOffset(GrVertexLayout vertexLayout);
725
726 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000728 * coordinates of some index.
729 *
730 * @param coordIndex the tex coord index to query
731 * @param vertexLayout layout to query
732 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000733 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000734 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000736 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000737 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000738
reed@google.comac10a2d2010-12-22 21:39:39 +0000739 /**
740 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000741 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000742 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000743 * @param stage the stage to query
744 * @param vertexLayout layout to query
745 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000746 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000747 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000748 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000749 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000750
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000751 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000752 * Helper function to compute the size of each vertex and the offsets of
753 * texture coordinates and color. Determines tex coord offsets by tex coord
754 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000755 * by StageTexCoordVertexLayoutBit.)
756 *
757 * @param vertexLayout the layout to query
758 * @param texCoordOffsetsByIdx after return it is the offset of each
759 * tex coord index in the vertex or -1 if
760 * index isn't used.
761 * @return size of a single vertex
762 */
763 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
764 int texCoordOffsetsByIdx[kMaxTexCoords],
765 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000766
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000767 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000768 * Helper function to compute the size of each vertex and the offsets of
769 * texture coordinates and color. Determines tex coord offsets by stage
770 * rather than by index. (Each stage can be mapped to any t.c. index
771 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000772 * tex coords then that stage's offset will be 0 (positions are always at 0).
773 *
774 * @param vertexLayout the layout to query
775 * @param texCoordOffsetsByStage after return it is the offset of each
776 * tex coord index in the vertex or -1 if
777 * index isn't used.
778 * @return size of a single vertex
779 */
780 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
781 int texCoordOffsetsByStage[kNumStages],
782 int *colorOffset);
reed@google.comac10a2d2010-12-22 21:39:39 +0000783protected:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000784
reed@google.comac10a2d2010-12-22 21:39:39 +0000785 // Helpers for GrDrawTarget subclasses that won't have private access to
786 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +0000787 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000788 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +0000789 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000790 { return sds.fState; }
791
792 // implemented by subclass
793 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
794 void** vertices,
795 void** indices) = 0;
796
797 virtual void releaseGeometryHelper() = 0;
798
799 virtual void clipWillChange(const GrClip& clip) = 0;
800
801 enum GeometrySrcType {
802 kArray_GeometrySrcType,
803 kReserved_GeometrySrcType,
804 kBuffer_GeometrySrcType
805 };
806
807 struct {
808 bool fLocked;
809 uint32_t fVertexCount;
810 uint32_t fIndexCount;
811 } fReservedGeometry;
812
813 struct GeometrySrc {
814 GeometrySrcType fVertexSrc;
815 union {
816 const GrVertexBuffer* fVertexBuffer;
817 const void* fVertexArray;
818 };
819 GeometrySrcType fIndexSrc;
820 union {
821 const GrIndexBuffer* fIndexBuffer;
822 const void* fIndexArray;
823 };
824 GrVertexLayout fVertexLayout;
825 } fGeometrySrc;
826
827 GrClip fClip;
828
reed@google.com8195f672011-01-12 18:14:28 +0000829 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000830
reed@google.comac10a2d2010-12-22 21:39:39 +0000831 // not meant for outside usage. Could cause problems if calls between
832 // the save and restore mess with reserved geometry state.
833 class AutoGeometrySrcRestore {
834 public:
835 AutoGeometrySrcRestore(GrDrawTarget* target) {
836 fTarget = target;
837 fGeometrySrc = fTarget->fGeometrySrc;
838 }
839 ~AutoGeometrySrcRestore() {
840 fTarget->fGeometrySrc = fGeometrySrc;
841 }
842 private:
843 GrDrawTarget *fTarget;
844 GeometrySrc fGeometrySrc;
845
846 AutoGeometrySrcRestore();
847 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
848 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
849 };
850
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000851private:
852 void VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000853};
854
855#endif