blob: 576bd7ae416efcb46ef64b6c2fa84aaf1984c594 [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.
109 * @param settings the stencil settings to use.
110 */
111 void setStencil(const GrStencilSettings& settings) {
112 fCurrDrawState.fStencilSettings = settings;
113 }
114
115 /**
116 * Shortcut to disable stencil testing and ops.
117 */
118 void disableStencil() {
119 fCurrDrawState.fStencilSettings.setDisabled();
120 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
122protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
reed@google.com8195f672011-01-12 18:14:28 +0000124 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000125 DrState() {
126 // make sure any pad is zero for memcmp
127 // all DrState members should default to something
128 // valid by the memset
129 memset(this, 0, sizeof(DrState));
130 GrAssert((intptr_t)(void*)NULL == 0LL);
131 GrAssert(fStencilSettings.isDisabled());
132 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000134 GrBlendCoeff fSrcBlend;
135 GrBlendCoeff fDstBlend;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000136 GrTexture* fTextures[kNumStages];
137 GrSamplerState fSamplerStates[kNumStages];
138 GrRenderTarget* fRenderTarget;
139 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000140 DrawFace fDrawFace;
141
142 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000143 GrMatrix fViewMatrix;
reed@google.com8195f672011-01-12 18:14:28 +0000144 bool operator ==(const DrState& s) const {
145 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 }
reed@google.com8195f672011-01-12 18:14:28 +0000147 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 };
149
150public:
151 ///////////////////////////////////////////////////////////////////////////
152
153 GrDrawTarget();
154
155 /**
156 * Sets the current clip to the region specified by clip. All draws will be
157 * clipped against this clip if kClip_StateBit is enabled.
158 *
159 * @param description of the clipping region
160 */
161 void setClip(const GrClip& clip);
162
163 /**
164 * Gets the current clip.
165 *
166 * @return the clip.
167 */
168 const GrClip& getClip() const;
169
170 /**
171 * Sets the texture used at the next drawing call
172 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000173 * @param stage The texture stage for which the texture will be set
174 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 * @param texture The texture to set. Can be NULL though there is no advantage
176 * to settings a NULL texture if doing non-textured drawing
177 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000178 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
180 /**
181 * Retrieves the currently set texture.
182 *
183 * @return The currently set texture. The return value will be NULL if no
184 * texture has been set, NULL was most recently passed to
185 * setTexture, or the last setTexture was destroyed.
186 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000187 const GrTexture* getTexture(int stage) const;
188 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000189
190 /**
191 * Sets the rendertarget used at the next drawing call
192 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000193 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 */
195 void setRenderTarget(GrRenderTarget* target);
196
197 /**
198 * Retrieves the currently set rendertarget.
199 *
200 * @return The currently set render target.
201 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000202 const GrRenderTarget* getRenderTarget() const;
203 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
205 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000206 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000208 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000209 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000211 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 * @param samplerState Specifies the sampler state.
213 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000214 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
216 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000217 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000219 * @param stage the stage of the sampler to set
220 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000222 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
223 GrAssert(stage >= 0 && stage < kNumStages);
224 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000225 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
227 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000228 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000229 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000230 * @param stage the stage to of sampler to get
231 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000232 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000233 const GrMatrix& getSamplerMatrix(int stage) const {
234 return fCurrDrawState.fSamplerStates[stage].getMatrix();
235 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000236
237 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000238 * Sets the matrix of a stage's sampler
239 *
240 * @param stage the stage of sampler set
241 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000242 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000243 const void setSamplerMatrix(int stage, const GrMatrix& matrix) {
244 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
245 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000246
247 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 * Sets the matrix applied to veretx positions.
249 *
250 * In the post-view-matrix space the rectangle [0,w]x[0,h]
251 * fully covers the render target. (w and h are the width and height of the
252 * the rendertarget.)
253 *
254 * @param m the matrix used to transform the vertex positions.
255 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000256 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000257
258 /**
259 * Multiplies the current view matrix by a matrix
260 *
261 * After this call V' = V*m where V is the old view matrix,
262 * m is the parameter to this function, and V' is the new view matrix.
263 * (We consider positions to be column vectors so position vector p is
264 * transformed by matrix X as p' = X*p.)
265 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000266 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000268 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
270 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000271 * Retrieves the current view matrix
272 * @return the current view matrix.
273 */
274 const GrMatrix& getViewMatrix() const;
275
276 /**
277 * Retrieves the inverse of the current view matrix.
278 *
279 * If the current view matrix is invertible, return true, and if matrix
280 * is non-null, copy the inverse into it. If the current view matrix is
281 * non-invertible, return false and ignore the matrix parameter.
282 *
283 * @param matrix if not null, will receive a copy of the current inverse.
284 */
285 bool getViewInverse(GrMatrix* matrix) const;
286
287 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 * Sets color for next draw to a premultiplied-alpha color.
289 *
290 * @param the color to set.
291 */
292 void setColor(GrColor);
293
294 /**
295 * Sets the color to be used for the next draw to be
296 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
297 *
298 * @param alpha The alpha value to set as the color.
299 */
300 void setAlpha(uint8_t alpha);
301
302 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000303 * Controls whether clockwise, counterclockwise, or both faces are drawn.
304 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000306 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
308 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000309 * Gets whether the target is drawing clockwise, counterclockwise,
310 * or both faces.
311 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000313 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
315 /**
316 * Enable render state settings.
317 *
318 * @param flags bitfield of StateBits specifing the states to enable
319 */
320 void enableState(uint32_t stateBits);
321
322 /**
323 * Disable render state settings.
324 *
325 * @param flags bitfield of StateBits specifing the states to disable
326 */
327 void disableState(uint32_t stateBits);
328
329 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000330 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
331 }
332
333 bool isClipState() const {
334 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 }
336
bsalomon@google.comd302f142011-03-03 13:54:13 +0000337 bool isColorWriteDisabled() const {
338 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
339 }
340
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 * Sets the blending function coeffecients.
343 *
344 * The blend function will be:
345 * D' = sat(S*srcCoef + D*dstCoef)
346 *
347 * where D is the existing destination color, S is the incoming source
348 * color, and D' is the new destination color that will be written. sat()
349 * is the saturation function.
350 *
351 * @param srcCoef coeffecient applied to the src color.
352 * @param dstCoef coeffecient applied to the dst color.
353 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000354 void setBlendFunc(GrBlendCoeff srcCoef, GrBlendCoeff dstCoef);
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
356 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000357 * Used to save and restore the GrGpu's drawing state
358 */
359 struct SavedDrawState {
360 private:
reed@google.com8195f672011-01-12 18:14:28 +0000361 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 friend class GrDrawTarget;
363 };
364
365 /**
366 * Saves the current draw state. The state can be restored at a later time
367 * with restoreDrawState.
368 *
369 * See also AutoStateRestore class.
370 *
371 * @param state will hold the state after the function returns.
372 */
373 void saveCurrentDrawState(SavedDrawState* state) const;
374
375 /**
376 * Restores previously saved draw state. The client guarantees that state
377 * was previously passed to saveCurrentDrawState and that the rendertarget
378 * and texture set at save are still valid.
379 *
380 * See also AutoStateRestore class.
381 *
382 * @param state the previously saved state to restore.
383 */
384 void restoreDrawState(const SavedDrawState& state);
385
386 /**
387 * Copies the draw state from another target to this target.
388 *
389 * @param srcTarget draw target used as src of the draw state.
390 */
391 void copyDrawState(const GrDrawTarget& srcTarget);
392
393 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000394 * The format of vertices is represented as a bitfield of flags.
395 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000396 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000397 * coordinates and per-vertex colors. Each stage can use any of the texture
398 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000400 * If no texture coordinates are specified for a stage then the stage is
401 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000402 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000403 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000404 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000405 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000406 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000409 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000410 * the order would be (position, texture coordinate 1[, color]).
411 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000412
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000413 /**
414 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000415 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000416 * @param stage the stage that will use texture coordinates.
417 * @param texCoordIdx the index of the texture coordinates to use
418 *
419 * @return the bit to add to a GrVertexLayout bitfield.
420 */
421 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
422 GrAssert(stage < kNumStages);
423 GrAssert(texCoordIdx < kMaxTexCoords);
424 return 1 << (stage + (texCoordIdx * kNumStages));
425 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000426
427 /**
428 * Determines if blend is effectively disabled.
429 *
430 * @return true if blend can be disabled without changing the rendering
431 * result given the current state including the vertex layout specified
432 * with the vertex source.
433 */
434 bool canDisableBlend() const;
435
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000436private:
437 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
438public:
439 /**
440 * Generates a bit indicating that a texture stage uses the position
441 * as its texture coordinate.
442 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000444 * coordinates.
445 *
446 * @return the bit to add to a GrVertexLayout bitfield.
447 */
448 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
449 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000451 }
452private:
453 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000455public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000457 /**
458 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 */
460 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000461
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000462 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
463 //<! vertices have colors
464 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
465 //<! use text vertices. (Pos
466 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000468 // text [GrGpuTextVertex vs
469 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000471 kDummyVertexLayoutBit,
472 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000474 // make sure we haven't exceeded the number of bits in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 GR_STATIC_ASSERT(kHighVertexLayoutBit < (1 << 8*sizeof(GrVertexLayout)));
476
477 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000478 * There are three paths for specifying geometry (vertices and optionally
479 * indices) to the draw target. When indexed drawing the indices and vertices
480 * can be each use a different path.
481 *
482 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
483 * caller's client has already provided vertex data in a format
484 * the time compatible with a GrVertexLayout. The array must contain the
485 * data at set*SourceToArray is called. The source stays in effect for
486 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
487 * again or one of the other two paths is chosen.
488 *
489 * 2. Reserve and Lock. This is most useful when the caller has data it must
490 * transform before drawing and will not likely render it again. The
491 * caller requests that the draw target make room for some amount of
492 * vertex and/or index data. The target provides ptrs to hold the data
493 * data. The caller can write the data into the pts up until the first
494 * drawIndexed or drawNonIndexed call. At this point the data is frozen
495 * and the ptrs are no longer guaranteed to be valid. All subsequent
496 * drawIndexed & drawNonIndexed calls will use this data until
497 * releaseReserved geometry is called. This must be called before another
498 * source is set.
499 *
500 * 3. Vertex and Index Buffers. This is most useful for geometry that will
501 * be rendered multiple times. SetVertexSourceToBuffer &
502 * SetIndexSourceToBuffer are used to set the buffer and subsequent
503 * drawIndexed and drawNonIndexed calls use this source until another
504 * source is set.
505 */
506
507 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 * Reserves space for vertices and/or indices. Draw target will use
509 * reserved vertices / indices at next draw.
510 *
511 * If succeeds:
512 * if vertexCount is nonzero, *vertices will be the array
513 * of vertices to be filled by caller. The next draw will read
514 * these vertices.
515 *
516 * if indecCount is nonzero, *indices will be the array of indices
517 * to be filled by caller. The next indexed draw will read from
518 * these indices.
519 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000520 * If a client does not already have a vertex buffer then this is the
521 * preferred way to allocate vertex/index array. It allows the subclass of
522 * GrDrawTarget to decide whether to put data in buffers, to group vertex
523 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000525 * Following the first draw after reserveAndLockGeometry the ptrs returned
526 * by releaseReservedGeometry are no longer valid and the geometry data
527 * cannot be further modified. The contents that were put in the reserved
528 * space can be drawn by multiple draws, however.
529 *
530 * reserveAndLockGeometry must be matched with a releaseReservedGeometry
531 * call after all draws that reference the reserved geometry data have
532 * been called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 *
534 * AutoGeometryRelease can be used to automatically call the release.
535 *
536 * @param vertexCount the number of vertices to reserve space for. Can be 0.
537 * @param indexCount the number of indices to reserve space for. Can be 0.
538 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
539 * @param vertices will point to reserved vertex space if vertexCount is
540 * non-zero. Illegal to pass NULL if vertexCount > 0.
541 * @param indices will point to reserved index space if indexCount is
542 * non-zero. Illegal to pass NULL if indexCount > 0.
543 *
544 * @return true if succeeded in allocating space for the vertices and false
545 * if not.
546 */
547 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
548 uint32_t vertexCount,
549 uint32_t indexCount,
550 void** vertices,
551 void** indices);
552 /**
553 * Provides hints to caller about the number of vertices and indices
554 * that can be allocated cheaply. This can be useful if caller is reserving
555 * space but doesn't know exactly how much geometry is needed.
556 *
557 * Also may hint whether the draw target should be flushed first. This is
558 * useful for deferred targets.
559 *
560 * @param vertexLayout layout of vertices caller would like to reserve
561 * @param vertexCount in: hint about how many vertices the caller would
562 * like to allocate.
563 * out: a hint about the number of vertices that can be
564 * allocated cheaply. Negative means no hint.
565 * Ignored if NULL.
566 * @param indexCount in: hint about how many indices the caller would
567 * like to allocate.
568 * out: a hint about the number of indices that can be
569 * allocated cheaply. Negative means no hint.
570 * Ignored if NULL.
571 *
572 * @return true if target should be flushed based on the input values.
573 */
574 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000575 int* vertexCount,
576 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
578 /**
579 * Releases reserved vertex/index data from reserveAndLockGeometry().
580 */
581 void releaseReservedGeometry();
582
583 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000584 * Sets source of vertex data for the next draw. Array must contain
585 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 *
587 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000588 * @param size size of the vertex data.
589 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000591 void setVertexSourceToArray(GrVertexLayout vertexLayout,
592 const void* vertexArray,
593 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000594
595 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000596 * Sets source of index data for the next indexed draw. Array must contain
597 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000599 * @param array cpu array containing index data.
600 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000601 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000602 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000603
604 /**
605 * Sets source of vertex data for the next draw. Data does not have to be
606 * in the buffer until drawIndexed or drawNonIndexed.
607 *
608 * @param buffer vertex buffer containing vertex data. Must be
609 * unlocked before draw call.
610 * @param vertexLayout layout of the vertex data in the buffer.
611 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000612 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
613 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000614
615 /**
616 * Sets source of index data for the next indexed draw. Data does not have
617 * to be in the buffer until drawIndexed or drawNonIndexed.
618 *
619 * @param buffer index buffer containing indices. Must be unlocked
620 * before indexed draw call.
621 */
622 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
623
624 /**
625 * Draws indexed geometry using the current state and current vertex / index
626 * sources.
627 *
628 * @param type The type of primitives to draw.
629 * @param startVertex the vertex in the vertex array/buffer corresponding
630 * to index 0
631 * @param startIndex first index to read from index src.
632 * @param vertexCount one greater than the max index.
633 * @param indexCount the number of index elements to read. The index count
634 * is effectively trimmed to the last completely
635 * specified primitive.
636 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000637 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000638 int startVertex,
639 int startIndex,
640 int vertexCount,
641 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000642
643 /**
644 * Draws non-indexed geometry using the current state and current vertex
645 * sources.
646 *
647 * @param type The type of primitives to draw.
648 * @param startVertex the vertex in the vertex array/buffer corresponding
649 * to index 0
650 * @param vertexCount one greater than the max index.
651 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000652 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000653 int startVertex,
654 int vertexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000655
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000656 /**
657 * Helper function for drawing rects. This does not use the current index
658 * and vertex sources. After returning, the vertex and index sources may
659 * have changed. They should be reestablished before the next drawIndexed
660 * or drawNonIndexed. This cannot be called between reserving and releasing
661 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000662 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000663 * drawNonIndexed.
664 * @param rect the rect to draw
665 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000666 * @param stageEnableBitfield bitmask indicating which stages are enabled.
667 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000668 * @param srcRects specifies rects for stages enabled by stageEnableMask.
669 * if stageEnableMask bit i is 1, srcRects is not NULL,
670 * and srcRects[i] is not NULL, then srcRects[i] will be
671 * used as coordinates for stage i. Otherwise, if stage i
672 * is enabled then rect is used as the coordinates.
673 * @param srcMatrices optional matrices applied to srcRects. If
674 * srcRect[i] is non-NULL and srcMatrices[i] is
675 * non-NULL then srcRect[i] will be transformed by
676 * srcMatrix[i]. srcMatrices can be NULL when no
677 * srcMatrices are desired.
678 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000679 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000680 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000681 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000682 const GrRect* srcRects[],
683 const GrMatrix* srcMatrices[]);
684
685 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000686 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000687 * matrices.
688 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000689 void drawSimpleRect(const GrRect& rect,
690 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000691 StageBitfield stageEnableBitfield) {
692 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000693 }
694
reed@google.comac10a2d2010-12-22 21:39:39 +0000695 ///////////////////////////////////////////////////////////////////////////
696
697 class AutoStateRestore : ::GrNoncopyable {
698 public:
699 AutoStateRestore(GrDrawTarget* target);
700 ~AutoStateRestore();
701
702 private:
703 GrDrawTarget* fDrawTarget;
704 SavedDrawState fDrawState;
705 };
706
707 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000708
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000709 class AutoViewMatrixRestore : ::GrNoncopyable {
710 public:
711 AutoViewMatrixRestore() {
712 fDrawTarget = NULL;
713 }
714
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000715 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000716 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
717 GrAssert(NULL != target);
718 }
719
720 void set(GrDrawTarget* target) {
721 GrAssert(NULL != target);
722 if (NULL != fDrawTarget) {
723 fDrawTarget->setViewMatrix(fMatrix);
724 }
725 fDrawTarget = target;
726 fMatrix = target->getViewMatrix();
727 }
728
729 ~AutoViewMatrixRestore() {
730 if (NULL != fDrawTarget) {
731 fDrawTarget->setViewMatrix(fMatrix);
732 }
733 }
734
735 private:
736 GrDrawTarget* fDrawTarget;
737 GrMatrix fMatrix;
738 };
739
740 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000741
742 class AutoReleaseGeometry : ::GrNoncopyable {
743 public:
744 AutoReleaseGeometry(GrDrawTarget* target,
745 GrVertexLayout vertexLayout,
746 uint32_t vertexCount,
747 uint32_t indexCount) {
748 fTarget = target;
749 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
750 vertexCount,
751 indexCount,
752 &fVertices,
753 &fIndices);
754 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000755
756 AutoReleaseGeometry() {
757 fSuccess = false;
758 }
759
reed@google.comac10a2d2010-12-22 21:39:39 +0000760 ~AutoReleaseGeometry() {
761 if (fSuccess) {
762 fTarget->releaseReservedGeometry();
763 }
764 }
765
bsalomon@google.com5782d712011-01-21 21:03:59 +0000766 bool set(GrDrawTarget* target,
767 GrVertexLayout vertexLayout,
768 uint32_t vertexCount,
769 uint32_t indexCount) {
770 if (fSuccess) {
771 fTarget->releaseReservedGeometry();
772 }
773 fTarget = target;
774 fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
775 vertexCount,
776 indexCount,
777 &fVertices,
778 &fIndices);
779 return fSuccess;
780 }
781
reed@google.comac10a2d2010-12-22 21:39:39 +0000782 bool succeeded() const { return fSuccess; }
783 void* vertices() const { return fVertices; }
784 void* indices() const { return fIndices; }
785
786 GrPoint* positions() const {
787 return static_cast<GrPoint*>(fVertices);
788 }
789
790 private:
791 GrDrawTarget* fTarget;
792 bool fSuccess;
793 void* fVertices;
794 void* fIndices;
795 };
796
797 ///////////////////////////////////////////////////////////////////////////
798
799 class AutoClipRestore : ::GrNoncopyable {
800 public:
801 AutoClipRestore(GrDrawTarget* target) {
802 fTarget = target;
803 fClip = fTarget->getClip();
804 }
805
806 ~AutoClipRestore() {
807 fTarget->setClip(fClip);
808 }
809 private:
810 GrDrawTarget* fTarget;
811 GrClip fClip;
812 };
813
814 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000815 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000816
reed@google.comac10a2d2010-12-22 21:39:39 +0000817 /**
818 * Helper function to compute the size of a vertex from a vertex layout
819 * @return size of a single vertex.
820 */
821 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000822
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000823 /**
824 * Helper function for determining the index of texture coordinates that
825 * is input for a texture stage. Note that a stage may instead use positions
826 * as texture coordinates, in which case the result of the function is
827 * indistinguishable from the case when the stage is disabled.
828 *
829 * @param stage the stage to query
830 * @param vertexLayout layout to query
831 *
832 * @return the texture coordinate index or -1 if the stage doesn't use
833 * separate (non-position) texture coordinates.
834 */
835 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000836
837 /**
838 * Helper function to compute the offset of texture coordinates in a vertex
839 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000840 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000841 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000842 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000843 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000844
845 /**
846 * Helper function to compute the offset of the color in a vertex
847 * @return offset of color in vertex layout or -1 if the
848 * layout has no color.
849 */
850 static int VertexColorOffset(GrVertexLayout vertexLayout);
851
852 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000853 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000854 * coordinates of some index.
855 *
856 * @param coordIndex the tex coord index to query
857 * @param vertexLayout layout to query
858 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000859 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000860 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000861 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000862 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000863 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000864
reed@google.comac10a2d2010-12-22 21:39:39 +0000865 /**
866 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000867 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000868 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000869 * @param stage the stage to query
870 * @param vertexLayout layout to query
871 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000872 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000873 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000874 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000875 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000876
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000877 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000878 * Helper function to compute the size of each vertex and the offsets of
879 * texture coordinates and color. Determines tex coord offsets by tex coord
880 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000881 * by StageTexCoordVertexLayoutBit.)
882 *
883 * @param vertexLayout the layout to query
884 * @param texCoordOffsetsByIdx after return it is the offset of each
885 * tex coord index in the vertex or -1 if
886 * index isn't used.
887 * @return size of a single vertex
888 */
889 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
890 int texCoordOffsetsByIdx[kMaxTexCoords],
891 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000892
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000893 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000894 * Helper function to compute the size of each vertex and the offsets of
895 * texture coordinates and color. Determines tex coord offsets by stage
896 * rather than by index. (Each stage can be mapped to any t.c. index
897 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000898 * tex coords then that stage's offset will be 0 (positions are always at 0).
899 *
900 * @param vertexLayout the layout to query
901 * @param texCoordOffsetsByStage after return it is the offset of each
902 * tex coord index in the vertex or -1 if
903 * index isn't used.
904 * @return size of a single vertex
905 */
906 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
907 int texCoordOffsetsByStage[kNumStages],
908 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000909
910 /**
911 * Accessing positions, texture coords, or colors, of a vertex within an
912 * array is a hassle involving casts and simple math. These helpers exist
913 * to keep GrDrawTarget clients' code a bit nicer looking.
914 */
915
916 /**
917 * Gets a pointer to a GrPoint of a vertex's position or texture
918 * coordinate.
919 * @param vertices the vetex array
920 * @param vertexIndex the index of the vertex in the array
921 * @param vertexSize the size of each vertex in the array
922 * @param offset the offset in bytes of the vertex component.
923 * Defaults to zero (corresponding to vertex position)
924 * @return pointer to the vertex component as a GrPoint
925 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000926 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000927 int vertexIndex,
928 int vertexSize,
929 int offset = 0) {
930 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000931 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000932 vertexIndex * vertexSize);
933 }
934 static const GrPoint* GetVertexPoint(const void* vertices,
935 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000936 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000937 int offset = 0) {
938 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000939 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000940 vertexIndex * vertexSize);
941 }
942
943 /**
944 * Gets a pointer to a GrColor inside a vertex within a vertex array.
945 * @param vertices the vetex array
946 * @param vertexIndex the index of the vertex in the array
947 * @param vertexSize the size of each vertex in the array
948 * @param offset the offset in bytes of the vertex color
949 * @return pointer to the vertex component as a GrColor
950 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000951 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000952 int vertexIndex,
953 int vertexSize,
954 int offset) {
955 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000956 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000957 vertexIndex * vertexSize);
958 }
959 static const GrColor* GetVertexColor(const void* vertices,
960 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000961 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000962 int offset) {
963 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000964 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000965 vertexIndex * vertexSize);
966 }
967
reed@google.comac10a2d2010-12-22 21:39:39 +0000968protected:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000969
reed@google.comac10a2d2010-12-22 21:39:39 +0000970 // Helpers for GrDrawTarget subclasses that won't have private access to
971 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +0000972 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +0000974 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +0000975 { return sds.fState; }
976
977 // implemented by subclass
978 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
979 void** vertices,
980 void** indices) = 0;
981
982 virtual void releaseGeometryHelper() = 0;
983
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000984 // subclass overrides to be notified when clip is set.
985 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000986
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000987 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
988 int vertexCount) = 0;
989
990 virtual void setIndexSourceToArrayHelper(const void* indexArray,
991 int indexCount) = 0;
992
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000993 // Helpers for drawRect, protected so subclasses that override drawRect
994 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000995 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000996 const GrRect* srcRects[]);
997
998 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000999 const GrMatrix* matrix,
1000 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001001 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001002 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001003 void* vertices);
1004
reed@google.comac10a2d2010-12-22 21:39:39 +00001005 enum GeometrySrcType {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001006 kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry
1007 kArray_GeometrySrcType, // src was set using set*SourceToArray
1008 kBuffer_GeometrySrcType // src was set using set*SourceToBuffer
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 };
1010
bsalomon@google.comd302f142011-03-03 13:54:13 +00001011 struct ReservedGeometry {
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 bool fLocked;
1013 uint32_t fVertexCount;
1014 uint32_t fIndexCount;
1015 } fReservedGeometry;
1016
1017 struct GeometrySrc {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001018 GeometrySrcType fVertexSrc;
1019 const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer
1020 GeometrySrcType fIndexSrc;
1021 const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer
1022 GrVertexLayout fVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +00001023 } fGeometrySrc;
1024
1025 GrClip fClip;
1026
reed@google.com8195f672011-01-12 18:14:28 +00001027 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001028
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001029 // Not meant for external use. Only setVertexSourceToBuffer and
1030 // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't
1031 // support nested reserveAndLockGeometry (and cpu arrays internally use the
1032 // same path).
reed@google.comac10a2d2010-12-22 21:39:39 +00001033 class AutoGeometrySrcRestore {
1034 public:
1035 AutoGeometrySrcRestore(GrDrawTarget* target) {
1036 fTarget = target;
1037 fGeometrySrc = fTarget->fGeometrySrc;
1038 }
1039 ~AutoGeometrySrcRestore() {
1040 fTarget->fGeometrySrc = fGeometrySrc;
1041 }
1042 private:
1043 GrDrawTarget *fTarget;
1044 GeometrySrc fGeometrySrc;
1045
1046 AutoGeometrySrcRestore();
1047 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
1048 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
1049 };
1050
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001051private:
1052 void VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +00001053};
1054
1055#endif