blob: 985cca7c1b46839e67c2ca9f80689d4a418fee38 [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;
35
36class GrDrawTarget : public GrRefCnt {
37public:
38 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000039 * Number of texture stages. Each stage takes as input a color and
40 * 2D texture coordinates. The color input to the first enabled stage is the
41 * per-vertex color or the constant color (setColor/setAlpha) if there are
42 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000043 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000044 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000045 * lookups are specified by a texture a sampler (setSamplerState). Texture
46 * coordinates for each stage come from the vertices based on a
47 * GrVertexLayout bitfield. The output fragment color is the output color of
48 * the last enabled stage. The presence or absence of texture coordinates
49 * for each stage in the vertex layout indicates whether a stage is enabled
50 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000051 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000052 enum {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000053 kNumStages = 3,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000054 kMaxTexCoords = kNumStages
55 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000056
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000057 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +000058 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +000059 */
bsalomon@google.comffca4002011-02-22 20:34:01 +000060 typedef int StageBitfield;
61 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +000062
63 /**
64 * Flags that affect rendering. Controlled using enable/disableState(). All
65 * default to disabled.
66 */
67 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000068 kDither_StateBit = 0x01, //<! Perform color dithering
69 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +000070 // target must support some form of AA
71 // (msaa, coverage sampling, etc). For
72 // GrGpu-created rendertarget/textures
73 // this is controlled by parameters
74 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000075 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +000076 // against the region specified by
77 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000078 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
79 // Useful while performing stencil
80 // ops.
81 kEdgeAA_StateBit = 0x10, //<! Perform edge anti-aliasing.
82 // Requires the edges to be passed in
83 // setEdgeAAData().
bsalomon@google.comd302f142011-03-03 13:54:13 +000084
85 // subclass may use additional bits internally
86 kDummyStateBit,
87 kLastPublicStateBit = kDummyStateBit-1
88 };
89
90 enum DrawFace {
91 kBoth_DrawFace,
92 kCCW_DrawFace,
93 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +000094 };
95
96 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000097 * The DrawTarget may reserve some of the high bits of the stencil. The draw
98 * target will automatically trim reference and mask values so that the
99 * client doesn't overwrite these bits.
100 * The number of bits available is relative to the currently set render
101 *target.
102 * @return the number of bits usable by the draw target client.
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000104 int getUsableStencilBits() const {
105 int bits = fCurrDrawState.fRenderTarget->stencilBits();
106 if (bits) {
107 return bits - 1;
108 } else {
109 return 0;
110 }
111 }
112
113 /**
114 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000115 * Changing the clip has the side-effect of possibly zeroing
116 * out the client settable stencil bits. So multipass algorithms
117 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000118 * @param settings the stencil settings to use.
119 */
120 void setStencil(const GrStencilSettings& settings) {
121 fCurrDrawState.fStencilSettings = settings;
122 }
123
124 /**
125 * Shortcut to disable stencil testing and ops.
126 */
127 void disableStencil() {
128 fCurrDrawState.fStencilSettings.setDisabled();
129 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
131protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
reed@google.com8195f672011-01-12 18:14:28 +0000133 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134 DrState() {
135 // make sure any pad is zero for memcmp
136 // all DrState members should default to something
137 // valid by the memset
138 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000139
140 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000141 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000142 fFirstCoverageStage = kNumStages;
143
144 // pedantic assertion that our ptrs will
145 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000146 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000147
148 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000149 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000150 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000151 }
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];
157 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000158 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000159 GrRenderTarget* fRenderTarget;
160 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000161 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000162 GrColor fColorFilterColor;
163 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000164
165 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000166 GrMatrix fViewMatrix;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000167 float fEdgeAAEdges[18];
reed@google.com8195f672011-01-12 18:14:28 +0000168 bool operator ==(const DrState& s) const {
169 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 }
reed@google.com8195f672011-01-12 18:14:28 +0000171 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 };
173
174public:
175 ///////////////////////////////////////////////////////////////////////////
176
177 GrDrawTarget();
178
179 /**
180 * Sets the current clip to the region specified by clip. All draws will be
181 * clipped against this clip if kClip_StateBit is enabled.
182 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000183 * Setting the clip may (or may not) zero out the client's stencil bits.
184 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 * @param description of the clipping region
186 */
187 void setClip(const GrClip& clip);
188
189 /**
190 * Gets the current clip.
191 *
192 * @return the clip.
193 */
194 const GrClip& getClip() const;
195
196 /**
197 * Sets the texture used at the next drawing call
198 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000199 * @param stage The texture stage for which the texture will be set
200 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 * @param texture The texture to set. Can be NULL though there is no advantage
202 * to settings a NULL texture if doing non-textured drawing
203 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000204 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000205
206 /**
207 * Retrieves the currently set texture.
208 *
209 * @return The currently set texture. The return value will be NULL if no
210 * texture has been set, NULL was most recently passed to
211 * setTexture, or the last setTexture was destroyed.
212 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000213 const GrTexture* getTexture(int stage) const;
214 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
216 /**
217 * Sets the rendertarget used at the next drawing call
218 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 */
221 void setRenderTarget(GrRenderTarget* target);
222
223 /**
224 * Retrieves the currently set rendertarget.
225 *
226 * @return The currently set render target.
227 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000228 const GrRenderTarget* getRenderTarget() const;
229 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
231 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000232 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000234 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000235 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000236 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000237 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 * @param samplerState Specifies the sampler state.
239 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000240 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
242 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000243 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000245 * @param stage the stage of the sampler to set
246 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000248 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
249 GrAssert(stage >= 0 && stage < kNumStages);
250 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000251 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000252
253 /**
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000254 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
255 * matrix
256 */
257 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
258 for (int i = 0; i < kNumStages; ++i) {
259 if ((1 << i) & stageMask) {
260 this->preConcatSamplerMatrix(i, matrix);
261 }
262 }
263 }
264
265 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000266 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000267 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000268 * @param stage the stage to of sampler to get
269 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000270 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000271 const GrMatrix& getSamplerMatrix(int stage) const {
272 return fCurrDrawState.fSamplerStates[stage].getMatrix();
273 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000274
275 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000276 * Sets the matrix of a stage's sampler
277 *
278 * @param stage the stage of sampler set
279 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000280 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000281 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000282 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
283 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000284
285 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 * Sets the matrix applied to veretx positions.
287 *
288 * In the post-view-matrix space the rectangle [0,w]x[0,h]
289 * fully covers the render target. (w and h are the width and height of the
290 * the rendertarget.)
291 *
292 * @param m the matrix used to transform the vertex positions.
293 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000294 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000295
296 /**
297 * Multiplies the current view matrix by a matrix
298 *
299 * After this call V' = V*m where V is the old view matrix,
300 * m is the parameter to this function, and V' is the new view matrix.
301 * (We consider positions to be column vectors so position vector p is
302 * transformed by matrix X as p' = X*p.)
303 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000304 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000306 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
308 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000309 * Multiplies the current view matrix by a matrix
310 *
311 * After this call V' = m*V where V is the old view matrix,
312 * m is the parameter to this function, and V' is the new view matrix.
313 * (We consider positions to be column vectors so position vector p is
314 * transformed by matrix X as p' = X*p.)
315 *
316 * @param m the matrix used to modify the view matrix.
317 */
318 void postConcatViewMatrix(const GrMatrix& m);
319
320 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000321 * Retrieves the current view matrix
322 * @return the current view matrix.
323 */
324 const GrMatrix& getViewMatrix() const;
325
326 /**
327 * Retrieves the inverse of the current view matrix.
328 *
329 * If the current view matrix is invertible, return true, and if matrix
330 * is non-null, copy the inverse into it. If the current view matrix is
331 * non-invertible, return false and ignore the matrix parameter.
332 *
333 * @param matrix if not null, will receive a copy of the current inverse.
334 */
335 bool getViewInverse(GrMatrix* matrix) const;
336
337 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 * Sets color for next draw to a premultiplied-alpha color.
339 *
340 * @param the color to set.
341 */
342 void setColor(GrColor);
343
344 /**
Scroggo97c88c22011-05-11 14:05:25 +0000345 * Add a color filter that can be represented by a color and a mode.
346 */
347 void setColorFilter(GrColor, SkXfermode::Mode);
348
349 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 * Sets the color to be used for the next draw to be
351 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
352 *
353 * @param alpha The alpha value to set as the color.
354 */
355 void setAlpha(uint8_t alpha);
356
357 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358 * Controls whether clockwise, counterclockwise, or both faces are drawn.
359 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000360 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
363 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000364 * A common pattern is to compute a color with the initial stages and then
365 * modulate that color by a coverage value in later stage(s) (AA, mask-
366 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
367 * computed based on the pre-coverage-modulated color. The division of
368 * stages between color-computing and coverage-computing is specified by
369 * this method. Initially this is kNumStages (all stages are color-
370 * computing).
371 */
372 void setFirstCoverageStage(int firstCoverageStage) {
373 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
374 }
375
376 /**
377 * Gets the index of the first coverage-computing stage.
378 */
379 int getFirstCoverageStage() const {
380 return fCurrDrawState.fFirstCoverageStage;
381 }
382
383 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000384 * Gets whether the target is drawing clockwise, counterclockwise,
385 * or both faces.
386 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000388 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
390 /**
391 * Enable render state settings.
392 *
393 * @param flags bitfield of StateBits specifing the states to enable
394 */
395 void enableState(uint32_t stateBits);
396
397 /**
398 * Disable render state settings.
399 *
400 * @param flags bitfield of StateBits specifing the states to disable
401 */
402 void disableState(uint32_t stateBits);
403
404 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000405 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
406 }
407
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000408 bool isAntialiasState() const {
409 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
410 }
411
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000412 bool isClipState() const {
413 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 }
415
bsalomon@google.comd302f142011-03-03 13:54:13 +0000416 bool isColorWriteDisabled() const {
417 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
418 }
419
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000421 * Sets the blending function coeffecients.
422 *
423 * The blend function will be:
424 * D' = sat(S*srcCoef + D*dstCoef)
425 *
426 * where D is the existing destination color, S is the incoming source
427 * color, and D' is the new destination color that will be written. sat()
428 * is the saturation function.
429 *
430 * @param srcCoef coeffecient applied to the src color.
431 * @param dstCoef coeffecient applied to the dst color.
432 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000433 void setBlendFunc(GrBlendCoeff srcCoef, GrBlendCoeff dstCoef);
reed@google.comac10a2d2010-12-22 21:39:39 +0000434
435 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000436 * Sets the blending function constant referenced by the following blending
437 * coeffecients:
438 * kConstC_BlendCoeff
439 * kIConstC_BlendCoeff
440 * kConstA_BlendCoeff
441 * kIConstA_BlendCoeff
442 *
443 * @param constant the constant to set
444 */
445 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
446
447 /**
448 * Retrieves the last value set by setBlendConstant()
449 * @return the blending constant value
450 */
451 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
452
453 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 * Used to save and restore the GrGpu's drawing state
455 */
456 struct SavedDrawState {
457 private:
reed@google.com8195f672011-01-12 18:14:28 +0000458 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 friend class GrDrawTarget;
460 };
461
462 /**
463 * Saves the current draw state. The state can be restored at a later time
464 * with restoreDrawState.
465 *
466 * See also AutoStateRestore class.
467 *
468 * @param state will hold the state after the function returns.
469 */
470 void saveCurrentDrawState(SavedDrawState* state) const;
471
472 /**
473 * Restores previously saved draw state. The client guarantees that state
474 * was previously passed to saveCurrentDrawState and that the rendertarget
475 * and texture set at save are still valid.
476 *
477 * See also AutoStateRestore class.
478 *
479 * @param state the previously saved state to restore.
480 */
481 void restoreDrawState(const SavedDrawState& state);
482
483 /**
484 * Copies the draw state from another target to this target.
485 *
486 * @param srcTarget draw target used as src of the draw state.
487 */
488 void copyDrawState(const GrDrawTarget& srcTarget);
489
490 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000491 * The format of vertices is represented as a bitfield of flags.
492 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000494 * coordinates and per-vertex colors. Each stage can use any of the texture
495 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000497 * If no texture coordinates are specified for a stage then the stage is
498 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000500 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000502 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000504 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000505 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000506 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000507 * the order would be (position, texture coordinate 1[, color]).
508 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000509
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000510 /**
511 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000512 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000513 * @param stage the stage that will use texture coordinates.
514 * @param texCoordIdx the index of the texture coordinates to use
515 *
516 * @return the bit to add to a GrVertexLayout bitfield.
517 */
518 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
519 GrAssert(stage < kNumStages);
520 GrAssert(texCoordIdx < kMaxTexCoords);
521 return 1 << (stage + (texCoordIdx * kNumStages));
522 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000523
524 /**
525 * Determines if blend is effectively disabled.
526 *
527 * @return true if blend can be disabled without changing the rendering
528 * result given the current state including the vertex layout specified
529 * with the vertex source.
530 */
531 bool canDisableBlend() const;
532
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000533 /**
534 * Sets the edge data required for edge antialiasing.
535 *
536 * @param edges 3 * 6 float values, representing the edge
537 * equations in Ax + By + C form
538 */
539 void setEdgeAAData(const float edges[18]);
540
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000541private:
542 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
543public:
544 /**
545 * Generates a bit indicating that a texture stage uses the position
546 * as its texture coordinate.
547 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000548 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000549 * coordinates.
550 *
551 * @return the bit to add to a GrVertexLayout bitfield.
552 */
553 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
554 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000556 }
557private:
558 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000559
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000560public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000562 /**
563 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000564 */
565 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000566
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000567 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
568 //<! vertices have colors
569 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
570 //<! use text vertices. (Pos
571 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000572 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000573 // text [GrGpuTextVertex vs
574 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000576 kDummyVertexLayoutBit,
577 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000579 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000580 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000581
582 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000583 * There are three paths for specifying geometry (vertices and optionally
584 * indices) to the draw target. When indexed drawing the indices and vertices
585 * can be each use a different path.
586 *
587 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
588 * caller's client has already provided vertex data in a format
589 * the time compatible with a GrVertexLayout. The array must contain the
590 * data at set*SourceToArray is called. The source stays in effect for
591 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
592 * again or one of the other two paths is chosen.
593 *
594 * 2. Reserve and Lock. This is most useful when the caller has data it must
595 * transform before drawing and will not likely render it again. The
596 * caller requests that the draw target make room for some amount of
597 * vertex and/or index data. The target provides ptrs to hold the data
598 * data. The caller can write the data into the pts up until the first
599 * drawIndexed or drawNonIndexed call. At this point the data is frozen
600 * and the ptrs are no longer guaranteed to be valid. All subsequent
601 * drawIndexed & drawNonIndexed calls will use this data until
602 * releaseReserved geometry is called. This must be called before another
603 * source is set.
604 *
605 * 3. Vertex and Index Buffers. This is most useful for geometry that will
606 * be rendered multiple times. SetVertexSourceToBuffer &
607 * SetIndexSourceToBuffer are used to set the buffer and subsequent
608 * drawIndexed and drawNonIndexed calls use this source until another
609 * source is set.
610 */
611
612 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 * Reserves space for vertices and/or indices. Draw target will use
614 * reserved vertices / indices at next draw.
615 *
616 * If succeeds:
617 * if vertexCount is nonzero, *vertices will be the array
618 * of vertices to be filled by caller. The next draw will read
619 * these vertices.
620 *
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000621 * if indexCount is nonzero, *indices will be the array of indices
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 * to be filled by caller. The next indexed draw will read from
623 * these indices.
624 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000625 * If a client does not already have a vertex buffer then this is the
626 * preferred way to allocate vertex/index array. It allows the subclass of
627 * GrDrawTarget to decide whether to put data in buffers, to group vertex
628 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000629 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000630 * Following the first draw after reserveAndLockGeometry the ptrs returned
631 * by releaseReservedGeometry are no longer valid and the geometry data
632 * cannot be further modified. The contents that were put in the reserved
633 * space can be drawn by multiple draws, however.
634 *
635 * reserveAndLockGeometry must be matched with a releaseReservedGeometry
636 * call after all draws that reference the reserved geometry data have
637 * been called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 *
639 * AutoGeometryRelease can be used to automatically call the release.
640 *
641 * @param vertexCount the number of vertices to reserve space for. Can be 0.
642 * @param indexCount the number of indices to reserve space for. Can be 0.
643 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
644 * @param vertices will point to reserved vertex space if vertexCount is
645 * non-zero. Illegal to pass NULL if vertexCount > 0.
646 * @param indices will point to reserved index space if indexCount is
647 * non-zero. Illegal to pass NULL if indexCount > 0.
648 *
649 * @return true if succeeded in allocating space for the vertices and false
650 * if not.
651 */
652 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
653 uint32_t vertexCount,
654 uint32_t indexCount,
655 void** vertices,
656 void** indices);
657 /**
658 * Provides hints to caller about the number of vertices and indices
659 * that can be allocated cheaply. This can be useful if caller is reserving
660 * space but doesn't know exactly how much geometry is needed.
661 *
662 * Also may hint whether the draw target should be flushed first. This is
663 * useful for deferred targets.
664 *
665 * @param vertexLayout layout of vertices caller would like to reserve
666 * @param vertexCount in: hint about how many vertices the caller would
667 * like to allocate.
668 * out: a hint about the number of vertices that can be
669 * allocated cheaply. Negative means no hint.
670 * Ignored if NULL.
671 * @param indexCount in: hint about how many indices the caller would
672 * like to allocate.
673 * out: a hint about the number of indices that can be
674 * allocated cheaply. Negative means no hint.
675 * Ignored if NULL.
676 *
677 * @return true if target should be flushed based on the input values.
678 */
679 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000680 int* vertexCount,
681 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000682
683 /**
684 * Releases reserved vertex/index data from reserveAndLockGeometry().
685 */
686 void releaseReservedGeometry();
687
688 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000689 * Sets source of vertex data for the next draw. Array must contain
690 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000691 *
692 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000693 * @param size size of the vertex data.
694 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000695 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000696 void setVertexSourceToArray(GrVertexLayout vertexLayout,
697 const void* vertexArray,
698 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000699
700 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000701 * Sets source of index data for the next indexed draw. Array must contain
702 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000704 * @param array cpu array containing index data.
705 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000706 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000707 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000708
709 /**
710 * Sets source of vertex data for the next draw. Data does not have to be
711 * in the buffer until drawIndexed or drawNonIndexed.
712 *
713 * @param buffer vertex buffer containing vertex data. Must be
714 * unlocked before draw call.
715 * @param vertexLayout layout of the vertex data in the buffer.
716 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000717 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
718 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000719
720 /**
721 * Sets source of index data for the next indexed draw. Data does not have
722 * to be in the buffer until drawIndexed or drawNonIndexed.
723 *
724 * @param buffer index buffer containing indices. Must be unlocked
725 * before indexed draw call.
726 */
727 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
728
729 /**
730 * Draws indexed geometry using the current state and current vertex / index
731 * sources.
732 *
733 * @param type The type of primitives to draw.
734 * @param startVertex the vertex in the vertex array/buffer corresponding
735 * to index 0
736 * @param startIndex first index to read from index src.
737 * @param vertexCount one greater than the max index.
738 * @param indexCount the number of index elements to read. The index count
739 * is effectively trimmed to the last completely
740 * specified primitive.
741 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000742 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000743 int startVertex,
744 int startIndex,
745 int vertexCount,
746 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000747
748 /**
749 * Draws non-indexed geometry using the current state and current vertex
750 * sources.
751 *
752 * @param type The type of primitives to draw.
753 * @param startVertex the vertex in the vertex array/buffer corresponding
754 * to index 0
755 * @param vertexCount one greater than the max index.
756 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000757 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000758 int startVertex,
759 int vertexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000760
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000761 /**
762 * Helper function for drawing rects. This does not use the current index
763 * and vertex sources. After returning, the vertex and index sources may
764 * have changed. They should be reestablished before the next drawIndexed
765 * or drawNonIndexed. This cannot be called between reserving and releasing
766 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000767 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000768 * drawNonIndexed.
769 * @param rect the rect to draw
770 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000771 * @param stageEnableBitfield bitmask indicating which stages are enabled.
772 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000773 * @param srcRects specifies rects for stages enabled by stageEnableMask.
774 * if stageEnableMask bit i is 1, srcRects is not NULL,
775 * and srcRects[i] is not NULL, then srcRects[i] will be
776 * used as coordinates for stage i. Otherwise, if stage i
777 * is enabled then rect is used as the coordinates.
778 * @param srcMatrices optional matrices applied to srcRects. If
779 * srcRect[i] is non-NULL and srcMatrices[i] is
780 * non-NULL then srcRect[i] will be transformed by
781 * srcMatrix[i]. srcMatrices can be NULL when no
782 * srcMatrices are desired.
783 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000784 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000785 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000786 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000787 const GrRect* srcRects[],
788 const GrMatrix* srcMatrices[]);
789
790 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000791 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000792 * matrices.
793 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000794 void drawSimpleRect(const GrRect& rect,
795 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000796 StageBitfield stageEnableBitfield) {
797 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000798 }
799
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000800 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000801 * Clear the render target. Ignores the clip and all other draw state
802 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
803 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000804 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000805 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000806
reed@google.comac10a2d2010-12-22 21:39:39 +0000807 ///////////////////////////////////////////////////////////////////////////
808
809 class AutoStateRestore : ::GrNoncopyable {
810 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000811 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000812 AutoStateRestore(GrDrawTarget* target);
813 ~AutoStateRestore();
814
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000815 /**
816 * if this object is already saving state for param target then
817 * this does nothing. Otherise, it restores previously saved state on
818 * previous target (if any) and saves current state on param target.
819 */
820 void set(GrDrawTarget* target);
821
reed@google.comac10a2d2010-12-22 21:39:39 +0000822 private:
823 GrDrawTarget* fDrawTarget;
824 SavedDrawState fDrawState;
825 };
826
827 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000828
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000829 class AutoViewMatrixRestore : ::GrNoncopyable {
830 public:
831 AutoViewMatrixRestore() {
832 fDrawTarget = NULL;
833 }
834
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000835 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000836 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
837 GrAssert(NULL != target);
838 }
839
840 void set(GrDrawTarget* target) {
841 GrAssert(NULL != target);
842 if (NULL != fDrawTarget) {
843 fDrawTarget->setViewMatrix(fMatrix);
844 }
845 fDrawTarget = target;
846 fMatrix = target->getViewMatrix();
847 }
848
849 ~AutoViewMatrixRestore() {
850 if (NULL != fDrawTarget) {
851 fDrawTarget->setViewMatrix(fMatrix);
852 }
853 }
854
855 private:
856 GrDrawTarget* fDrawTarget;
857 GrMatrix fMatrix;
858 };
859
860 ///////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000861
862 class AutoReleaseGeometry : ::GrNoncopyable {
863 public:
864 AutoReleaseGeometry(GrDrawTarget* target,
865 GrVertexLayout vertexLayout,
866 uint32_t vertexCount,
867 uint32_t indexCount) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000868 fTarget = NULL;
869 this->set(target, vertexLayout, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000870 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000871
872 AutoReleaseGeometry() {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000873 fTarget = NULL;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000874 }
875
reed@google.comac10a2d2010-12-22 21:39:39 +0000876 ~AutoReleaseGeometry() {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000877 if (NULL != fTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000878 fTarget->releaseReservedGeometry();
879 }
880 }
881
bsalomon@google.com5782d712011-01-21 21:03:59 +0000882 bool set(GrDrawTarget* target,
883 GrVertexLayout vertexLayout,
884 uint32_t vertexCount,
885 uint32_t indexCount) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000886 if (NULL != fTarget) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000887 fTarget->releaseReservedGeometry();
888 }
889 fTarget = target;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000890 if (NULL != fTarget) {
891 if (!fTarget->reserveAndLockGeometry(vertexLayout,
892 vertexCount,
893 indexCount,
894 &fVertices,
895 &fIndices)) {
896 fTarget = NULL;
897 }
898 }
899 return NULL != fTarget;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000900 }
901
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000902 bool succeeded() const { return NULL != fTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000903 void* vertices() const { return fVertices; }
904 void* indices() const { return fIndices; }
905
906 GrPoint* positions() const {
907 return static_cast<GrPoint*>(fVertices);
908 }
909
910 private:
911 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000912 void* fVertices;
913 void* fIndices;
914 };
915
916 ///////////////////////////////////////////////////////////////////////////
917
918 class AutoClipRestore : ::GrNoncopyable {
919 public:
920 AutoClipRestore(GrDrawTarget* target) {
921 fTarget = target;
922 fClip = fTarget->getClip();
923 }
924
925 ~AutoClipRestore() {
926 fTarget->setClip(fClip);
927 }
928 private:
929 GrDrawTarget* fTarget;
930 GrClip fClip;
931 };
932
933 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000934 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +0000935
reed@google.comac10a2d2010-12-22 21:39:39 +0000936 /**
937 * Helper function to compute the size of a vertex from a vertex layout
938 * @return size of a single vertex.
939 */
940 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000941
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000942 /**
943 * Helper function for determining the index of texture coordinates that
944 * is input for a texture stage. Note that a stage may instead use positions
945 * as texture coordinates, in which case the result of the function is
946 * indistinguishable from the case when the stage is disabled.
947 *
948 * @param stage the stage to query
949 * @param vertexLayout layout to query
950 *
951 * @return the texture coordinate index or -1 if the stage doesn't use
952 * separate (non-position) texture coordinates.
953 */
954 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000955
956 /**
957 * Helper function to compute the offset of texture coordinates in a vertex
958 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +0000959 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000960 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000961 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000962 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000963
964 /**
965 * Helper function to compute the offset of the color in a vertex
966 * @return offset of color in vertex layout or -1 if the
967 * layout has no color.
968 */
969 static int VertexColorOffset(GrVertexLayout vertexLayout);
970
971 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000972 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000973 * coordinates of some index.
974 *
975 * @param coordIndex the tex coord index to query
976 * @param vertexLayout layout to query
977 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000978 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000979 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000981 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000982 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000983
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 /**
985 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000986 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +0000987 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000988 * @param stage the stage to query
989 * @param vertexLayout layout to query
990 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000991 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000992 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +0000993 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000994 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000996 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000997 * Helper function to compute the size of each vertex and the offsets of
998 * texture coordinates and color. Determines tex coord offsets by tex coord
999 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001000 * by StageTexCoordVertexLayoutBit.)
1001 *
1002 * @param vertexLayout the layout to query
1003 * @param texCoordOffsetsByIdx after return it is the offset of each
1004 * tex coord index in the vertex or -1 if
1005 * index isn't used.
1006 * @return size of a single vertex
1007 */
1008 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1009 int texCoordOffsetsByIdx[kMaxTexCoords],
1010 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001011
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001012 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001013 * Helper function to compute the size of each vertex and the offsets of
1014 * texture coordinates and color. Determines tex coord offsets by stage
1015 * rather than by index. (Each stage can be mapped to any t.c. index
1016 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001017 * tex coords then that stage's offset will be 0 (positions are always at 0).
1018 *
1019 * @param vertexLayout the layout to query
1020 * @param texCoordOffsetsByStage after return it is the offset of each
1021 * tex coord index in the vertex or -1 if
1022 * index isn't used.
1023 * @return size of a single vertex
1024 */
1025 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1026 int texCoordOffsetsByStage[kNumStages],
1027 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001028
1029 /**
1030 * Accessing positions, texture coords, or colors, of a vertex within an
1031 * array is a hassle involving casts and simple math. These helpers exist
1032 * to keep GrDrawTarget clients' code a bit nicer looking.
1033 */
1034
1035 /**
1036 * Gets a pointer to a GrPoint of a vertex's position or texture
1037 * coordinate.
1038 * @param vertices the vetex array
1039 * @param vertexIndex the index of the vertex in the array
1040 * @param vertexSize the size of each vertex in the array
1041 * @param offset the offset in bytes of the vertex component.
1042 * Defaults to zero (corresponding to vertex position)
1043 * @return pointer to the vertex component as a GrPoint
1044 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001045 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001046 int vertexIndex,
1047 int vertexSize,
1048 int offset = 0) {
1049 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001050 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001051 vertexIndex * vertexSize);
1052 }
1053 static const GrPoint* GetVertexPoint(const void* vertices,
1054 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001055 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001056 int offset = 0) {
1057 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001058 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001059 vertexIndex * vertexSize);
1060 }
1061
1062 /**
1063 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1064 * @param vertices the vetex array
1065 * @param vertexIndex the index of the vertex in the array
1066 * @param vertexSize the size of each vertex in the array
1067 * @param offset the offset in bytes of the vertex color
1068 * @return pointer to the vertex component as a GrColor
1069 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001070 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001071 int vertexIndex,
1072 int vertexSize,
1073 int offset) {
1074 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001075 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001076 vertexIndex * vertexSize);
1077 }
1078 static const GrColor* GetVertexColor(const void* vertices,
1079 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001080 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001081 int offset) {
1082 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001083 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001084 vertexIndex * vertexSize);
1085 }
1086
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001087 static void VertexLayoutUnitTest();
1088
reed@google.comac10a2d2010-12-22 21:39:39 +00001089protected:
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001090 // given a vertex layout and a draw state, will a stage be used?
1091 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
1092 const DrState& state) {
1093 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1094 }
1095
1096 bool isStageEnabled(int stage) const {
1097 return StageWillBeUsed(stage, fGeometrySrc.fVertexLayout, fCurrDrawState);
1098 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001099
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 // Helpers for GrDrawTarget subclasses that won't have private access to
1101 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001102 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001104 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 { return sds.fState; }
1106
1107 // implemented by subclass
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001108 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
1109 void** vertices,
1110 void** indices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001111
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001112 virtual void onReleaseGeometry() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001113
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001114 // subclass overrides to be notified when clip is set.
1115 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001116
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001117 virtual void onSetVertexSourceToArray(const void* vertexArray,
1118 int vertexCount) = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001119
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001120 virtual void onSetIndexSourceToArray(const void* indexArray,
1121 int indexCount) = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001122
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001123 // Helpers for drawRect, protected so subclasses that override drawRect
1124 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001125 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001126 const GrRect* srcRects[]);
1127
1128 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001129 const GrMatrix* matrix,
1130 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001131 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001132 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001133 void* vertices);
1134
reed@google.comac10a2d2010-12-22 21:39:39 +00001135 enum GeometrySrcType {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001136 kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry
1137 kArray_GeometrySrcType, // src was set using set*SourceToArray
1138 kBuffer_GeometrySrcType // src was set using set*SourceToBuffer
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 };
1140
bsalomon@google.comd302f142011-03-03 13:54:13 +00001141 struct ReservedGeometry {
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 bool fLocked;
1143 uint32_t fVertexCount;
1144 uint32_t fIndexCount;
1145 } fReservedGeometry;
1146
1147 struct GeometrySrc {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001148 GeometrySrcType fVertexSrc;
1149 const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer
1150 GeometrySrcType fIndexSrc;
1151 const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer
1152 GrVertexLayout fVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 } fGeometrySrc;
1154
1155 GrClip fClip;
1156
reed@google.com8195f672011-01-12 18:14:28 +00001157 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001159 // Not meant for external use. Only setVertexSourceToBuffer and
1160 // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't
1161 // support nested reserveAndLockGeometry (and cpu arrays internally use the
1162 // same path).
reed@google.comac10a2d2010-12-22 21:39:39 +00001163 class AutoGeometrySrcRestore {
1164 public:
1165 AutoGeometrySrcRestore(GrDrawTarget* target) {
1166 fTarget = target;
1167 fGeometrySrc = fTarget->fGeometrySrc;
1168 }
1169 ~AutoGeometrySrcRestore() {
1170 fTarget->fGeometrySrc = fGeometrySrc;
1171 }
1172 private:
1173 GrDrawTarget *fTarget;
1174 GeometrySrc fGeometrySrc;
1175
1176 AutoGeometrySrcRestore();
1177 AutoGeometrySrcRestore(const AutoGeometrySrcRestore&);
1178 AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&);
1179 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001180};
1181
1182#endif