blob: 5020e1659cf26757cf6c49d8785173febd3e4cb2 [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
Scroggo97c88c22011-05-11 14:05:25 +000029#include "SkXfermode.h"
30
reed@google.comac10a2d2010-12-22 21:39:39 +000031class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000032class GrClipIterator;
33class GrVertexBuffer;
34class GrIndexBuffer;
junov@google.comf93e7172011-03-31 21:26:24 +000035class GrEffect;
reed@google.comac10a2d2010-12-22 21:39:39 +000036
37class GrDrawTarget : public GrRefCnt {
38public:
39 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000040 * Number of texture stages. Each stage takes as input a color and
41 * 2D texture coordinates. The color input to the first enabled stage is the
42 * per-vertex color or the constant color (setColor/setAlpha) if there are
43 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000044 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000045 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000046 * lookups are specified by a texture a sampler (setSamplerState). Texture
47 * coordinates for each stage come from the vertices based on a
48 * GrVertexLayout bitfield. The output fragment color is the output color of
49 * the last enabled stage. The presence or absence of texture coordinates
50 * for each stage in the vertex layout indicates whether a stage is enabled
51 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000052 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000053 enum {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000054 kNumStages = 2,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000055 kMaxTexCoords = kNumStages
56 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000057
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000058 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +000059 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +000060 */
bsalomon@google.comffca4002011-02-22 20:34:01 +000061 typedef int StageBitfield;
62 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +000063
64 /**
65 * Flags that affect rendering. Controlled using enable/disableState(). All
66 * default to disabled.
67 */
68 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000069 kDither_StateBit = 0x01, //<! Perform color dithering
70 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +000071 // target must support some form of AA
72 // (msaa, coverage sampling, etc). For
73 // GrGpu-created rendertarget/textures
74 // this is controlled by parameters
75 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000076 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +000077 // against the region specified by
78 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000079 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
80 // Useful while performing stencil
81 // ops.
82 kEdgeAA_StateBit = 0x10, //<! Perform edge anti-aliasing.
83 // Requires the edges to be passed in
84 // setEdgeAAData().
bsalomon@google.comd302f142011-03-03 13:54:13 +000085
86 // subclass may use additional bits internally
87 kDummyStateBit,
88 kLastPublicStateBit = kDummyStateBit-1
89 };
90
91 enum DrawFace {
92 kBoth_DrawFace,
93 kCCW_DrawFace,
94 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +000095 };
96
97 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000098 * The DrawTarget may reserve some of the high bits of the stencil. The draw
99 * target will automatically trim reference and mask values so that the
100 * client doesn't overwrite these bits.
101 * The number of bits available is relative to the currently set render
102 *target.
103 * @return the number of bits usable by the draw target client.
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000105 int getUsableStencilBits() const {
106 int bits = fCurrDrawState.fRenderTarget->stencilBits();
107 if (bits) {
108 return bits - 1;
109 } else {
110 return 0;
111 }
112 }
113
114 /**
115 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000116 * Changing the clip has the side-effect of possibly zeroing
117 * out the client settable stencil bits. So multipass algorithms
118 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119 * @param settings the stencil settings to use.
120 */
121 void setStencil(const GrStencilSettings& settings) {
122 fCurrDrawState.fStencilSettings = settings;
123 }
124
125 /**
126 * Shortcut to disable stencil testing and ops.
127 */
128 void disableStencil() {
129 fCurrDrawState.fStencilSettings.setDisabled();
130 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
132protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
reed@google.com8195f672011-01-12 18:14:28 +0000134 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000135 DrState() {
136 // make sure any pad is zero for memcmp
137 // all DrState members should default to something
138 // valid by the memset
139 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000140
141 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000142 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000143 fFirstCoverageStage = kNumStages;
144
145 // pedantic assertion that our ptrs will
146 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000147 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000148
149 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000150 GrAssert(fStencilSettings.isDisabled());
151 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000153 GrBlendCoeff fSrcBlend;
154 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000155 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000156 GrTexture* fTextures[kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000157 GrEffect* fEffects[kNumStages];
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000158 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000159 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000160 GrRenderTarget* fRenderTarget;
161 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000162 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000163 GrColor fColorFilterColor;
164 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000165
166 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000167 GrMatrix fViewMatrix;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000168 float fEdgeAAEdges[18];
reed@google.com8195f672011-01-12 18:14:28 +0000169 bool operator ==(const DrState& s) const {
170 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 }
reed@google.com8195f672011-01-12 18:14:28 +0000172 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 };
174
175public:
176 ///////////////////////////////////////////////////////////////////////////
177
178 GrDrawTarget();
179
180 /**
181 * Sets the current clip to the region specified by clip. All draws will be
182 * clipped against this clip if kClip_StateBit is enabled.
183 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000184 * Setting the clip may (or may not) zero out the client's stencil bits.
185 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 * @param description of the clipping region
187 */
188 void setClip(const GrClip& clip);
189
190 /**
191 * Gets the current clip.
192 *
193 * @return the clip.
194 */
195 const GrClip& getClip() const;
196
197 /**
198 * Sets the texture used at the next drawing call
199 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000200 * @param stage The texture stage for which the texture will be set
201 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 * @param texture The texture to set. Can be NULL though there is no advantage
203 * to settings a NULL texture if doing non-textured drawing
204 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000205 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
207 /**
208 * Retrieves the currently set texture.
209 *
210 * @return The currently set texture. The return value will be NULL if no
211 * texture has been set, NULL was most recently passed to
212 * setTexture, or the last setTexture was destroyed.
213 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214 const GrTexture* getTexture(int stage) const;
215 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000216
217 /**
218 * Sets the rendertarget used at the next drawing call
219 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000220 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 */
222 void setRenderTarget(GrRenderTarget* target);
223
224 /**
225 * Retrieves the currently set rendertarget.
226 *
227 * @return The currently set render target.
228 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000229 const GrRenderTarget* getRenderTarget() const;
230 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
232 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000233 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000235 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000236 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000238 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 * @param samplerState Specifies the sampler state.
240 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000241 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
243 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000244 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000246 * @param stage the stage of the sampler to set
247 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000249 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
250 GrAssert(stage >= 0 && stage < kNumStages);
251 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000252 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
254 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000255 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000256 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000257 * @param stage the stage to of sampler to get
258 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000259 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000260 const GrMatrix& getSamplerMatrix(int stage) const {
261 return fCurrDrawState.fSamplerStates[stage].getMatrix();
262 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000263
264 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000265 * Sets the matrix of a stage's sampler
266 *
267 * @param stage the stage of sampler set
268 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000269 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000270 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000271 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
272 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000273
274 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 * Sets the matrix applied to veretx positions.
276 *
277 * In the post-view-matrix space the rectangle [0,w]x[0,h]
278 * fully covers the render target. (w and h are the width and height of the
279 * the rendertarget.)
280 *
281 * @param m the matrix used to transform the vertex positions.
282 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000283 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
285 /**
286 * Multiplies the current view matrix by a matrix
287 *
288 * After this call V' = V*m where V is the old view matrix,
289 * m is the parameter to this function, and V' is the new view matrix.
290 * (We consider positions to be column vectors so position vector p is
291 * transformed by matrix X as p' = X*p.)
292 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000293 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000294 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000295 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000296
297 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000298 * Multiplies the current view matrix by a matrix
299 *
300 * After this call V' = m*V where V is the old view matrix,
301 * m is the parameter to this function, and V' is the new view matrix.
302 * (We consider positions to be column vectors so position vector p is
303 * transformed by matrix X as p' = X*p.)
304 *
305 * @param m the matrix used to modify the view matrix.
306 */
307 void postConcatViewMatrix(const GrMatrix& m);
308
309 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000310 * Retrieves the current view matrix
311 * @return the current view matrix.
312 */
313 const GrMatrix& getViewMatrix() const;
314
315 /**
316 * Retrieves the inverse of the current view matrix.
317 *
318 * If the current view matrix is invertible, return true, and if matrix
319 * is non-null, copy the inverse into it. If the current view matrix is
320 * non-invertible, return false and ignore the matrix parameter.
321 *
322 * @param matrix if not null, will receive a copy of the current inverse.
323 */
324 bool getViewInverse(GrMatrix* matrix) const;
325
326 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000327 * Sets color for next draw to a premultiplied-alpha color.
328 *
329 * @param the color to set.
330 */
331 void setColor(GrColor);
332
333 /**
Scroggo97c88c22011-05-11 14:05:25 +0000334 * Add a color filter that can be represented by a color and a mode.
335 */
336 void setColorFilter(GrColor, SkXfermode::Mode);
337
338 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000339 * Sets the color to be used for the next draw to be
340 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
341 *
342 * @param alpha The alpha value to set as the color.
343 */
344 void setAlpha(uint8_t alpha);
345
346 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347 * Controls whether clockwise, counterclockwise, or both faces are drawn.
348 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000351
352 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000353 * A common pattern is to compute a color with the initial stages and then
354 * modulate that color by a coverage value in later stage(s) (AA, mask-
355 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
356 * computed based on the pre-coverage-modulated color. The division of
357 * stages between color-computing and coverage-computing is specified by
358 * this method. Initially this is kNumStages (all stages are color-
359 * computing).
360 */
361 void setFirstCoverageStage(int firstCoverageStage) {
362 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
363 }
364
365 /**
366 * Gets the index of the first coverage-computing stage.
367 */
368 int getFirstCoverageStage() const {
369 return fCurrDrawState.fFirstCoverageStage;
370 }
371
372 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 * Gets whether the target is drawing clockwise, counterclockwise,
374 * or both faces.
375 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000377 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000378
379 /**
380 * Enable render state settings.
381 *
382 * @param flags bitfield of StateBits specifing the states to enable
383 */
384 void enableState(uint32_t stateBits);
385
386 /**
387 * Disable render state settings.
388 *
389 * @param flags bitfield of StateBits specifing the states to disable
390 */
391 void disableState(uint32_t stateBits);
392
393 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000394 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
395 }
396
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000397 bool isAntialiasState() const {
398 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
399 }
400
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000401 bool isClipState() const {
402 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000403 }
404
bsalomon@google.comd302f142011-03-03 13:54:13 +0000405 bool isColorWriteDisabled() const {
406 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
407 }
408
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 * Sets the blending function coeffecients.
411 *
412 * The blend function will be:
413 * D' = sat(S*srcCoef + D*dstCoef)
414 *
415 * where D is the existing destination color, S is the incoming source
416 * color, and D' is the new destination color that will be written. sat()
417 * is the saturation function.
418 *
419 * @param srcCoef coeffecient applied to the src color.
420 * @param dstCoef coeffecient applied to the dst color.
421 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000422 void setBlendFunc(GrBlendCoeff srcCoef, GrBlendCoeff dstCoef);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
424 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000425 * Sets the blending function constant referenced by the following blending
426 * coeffecients:
427 * kConstC_BlendCoeff
428 * kIConstC_BlendCoeff
429 * kConstA_BlendCoeff
430 * kIConstA_BlendCoeff
431 *
432 * @param constant the constant to set
433 */
434 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
435
436 /**
437 * Retrieves the last value set by setBlendConstant()
438 * @return the blending constant value
439 */
440 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
441
442 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 * Used to save and restore the GrGpu's drawing state
444 */
445 struct SavedDrawState {
446 private:
reed@google.com8195f672011-01-12 18:14:28 +0000447 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 friend class GrDrawTarget;
449 };
450
451 /**
452 * Saves the current draw state. The state can be restored at a later time
453 * with restoreDrawState.
454 *
455 * See also AutoStateRestore class.
456 *
457 * @param state will hold the state after the function returns.
458 */
459 void saveCurrentDrawState(SavedDrawState* state) const;
460
461 /**
462 * Restores previously saved draw state. The client guarantees that state
463 * was previously passed to saveCurrentDrawState and that the rendertarget
464 * and texture set at save are still valid.
465 *
466 * See also AutoStateRestore class.
467 *
468 * @param state the previously saved state to restore.
469 */
470 void restoreDrawState(const SavedDrawState& state);
471
472 /**
473 * Copies the draw state from another target to this target.
474 *
475 * @param srcTarget draw target used as src of the draw state.
476 */
477 void copyDrawState(const GrDrawTarget& srcTarget);
478
479 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000480 * The format of vertices is represented as a bitfield of flags.
481 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000483 * coordinates and per-vertex colors. Each stage can use any of the texture
484 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000486 * If no texture coordinates are specified for a stage then the stage is
487 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000489 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000490 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000491 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000494 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000495 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000496 * the order would be (position, texture coordinate 1[, color]).
497 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000499 /**
500 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000502 * @param stage the stage that will use texture coordinates.
503 * @param texCoordIdx the index of the texture coordinates to use
504 *
505 * @return the bit to add to a GrVertexLayout bitfield.
506 */
507 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
508 GrAssert(stage < kNumStages);
509 GrAssert(texCoordIdx < kMaxTexCoords);
510 return 1 << (stage + (texCoordIdx * kNumStages));
511 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000512
513 /**
514 * Determines if blend is effectively disabled.
515 *
516 * @return true if blend can be disabled without changing the rendering
517 * result given the current state including the vertex layout specified
518 * with the vertex source.
519 */
520 bool canDisableBlend() const;
521
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000522 /**
523 * Sets the edge data required for edge antialiasing.
524 *
525 * @param edges 3 * 6 float values, representing the edge
526 * equations in Ax + By + C form
527 */
528 void setEdgeAAData(const float edges[18]);
529
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000530private:
531 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
532public:
533 /**
534 * Generates a bit indicating that a texture stage uses the position
535 * as its texture coordinate.
536 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000538 * coordinates.
539 *
540 * @return the bit to add to a GrVertexLayout bitfield.
541 */
542 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
543 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000545 }
546private:
547 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000548
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000549public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000550
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000551 /**
552 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000553 */
554 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000556 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
557 //<! vertices have colors
558 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
559 //<! use text vertices. (Pos
560 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000562 // text [GrGpuTextVertex vs
563 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000564 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000565 kDummyVertexLayoutBit,
566 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000568 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000569 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
571 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000572 * There are three paths for specifying geometry (vertices and optionally
573 * indices) to the draw target. When indexed drawing the indices and vertices
574 * can be each use a different path.
575 *
576 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
577 * caller's client has already provided vertex data in a format
578 * the time compatible with a GrVertexLayout. The array must contain the
579 * data at set*SourceToArray is called. The source stays in effect for
580 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
581 * again or one of the other two paths is chosen.
582 *
583 * 2. Reserve and Lock. This is most useful when the caller has data it must
584 * transform before drawing and will not likely render it again. The
585 * caller requests that the draw target make room for some amount of
586 * vertex and/or index data. The target provides ptrs to hold the data
587 * data. The caller can write the data into the pts up until the first
588 * drawIndexed or drawNonIndexed call. At this point the data is frozen
589 * and the ptrs are no longer guaranteed to be valid. All subsequent
590 * drawIndexed & drawNonIndexed calls will use this data until
591 * releaseReserved geometry is called. This must be called before another
592 * source is set.
593 *
594 * 3. Vertex and Index Buffers. This is most useful for geometry that will
595 * be rendered multiple times. SetVertexSourceToBuffer &
596 * SetIndexSourceToBuffer are used to set the buffer and subsequent
597 * drawIndexed and drawNonIndexed calls use this source until another
598 * source is set.
599 */
600
601 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000602 * Reserves space for vertices and/or indices. Draw target will use
603 * reserved vertices / indices at next draw.
604 *
605 * If succeeds:
606 * if vertexCount is nonzero, *vertices will be the array
607 * of vertices to be filled by caller. The next draw will read
608 * these vertices.
609 *
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000610 * if indexCount is nonzero, *indices will be the array of indices
reed@google.comac10a2d2010-12-22 21:39:39 +0000611 * to be filled by caller. The next indexed draw will read from
612 * these indices.
613 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000614 * If a client does not already have a vertex buffer then this is the
615 * preferred way to allocate vertex/index array. It allows the subclass of
616 * GrDrawTarget to decide whether to put data in buffers, to group vertex
617 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000619 * Following the first draw after reserveAndLockGeometry the ptrs returned
620 * by releaseReservedGeometry are no longer valid and the geometry data
621 * cannot be further modified. The contents that were put in the reserved
622 * space can be drawn by multiple draws, however.
623 *
624 * reserveAndLockGeometry must be matched with a releaseReservedGeometry
625 * call after all draws that reference the reserved geometry data have
626 * been called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 *
628 * AutoGeometryRelease can be used to automatically call the release.
629 *
630 * @param vertexCount the number of vertices to reserve space for. Can be 0.
631 * @param indexCount the number of indices to reserve space for. Can be 0.
632 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
633 * @param vertices will point to reserved vertex space if vertexCount is
634 * non-zero. Illegal to pass NULL if vertexCount > 0.
635 * @param indices will point to reserved index space if indexCount is
636 * non-zero. Illegal to pass NULL if indexCount > 0.
637 *
638 * @return true if succeeded in allocating space for the vertices and false
639 * if not.
640 */
641 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
642 uint32_t vertexCount,
643 uint32_t indexCount,
644 void** vertices,
645 void** indices);
646 /**
647 * Provides hints to caller about the number of vertices and indices
648 * that can be allocated cheaply. This can be useful if caller is reserving
649 * space but doesn't know exactly how much geometry is needed.
650 *
651 * Also may hint whether the draw target should be flushed first. This is
652 * useful for deferred targets.
653 *
654 * @param vertexLayout layout of vertices caller would like to reserve
655 * @param vertexCount in: hint about how many vertices the caller would
656 * like to allocate.
657 * out: a hint about the number of vertices that can be
658 * allocated cheaply. Negative means no hint.
659 * Ignored if NULL.
660 * @param indexCount in: hint about how many indices the caller would
661 * like to allocate.
662 * out: a hint about the number of indices that can be
663 * allocated cheaply. Negative means no hint.
664 * Ignored if NULL.
665 *
666 * @return true if target should be flushed based on the input values.
667 */
668 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000669 int* vertexCount,
670 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000671
672 /**
673 * Releases reserved vertex/index data from reserveAndLockGeometry().
674 */
675 void releaseReservedGeometry();
676
677 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000678 * Sets source of vertex data for the next draw. Array must contain
679 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000680 *
681 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000682 * @param size size of the vertex data.
683 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000684 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000685 void setVertexSourceToArray(GrVertexLayout vertexLayout,
686 const void* vertexArray,
687 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000688
689 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000690 * Sets source of index data for the next indexed draw. Array must contain
691 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000692 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000693 * @param array cpu array containing index data.
694 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000695 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000696 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000697
698 /**
699 * Sets source of vertex data for the next draw. Data does not have to be
700 * in the buffer until drawIndexed or drawNonIndexed.
701 *
702 * @param buffer vertex buffer containing vertex data. Must be
703 * unlocked before draw call.
704 * @param vertexLayout layout of the vertex data in the buffer.
705 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000706 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
707 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000708
709 /**
710 * Sets source of index data for the next indexed draw. Data does not have
711 * to be in the buffer until drawIndexed or drawNonIndexed.
712 *
713 * @param buffer index buffer containing indices. Must be unlocked
714 * before indexed draw call.
715 */
716 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
717
718 /**
719 * Draws indexed geometry using the current state and current vertex / index
720 * sources.
721 *
722 * @param type The type of primitives to draw.
723 * @param startVertex the vertex in the vertex array/buffer corresponding
724 * to index 0
725 * @param startIndex first index to read from index src.
726 * @param vertexCount one greater than the max index.
727 * @param indexCount the number of index elements to read. The index count
728 * is effectively trimmed to the last completely
729 * specified primitive.
730 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000731 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000732 int startVertex,
733 int startIndex,
734 int vertexCount,
735 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000736
737 /**
738 * Draws non-indexed geometry using the current state and current vertex
739 * sources.
740 *
741 * @param type The type of primitives to draw.
742 * @param startVertex the vertex in the vertex array/buffer corresponding
743 * to index 0
744 * @param vertexCount one greater than the max index.
745 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000746 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000747 int startVertex,
748 int vertexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000749
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000750 /**
751 * Helper function for drawing rects. This does not use the current index
752 * and vertex sources. After returning, the vertex and index sources may
753 * have changed. They should be reestablished before the next drawIndexed
754 * or drawNonIndexed. This cannot be called between reserving and releasing
755 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000756 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000757 * drawNonIndexed.
758 * @param rect the rect to draw
759 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000760 * @param stageEnableBitfield bitmask indicating which stages are enabled.
761 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000762 * @param srcRects specifies rects for stages enabled by stageEnableMask.
763 * if stageEnableMask bit i is 1, srcRects is not NULL,
764 * and srcRects[i] is not NULL, then srcRects[i] will be
765 * used as coordinates for stage i. Otherwise, if stage i
766 * is enabled then rect is used as the coordinates.
767 * @param srcMatrices optional matrices applied to srcRects. If
768 * srcRect[i] is non-NULL and srcMatrices[i] is
769 * non-NULL then srcRect[i] will be transformed by
770 * srcMatrix[i]. srcMatrices can be NULL when no
771 * srcMatrices are desired.
772 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000773 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000774 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000775 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000776 const GrRect* srcRects[],
777 const GrMatrix* srcMatrices[]);
778
779 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000780 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000781 * matrices.
782 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000783 void drawSimpleRect(const GrRect& rect,
784 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000785 StageBitfield stageEnableBitfield) {
786 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000787 }
788
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000789 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000790 * Clear the render target. Ignores the clip and all other draw state
791 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
792 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000793 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000794 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000795
reed@google.comac10a2d2010-12-22 21:39:39 +0000796 ///////////////////////////////////////////////////////////////////////////
797
798 class AutoStateRestore : ::GrNoncopyable {
799 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000800 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000801 AutoStateRestore(GrDrawTarget* target);
802 ~AutoStateRestore();
803
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000804 /**
805 * if this object is already saving state for param target then
806 * this does nothing. Otherise, it restores previously saved state on
807 * previous target (if any) and saves current state on param target.
808 */
809 void set(GrDrawTarget* target);
810
reed@google.comac10a2d2010-12-22 21:39:39 +0000811 private:
812 GrDrawTarget* fDrawTarget;
813 SavedDrawState fDrawState;
814 };
815
816 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000817
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000818 class AutoViewMatrixRestore : ::GrNoncopyable {
819 public:
820 AutoViewMatrixRestore() {
821 fDrawTarget = NULL;
822 }
823
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000824 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000825 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
826 GrAssert(NULL != target);
827 }
828
829 void set(GrDrawTarget* target) {
830 GrAssert(NULL != target);
831 if (NULL != fDrawTarget) {
832 fDrawTarget->setViewMatrix(fMatrix);
833 }
834 fDrawTarget = target;
835 fMatrix = target->getViewMatrix();
836 }
837
838 ~AutoViewMatrixRestore() {
839 if (NULL != fDrawTarget) {
840 fDrawTarget->setViewMatrix(fMatrix);
841 }
842 }
843
844 private:
845 GrDrawTarget* fDrawTarget;
846 GrMatrix fMatrix;
847 };
848
849 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000850
851 class AutoReleaseGeometry : ::GrNoncopyable {
852 public:
853 AutoReleaseGeometry(GrDrawTarget* target,
854 GrVertexLayout vertexLayout,
855 uint32_t vertexCount,
856 uint32_t indexCount) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000857 fTarget = NULL;
858 this->set(target, vertexLayout, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000859 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000860
861 AutoReleaseGeometry() {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000862 fTarget = NULL;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000863 }
864
reed@google.comac10a2d2010-12-22 21:39:39 +0000865 ~AutoReleaseGeometry() {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000866 if (NULL != fTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000867 fTarget->releaseReservedGeometry();
868 }
869 }
870
bsalomon@google.com5782d712011-01-21 21:03:59 +0000871 bool set(GrDrawTarget* target,
872 GrVertexLayout vertexLayout,
873 uint32_t vertexCount,
874 uint32_t indexCount) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000875 if (NULL != fTarget) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000876 fTarget->releaseReservedGeometry();
877 }
878 fTarget = target;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000879 if (NULL != fTarget) {
880 if (!fTarget->reserveAndLockGeometry(vertexLayout,
881 vertexCount,
882 indexCount,
883 &fVertices,
884 &fIndices)) {
885 fTarget = NULL;
886 }
887 }
888 return NULL != fTarget;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000889 }
890
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000891 bool succeeded() const { return NULL != fTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000892 void* vertices() const { return fVertices; }
893 void* indices() const { return fIndices; }
894
895 GrPoint* positions() const {
896 return static_cast<GrPoint*>(fVertices);
897 }
898
899 private:
900 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000901 void* fVertices;
902 void* fIndices;
903 };
904
905 ///////////////////////////////////////////////////////////////////////////
906
907 class AutoClipRestore : ::GrNoncopyable {
908 public:
909 AutoClipRestore(GrDrawTarget* target) {
910 fTarget = target;
911 fClip = fTarget->getClip();
912 }
913
914 ~AutoClipRestore() {
915 fTarget->setClip(fClip);
916 }
917 private:
918 GrDrawTarget* fTarget;
919 GrClip fClip;
920 };
921
922 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000923 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000924
reed@google.comac10a2d2010-12-22 21:39:39 +0000925 /**
926 * Helper function to compute the size of a vertex from a vertex layout
927 * @return size of a single vertex.
928 */
929 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000930
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000931 /**
932 * Helper function for determining the index of texture coordinates that
933 * is input for a texture stage. Note that a stage may instead use positions
934 * as texture coordinates, in which case the result of the function is
935 * indistinguishable from the case when the stage is disabled.
936 *
937 * @param stage the stage to query
938 * @param vertexLayout layout to query
939 *
940 * @return the texture coordinate index or -1 if the stage doesn't use
941 * separate (non-position) texture coordinates.
942 */
943 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000944
945 /**
946 * Helper function to compute the offset of texture coordinates in a vertex
947 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000948 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000949 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000950 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000951 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000952
953 /**
954 * Helper function to compute the offset of the color in a vertex
955 * @return offset of color in vertex layout or -1 if the
956 * layout has no color.
957 */
958 static int VertexColorOffset(GrVertexLayout vertexLayout);
959
960 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000961 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000962 * coordinates of some index.
963 *
964 * @param coordIndex the tex coord index to query
965 * @param vertexLayout layout to query
966 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000967 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000968 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000969 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000970 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000971 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000972
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 /**
974 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000975 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000976 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000977 * @param stage the stage to query
978 * @param vertexLayout layout to query
979 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000980 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000981 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000983 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000985 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000986 * Helper function to compute the size of each vertex and the offsets of
987 * texture coordinates and color. Determines tex coord offsets by tex coord
988 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000989 * by StageTexCoordVertexLayoutBit.)
990 *
991 * @param vertexLayout the layout to query
992 * @param texCoordOffsetsByIdx after return it is the offset of each
993 * tex coord index in the vertex or -1 if
994 * index isn't used.
995 * @return size of a single vertex
996 */
997 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
998 int texCoordOffsetsByIdx[kMaxTexCoords],
999 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001000
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001001 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001002 * Helper function to compute the size of each vertex and the offsets of
1003 * texture coordinates and color. Determines tex coord offsets by stage
1004 * rather than by index. (Each stage can be mapped to any t.c. index
1005 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001006 * tex coords then that stage's offset will be 0 (positions are always at 0).
1007 *
1008 * @param vertexLayout the layout to query
1009 * @param texCoordOffsetsByStage after return it is the offset of each
1010 * tex coord index in the vertex or -1 if
1011 * index isn't used.
1012 * @return size of a single vertex
1013 */
1014 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1015 int texCoordOffsetsByStage[kNumStages],
1016 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001017
1018 /**
1019 * Accessing positions, texture coords, or colors, of a vertex within an
1020 * array is a hassle involving casts and simple math. These helpers exist
1021 * to keep GrDrawTarget clients' code a bit nicer looking.
1022 */
1023
1024 /**
1025 * Gets a pointer to a GrPoint of a vertex's position or texture
1026 * coordinate.
1027 * @param vertices the vetex array
1028 * @param vertexIndex the index of the vertex in the array
1029 * @param vertexSize the size of each vertex in the array
1030 * @param offset the offset in bytes of the vertex component.
1031 * Defaults to zero (corresponding to vertex position)
1032 * @return pointer to the vertex component as a GrPoint
1033 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001034 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001035 int vertexIndex,
1036 int vertexSize,
1037 int offset = 0) {
1038 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001039 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001040 vertexIndex * vertexSize);
1041 }
1042 static const GrPoint* GetVertexPoint(const void* vertices,
1043 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001044 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001045 int offset = 0) {
1046 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001047 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001048 vertexIndex * vertexSize);
1049 }
1050
1051 /**
1052 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1053 * @param vertices the vetex array
1054 * @param vertexIndex the index of the vertex in the array
1055 * @param vertexSize the size of each vertex in the array
1056 * @param offset the offset in bytes of the vertex color
1057 * @return pointer to the vertex component as a GrColor
1058 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001059 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001060 int vertexIndex,
1061 int vertexSize,
1062 int offset) {
1063 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001064 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001065 vertexIndex * vertexSize);
1066 }
1067 static const GrColor* GetVertexColor(const void* vertices,
1068 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001069 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001070 int offset) {
1071 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001072 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001073 vertexIndex * vertexSize);
1074 }
1075
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001076 static void VertexLayoutUnitTest();
1077
reed@google.comac10a2d2010-12-22 21:39:39 +00001078protected:
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001079 // given a vertex layout and a draw state, will a stage be used?
1080 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
1081 const DrState& state) {
1082 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1083 }
1084
1085 bool isStageEnabled(int stage) const {
1086 return StageWillBeUsed(stage, fGeometrySrc.fVertexLayout, fCurrDrawState);
1087 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001088
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 // Helpers for GrDrawTarget subclasses that won't have private access to
1090 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001091 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001092 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001093 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 { return sds.fState; }
1095
1096 // implemented by subclass
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001097 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
1098 void** vertices,
1099 void** indices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001101 virtual void onReleaseGeometry() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001103 // subclass overrides to be notified when clip is set.
1104 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001105
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001106 virtual void onSetVertexSourceToArray(const void* vertexArray,
1107 int vertexCount) = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001108
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001109 virtual void onSetIndexSourceToArray(const void* indexArray,
1110 int indexCount) = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001111
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001112 // Helpers for drawRect, protected so subclasses that override drawRect
1113 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001114 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001115 const GrRect* srcRects[]);
1116
1117 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001118 const GrMatrix* matrix,
1119 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001120 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001121 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001122 void* vertices);
1123
reed@google.comac10a2d2010-12-22 21:39:39 +00001124 enum GeometrySrcType {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001125 kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry
1126 kArray_GeometrySrcType, // src was set using set*SourceToArray
1127 kBuffer_GeometrySrcType // src was set using set*SourceToBuffer
reed@google.comac10a2d2010-12-22 21:39:39 +00001128 };
1129
bsalomon@google.comd302f142011-03-03 13:54:13 +00001130 struct ReservedGeometry {
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 bool fLocked;
1132 uint32_t fVertexCount;
1133 uint32_t fIndexCount;
1134 } fReservedGeometry;
1135
1136 struct GeometrySrc {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001137 GeometrySrcType fVertexSrc;
1138 const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer
1139 GeometrySrcType fIndexSrc;
1140 const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer
1141 GrVertexLayout fVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 } fGeometrySrc;
1143
1144 GrClip fClip;
1145
reed@google.com8195f672011-01-12 18:14:28 +00001146 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001148 // Not meant for external use. Only setVertexSourceToBuffer and
1149 // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't
1150 // support nested reserveAndLockGeometry (and cpu arrays internally use the
1151 // same path).
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 class AutoGeometrySrcRestore {
1153 public:
1154 AutoGeometrySrcRestore(GrDrawTarget* target) {
1155 fTarget = target;
1156 fGeometrySrc = fTarget->fGeometrySrc;
1157 }
1158 ~AutoGeometrySrcRestore() {
1159 fTarget->fGeometrySrc = fGeometrySrc;
1160 }
1161 private:
1162 GrDrawTarget *fTarget;
1163 GeometrySrc fGeometrySrc;
1164
1165 AutoGeometrySrcRestore();
1166 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
1167 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
1168 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001169};
1170
1171#endif