blob: 285665b0c8a5a11465f3eb203066a636a801c3bd [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"
bsalomon@google.comd302f142011-03-03 13:54:13 +000026#include "GrTexture.h"
27#include "GrStencil.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000030class 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.comc6cf7232011-02-17 16:43:10 +000043 * lookups are specified by a texture a sampler (setSamplerState). Texture
44 * coordinates for each stage come from the vertices based on a
45 * GrVertexLayout bitfield. The output fragment color is the output color of
46 * the last enabled stage. The presence or absence of texture coordinates
47 * for each stage in the vertex layout indicates whether a stage is enabled
48 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000049 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000050 enum {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000051 kNumStages = 2,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000052 kMaxTexCoords = kNumStages
53 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000054
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000055 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +000056 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +000057 */
bsalomon@google.comffca4002011-02-22 20:34:01 +000058 typedef int StageBitfield;
59 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +000060
61 /**
62 * Flags that affect rendering. Controlled using enable/disableState(). All
63 * default to disabled.
64 */
65 enum StateBits {
66 kDither_StateBit = 0x1,//<! Perform color dithering
67 kAntialias_StateBit = 0x2,//<! Perform anti-aliasing. The render-
68 // target must support some form of AA
69 // (msaa, coverage sampling, etc). For
70 // GrGpu-created rendertarget/textures
71 // this is controlled by parameters
72 // passed to createTexture.
73 kClip_StateBit = 0x4,//<! Controls whether drawing is clipped
74 // against the region specified by
75 // setClip.
bsalomon@google.comd302f142011-03-03 13:54:13 +000076 kNoColorWrites_StateBit = 0x8,//<! If set it disables writing colors.
77 // Useful while performing stencil ops.
78
79 // subclass may use additional bits internally
80 kDummyStateBit,
81 kLastPublicStateBit = kDummyStateBit-1
82 };
83
84 enum DrawFace {
85 kBoth_DrawFace,
86 kCCW_DrawFace,
87 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +000088 };
89
90 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000091 * The DrawTarget may reserve some of the high bits of the stencil. The draw
92 * target will automatically trim reference and mask values so that the
93 * client doesn't overwrite these bits.
94 * The number of bits available is relative to the currently set render
95 *target.
96 * @return the number of bits usable by the draw target client.
reed@google.comac10a2d2010-12-22 21:39:39 +000097 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000098 int getUsableStencilBits() const {
99 int bits = fCurrDrawState.fRenderTarget->stencilBits();
100 if (bits) {
101 return bits - 1;
102 } else {
103 return 0;
104 }
105 }
106
107 /**
108 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000109 * Changing the clip has the side-effect of possibly zeroing
110 * out the client settable stencil bits. So multipass algorithms
111 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000112 * @param settings the stencil settings to use.
113 */
114 void setStencil(const GrStencilSettings& settings) {
115 fCurrDrawState.fStencilSettings = settings;
116 }
117
118 /**
119 * Shortcut to disable stencil testing and ops.
120 */
121 void disableStencil() {
122 fCurrDrawState.fStencilSettings.setDisabled();
123 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
125protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
reed@google.com8195f672011-01-12 18:14:28 +0000127 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000128 DrState() {
129 // make sure any pad is zero for memcmp
130 // all DrState members should default to something
131 // valid by the memset
132 memset(this, 0, sizeof(DrState));
133 GrAssert((intptr_t)(void*)NULL == 0LL);
134 GrAssert(fStencilSettings.isDisabled());
135 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000137 GrBlendCoeff fSrcBlend;
138 GrBlendCoeff fDstBlend;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 GrTexture* fTextures[kNumStages];
140 GrSamplerState fSamplerStates[kNumStages];
141 GrRenderTarget* fRenderTarget;
142 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000143 DrawFace fDrawFace;
144
145 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000146 GrMatrix fViewMatrix;
reed@google.com8195f672011-01-12 18:14:28 +0000147 bool operator ==(const DrState& s) const {
148 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 }
reed@google.com8195f672011-01-12 18:14:28 +0000150 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 };
152
153public:
154 ///////////////////////////////////////////////////////////////////////////
155
156 GrDrawTarget();
157
158 /**
159 * Sets the current clip to the region specified by clip. All draws will be
160 * clipped against this clip if kClip_StateBit is enabled.
161 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000162 * Setting the clip may (or may not) zero out the client's stencil bits.
163 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 * @param description of the clipping region
165 */
166 void setClip(const GrClip& clip);
167
168 /**
169 * Gets the current clip.
170 *
171 * @return the clip.
172 */
173 const GrClip& getClip() const;
174
175 /**
176 * Sets the texture used at the next drawing call
177 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000178 * @param stage The texture stage for which the texture will be set
179 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000180 * @param texture The texture to set. Can be NULL though there is no advantage
181 * to settings a NULL texture if doing non-textured drawing
182 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000183 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
185 /**
186 * Retrieves the currently set texture.
187 *
188 * @return The currently set texture. The return value will be NULL if no
189 * texture has been set, NULL was most recently passed to
190 * setTexture, or the last setTexture was destroyed.
191 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000192 const GrTexture* getTexture(int stage) const;
193 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
195 /**
196 * Sets the rendertarget used at the next drawing call
197 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000198 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 */
200 void setRenderTarget(GrRenderTarget* target);
201
202 /**
203 * Retrieves the currently set rendertarget.
204 *
205 * @return The currently set render target.
206 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000207 const GrRenderTarget* getRenderTarget() const;
208 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000209
210 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000211 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000213 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000214 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000216 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 * @param samplerState Specifies the sampler state.
218 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000219 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
221 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000222 * Concats the matrix of a stage's sampler.
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
225 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000227 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
228 GrAssert(stage >= 0 && stage < kNumStages);
229 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000230 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
232 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000233 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000234 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000235 * @param stage the stage to of sampler to get
236 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000237 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000238 const GrMatrix& getSamplerMatrix(int stage) const {
239 return fCurrDrawState.fSamplerStates[stage].getMatrix();
240 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000241
242 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000243 * Sets the matrix of a stage's sampler
244 *
245 * @param stage the stage of sampler set
246 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000247 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000248 const void setSamplerMatrix(int stage, const GrMatrix& matrix) {
249 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
250 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000251
252 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 * Sets the matrix applied to veretx positions.
254 *
255 * In the post-view-matrix space the rectangle [0,w]x[0,h]
256 * fully covers the render target. (w and h are the width and height of the
257 * the rendertarget.)
258 *
259 * @param m the matrix used to transform the vertex positions.
260 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000261 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262
263 /**
264 * Multiplies the current view matrix by a matrix
265 *
266 * After this call V' = V*m where V is the old view matrix,
267 * m is the parameter to this function, and V' is the new view matrix.
268 * (We consider positions to be column vectors so position vector p is
269 * transformed by matrix X as p' = X*p.)
270 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000271 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000273 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000274
275 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000276 * Retrieves the current view matrix
277 * @return the current view matrix.
278 */
279 const GrMatrix& getViewMatrix() const;
280
281 /**
282 * Retrieves the inverse of the current view matrix.
283 *
284 * If the current view matrix is invertible, return true, and if matrix
285 * is non-null, copy the inverse into it. If the current view matrix is
286 * non-invertible, return false and ignore the matrix parameter.
287 *
288 * @param matrix if not null, will receive a copy of the current inverse.
289 */
290 bool getViewInverse(GrMatrix* matrix) const;
291
292 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 * Sets color for next draw to a premultiplied-alpha color.
294 *
295 * @param the color to set.
296 */
297 void setColor(GrColor);
298
299 /**
300 * Sets the color to be used for the next draw to be
301 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
302 *
303 * @param alpha The alpha value to set as the color.
304 */
305 void setAlpha(uint8_t alpha);
306
307 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000308 * Controls whether clockwise, counterclockwise, or both faces are drawn.
309 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000310 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000311 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
313 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000314 * Gets whether the target is drawing clockwise, counterclockwise,
315 * or both faces.
316 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000318 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
320 /**
321 * Enable render state settings.
322 *
323 * @param flags bitfield of StateBits specifing the states to enable
324 */
325 void enableState(uint32_t stateBits);
326
327 /**
328 * Disable render state settings.
329 *
330 * @param flags bitfield of StateBits specifing the states to disable
331 */
332 void disableState(uint32_t stateBits);
333
334 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000335 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
336 }
337
338 bool isClipState() const {
339 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000340 }
341
bsalomon@google.comd302f142011-03-03 13:54:13 +0000342 bool isColorWriteDisabled() const {
343 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
344 }
345
reed@google.comac10a2d2010-12-22 21:39:39 +0000346 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 * Sets the blending function coeffecients.
348 *
349 * The blend function will be:
350 * D' = sat(S*srcCoef + D*dstCoef)
351 *
352 * where D is the existing destination color, S is the incoming source
353 * color, and D' is the new destination color that will be written. sat()
354 * is the saturation function.
355 *
356 * @param srcCoef coeffecient applied to the src color.
357 * @param dstCoef coeffecient applied to the dst color.
358 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000359 void setBlendFunc(GrBlendCoeff srcCoef, GrBlendCoeff dstCoef);
reed@google.comac10a2d2010-12-22 21:39:39 +0000360
361 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 * Used to save and restore the GrGpu's drawing state
363 */
364 struct SavedDrawState {
365 private:
reed@google.com8195f672011-01-12 18:14:28 +0000366 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 friend class GrDrawTarget;
368 };
369
370 /**
371 * Saves the current draw state. The state can be restored at a later time
372 * with restoreDrawState.
373 *
374 * See also AutoStateRestore class.
375 *
376 * @param state will hold the state after the function returns.
377 */
378 void saveCurrentDrawState(SavedDrawState* state) const;
379
380 /**
381 * Restores previously saved draw state. The client guarantees that state
382 * was previously passed to saveCurrentDrawState and that the rendertarget
383 * and texture set at save are still valid.
384 *
385 * See also AutoStateRestore class.
386 *
387 * @param state the previously saved state to restore.
388 */
389 void restoreDrawState(const SavedDrawState& state);
390
391 /**
392 * Copies the draw state from another target to this target.
393 *
394 * @param srcTarget draw target used as src of the draw state.
395 */
396 void copyDrawState(const GrDrawTarget& srcTarget);
397
398 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000399 * The format of vertices is represented as a bitfield of flags.
400 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000401 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000402 * coordinates and per-vertex colors. Each stage can use any of the texture
403 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000405 * If no texture coordinates are specified for a stage then the stage is
406 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000409 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000410 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000411 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000412 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000413 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000414 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000415 * the order would be (position, texture coordinate 1[, color]).
416 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000417
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000418 /**
419 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000420 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000421 * @param stage the stage that will use texture coordinates.
422 * @param texCoordIdx the index of the texture coordinates to use
423 *
424 * @return the bit to add to a GrVertexLayout bitfield.
425 */
426 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
427 GrAssert(stage < kNumStages);
428 GrAssert(texCoordIdx < kMaxTexCoords);
429 return 1 << (stage + (texCoordIdx * kNumStages));
430 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000431
432 /**
433 * Determines if blend is effectively disabled.
434 *
435 * @return true if blend can be disabled without changing the rendering
436 * result given the current state including the vertex layout specified
437 * with the vertex source.
438 */
439 bool canDisableBlend() const;
440
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000441private:
442 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
443public:
444 /**
445 * Generates a bit indicating that a texture stage uses the position
446 * as its texture coordinate.
447 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000449 * coordinates.
450 *
451 * @return the bit to add to a GrVertexLayout bitfield.
452 */
453 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
454 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000456 }
457private:
458 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000459
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000460public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000461
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000462 /**
463 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000464 */
465 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000467 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
468 //<! vertices have colors
469 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
470 //<! use text vertices. (Pos
471 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000473 // text [GrGpuTextVertex vs
474 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000476 kDummyVertexLayoutBit,
477 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000479 // make sure we haven't exceeded the number of bits in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout)));
481
482 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000483 * There are three paths for specifying geometry (vertices and optionally
484 * indices) to the draw target. When indexed drawing the indices and vertices
485 * can be each use a different path.
486 *
487 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
488 * caller's client has already provided vertex data in a format
489 * the time compatible with a GrVertexLayout. The array must contain the
490 * data at set*SourceToArray is called. The source stays in effect for
491 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
492 * again or one of the other two paths is chosen.
493 *
494 * 2. Reserve and Lock. This is most useful when the caller has data it must
495 * transform before drawing and will not likely render it again. The
496 * caller requests that the draw target make room for some amount of
497 * vertex and/or index data. The target provides ptrs to hold the data
498 * data. The caller can write the data into the pts up until the first
499 * drawIndexed or drawNonIndexed call. At this point the data is frozen
500 * and the ptrs are no longer guaranteed to be valid. All subsequent
501 * drawIndexed & drawNonIndexed calls will use this data until
502 * releaseReserved geometry is called. This must be called before another
503 * source is set.
504 *
505 * 3. Vertex and Index Buffers. This is most useful for geometry that will
506 * be rendered multiple times. SetVertexSourceToBuffer &
507 * SetIndexSourceToBuffer are used to set the buffer and subsequent
508 * drawIndexed and drawNonIndexed calls use this source until another
509 * source is set.
510 */
511
512 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 * Reserves space for vertices and/or indices. Draw target will use
514 * reserved vertices / indices at next draw.
515 *
516 * If succeeds:
517 * if vertexCount is nonzero, *vertices will be the array
518 * of vertices to be filled by caller. The next draw will read
519 * these vertices.
520 *
521 * if indecCount is nonzero, *indices will be the array of indices
522 * to be filled by caller. The next indexed draw will read from
523 * these indices.
524 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000525 * If a client does not already have a vertex buffer then this is the
526 * preferred way to allocate vertex/index array. It allows the subclass of
527 * GrDrawTarget to decide whether to put data in buffers, to group vertex
528 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000530 * Following the first draw after reserveAndLockGeometry the ptrs returned
531 * by releaseReservedGeometry are no longer valid and the geometry data
532 * cannot be further modified. The contents that were put in the reserved
533 * space can be drawn by multiple draws, however.
534 *
535 * reserveAndLockGeometry must be matched with a releaseReservedGeometry
536 * call after all draws that reference the reserved geometry data have
537 * been called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 *
539 * AutoGeometryRelease can be used to automatically call the release.
540 *
541 * @param vertexCount the number of vertices to reserve space for. Can be 0.
542 * @param indexCount the number of indices to reserve space for. Can be 0.
543 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
544 * @param vertices will point to reserved vertex space if vertexCount is
545 * non-zero. Illegal to pass NULL if vertexCount > 0.
546 * @param indices will point to reserved index space if indexCount is
547 * non-zero. Illegal to pass NULL if indexCount > 0.
548 *
549 * @return true if succeeded in allocating space for the vertices and false
550 * if not.
551 */
552 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
553 uint32_t vertexCount,
554 uint32_t indexCount,
555 void** vertices,
556 void** indices);
557 /**
558 * Provides hints to caller about the number of vertices and indices
559 * that can be allocated cheaply. This can be useful if caller is reserving
560 * space but doesn't know exactly how much geometry is needed.
561 *
562 * Also may hint whether the draw target should be flushed first. This is
563 * useful for deferred targets.
564 *
565 * @param vertexLayout layout of vertices caller would like to reserve
566 * @param vertexCount in: hint about how many vertices the caller would
567 * like to allocate.
568 * out: a hint about the number of vertices that can be
569 * allocated cheaply. Negative means no hint.
570 * Ignored if NULL.
571 * @param indexCount in: hint about how many indices the caller would
572 * like to allocate.
573 * out: a hint about the number of indices that can be
574 * allocated cheaply. Negative means no hint.
575 * Ignored if NULL.
576 *
577 * @return true if target should be flushed based on the input values.
578 */
579 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000580 int* vertexCount,
581 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
583 /**
584 * Releases reserved vertex/index data from reserveAndLockGeometry().
585 */
586 void releaseReservedGeometry();
587
588 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000589 * Sets source of vertex data for the next draw. Array must contain
590 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 *
592 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000593 * @param size size of the vertex data.
594 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000596 void setVertexSourceToArray(GrVertexLayout vertexLayout,
597 const void* vertexArray,
598 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000599
600 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000601 * Sets source of index data for the next indexed draw. Array must contain
602 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000604 * @param array cpu array containing index data.
605 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000607 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000608
609 /**
610 * Sets source of vertex data for the next draw. Data does not have to be
611 * in the buffer until drawIndexed or drawNonIndexed.
612 *
613 * @param buffer vertex buffer containing vertex data. Must be
614 * unlocked before draw call.
615 * @param vertexLayout layout of the vertex data in the buffer.
616 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000617 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
618 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619
620 /**
621 * Sets source of index data for the next indexed draw. Data does not have
622 * to be in the buffer until drawIndexed or drawNonIndexed.
623 *
624 * @param buffer index buffer containing indices. Must be unlocked
625 * before indexed draw call.
626 */
627 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
628
629 /**
630 * Draws indexed geometry using the current state and current vertex / index
631 * sources.
632 *
633 * @param type The type of primitives to draw.
634 * @param startVertex the vertex in the vertex array/buffer corresponding
635 * to index 0
636 * @param startIndex first index to read from index src.
637 * @param vertexCount one greater than the max index.
638 * @param indexCount the number of index elements to read. The index count
639 * is effectively trimmed to the last completely
640 * specified primitive.
641 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000642 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000643 int startVertex,
644 int startIndex,
645 int vertexCount,
646 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000647
648 /**
649 * Draws non-indexed geometry using the current state and current vertex
650 * sources.
651 *
652 * @param type The type of primitives to draw.
653 * @param startVertex the vertex in the vertex array/buffer corresponding
654 * to index 0
655 * @param vertexCount one greater than the max index.
656 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000657 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000658 int startVertex,
659 int vertexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000660
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000661 /**
662 * Helper function for drawing rects. This does not use the current index
663 * and vertex sources. After returning, the vertex and index sources may
664 * have changed. They should be reestablished before the next drawIndexed
665 * or drawNonIndexed. This cannot be called between reserving and releasing
666 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000667 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000668 * drawNonIndexed.
669 * @param rect the rect to draw
670 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000671 * @param stageEnableBitfield bitmask indicating which stages are enabled.
672 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000673 * @param srcRects specifies rects for stages enabled by stageEnableMask.
674 * if stageEnableMask bit i is 1, srcRects is not NULL,
675 * and srcRects[i] is not NULL, then srcRects[i] will be
676 * used as coordinates for stage i. Otherwise, if stage i
677 * is enabled then rect is used as the coordinates.
678 * @param srcMatrices optional matrices applied to srcRects. If
679 * srcRect[i] is non-NULL and srcMatrices[i] is
680 * non-NULL then srcRect[i] will be transformed by
681 * srcMatrix[i]. srcMatrices can be NULL when no
682 * srcMatrices are desired.
683 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000684 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000685 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000686 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000687 const GrRect* srcRects[],
688 const GrMatrix* srcMatrices[]);
689
690 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000691 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000692 * matrices.
693 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000694 void drawSimpleRect(const GrRect& rect,
695 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000696 StageBitfield stageEnableBitfield) {
697 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000698 }
699
reed@google.comac10a2d2010-12-22 21:39:39 +0000700 ///////////////////////////////////////////////////////////////////////////
701
702 class AutoStateRestore : ::GrNoncopyable {
703 public:
704 AutoStateRestore(GrDrawTarget* target);
705 ~AutoStateRestore();
706
707 private:
708 GrDrawTarget* fDrawTarget;
709 SavedDrawState fDrawState;
710 };
711
712 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000713
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000714 class AutoViewMatrixRestore : ::GrNoncopyable {
715 public:
716 AutoViewMatrixRestore() {
717 fDrawTarget = NULL;
718 }
719
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000720 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000721 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
722 GrAssert(NULL != target);
723 }
724
725 void set(GrDrawTarget* target) {
726 GrAssert(NULL != target);
727 if (NULL != fDrawTarget) {
728 fDrawTarget->setViewMatrix(fMatrix);
729 }
730 fDrawTarget = target;
731 fMatrix = target->getViewMatrix();
732 }
733
734 ~AutoViewMatrixRestore() {
735 if (NULL != fDrawTarget) {
736 fDrawTarget->setViewMatrix(fMatrix);
737 }
738 }
739
740 private:
741 GrDrawTarget* fDrawTarget;
742 GrMatrix fMatrix;
743 };
744
745 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000746
747 class AutoReleaseGeometry : ::GrNoncopyable {
748 public:
749 AutoReleaseGeometry(GrDrawTarget* target,
750 GrVertexLayout vertexLayout,
751 uint32_t vertexCount,
752 uint32_t indexCount) {
753 fTarget = target;
754 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
755 vertexCount,
756 indexCount,
757 &fVertices,
758 &fIndices);
759 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000760
761 AutoReleaseGeometry() {
762 fSuccess = false;
763 }
764
reed@google.comac10a2d2010-12-22 21:39:39 +0000765 ~AutoReleaseGeometry() {
766 if (fSuccess) {
767 fTarget->releaseReservedGeometry();
768 }
769 }
770
bsalomon@google.com5782d712011-01-21 21:03:59 +0000771 bool set(GrDrawTarget* target,
772 GrVertexLayout vertexLayout,
773 uint32_t vertexCount,
774 uint32_t indexCount) {
775 if (fSuccess) {
776 fTarget->releaseReservedGeometry();
777 }
778 fTarget = target;
779 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
780 vertexCount,
781 indexCount,
782 &fVertices,
783 &fIndices);
784 return fSuccess;
785 }
786
reed@google.comac10a2d2010-12-22 21:39:39 +0000787 bool succeeded() const { return fSuccess; }
788 void* vertices() const { return fVertices; }
789 void* indices() const { return fIndices; }
790
791 GrPoint* positions() const {
792 return static_cast<GrPoint*>(fVertices);
793 }
794
795 private:
796 GrDrawTarget* fTarget;
797 bool fSuccess;
798 void* fVertices;
799 void* fIndices;
800 };
801
802 ///////////////////////////////////////////////////////////////////////////
803
804 class AutoClipRestore : ::GrNoncopyable {
805 public:
806 AutoClipRestore(GrDrawTarget* target) {
807 fTarget = target;
808 fClip = fTarget->getClip();
809 }
810
811 ~AutoClipRestore() {
812 fTarget->setClip(fClip);
813 }
814 private:
815 GrDrawTarget* fTarget;
816 GrClip fClip;
817 };
818
819 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000820 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000821
reed@google.comac10a2d2010-12-22 21:39:39 +0000822 /**
823 * Helper function to compute the size of a vertex from a vertex layout
824 * @return size of a single vertex.
825 */
826 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000827
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000828 /**
829 * Helper function for determining the index of texture coordinates that
830 * is input for a texture stage. Note that a stage may instead use positions
831 * as texture coordinates, in which case the result of the function is
832 * indistinguishable from the case when the stage is disabled.
833 *
834 * @param stage the stage to query
835 * @param vertexLayout layout to query
836 *
837 * @return the texture coordinate index or -1 if the stage doesn't use
838 * separate (non-position) texture coordinates.
839 */
840 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000841
842 /**
843 * Helper function to compute the offset of texture coordinates in a vertex
844 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000845 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000846 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000847 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000848 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000849
850 /**
851 * Helper function to compute the offset of the color in a vertex
852 * @return offset of color in vertex layout or -1 if the
853 * layout has no color.
854 */
855 static int VertexColorOffset(GrVertexLayout vertexLayout);
856
857 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000858 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000859 * coordinates of some index.
860 *
861 * @param coordIndex the tex coord index to query
862 * @param vertexLayout layout to query
863 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000864 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000865 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000866 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000867 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000868 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000869
reed@google.comac10a2d2010-12-22 21:39:39 +0000870 /**
871 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000872 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000873 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000874 * @param stage the stage to query
875 * @param vertexLayout layout to query
876 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000877 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000878 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000879 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000880 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000881
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000882 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000883 * Helper function to compute the size of each vertex and the offsets of
884 * texture coordinates and color. Determines tex coord offsets by tex coord
885 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000886 * by StageTexCoordVertexLayoutBit.)
887 *
888 * @param vertexLayout the layout to query
889 * @param texCoordOffsetsByIdx after return it is the offset of each
890 * tex coord index in the vertex or -1 if
891 * index isn't used.
892 * @return size of a single vertex
893 */
894 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
895 int texCoordOffsetsByIdx[kMaxTexCoords],
896 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000897
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000898 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000899 * Helper function to compute the size of each vertex and the offsets of
900 * texture coordinates and color. Determines tex coord offsets by stage
901 * rather than by index. (Each stage can be mapped to any t.c. index
902 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000903 * tex coords then that stage's offset will be 0 (positions are always at 0).
904 *
905 * @param vertexLayout the layout to query
906 * @param texCoordOffsetsByStage after return it is the offset of each
907 * tex coord index in the vertex or -1 if
908 * index isn't used.
909 * @return size of a single vertex
910 */
911 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
912 int texCoordOffsetsByStage[kNumStages],
913 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000914
915 /**
916 * Accessing positions, texture coords, or colors, of a vertex within an
917 * array is a hassle involving casts and simple math. These helpers exist
918 * to keep GrDrawTarget clients' code a bit nicer looking.
919 */
920
921 /**
922 * Gets a pointer to a GrPoint of a vertex's position or texture
923 * coordinate.
924 * @param vertices the vetex array
925 * @param vertexIndex the index of the vertex in the array
926 * @param vertexSize the size of each vertex in the array
927 * @param offset the offset in bytes of the vertex component.
928 * Defaults to zero (corresponding to vertex position)
929 * @return pointer to the vertex component as a GrPoint
930 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000931 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000932 int vertexIndex,
933 int vertexSize,
934 int offset = 0) {
935 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000936 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000937 vertexIndex * vertexSize);
938 }
939 static const GrPoint* GetVertexPoint(const void* vertices,
940 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000941 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000942 int offset = 0) {
943 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000944 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000945 vertexIndex * vertexSize);
946 }
947
948 /**
949 * Gets a pointer to a GrColor inside a vertex within a vertex array.
950 * @param vertices the vetex array
951 * @param vertexIndex the index of the vertex in the array
952 * @param vertexSize the size of each vertex in the array
953 * @param offset the offset in bytes of the vertex color
954 * @return pointer to the vertex component as a GrColor
955 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000956 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000957 int vertexIndex,
958 int vertexSize,
959 int offset) {
960 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000961 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000962 vertexIndex * vertexSize);
963 }
964 static const GrColor* GetVertexColor(const void* vertices,
965 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000966 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000967 int offset) {
968 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000969 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000970 vertexIndex * vertexSize);
971 }
972
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000973 static void VertexLayoutUnitTest();
974
reed@google.comac10a2d2010-12-22 21:39:39 +0000975protected:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000976
reed@google.comac10a2d2010-12-22 21:39:39 +0000977 // Helpers for GrDrawTarget subclasses that won't have private access to
978 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +0000979 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +0000981 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 { return sds.fState; }
983
984 // implemented by subclass
985 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
986 void** vertices,
987 void** indices) = 0;
988
989 virtual void releaseGeometryHelper() = 0;
990
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000991 // subclass overrides to be notified when clip is set.
992 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000993
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000994 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
995 int vertexCount) = 0;
996
997 virtual void setIndexSourceToArrayHelper(const void* indexArray,
998 int indexCount) = 0;
999
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001000 // Helpers for drawRect, protected so subclasses that override drawRect
1001 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001002 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001003 const GrRect* srcRects[]);
1004
1005 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001006 const GrMatrix* matrix,
1007 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001008 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001009 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001010 void* vertices);
1011
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 enum GeometrySrcType {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001013 kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry
1014 kArray_GeometrySrcType, // src was set using set*SourceToArray
1015 kBuffer_GeometrySrcType // src was set using set*SourceToBuffer
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 };
1017
bsalomon@google.comd302f142011-03-03 13:54:13 +00001018 struct ReservedGeometry {
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 bool fLocked;
1020 uint32_t fVertexCount;
1021 uint32_t fIndexCount;
1022 } fReservedGeometry;
1023
1024 struct GeometrySrc {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001025 GeometrySrcType fVertexSrc;
1026 const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer
1027 GeometrySrcType fIndexSrc;
1028 const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer
1029 GrVertexLayout fVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 } fGeometrySrc;
1031
1032 GrClip fClip;
1033
reed@google.com8195f672011-01-12 18:14:28 +00001034 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001035
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001036 // Not meant for external use. Only setVertexSourceToBuffer and
1037 // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't
1038 // support nested reserveAndLockGeometry (and cpu arrays internally use the
1039 // same path).
reed@google.comac10a2d2010-12-22 21:39:39 +00001040 class AutoGeometrySrcRestore {
1041 public:
1042 AutoGeometrySrcRestore(GrDrawTarget* target) {
1043 fTarget = target;
1044 fGeometrySrc = fTarget->fGeometrySrc;
1045 }
1046 ~AutoGeometrySrcRestore() {
1047 fTarget->fGeometrySrc = fGeometrySrc;
1048 }
1049 private:
1050 GrDrawTarget *fTarget;
1051 GeometrySrc fGeometrySrc;
1052
1053 AutoGeometrySrcRestore();
1054 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
1055 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
1056 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001057};
1058
1059#endif