blob: e7f37f167e994eb2feacc087992833a5b93a7133 [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 {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000053 kNumStages = 2,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000054 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
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000241 * pre-view-matrix position values.
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 *
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 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000249 * Multiplies the current texture matrix for a stage by a matrix
250 *
251 * After this call T' = T*m where T is the old tex matrix,
252 * m is the parameter to this function, and T' is the new tex matrix.
253 * (We consider positions to be column vectors so tex cood vector t is
254 * transformed by matrix X as t' = X*t.)
255 *
256 * @param m the matrix used to modify the texture matrix matrix.
257 */
258 void concatTextureMatrix(int stage, const GrMatrix& m);
259
260 /**
261 * Retrieves the current texture matrix for a stage
262 * @param stage index of stage
263 * @return the stage's current texture matrix.
264 */
265 const GrMatrix& getTextureMatrix(int stage) const;
266
267 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 * Sets the matrix applied to veretx positions.
269 *
270 * In the post-view-matrix space the rectangle [0,w]x[0,h]
271 * fully covers the render target. (w and h are the width and height of the
272 * the rendertarget.)
273 *
274 * @param m the matrix used to transform the vertex positions.
275 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000276 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000277
278 /**
279 * Multiplies the current view matrix by a matrix
280 *
281 * After this call V' = V*m where V is the old view matrix,
282 * m is the parameter to this function, and V' is the new view matrix.
283 * (We consider positions to be column vectors so position vector p is
284 * transformed by matrix X as p' = X*p.)
285 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000286 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 */
288 void concatViewMatrix(const GrMatrix& m);
289
290 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000291 * Retrieves the current view matrix
292 * @return the current view matrix.
293 */
294 const GrMatrix& getViewMatrix() const;
295
296 /**
297 * Retrieves the inverse of the current view matrix.
298 *
299 * If the current view matrix is invertible, return true, and if matrix
300 * is non-null, copy the inverse into it. If the current view matrix is
301 * non-invertible, return false and ignore the matrix parameter.
302 *
303 * @param matrix if not null, will receive a copy of the current inverse.
304 */
305 bool getViewInverse(GrMatrix* matrix) const;
306
307 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000308 * Sets color for next draw to a premultiplied-alpha color.
309 *
310 * @param the color to set.
311 */
312 void setColor(GrColor);
313
314 /**
315 * Sets the color to be used for the next draw to be
316 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
317 *
318 * @param alpha The alpha value to set as the color.
319 */
320 void setAlpha(uint8_t alpha);
321
322 /**
323 * Sets pass for path rendering
324 *
325 * @param pass of path rendering
326 */
327 void setStencilPass(StencilPass pass);
328
329 /**
330 * Reveses the in/out decision of the fill rule for path rendering.
331 * Only affects kEOFillColor_StencilPass and kWindingFillColor_StencilPass
332 *
333 * @param reverse true to reverse, false otherwise
334 */
335 void setReverseFill(bool reverse);
336
337 /**
338 * Enable render state settings.
339 *
340 * @param flags bitfield of StateBits specifing the states to enable
341 */
342 void enableState(uint32_t stateBits);
343
344 /**
345 * Disable render state settings.
346 *
347 * @param flags bitfield of StateBits specifing the states to disable
348 */
349 void disableState(uint32_t stateBits);
350
351 bool isDitherState() const {
352 return fCurrDrawState.fFlagBits & kDither_StateBit;
353 }
354
355 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000356 * Sets the blending function coeffecients.
357 *
358 * The blend function will be:
359 * D' = sat(S*srcCoef + D*dstCoef)
360 *
361 * where D is the existing destination color, S is the incoming source
362 * color, and D' is the new destination color that will be written. sat()
363 * is the saturation function.
364 *
365 * @param srcCoef coeffecient applied to the src color.
366 * @param dstCoef coeffecient applied to the dst color.
367 */
368 void setBlendFunc(BlendCoeff srcCoef, BlendCoeff dstCoef);
369
370 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 * Used to save and restore the GrGpu's drawing state
372 */
373 struct SavedDrawState {
374 private:
reed@google.com8195f672011-01-12 18:14:28 +0000375 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 friend class GrDrawTarget;
377 };
378
379 /**
380 * Saves the current draw state. The state can be restored at a later time
381 * with restoreDrawState.
382 *
383 * See also AutoStateRestore class.
384 *
385 * @param state will hold the state after the function returns.
386 */
387 void saveCurrentDrawState(SavedDrawState* state) const;
388
389 /**
390 * Restores previously saved draw state. The client guarantees that state
391 * was previously passed to saveCurrentDrawState and that the rendertarget
392 * and texture set at save are still valid.
393 *
394 * See also AutoStateRestore class.
395 *
396 * @param state the previously saved state to restore.
397 */
398 void restoreDrawState(const SavedDrawState& state);
399
400 /**
401 * Copies the draw state from another target to this target.
402 *
403 * @param srcTarget draw target used as src of the draw state.
404 */
405 void copyDrawState(const GrDrawTarget& srcTarget);
406
407 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 * The format of vertices is represented as a bitfield of flags.
409 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000410 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000411 * coordinates and per-vertex colors. Each stage can use any of the texture
412 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000414 * If no texture coordinates are specified for a stage then the stage is
415 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000416 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000417 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000419 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000421 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000422 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000423 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000424 * the order would be (position, texture coordinate 1[, color]).
425 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000426
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000427 /**
428 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000429 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000430 * @param stage the stage that will use texture coordinates.
431 * @param texCoordIdx the index of the texture coordinates to use
432 *
433 * @return the bit to add to a GrVertexLayout bitfield.
434 */
435 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
436 GrAssert(stage < kNumStages);
437 GrAssert(texCoordIdx < kMaxTexCoords);
438 return 1 << (stage + (texCoordIdx * kNumStages));
439 }
440private:
441 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
442public:
443 /**
444 * Generates a bit indicating that a texture stage uses the position
445 * as its texture coordinate.
446 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000448 * coordinates.
449 *
450 * @return the bit to add to a GrVertexLayout bitfield.
451 */
452 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
453 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000455 }
456private:
457 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000459public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000461 /**
462 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000463 */
464 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000466 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
467 //<! vertices have colors
468 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
469 //<! use text vertices. (Pos
470 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000471 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000472 // text [GrGpuTextVertex vs
473 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 // for below assert
475 kDummy,
476 kHighVertexLayoutBit = kDummy - 1
477 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000478 // make sure we haven't exceeded the number of bits in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout)));
480
481 /**
482 * Reserves space for vertices and/or indices. Draw target will use
483 * reserved vertices / indices at next draw.
484 *
485 * If succeeds:
486 * if vertexCount is nonzero, *vertices will be the array
487 * of vertices to be filled by caller. The next draw will read
488 * these vertices.
489 *
490 * if indecCount is nonzero, *indices will be the array of indices
491 * to be filled by caller. The next indexed draw will read from
492 * these indices.
493 *
494 * If a client does not already have a vertex buffer or cpu arrays then this
495 * is the preferred way to allocate vertex/index array. It allows the
496 * subclass of GrDrawTarget to decide whether to put data in buffers, to
497 * group vertex data that uses the same state (e.g. for deferred rendering),
498 * etc.
499 *
500 * This must be matched with a releaseReservedGeometry call after all
501 * draws that reference the reserved geometry data have been called.
502 *
503 * AutoGeometryRelease can be used to automatically call the release.
504 *
505 * @param vertexCount the number of vertices to reserve space for. Can be 0.
506 * @param indexCount the number of indices to reserve space for. Can be 0.
507 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
508 * @param vertices will point to reserved vertex space if vertexCount is
509 * non-zero. Illegal to pass NULL if vertexCount > 0.
510 * @param indices will point to reserved index space if indexCount is
511 * non-zero. Illegal to pass NULL if indexCount > 0.
512 *
513 * @return true if succeeded in allocating space for the vertices and false
514 * if not.
515 */
516 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
517 uint32_t vertexCount,
518 uint32_t indexCount,
519 void** vertices,
520 void** indices);
521 /**
522 * Provides hints to caller about the number of vertices and indices
523 * that can be allocated cheaply. This can be useful if caller is reserving
524 * space but doesn't know exactly how much geometry is needed.
525 *
526 * Also may hint whether the draw target should be flushed first. This is
527 * useful for deferred targets.
528 *
529 * @param vertexLayout layout of vertices caller would like to reserve
530 * @param vertexCount in: hint about how many vertices the caller would
531 * like to allocate.
532 * out: a hint about the number of vertices that can be
533 * allocated cheaply. Negative means no hint.
534 * Ignored if NULL.
535 * @param indexCount in: hint about how many indices the caller would
536 * like to allocate.
537 * out: a hint about the number of indices that can be
538 * allocated cheaply. Negative means no hint.
539 * Ignored if NULL.
540 *
541 * @return true if target should be flushed based on the input values.
542 */
543 virtual bool geometryHints(GrVertexLayout vertexLayout,
544 int32_t* vertexCount,
545 int32_t* indexCount) const;
546
547 /**
548 * Releases reserved vertex/index data from reserveAndLockGeometry().
549 */
550 void releaseReservedGeometry();
551
552 /**
553 * Sets source of vertex data for the next draw. Data does not have to be
554 * in the array until drawIndexed or drawNonIndexed.
555 *
556 * @param array cpu array containing vertex data.
557 * @param vertexLayout layout of the vertex data in the array.
558 */
559 void setVertexSourceToArray(const void* array, GrVertexLayout vertexLayout);
560
561 /**
562 * Sets source of index data for the next indexed draw. Data does not have
563 * to be in the array until drawIndexed or drawNonIndexed.
564 *
565 * @param array cpu array containing index data.
566 */
567 void setIndexSourceToArray(const void* array);
568
569 /**
570 * Sets source of vertex data for the next draw. Data does not have to be
571 * in the buffer until drawIndexed or drawNonIndexed.
572 *
573 * @param buffer vertex buffer containing vertex data. Must be
574 * unlocked before draw call.
575 * @param vertexLayout layout of the vertex data in the buffer.
576 */
577 void setVertexSourceToBuffer(const GrVertexBuffer* buffer,
578 GrVertexLayout vertexLayout);
579
580 /**
581 * Sets source of index data for the next indexed draw. Data does not have
582 * to be in the buffer until drawIndexed or drawNonIndexed.
583 *
584 * @param buffer index buffer containing indices. Must be unlocked
585 * before indexed draw call.
586 */
587 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
588
589 /**
590 * Draws indexed geometry using the current state and current vertex / index
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 startIndex first index to read from index src.
597 * @param vertexCount one greater than the max index.
598 * @param indexCount the number of index elements to read. The index count
599 * is effectively trimmed to the last completely
600 * specified primitive.
601 */
602 virtual void drawIndexed(PrimitiveType type,
603 uint32_t startVertex,
604 uint32_t startIndex,
605 uint32_t vertexCount,
606 uint32_t indexCount) = 0;
607
608 /**
609 * Draws non-indexed geometry using the current state and current vertex
610 * sources.
611 *
612 * @param type The type of primitives to draw.
613 * @param startVertex the vertex in the vertex array/buffer corresponding
614 * to index 0
615 * @param vertexCount one greater than the max index.
616 */
617 virtual void drawNonIndexed(PrimitiveType type,
618 uint32_t startVertex,
619 uint32_t vertexCount) = 0;
620
621 ///////////////////////////////////////////////////////////////////////////
622
623 class AutoStateRestore : ::GrNoncopyable {
624 public:
625 AutoStateRestore(GrDrawTarget* target);
626 ~AutoStateRestore();
627
628 private:
629 GrDrawTarget* fDrawTarget;
630 SavedDrawState fDrawState;
631 };
632
633 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000634
635 class AutoViewMatrixRestore : ::GrNoncopyable {
636 public:
637 AutoViewMatrixRestore() {
638 fDrawTarget = NULL;
639 }
640
641 AutoViewMatrixRestore(GrDrawTarget* target)
642 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
643 GrAssert(NULL != target);
644 }
645
646 void set(GrDrawTarget* target) {
647 GrAssert(NULL != target);
648 if (NULL != fDrawTarget) {
649 fDrawTarget->setViewMatrix(fMatrix);
650 }
651 fDrawTarget = target;
652 fMatrix = target->getViewMatrix();
653 }
654
655 ~AutoViewMatrixRestore() {
656 if (NULL != fDrawTarget) {
657 fDrawTarget->setViewMatrix(fMatrix);
658 }
659 }
660
661 private:
662 GrDrawTarget* fDrawTarget;
663 GrMatrix fMatrix;
664 };
665
666 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000667
668 class AutoReleaseGeometry : ::GrNoncopyable {
669 public:
670 AutoReleaseGeometry(GrDrawTarget* target,
671 GrVertexLayout vertexLayout,
672 uint32_t vertexCount,
673 uint32_t indexCount) {
674 fTarget = target;
675 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
676 vertexCount,
677 indexCount,
678 &fVertices,
679 &fIndices);
680 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681
682 AutoReleaseGeometry() {
683 fSuccess = false;
684 }
685
reed@google.comac10a2d2010-12-22 21:39:39 +0000686 ~AutoReleaseGeometry() {
687 if (fSuccess) {
688 fTarget->releaseReservedGeometry();
689 }
690 }
691
bsalomon@google.com5782d712011-01-21 21:03:59 +0000692 bool set(GrDrawTarget* target,
693 GrVertexLayout vertexLayout,
694 uint32_t vertexCount,
695 uint32_t indexCount) {
696 if (fSuccess) {
697 fTarget->releaseReservedGeometry();
698 }
699 fTarget = target;
700 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
701 vertexCount,
702 indexCount,
703 &fVertices,
704 &fIndices);
705 return fSuccess;
706 }
707
reed@google.comac10a2d2010-12-22 21:39:39 +0000708 bool succeeded() const { return fSuccess; }
709 void* vertices() const { return fVertices; }
710 void* indices() const { return fIndices; }
711
712 GrPoint* positions() const {
713 return static_cast<GrPoint*>(fVertices);
714 }
715
716 private:
717 GrDrawTarget* fTarget;
718 bool fSuccess;
719 void* fVertices;
720 void* fIndices;
721 };
722
723 ///////////////////////////////////////////////////////////////////////////
724
725 class AutoClipRestore : ::GrNoncopyable {
726 public:
727 AutoClipRestore(GrDrawTarget* target) {
728 fTarget = target;
729 fClip = fTarget->getClip();
730 }
731
732 ~AutoClipRestore() {
733 fTarget->setClip(fClip);
734 }
735 private:
736 GrDrawTarget* fTarget;
737 GrClip fClip;
738 };
739
740 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000741 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000742
reed@google.comac10a2d2010-12-22 21:39:39 +0000743 /**
744 * Helper function to compute the size of a vertex from a vertex layout
745 * @return size of a single vertex.
746 */
747 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000748
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000749 /**
750 * Helper function for determining the index of texture coordinates that
751 * is input for a texture stage. Note that a stage may instead use positions
752 * as texture coordinates, in which case the result of the function is
753 * indistinguishable from the case when the stage is disabled.
754 *
755 * @param stage the stage to query
756 * @param vertexLayout layout to query
757 *
758 * @return the texture coordinate index or -1 if the stage doesn't use
759 * separate (non-position) texture coordinates.
760 */
761 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000762
763 /**
764 * Helper function to compute the offset of texture coordinates in a vertex
765 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000766 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000767 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000768 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000769 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000770
771 /**
772 * Helper function to compute the offset of the color in a vertex
773 * @return offset of color in vertex layout or -1 if the
774 * layout has no color.
775 */
776 static int VertexColorOffset(GrVertexLayout vertexLayout);
777
778 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000779 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000780 * coordinates of some index.
781 *
782 * @param coordIndex the tex coord index to query
783 * @param vertexLayout layout to query
784 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000785 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000786 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000787 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000788 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000789 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000790
reed@google.comac10a2d2010-12-22 21:39:39 +0000791 /**
792 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000793 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000794 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000795 * @param stage the stage to query
796 * @param vertexLayout layout to query
797 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000798 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000799 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000800 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000801 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000802
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000803 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000804 * Helper function to compute the size of each vertex and the offsets of
805 * texture coordinates and color. Determines tex coord offsets by tex coord
806 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000807 * by StageTexCoordVertexLayoutBit.)
808 *
809 * @param vertexLayout the layout to query
810 * @param texCoordOffsetsByIdx after return it is the offset of each
811 * tex coord index in the vertex or -1 if
812 * index isn't used.
813 * @return size of a single vertex
814 */
815 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
816 int texCoordOffsetsByIdx[kMaxTexCoords],
817 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000818
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000819 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000820 * Helper function to compute the size of each vertex and the offsets of
821 * texture coordinates and color. Determines tex coord offsets by stage
822 * rather than by index. (Each stage can be mapped to any t.c. index
823 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000824 * tex coords then that stage's offset will be 0 (positions are always at 0).
825 *
826 * @param vertexLayout the layout to query
827 * @param texCoordOffsetsByStage after return it is the offset of each
828 * tex coord index in the vertex or -1 if
829 * index isn't used.
830 * @return size of a single vertex
831 */
832 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
833 int texCoordOffsetsByStage[kNumStages],
834 int *colorOffset);
reed@google.comac10a2d2010-12-22 21:39:39 +0000835protected:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000836
reed@google.comac10a2d2010-12-22 21:39:39 +0000837 // Helpers for GrDrawTarget subclasses that won't have private access to
838 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +0000839 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000840 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +0000841 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000842 { return sds.fState; }
843
844 // implemented by subclass
845 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
846 void** vertices,
847 void** indices) = 0;
848
849 virtual void releaseGeometryHelper() = 0;
850
851 virtual void clipWillChange(const GrClip& clip) = 0;
852
853 enum GeometrySrcType {
854 kArray_GeometrySrcType,
855 kReserved_GeometrySrcType,
856 kBuffer_GeometrySrcType
857 };
858
859 struct {
860 bool fLocked;
861 uint32_t fVertexCount;
862 uint32_t fIndexCount;
863 } fReservedGeometry;
864
865 struct GeometrySrc {
866 GeometrySrcType fVertexSrc;
867 union {
868 const GrVertexBuffer* fVertexBuffer;
869 const void* fVertexArray;
870 };
871 GeometrySrcType fIndexSrc;
872 union {
873 const GrIndexBuffer* fIndexBuffer;
874 const void* fIndexArray;
875 };
876 GrVertexLayout fVertexLayout;
877 } fGeometrySrc;
878
879 GrClip fClip;
880
reed@google.com8195f672011-01-12 18:14:28 +0000881 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000882
reed@google.comac10a2d2010-12-22 21:39:39 +0000883 // not meant for outside usage. Could cause problems if calls between
884 // the save and restore mess with reserved geometry state.
885 class AutoGeometrySrcRestore {
886 public:
887 AutoGeometrySrcRestore(GrDrawTarget* target) {
888 fTarget = target;
889 fGeometrySrc = fTarget->fGeometrySrc;
890 }
891 ~AutoGeometrySrcRestore() {
892 fTarget->fGeometrySrc = fGeometrySrc;
893 }
894 private:
895 GrDrawTarget *fTarget;
896 GeometrySrc fGeometrySrc;
897
898 AutoGeometrySrcRestore();
899 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
900 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
901 };
902
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000903private:
904 void VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000905};
906
907#endif