blob: 35f912b7c484dc50c11f89228b06488025a60285 [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.com080773c2011-03-15 19:09:25 +0000139 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000140 GrTexture* fTextures[kNumStages];
141 GrSamplerState fSamplerStates[kNumStages];
142 GrRenderTarget* fRenderTarget;
143 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000144 DrawFace fDrawFace;
145
146 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000147 GrMatrix fViewMatrix;
reed@google.com8195f672011-01-12 18:14:28 +0000148 bool operator ==(const DrState& s) const {
149 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 }
reed@google.com8195f672011-01-12 18:14:28 +0000151 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 };
153
154public:
155 ///////////////////////////////////////////////////////////////////////////
156
157 GrDrawTarget();
158
159 /**
160 * Sets the current clip to the region specified by clip. All draws will be
161 * clipped against this clip if kClip_StateBit is enabled.
162 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000163 * Setting the clip may (or may not) zero out the client's stencil bits.
164 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 * @param description of the clipping region
166 */
167 void setClip(const GrClip& clip);
168
169 /**
170 * Gets the current clip.
171 *
172 * @return the clip.
173 */
174 const GrClip& getClip() const;
175
176 /**
177 * Sets the texture used at the next drawing call
178 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000179 * @param stage The texture stage for which the texture will be set
180 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000181 * @param texture The texture to set. Can be NULL though there is no advantage
182 * to settings a NULL texture if doing non-textured drawing
183 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000184 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
186 /**
187 * Retrieves the currently set texture.
188 *
189 * @return The currently set texture. The return value will be NULL if no
190 * texture has been set, NULL was most recently passed to
191 * setTexture, or the last setTexture was destroyed.
192 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000193 const GrTexture* getTexture(int stage) const;
194 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195
196 /**
197 * Sets the rendertarget used at the next drawing call
198 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000199 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 */
201 void setRenderTarget(GrRenderTarget* target);
202
203 /**
204 * Retrieves the currently set rendertarget.
205 *
206 * @return The currently set render target.
207 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000208 const GrRenderTarget* getRenderTarget() const;
209 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
211 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000212 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000214 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000215 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000217 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 * @param samplerState Specifies the sampler state.
219 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000220 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000223 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000225 * @param stage the stage of the sampler to set
226 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
229 GrAssert(stage >= 0 && stage < kNumStages);
230 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000231 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
233 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000234 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000235 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000236 * @param stage the stage to of sampler to get
237 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000238 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000239 const GrMatrix& getSamplerMatrix(int stage) const {
240 return fCurrDrawState.fSamplerStates[stage].getMatrix();
241 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000242
243 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000244 * Sets the matrix of a stage's sampler
245 *
246 * @param stage the stage of sampler set
247 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000248 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000249 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000250 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
251 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000252
253 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 * Sets the matrix applied to veretx positions.
255 *
256 * In the post-view-matrix space the rectangle [0,w]x[0,h]
257 * fully covers the render target. (w and h are the width and height of the
258 * the rendertarget.)
259 *
260 * @param m the matrix used to transform the vertex positions.
261 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000262 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000263
264 /**
265 * Multiplies the current view matrix by a matrix
266 *
267 * After this call V' = V*m where V is the old view matrix,
268 * m is the parameter to this function, and V' is the new view matrix.
269 * (We consider positions to be column vectors so position vector p is
270 * transformed by matrix X as p' = X*p.)
271 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000272 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000274 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
276 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000277 * Retrieves the current view matrix
278 * @return the current view matrix.
279 */
280 const GrMatrix& getViewMatrix() const;
281
282 /**
283 * Retrieves the inverse of the current view matrix.
284 *
285 * If the current view matrix is invertible, return true, and if matrix
286 * is non-null, copy the inverse into it. If the current view matrix is
287 * non-invertible, return false and ignore the matrix parameter.
288 *
289 * @param matrix if not null, will receive a copy of the current inverse.
290 */
291 bool getViewInverse(GrMatrix* matrix) const;
292
293 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000294 * Sets color for next draw to a premultiplied-alpha color.
295 *
296 * @param the color to set.
297 */
298 void setColor(GrColor);
299
300 /**
301 * Sets the color to be used for the next draw to be
302 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
303 *
304 * @param alpha The alpha value to set as the color.
305 */
306 void setAlpha(uint8_t alpha);
307
308 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000309 * Controls whether clockwise, counterclockwise, or both faces are drawn.
310 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000312 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000313
314 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000315 * Gets whether the target is drawing clockwise, counterclockwise,
316 * or both faces.
317 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000319 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000320
321 /**
322 * Enable render state settings.
323 *
324 * @param flags bitfield of StateBits specifing the states to enable
325 */
326 void enableState(uint32_t stateBits);
327
328 /**
329 * Disable render state settings.
330 *
331 * @param flags bitfield of StateBits specifing the states to disable
332 */
333 void disableState(uint32_t stateBits);
334
335 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000336 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
337 }
338
339 bool isClipState() const {
340 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 }
342
bsalomon@google.comd302f142011-03-03 13:54:13 +0000343 bool isColorWriteDisabled() const {
344 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
345 }
346
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 * Sets the blending function coeffecients.
349 *
350 * The blend function will be:
351 * D' = sat(S*srcCoef + D*dstCoef)
352 *
353 * where D is the existing destination color, S is the incoming source
354 * color, and D' is the new destination color that will be written. sat()
355 * is the saturation function.
356 *
357 * @param srcCoef coeffecient applied to the src color.
358 * @param dstCoef coeffecient applied to the dst color.
359 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000360 void setBlendFunc(GrBlendCoeff srcCoef, GrBlendCoeff dstCoef);
reed@google.comac10a2d2010-12-22 21:39:39 +0000361
362 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000363 * Sets the blending function constant referenced by the following blending
364 * coeffecients:
365 * kConstC_BlendCoeff
366 * kIConstC_BlendCoeff
367 * kConstA_BlendCoeff
368 * kIConstA_BlendCoeff
369 *
370 * @param constant the constant to set
371 */
372 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
373
374 /**
375 * Retrieves the last value set by setBlendConstant()
376 * @return the blending constant value
377 */
378 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
379
380 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 * Used to save and restore the GrGpu's drawing state
382 */
383 struct SavedDrawState {
384 private:
reed@google.com8195f672011-01-12 18:14:28 +0000385 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000386 friend class GrDrawTarget;
387 };
388
389 /**
390 * Saves the current draw state. The state can be restored at a later time
391 * with restoreDrawState.
392 *
393 * See also AutoStateRestore class.
394 *
395 * @param state will hold the state after the function returns.
396 */
397 void saveCurrentDrawState(SavedDrawState* state) const;
398
399 /**
400 * Restores previously saved draw state. The client guarantees that state
401 * was previously passed to saveCurrentDrawState and that the rendertarget
402 * and texture set at save are still valid.
403 *
404 * See also AutoStateRestore class.
405 *
406 * @param state the previously saved state to restore.
407 */
408 void restoreDrawState(const SavedDrawState& state);
409
410 /**
411 * Copies the draw state from another target to this target.
412 *
413 * @param srcTarget draw target used as src of the draw state.
414 */
415 void copyDrawState(const GrDrawTarget& srcTarget);
416
417 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000418 * The format of vertices is represented as a bitfield of flags.
419 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000420 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000421 * coordinates and per-vertex colors. Each stage can use any of the texture
422 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000424 * If no texture coordinates are specified for a stage then the stage is
425 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000426 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000427 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000428 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000429 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000430 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000431 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000432 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000433 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000434 * the order would be (position, texture coordinate 1[, color]).
435 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000436
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000437 /**
438 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000439 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000440 * @param stage the stage that will use texture coordinates.
441 * @param texCoordIdx the index of the texture coordinates to use
442 *
443 * @return the bit to add to a GrVertexLayout bitfield.
444 */
445 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
446 GrAssert(stage < kNumStages);
447 GrAssert(texCoordIdx < kMaxTexCoords);
448 return 1 << (stage + (texCoordIdx * kNumStages));
449 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000450
451 /**
452 * Determines if blend is effectively disabled.
453 *
454 * @return true if blend can be disabled without changing the rendering
455 * result given the current state including the vertex layout specified
456 * with the vertex source.
457 */
458 bool canDisableBlend() const;
459
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000460private:
461 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
462public:
463 /**
464 * Generates a bit indicating that a texture stage uses the position
465 * as its texture coordinate.
466 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000468 * coordinates.
469 *
470 * @return the bit to add to a GrVertexLayout bitfield.
471 */
472 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
473 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000475 }
476private:
477 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000479public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000480
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000481 /**
482 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 */
484 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000486 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
487 //<! vertices have colors
488 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
489 //<! use text vertices. (Pos
490 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000492 // text [GrGpuTextVertex vs
493 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000495 kDummyVertexLayoutBit,
496 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000498 // make sure we haven't exceeded the number of bits in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout)));
500
501 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000502 * There are three paths for specifying geometry (vertices and optionally
503 * indices) to the draw target. When indexed drawing the indices and vertices
504 * can be each use a different path.
505 *
506 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
507 * caller's client has already provided vertex data in a format
508 * the time compatible with a GrVertexLayout. The array must contain the
509 * data at set*SourceToArray is called. The source stays in effect for
510 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
511 * again or one of the other two paths is chosen.
512 *
513 * 2. Reserve and Lock. This is most useful when the caller has data it must
514 * transform before drawing and will not likely render it again. The
515 * caller requests that the draw target make room for some amount of
516 * vertex and/or index data. The target provides ptrs to hold the data
517 * data. The caller can write the data into the pts up until the first
518 * drawIndexed or drawNonIndexed call. At this point the data is frozen
519 * and the ptrs are no longer guaranteed to be valid. All subsequent
520 * drawIndexed & drawNonIndexed calls will use this data until
521 * releaseReserved geometry is called. This must be called before another
522 * source is set.
523 *
524 * 3. Vertex and Index Buffers. This is most useful for geometry that will
525 * be rendered multiple times. SetVertexSourceToBuffer &
526 * SetIndexSourceToBuffer are used to set the buffer and subsequent
527 * drawIndexed and drawNonIndexed calls use this source until another
528 * source is set.
529 */
530
531 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 * Reserves space for vertices and/or indices. Draw target will use
533 * reserved vertices / indices at next draw.
534 *
535 * If succeeds:
536 * if vertexCount is nonzero, *vertices will be the array
537 * of vertices to be filled by caller. The next draw will read
538 * these vertices.
539 *
540 * if indecCount is nonzero, *indices will be the array of indices
541 * to be filled by caller. The next indexed draw will read from
542 * these indices.
543 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000544 * If a client does not already have a vertex buffer then this is the
545 * preferred way to allocate vertex/index array. It allows the subclass of
546 * GrDrawTarget to decide whether to put data in buffers, to group vertex
547 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000549 * Following the first draw after reserveAndLockGeometry the ptrs returned
550 * by releaseReservedGeometry are no longer valid and the geometry data
551 * cannot be further modified. The contents that were put in the reserved
552 * space can be drawn by multiple draws, however.
553 *
554 * reserveAndLockGeometry must be matched with a releaseReservedGeometry
555 * call after all draws that reference the reserved geometry data have
556 * been called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 *
558 * AutoGeometryRelease can be used to automatically call the release.
559 *
560 * @param vertexCount the number of vertices to reserve space for. Can be 0.
561 * @param indexCount the number of indices to reserve space for. Can be 0.
562 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
563 * @param vertices will point to reserved vertex space if vertexCount is
564 * non-zero. Illegal to pass NULL if vertexCount > 0.
565 * @param indices will point to reserved index space if indexCount is
566 * non-zero. Illegal to pass NULL if indexCount > 0.
567 *
568 * @return true if succeeded in allocating space for the vertices and false
569 * if not.
570 */
571 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
572 uint32_t vertexCount,
573 uint32_t indexCount,
574 void** vertices,
575 void** indices);
576 /**
577 * Provides hints to caller about the number of vertices and indices
578 * that can be allocated cheaply. This can be useful if caller is reserving
579 * space but doesn't know exactly how much geometry is needed.
580 *
581 * Also may hint whether the draw target should be flushed first. This is
582 * useful for deferred targets.
583 *
584 * @param vertexLayout layout of vertices caller would like to reserve
585 * @param vertexCount in: hint about how many vertices the caller would
586 * like to allocate.
587 * out: a hint about the number of vertices that can be
588 * allocated cheaply. Negative means no hint.
589 * Ignored if NULL.
590 * @param indexCount in: hint about how many indices the caller would
591 * like to allocate.
592 * out: a hint about the number of indices that can be
593 * allocated cheaply. Negative means no hint.
594 * Ignored if NULL.
595 *
596 * @return true if target should be flushed based on the input values.
597 */
598 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000599 int* vertexCount,
600 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000601
602 /**
603 * Releases reserved vertex/index data from reserveAndLockGeometry().
604 */
605 void releaseReservedGeometry();
606
607 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000608 * Sets source of vertex data for the next draw. Array must contain
609 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 *
611 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000612 * @param size size of the vertex data.
613 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000615 void setVertexSourceToArray(GrVertexLayout vertexLayout,
616 const void* vertexArray,
617 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000618
619 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000620 * Sets source of index data for the next indexed draw. Array must contain
621 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000623 * @param array cpu array containing index data.
624 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000626 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
628 /**
629 * Sets source of vertex data for the next draw. Data does not have to be
630 * in the buffer until drawIndexed or drawNonIndexed.
631 *
632 * @param buffer vertex buffer containing vertex data. Must be
633 * unlocked before draw call.
634 * @param vertexLayout layout of the vertex data in the buffer.
635 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000636 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
637 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000638
639 /**
640 * Sets source of index data for the next indexed draw. Data does not have
641 * to be in the buffer until drawIndexed or drawNonIndexed.
642 *
643 * @param buffer index buffer containing indices. Must be unlocked
644 * before indexed draw call.
645 */
646 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
647
648 /**
649 * Draws indexed geometry using the current state and current vertex / index
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 startIndex first index to read from index src.
656 * @param vertexCount one greater than the max index.
657 * @param indexCount the number of index elements to read. The index count
658 * is effectively trimmed to the last completely
659 * specified primitive.
660 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000661 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000662 int startVertex,
663 int startIndex,
664 int vertexCount,
665 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000666
667 /**
668 * Draws non-indexed geometry using the current state and current vertex
669 * sources.
670 *
671 * @param type The type of primitives to draw.
672 * @param startVertex the vertex in the vertex array/buffer corresponding
673 * to index 0
674 * @param vertexCount one greater than the max index.
675 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000676 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000677 int startVertex,
678 int vertexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000679
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000680 /**
681 * Helper function for drawing rects. This does not use the current index
682 * and vertex sources. After returning, the vertex and index sources may
683 * have changed. They should be reestablished before the next drawIndexed
684 * or drawNonIndexed. This cannot be called between reserving and releasing
685 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000686 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000687 * drawNonIndexed.
688 * @param rect the rect to draw
689 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000690 * @param stageEnableBitfield bitmask indicating which stages are enabled.
691 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000692 * @param srcRects specifies rects for stages enabled by stageEnableMask.
693 * if stageEnableMask bit i is 1, srcRects is not NULL,
694 * and srcRects[i] is not NULL, then srcRects[i] will be
695 * used as coordinates for stage i. Otherwise, if stage i
696 * is enabled then rect is used as the coordinates.
697 * @param srcMatrices optional matrices applied to srcRects. If
698 * srcRect[i] is non-NULL and srcMatrices[i] is
699 * non-NULL then srcRect[i] will be transformed by
700 * srcMatrix[i]. srcMatrices can be NULL when no
701 * srcMatrices are desired.
702 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000703 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000704 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000705 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000706 const GrRect* srcRects[],
707 const GrMatrix* srcMatrices[]);
708
709 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000710 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000711 * matrices.
712 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000713 void drawSimpleRect(const GrRect& rect,
714 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000715 StageBitfield stageEnableBitfield) {
716 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000717 }
718
reed@google.comac10a2d2010-12-22 21:39:39 +0000719 ///////////////////////////////////////////////////////////////////////////
720
721 class AutoStateRestore : ::GrNoncopyable {
722 public:
723 AutoStateRestore(GrDrawTarget* target);
724 ~AutoStateRestore();
725
726 private:
727 GrDrawTarget* fDrawTarget;
728 SavedDrawState fDrawState;
729 };
730
731 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000732
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000733 class AutoViewMatrixRestore : ::GrNoncopyable {
734 public:
735 AutoViewMatrixRestore() {
736 fDrawTarget = NULL;
737 }
738
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000739 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000740 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
741 GrAssert(NULL != target);
742 }
743
744 void set(GrDrawTarget* target) {
745 GrAssert(NULL != target);
746 if (NULL != fDrawTarget) {
747 fDrawTarget->setViewMatrix(fMatrix);
748 }
749 fDrawTarget = target;
750 fMatrix = target->getViewMatrix();
751 }
752
753 ~AutoViewMatrixRestore() {
754 if (NULL != fDrawTarget) {
755 fDrawTarget->setViewMatrix(fMatrix);
756 }
757 }
758
759 private:
760 GrDrawTarget* fDrawTarget;
761 GrMatrix fMatrix;
762 };
763
764 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000765
766 class AutoReleaseGeometry : ::GrNoncopyable {
767 public:
768 AutoReleaseGeometry(GrDrawTarget* target,
769 GrVertexLayout vertexLayout,
770 uint32_t vertexCount,
771 uint32_t indexCount) {
772 fTarget = target;
773 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
774 vertexCount,
775 indexCount,
776 &fVertices,
777 &fIndices);
778 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000779
780 AutoReleaseGeometry() {
781 fSuccess = false;
782 }
783
reed@google.comac10a2d2010-12-22 21:39:39 +0000784 ~AutoReleaseGeometry() {
785 if (fSuccess) {
786 fTarget->releaseReservedGeometry();
787 }
788 }
789
bsalomon@google.com5782d712011-01-21 21:03:59 +0000790 bool set(GrDrawTarget* target,
791 GrVertexLayout vertexLayout,
792 uint32_t vertexCount,
793 uint32_t indexCount) {
794 if (fSuccess) {
795 fTarget->releaseReservedGeometry();
796 }
797 fTarget = target;
798 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
799 vertexCount,
800 indexCount,
801 &fVertices,
802 &fIndices);
803 return fSuccess;
804 }
805
reed@google.comac10a2d2010-12-22 21:39:39 +0000806 bool succeeded() const { return fSuccess; }
807 void* vertices() const { return fVertices; }
808 void* indices() const { return fIndices; }
809
810 GrPoint* positions() const {
811 return static_cast<GrPoint*>(fVertices);
812 }
813
814 private:
815 GrDrawTarget* fTarget;
816 bool fSuccess;
817 void* fVertices;
818 void* fIndices;
819 };
820
821 ///////////////////////////////////////////////////////////////////////////
822
823 class AutoClipRestore : ::GrNoncopyable {
824 public:
825 AutoClipRestore(GrDrawTarget* target) {
826 fTarget = target;
827 fClip = fTarget->getClip();
828 }
829
830 ~AutoClipRestore() {
831 fTarget->setClip(fClip);
832 }
833 private:
834 GrDrawTarget* fTarget;
835 GrClip fClip;
836 };
837
838 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000839 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000840
reed@google.comac10a2d2010-12-22 21:39:39 +0000841 /**
842 * Helper function to compute the size of a vertex from a vertex layout
843 * @return size of a single vertex.
844 */
845 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000846
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000847 /**
848 * Helper function for determining the index of texture coordinates that
849 * is input for a texture stage. Note that a stage may instead use positions
850 * as texture coordinates, in which case the result of the function is
851 * indistinguishable from the case when the stage is disabled.
852 *
853 * @param stage the stage to query
854 * @param vertexLayout layout to query
855 *
856 * @return the texture coordinate index or -1 if the stage doesn't use
857 * separate (non-position) texture coordinates.
858 */
859 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000860
861 /**
862 * Helper function to compute the offset of texture coordinates in a vertex
863 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000864 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000865 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000866 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000867 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000868
869 /**
870 * Helper function to compute the offset of the color in a vertex
871 * @return offset of color in vertex layout or -1 if the
872 * layout has no color.
873 */
874 static int VertexColorOffset(GrVertexLayout vertexLayout);
875
876 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000877 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000878 * coordinates of some index.
879 *
880 * @param coordIndex the tex coord index to query
881 * @param vertexLayout layout to query
882 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000883 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000884 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000885 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000886 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000887 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000888
reed@google.comac10a2d2010-12-22 21:39:39 +0000889 /**
890 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000891 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000892 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000893 * @param stage the stage to query
894 * @param vertexLayout layout to query
895 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000896 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000897 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000898 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000899 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000900
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000901 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000902 * Helper function to compute the size of each vertex and the offsets of
903 * texture coordinates and color. Determines tex coord offsets by tex coord
904 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000905 * by StageTexCoordVertexLayoutBit.)
906 *
907 * @param vertexLayout the layout to query
908 * @param texCoordOffsetsByIdx after return it is the offset of each
909 * tex coord index in the vertex or -1 if
910 * index isn't used.
911 * @return size of a single vertex
912 */
913 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
914 int texCoordOffsetsByIdx[kMaxTexCoords],
915 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000916
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000917 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000918 * Helper function to compute the size of each vertex and the offsets of
919 * texture coordinates and color. Determines tex coord offsets by stage
920 * rather than by index. (Each stage can be mapped to any t.c. index
921 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000922 * tex coords then that stage's offset will be 0 (positions are always at 0).
923 *
924 * @param vertexLayout the layout to query
925 * @param texCoordOffsetsByStage after return it is the offset of each
926 * tex coord index in the vertex or -1 if
927 * index isn't used.
928 * @return size of a single vertex
929 */
930 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
931 int texCoordOffsetsByStage[kNumStages],
932 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000933
934 /**
935 * Accessing positions, texture coords, or colors, of a vertex within an
936 * array is a hassle involving casts and simple math. These helpers exist
937 * to keep GrDrawTarget clients' code a bit nicer looking.
938 */
939
940 /**
941 * Gets a pointer to a GrPoint of a vertex's position or texture
942 * coordinate.
943 * @param vertices the vetex array
944 * @param vertexIndex the index of the vertex in the array
945 * @param vertexSize the size of each vertex in the array
946 * @param offset the offset in bytes of the vertex component.
947 * Defaults to zero (corresponding to vertex position)
948 * @return pointer to the vertex component as a GrPoint
949 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000950 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000951 int vertexIndex,
952 int vertexSize,
953 int offset = 0) {
954 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000955 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000956 vertexIndex * vertexSize);
957 }
958 static const GrPoint* GetVertexPoint(const void* vertices,
959 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000960 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000961 int offset = 0) {
962 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000963 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000964 vertexIndex * vertexSize);
965 }
966
967 /**
968 * Gets a pointer to a GrColor inside a vertex within a vertex array.
969 * @param vertices the vetex array
970 * @param vertexIndex the index of the vertex in the array
971 * @param vertexSize the size of each vertex in the array
972 * @param offset the offset in bytes of the vertex color
973 * @return pointer to the vertex component as a GrColor
974 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000975 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000976 int vertexIndex,
977 int vertexSize,
978 int offset) {
979 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000980 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000981 vertexIndex * vertexSize);
982 }
983 static const GrColor* GetVertexColor(const void* vertices,
984 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000985 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000986 int offset) {
987 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000988 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000989 vertexIndex * vertexSize);
990 }
991
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000992 static void VertexLayoutUnitTest();
993
reed@google.comac10a2d2010-12-22 21:39:39 +0000994protected:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000995
reed@google.comac10a2d2010-12-22 21:39:39 +0000996 // Helpers for GrDrawTarget subclasses that won't have private access to
997 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +0000998 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001000 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 { return sds.fState; }
1002
1003 // implemented by subclass
1004 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
1005 void** vertices,
1006 void** indices) = 0;
1007
1008 virtual void releaseGeometryHelper() = 0;
1009
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001010 // subclass overrides to be notified when clip is set.
1011 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001012
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001013 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
1014 int vertexCount) = 0;
1015
1016 virtual void setIndexSourceToArrayHelper(const void* indexArray,
1017 int indexCount) = 0;
1018
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001019 // Helpers for drawRect, protected so subclasses that override drawRect
1020 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001021 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001022 const GrRect* srcRects[]);
1023
1024 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001025 const GrMatrix* matrix,
1026 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001027 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001028 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001029 void* vertices);
1030
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 enum GeometrySrcType {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001032 kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry
1033 kArray_GeometrySrcType, // src was set using set*SourceToArray
1034 kBuffer_GeometrySrcType // src was set using set*SourceToBuffer
reed@google.comac10a2d2010-12-22 21:39:39 +00001035 };
1036
bsalomon@google.comd302f142011-03-03 13:54:13 +00001037 struct ReservedGeometry {
reed@google.comac10a2d2010-12-22 21:39:39 +00001038 bool fLocked;
1039 uint32_t fVertexCount;
1040 uint32_t fIndexCount;
1041 } fReservedGeometry;
1042
1043 struct GeometrySrc {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001044 GeometrySrcType fVertexSrc;
1045 const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer
1046 GeometrySrcType fIndexSrc;
1047 const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer
1048 GrVertexLayout fVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 } fGeometrySrc;
1050
1051 GrClip fClip;
1052
reed@google.com8195f672011-01-12 18:14:28 +00001053 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001055 // Not meant for external use. Only setVertexSourceToBuffer and
1056 // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't
1057 // support nested reserveAndLockGeometry (and cpu arrays internally use the
1058 // same path).
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 class AutoGeometrySrcRestore {
1060 public:
1061 AutoGeometrySrcRestore(GrDrawTarget* target) {
1062 fTarget = target;
1063 fGeometrySrc = fTarget->fGeometrySrc;
1064 }
1065 ~AutoGeometrySrcRestore() {
1066 fTarget->fGeometrySrc = fGeometrySrc;
1067 }
1068 private:
1069 GrDrawTarget *fTarget;
1070 GeometrySrc fGeometrySrc;
1071
1072 AutoGeometrySrcRestore();
1073 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
1074 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
1075 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001076};
1077
1078#endif