blob: de4808aa457938dbbbf7f06808ea63ec174d28a1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrDrawTarget_DEFINED
12#define GrDrawTarget_DEFINED
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrMatrix.h"
15#include "GrColor.h"
16#include "GrRefCnt.h"
17#include "GrSamplerState.h"
18#include "GrClip.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000019#include "GrTexture.h"
20#include "GrStencil.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
Scroggo97c88c22011-05-11 14:05:25 +000022#include "SkXfermode.h"
23
reed@google.comac10a2d2010-12-22 21:39:39 +000024class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrClipIterator;
26class GrVertexBuffer;
27class GrIndexBuffer;
28
29class GrDrawTarget : public GrRefCnt {
30public:
31 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000032 * Number of texture stages. Each stage takes as input a color and
33 * 2D texture coordinates. The color input to the first enabled stage is the
34 * per-vertex color or the constant color (setColor/setAlpha) if there are
35 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000036 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000037 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000038 * lookups are specified by a texture a sampler (setSamplerState). Texture
39 * coordinates for each stage come from the vertices based on a
40 * GrVertexLayout bitfield. The output fragment color is the output color of
41 * the last enabled stage. The presence or absence of texture coordinates
42 * for each stage in the vertex layout indicates whether a stage is enabled
43 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000044 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000045 enum {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000046 kNumStages = 3,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000047 kMaxTexCoords = kNumStages
48 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000049
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000050
51 /**
52 * The absolute maximum number of edges that may be specified for
53 * a single draw call when performing edge antialiasing. This is used for
54 * the size of several static buffers, so implementations of getMaxEdges()
55 * (below) should clamp to this value.
56 */
57 enum {
58 kMaxEdges = 32
59 };
60
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000061 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +000062 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +000063 */
bsalomon@google.comffca4002011-02-22 20:34:01 +000064 typedef int StageBitfield;
65 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 /**
68 * Flags that affect rendering. Controlled using enable/disableState(). All
69 * default to disabled.
70 */
71 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000072 kDither_StateBit = 0x01, //<! Perform color dithering
73 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +000074 // target must support some form of AA
75 // (msaa, coverage sampling, etc). For
76 // GrGpu-created rendertarget/textures
77 // this is controlled by parameters
78 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000079 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +000080 // against the region specified by
81 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000082 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
83 // Useful while performing stencil
84 // ops.
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +000085 kEdgeAAConcave_StateBit = 0x10,//<! If set, edge AA will test edge
86 // pairs for convexity while
87 // rasterizing. Set this if the
88 // source polygon is non-convex.
bsalomon@google.comd302f142011-03-03 13:54:13 +000089
90 // subclass may use additional bits internally
91 kDummyStateBit,
92 kLastPublicStateBit = kDummyStateBit-1
93 };
94
95 enum DrawFace {
96 kBoth_DrawFace,
97 kCCW_DrawFace,
98 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +000099 };
100
101 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102 * The DrawTarget may reserve some of the high bits of the stencil. The draw
103 * target will automatically trim reference and mask values so that the
104 * client doesn't overwrite these bits.
105 * The number of bits available is relative to the currently set render
106 *target.
107 * @return the number of bits usable by the draw target client.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000109 int getUsableStencilBits() const {
110 int bits = fCurrDrawState.fRenderTarget->stencilBits();
111 if (bits) {
112 return bits - 1;
113 } else {
114 return 0;
115 }
116 }
117
118 /**
119 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000120 * Changing the clip has the side-effect of possibly zeroing
121 * out the client settable stencil bits. So multipass algorithms
122 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000123 * @param settings the stencil settings to use.
124 */
125 void setStencil(const GrStencilSettings& settings) {
126 fCurrDrawState.fStencilSettings = settings;
127 }
128
129 /**
130 * Shortcut to disable stencil testing and ops.
131 */
132 void disableStencil() {
133 fCurrDrawState.fStencilSettings.setDisabled();
134 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000136 class Edge {
137 public:
138 Edge() {}
139 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
140 GrPoint intersect(const Edge& other) {
141 return GrPoint::Make(
142 (fY * other.fZ - other.fY * fZ) /
143 (fX * other.fY - other.fX * fY),
144 (fX * other.fZ - other.fX * fZ) /
145 (other.fX * fY - fX * other.fY));
146 }
147 float fX, fY, fZ;
148 };
149
reed@google.comac10a2d2010-12-22 21:39:39 +0000150protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
reed@google.com8195f672011-01-12 18:14:28 +0000152 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000153 DrState() {
154 // make sure any pad is zero for memcmp
155 // all DrState members should default to something
156 // valid by the memset
157 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000158
159 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000160 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000161 fFirstCoverageStage = kNumStages;
162
163 // pedantic assertion that our ptrs will
164 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000165 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000166
167 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000168 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000169 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000170 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000172 GrBlendCoeff fSrcBlend;
173 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000174 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000175 GrTexture* fTextures[kNumStages];
176 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000177 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000178 GrRenderTarget* fRenderTarget;
179 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000180 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000181 GrColor fColorFilterColor;
182 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000183
184 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000185 GrMatrix fViewMatrix;
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000186 Edge fEdgeAAEdges[kMaxEdges];
187 int fEdgeAANumEdges;
reed@google.com8195f672011-01-12 18:14:28 +0000188 bool operator ==(const DrState& s) const {
189 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 }
reed@google.com8195f672011-01-12 18:14:28 +0000191 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 };
193
194public:
195 ///////////////////////////////////////////////////////////////////////////
196
197 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000198 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000199
200 /**
201 * Sets the current clip to the region specified by clip. All draws will be
202 * clipped against this clip if kClip_StateBit is enabled.
203 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000204 * Setting the clip may (or may not) zero out the client's stencil bits.
205 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 * @param description of the clipping region
207 */
208 void setClip(const GrClip& clip);
209
210 /**
211 * Gets the current clip.
212 *
213 * @return the clip.
214 */
215 const GrClip& getClip() const;
216
217 /**
218 * Sets the texture used at the next drawing call
219 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000220 * @param stage The texture stage for which the texture will be set
221 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000222 * @param texture The texture to set. Can be NULL though there is no advantage
223 * to settings a NULL texture if doing non-textured drawing
224 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000225 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
227 /**
228 * Retrieves the currently set texture.
229 *
230 * @return The currently set texture. The return value will be NULL if no
231 * texture has been set, NULL was most recently passed to
232 * setTexture, or the last setTexture was destroyed.
233 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000234 const GrTexture* getTexture(int stage) const;
235 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
237 /**
238 * Sets the rendertarget used at the next drawing call
239 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000240 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 */
242 void setRenderTarget(GrRenderTarget* target);
243
244 /**
245 * Retrieves the currently set rendertarget.
246 *
247 * @return The currently set render target.
248 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000249 const GrRenderTarget* getRenderTarget() const;
250 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
252 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000253 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000255 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000256 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000258 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 * @param samplerState Specifies the sampler state.
260 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000261 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262
263 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000264 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000266 * @param stage the stage of the sampler to set
267 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000269 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
270 GrAssert(stage >= 0 && stage < kNumStages);
271 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000272 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
274 /**
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000275 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
276 * matrix
277 */
278 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
279 for (int i = 0; i < kNumStages; ++i) {
280 if ((1 << i) & stageMask) {
281 this->preConcatSamplerMatrix(i, matrix);
282 }
283 }
284 }
285
286 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000287 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000288 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000289 * @param stage the stage to of sampler to get
290 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000291 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000292 const GrMatrix& getSamplerMatrix(int stage) const {
293 return fCurrDrawState.fSamplerStates[stage].getMatrix();
294 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000295
296 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000297 * Sets the matrix of a stage's sampler
298 *
299 * @param stage the stage of sampler set
300 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000301 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000302 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000303 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
304 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000305
306 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 * Sets the matrix applied to veretx positions.
308 *
309 * In the post-view-matrix space the rectangle [0,w]x[0,h]
310 * fully covers the render target. (w and h are the width and height of the
311 * the rendertarget.)
312 *
313 * @param m the matrix used to transform the vertex positions.
314 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000315 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
317 /**
318 * Multiplies the current view matrix by a matrix
319 *
320 * After this call V' = V*m where V is the old view matrix,
321 * m is the parameter to this function, and V' is the new view matrix.
322 * (We consider positions to be column vectors so position vector p is
323 * transformed by matrix X as p' = X*p.)
324 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000325 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000326 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000327 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
329 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000330 * Multiplies the current view matrix by a matrix
331 *
332 * After this call V' = m*V where V is the old view matrix,
333 * m is the parameter to this function, and V' is the new view matrix.
334 * (We consider positions to be column vectors so position vector p is
335 * transformed by matrix X as p' = X*p.)
336 *
337 * @param m the matrix used to modify the view matrix.
338 */
339 void postConcatViewMatrix(const GrMatrix& m);
340
341 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000342 * Retrieves the current view matrix
343 * @return the current view matrix.
344 */
345 const GrMatrix& getViewMatrix() const;
346
347 /**
348 * Retrieves the inverse of the current view matrix.
349 *
350 * If the current view matrix is invertible, return true, and if matrix
351 * is non-null, copy the inverse into it. If the current view matrix is
352 * non-invertible, return false and ignore the matrix parameter.
353 *
354 * @param matrix if not null, will receive a copy of the current inverse.
355 */
356 bool getViewInverse(GrMatrix* matrix) const;
357
358 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 * Sets color for next draw to a premultiplied-alpha color.
360 *
361 * @param the color to set.
362 */
363 void setColor(GrColor);
364
365 /**
Scroggo97c88c22011-05-11 14:05:25 +0000366 * Add a color filter that can be represented by a color and a mode.
367 */
368 void setColorFilter(GrColor, SkXfermode::Mode);
369
370 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 * Sets the color to be used for the next draw to be
372 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
373 *
374 * @param alpha The alpha value to set as the color.
375 */
376 void setAlpha(uint8_t alpha);
377
378 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000379 * Controls whether clockwise, counterclockwise, or both faces are drawn.
380 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
384 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000385 * A common pattern is to compute a color with the initial stages and then
386 * modulate that color by a coverage value in later stage(s) (AA, mask-
387 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
388 * computed based on the pre-coverage-modulated color. The division of
389 * stages between color-computing and coverage-computing is specified by
390 * this method. Initially this is kNumStages (all stages are color-
391 * computing).
392 */
393 void setFirstCoverageStage(int firstCoverageStage) {
394 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
395 }
396
397 /**
398 * Gets the index of the first coverage-computing stage.
399 */
400 int getFirstCoverageStage() const {
401 return fCurrDrawState.fFirstCoverageStage;
402 }
403
404 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000405 * Gets whether the target is drawing clockwise, counterclockwise,
406 * or both faces.
407 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000409 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
411 /**
412 * Enable render state settings.
413 *
414 * @param flags bitfield of StateBits specifing the states to enable
415 */
416 void enableState(uint32_t stateBits);
417
418 /**
419 * Disable render state settings.
420 *
421 * @param flags bitfield of StateBits specifing the states to disable
422 */
423 void disableState(uint32_t stateBits);
424
425 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000426 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
427 }
428
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000429 bool isAntialiasState() const {
430 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
431 }
432
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000433 bool isClipState() const {
434 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 }
436
bsalomon@google.comd302f142011-03-03 13:54:13 +0000437 bool isColorWriteDisabled() const {
438 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
439 }
440
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 * Sets the blending function coeffecients.
443 *
444 * The blend function will be:
445 * D' = sat(S*srcCoef + D*dstCoef)
446 *
447 * where D is the existing destination color, S is the incoming source
448 * color, and D' is the new destination color that will be written. sat()
449 * is the saturation function.
450 *
451 * @param srcCoef coeffecient applied to the src color.
452 * @param dstCoef coeffecient applied to the dst color.
453 */
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000454 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
456 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000457 * Sets the blending function constant referenced by the following blending
458 * coeffecients:
459 * kConstC_BlendCoeff
460 * kIConstC_BlendCoeff
461 * kConstA_BlendCoeff
462 * kIConstA_BlendCoeff
463 *
464 * @param constant the constant to set
465 */
466 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
467
468 /**
469 * Retrieves the last value set by setBlendConstant()
470 * @return the blending constant value
471 */
472 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
473
474 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 * Used to save and restore the GrGpu's drawing state
476 */
477 struct SavedDrawState {
478 private:
reed@google.com8195f672011-01-12 18:14:28 +0000479 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 friend class GrDrawTarget;
481 };
482
483 /**
484 * Saves the current draw state. The state can be restored at a later time
485 * with restoreDrawState.
486 *
487 * See also AutoStateRestore class.
488 *
489 * @param state will hold the state after the function returns.
490 */
491 void saveCurrentDrawState(SavedDrawState* state) const;
492
493 /**
494 * Restores previously saved draw state. The client guarantees that state
495 * was previously passed to saveCurrentDrawState and that the rendertarget
496 * and texture set at save are still valid.
497 *
498 * See also AutoStateRestore class.
499 *
500 * @param state the previously saved state to restore.
501 */
502 void restoreDrawState(const SavedDrawState& state);
503
504 /**
505 * Copies the draw state from another target to this target.
506 *
507 * @param srcTarget draw target used as src of the draw state.
508 */
509 void copyDrawState(const GrDrawTarget& srcTarget);
510
511 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000512 * The format of vertices is represented as a bitfield of flags.
513 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000514 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000515 * coordinates and per-vertex colors. Each stage can use any of the texture
516 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000518 * If no texture coordinates are specified for a stage then the stage is
519 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000521 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000523 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000526 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000527 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000528 * the order would be (position, texture coordinate 1[, color]).
529 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000531 /**
532 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000533 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000534 * @param stage the stage that will use texture coordinates.
535 * @param texCoordIdx the index of the texture coordinates to use
536 *
537 * @return the bit to add to a GrVertexLayout bitfield.
538 */
539 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
540 GrAssert(stage < kNumStages);
541 GrAssert(texCoordIdx < kMaxTexCoords);
542 return 1 << (stage + (texCoordIdx * kNumStages));
543 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000544
545 /**
546 * Determines if blend is effectively disabled.
547 *
548 * @return true if blend can be disabled without changing the rendering
549 * result given the current state including the vertex layout specified
550 * with the vertex source.
551 */
552 bool canDisableBlend() const;
553
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000554 /**
555 * Sets the edge data required for edge antialiasing.
556 *
557 * @param edges 3 * 6 float values, representing the edge
558 * equations in Ax + By + C form
559 */
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000560 void setEdgeAAData(const Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000561
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000562private:
563 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
564public:
565 /**
566 * Generates a bit indicating that a texture stage uses the position
567 * as its texture coordinate.
568 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000570 * coordinates.
571 *
572 * @return the bit to add to a GrVertexLayout bitfield.
573 */
574 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
575 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000576 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000577 }
578private:
579 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000580
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000581public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000582
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000583 /**
584 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 */
586 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000587
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000588 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
589 //<! vertices have colors
590 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
591 //<! use text vertices. (Pos
592 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000593 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000594 // text [GrGpuTextVertex vs
595 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000597 kDummyVertexLayoutBit,
598 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000600 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000601 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
603 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000604 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000605 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000606 * can use a different method. Once geometry is specified it can be used for
607 * multiple drawIndexed and drawNonIndexed calls.
608 *
609 * Sometimes it is necessary to perform a draw while upstack code has
610 * already specified geometry that it isn't finished with. There are push
611 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000612 *
613 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
614 * caller's client has already provided vertex data in a format
615 * the time compatible with a GrVertexLayout. The array must contain the
616 * data at set*SourceToArray is called. The source stays in effect for
617 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
618 * again or one of the other two paths is chosen.
619 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000620 * 2. Reserve. This is most useful when the caller has data it must
621 * transform before drawing and is not long-lived. The caller requests
622 * that the draw target make room for some amount of vertex and/or index
623 * data. The target provides ptrs to hold the vertex and/or index data.
624 *
625 * The data is writable up until the next drawIndexed, drawNonIndexed,
626 * or pushGeometrySource At this point the data is frozen and the ptrs
627 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000628 *
629 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000630 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
631 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
632 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000633 */
634
635 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000636 * Reserves space for vertices. Draw target will use reserved vertices at
637 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 *
639 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000640 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000641 * of vertices to be filled by caller. The next draw will read
642 * these vertices.
643 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000644 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000645 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000646 * GrDrawTarget to decide whether to put data in buffers, to group vertex
647 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000649 * After the next draw or pushGeometrySource the vertices ptr is no longer
650 * valid and the geometry data cannot be further modified. The contents
651 * that were put in the reserved space can be drawn by multiple draws,
652 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000653 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000654 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000655 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 * @param vertices will point to reserved vertex space if vertexCount is
657 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000658 *
659 * @return true if succeeded in allocating space for the vertices and false
660 * if not.
661 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000662 bool reserveVertexSpace(GrVertexLayout vertexLayout,
663 int vertexCount,
664 void** vertices);
665 /**
666 * Reserves space for indices. Draw target will use the reserved indices at
667 * the next indexed draw.
668 *
669 * If succeeds:
670 * if indexCount > 0, *indices will be the array
671 * of indices to be filled by caller. The next draw will read
672 * these indices.
673 *
674 * If a client does not already have a index buffer then this is the
675 * preferred way to allocate index data. It allows the subclass of
676 * GrDrawTarget to decide whether to put data in buffers, to group index
677 * data that uses the same state (e.g. for deferred rendering), etc.
678 *
679 * After the next indexed draw or pushGeometrySource the indices ptr is no
680 * longer valid and the geometry data cannot be further modified. The
681 * contents that were put in the reserved space can be drawn by multiple
682 * draws, however.
683 *
684 * @param indexCount the number of indices to reserve space for. Can be 0.
685 * @param indices will point to reserved index space if indexCount is
686 * non-zero. Illegal to pass NULL if indexCount > 0.
687 */
688
689 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000690 /**
691 * Provides hints to caller about the number of vertices and indices
692 * that can be allocated cheaply. This can be useful if caller is reserving
693 * space but doesn't know exactly how much geometry is needed.
694 *
695 * Also may hint whether the draw target should be flushed first. This is
696 * useful for deferred targets.
697 *
698 * @param vertexLayout layout of vertices caller would like to reserve
699 * @param vertexCount in: hint about how many vertices the caller would
700 * like to allocate.
701 * out: a hint about the number of vertices that can be
702 * allocated cheaply. Negative means no hint.
703 * Ignored if NULL.
704 * @param indexCount in: hint about how many indices the caller would
705 * like to allocate.
706 * out: a hint about the number of indices that can be
707 * allocated cheaply. Negative means no hint.
708 * Ignored if NULL.
709 *
710 * @return true if target should be flushed based on the input values.
711 */
712 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000713 int* vertexCount,
714 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000715
716 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000717 * Sets source of vertex data for the next draw. Array must contain
718 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000719 *
720 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000721 * @param size size of the vertex data.
722 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000723 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000724 void setVertexSourceToArray(GrVertexLayout vertexLayout,
725 const void* vertexArray,
726 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000727
728 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000729 * Sets source of index data for the next indexed draw. Array must contain
730 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000732 * @param array cpu array containing index data.
733 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000734 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000735 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000736
737 /**
738 * Sets source of vertex data for the next draw. Data does not have to be
739 * in the buffer until drawIndexed or drawNonIndexed.
740 *
741 * @param buffer vertex buffer containing vertex data. Must be
742 * unlocked before draw call.
743 * @param vertexLayout layout of the vertex data in the buffer.
744 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000745 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
746 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000747
748 /**
749 * Sets source of index data for the next indexed draw. Data does not have
750 * to be in the buffer until drawIndexed or drawNonIndexed.
751 *
752 * @param buffer index buffer containing indices. Must be unlocked
753 * before indexed draw call.
754 */
755 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000756
757 /**
758 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
759 * source to reserved, array, or buffer before next draw. May be able to free
760 * up temporary storage allocated by setVertexSourceToArray or
761 * reserveVertexSpace.
762 */
763 void resetVertexSource();
764
765 /**
766 * Resets index source. Indexed Drawing from reset indices is illegal. Set
767 * index source to reserved, array, or buffer before next indexed draw. May
768 * be able to free up temporary storage allocated by setIndexSourceToArray
769 * or reserveIndexSpace.
770 */
771 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000772
773 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774 * Pushes and resets the vertex/index sources. Any reserved vertex / index
775 * data is finalized (i.e. cannot be updated after the matching pop but can
776 * be drawn from). Must be balanced by a pop.
777 */
778 void pushGeometrySource();
779
780 /**
781 * Pops the vertex / index sources from the matching push.
782 */
783 void popGeometrySource();
784
785 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000786 * Draws indexed geometry using the current state and current vertex / index
787 * sources.
788 *
789 * @param type The type of primitives to draw.
790 * @param startVertex the vertex in the vertex array/buffer corresponding
791 * to index 0
792 * @param startIndex first index to read from index src.
793 * @param vertexCount one greater than the max index.
794 * @param indexCount the number of index elements to read. The index count
795 * is effectively trimmed to the last completely
796 * specified primitive.
797 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000798 void drawIndexed(GrPrimitiveType type,
799 int startVertex,
800 int startIndex,
801 int vertexCount,
802 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000803
804 /**
805 * Draws non-indexed geometry using the current state and current vertex
806 * sources.
807 *
808 * @param type The type of primitives to draw.
809 * @param startVertex the vertex in the vertex array/buffer corresponding
810 * to index 0
811 * @param vertexCount one greater than the max index.
812 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000813 void drawNonIndexed(GrPrimitiveType type,
814 int startVertex,
815 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000816
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000817 /**
818 * Helper function for drawing rects. This does not use the current index
819 * and vertex sources. After returning, the vertex and index sources may
820 * have changed. They should be reestablished before the next drawIndexed
821 * or drawNonIndexed. This cannot be called between reserving and releasing
822 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000823 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000824 * drawNonIndexed.
825 * @param rect the rect to draw
826 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000827 * @param stageEnableBitfield bitmask indicating which stages are enabled.
828 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000829 * @param srcRects specifies rects for stages enabled by stageEnableMask.
830 * if stageEnableMask bit i is 1, srcRects is not NULL,
831 * and srcRects[i] is not NULL, then srcRects[i] will be
832 * used as coordinates for stage i. Otherwise, if stage i
833 * is enabled then rect is used as the coordinates.
834 * @param srcMatrices optional matrices applied to srcRects. If
835 * srcRect[i] is non-NULL and srcMatrices[i] is
836 * non-NULL then srcRect[i] will be transformed by
837 * srcMatrix[i]. srcMatrices can be NULL when no
838 * srcMatrices are desired.
839 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000840 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000841 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000842 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000843 const GrRect* srcRects[],
844 const GrMatrix* srcMatrices[]);
845
846 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000847 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000848 * matrices.
849 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000850 void drawSimpleRect(const GrRect& rect,
851 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000852 StageBitfield stageEnableBitfield) {
853 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000854 }
855
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000856 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000857 * Clear the render target. Ignores the clip and all other draw state
858 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
859 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000860 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000861 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000862
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000863 /**
864 * Returns the maximum number of edges that may be specified in a single
865 * draw call when performing edge antialiasing. This is usually limited
866 * by the number of fragment uniforms which may be uploaded. Must be a
867 * minimum of six, since a triangle's vertices each belong to two boundary
868 * edges which may be distinct.
869 */
870 virtual int getMaxEdges() const { return 6; }
871
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000872 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000873
874 class AutoStateRestore : ::GrNoncopyable {
875 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000876 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000877 AutoStateRestore(GrDrawTarget* target);
878 ~AutoStateRestore();
879
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000880 /**
881 * if this object is already saving state for param target then
882 * this does nothing. Otherise, it restores previously saved state on
883 * previous target (if any) and saves current state on param target.
884 */
885 void set(GrDrawTarget* target);
886
reed@google.comac10a2d2010-12-22 21:39:39 +0000887 private:
888 GrDrawTarget* fDrawTarget;
889 SavedDrawState fDrawState;
890 };
891
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000892 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000893
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000894 class AutoViewMatrixRestore : ::GrNoncopyable {
895 public:
896 AutoViewMatrixRestore() {
897 fDrawTarget = NULL;
898 }
899
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000900 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000901 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
902 GrAssert(NULL != target);
903 }
904
905 void set(GrDrawTarget* target) {
906 GrAssert(NULL != target);
907 if (NULL != fDrawTarget) {
908 fDrawTarget->setViewMatrix(fMatrix);
909 }
910 fDrawTarget = target;
911 fMatrix = target->getViewMatrix();
912 }
913
914 ~AutoViewMatrixRestore() {
915 if (NULL != fDrawTarget) {
916 fDrawTarget->setViewMatrix(fMatrix);
917 }
918 }
919
920 private:
921 GrDrawTarget* fDrawTarget;
922 GrMatrix fMatrix;
923 };
924
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000925 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000926
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000927 /**
928 * Sets the view matrix to I and preconcats all stage matrices enabled in
929 * mask by the view inverse. Destructor undoes these changes.
930 */
931 class AutoDeviceCoordDraw : ::GrNoncopyable {
932 public:
933 AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask);
934 ~AutoDeviceCoordDraw();
935 private:
936 GrDrawTarget* fDrawTarget;
937 GrMatrix fViewMatrix;
938 GrMatrix fSamplerMatrices[kNumStages];
939 int fStageMask;
940 };
941
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000942 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000943
reed@google.comac10a2d2010-12-22 21:39:39 +0000944 class AutoReleaseGeometry : ::GrNoncopyable {
945 public:
946 AutoReleaseGeometry(GrDrawTarget* target,
947 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000948 int vertexCount,
949 int indexCount);
950 AutoReleaseGeometry();
951 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000952 bool set(GrDrawTarget* target,
953 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000954 int vertexCount,
955 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000956 bool succeeded() const { return NULL != fTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000957 void* vertices() const { return fVertices; }
958 void* indices() const { return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000959 GrPoint* positions() const {
960 return static_cast<GrPoint*>(fVertices);
961 }
962
963 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000964 void reset();
965
reed@google.comac10a2d2010-12-22 21:39:39 +0000966 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000967 void* fVertices;
968 void* fIndices;
969 };
970
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000971 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000972
973 class AutoClipRestore : ::GrNoncopyable {
974 public:
975 AutoClipRestore(GrDrawTarget* target) {
976 fTarget = target;
977 fClip = fTarget->getClip();
978 }
979
980 ~AutoClipRestore() {
981 fTarget->setClip(fClip);
982 }
983 private:
984 GrDrawTarget* fTarget;
985 GrClip fClip;
986 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000987
988 ////////////////////////////////////////////////////////////////////////////
989
990 class AutoGeometryPush : ::GrNoncopyable {
991 public:
992 AutoGeometryPush(GrDrawTarget* target) {
993 GrAssert(NULL != target);
994 fTarget = target;
995 target->pushGeometrySource();
996 }
997 ~AutoGeometryPush() {
998 fTarget->popGeometrySource();
999 }
1000 private:
1001 GrDrawTarget* fTarget;
1002 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
1004 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001005 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001006
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 /**
1008 * Helper function to compute the size of a vertex from a vertex layout
1009 * @return size of a single vertex.
1010 */
1011 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001012
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001013 /**
1014 * Helper function for determining the index of texture coordinates that
1015 * is input for a texture stage. Note that a stage may instead use positions
1016 * as texture coordinates, in which case the result of the function is
1017 * indistinguishable from the case when the stage is disabled.
1018 *
1019 * @param stage the stage to query
1020 * @param vertexLayout layout to query
1021 *
1022 * @return the texture coordinate index or -1 if the stage doesn't use
1023 * separate (non-position) texture coordinates.
1024 */
1025 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001026
1027 /**
1028 * Helper function to compute the offset of texture coordinates in a vertex
1029 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001030 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001031 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001032 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001033 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001034
1035 /**
1036 * Helper function to compute the offset of the color in a vertex
1037 * @return offset of color in vertex layout or -1 if the
1038 * layout has no color.
1039 */
1040 static int VertexColorOffset(GrVertexLayout vertexLayout);
1041
1042 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001043 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001044 * coordinates of some index.
1045 *
1046 * @param coordIndex the tex coord index to query
1047 * @param vertexLayout layout to query
1048 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001049 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001050 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001052 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001053 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001054
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 /**
1056 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001057 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001059 * @param stage the stage to query
1060 * @param vertexLayout layout to query
1061 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001062 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001063 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001065 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001066
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001067 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001068 * Helper function to compute the size of each vertex and the offsets of
1069 * texture coordinates and color. Determines tex coord offsets by tex coord
1070 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001071 * by StageTexCoordVertexLayoutBit.)
1072 *
1073 * @param vertexLayout the layout to query
1074 * @param texCoordOffsetsByIdx after return it is the offset of each
1075 * tex coord index in the vertex or -1 if
1076 * index isn't used.
1077 * @return size of a single vertex
1078 */
1079 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1080 int texCoordOffsetsByIdx[kMaxTexCoords],
1081 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001082
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001083 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001084 * Helper function to compute the size of each vertex and the offsets of
1085 * texture coordinates and color. Determines tex coord offsets by stage
1086 * rather than by index. (Each stage can be mapped to any t.c. index
1087 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001088 * tex coords then that stage's offset will be 0 (positions are always at 0).
1089 *
1090 * @param vertexLayout the layout to query
1091 * @param texCoordOffsetsByStage after return it is the offset of each
1092 * tex coord index in the vertex or -1 if
1093 * index isn't used.
1094 * @return size of a single vertex
1095 */
1096 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1097 int texCoordOffsetsByStage[kNumStages],
1098 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001099
1100 /**
1101 * Accessing positions, texture coords, or colors, of a vertex within an
1102 * array is a hassle involving casts and simple math. These helpers exist
1103 * to keep GrDrawTarget clients' code a bit nicer looking.
1104 */
1105
1106 /**
1107 * Gets a pointer to a GrPoint of a vertex's position or texture
1108 * coordinate.
1109 * @param vertices the vetex array
1110 * @param vertexIndex the index of the vertex in the array
1111 * @param vertexSize the size of each vertex in the array
1112 * @param offset the offset in bytes of the vertex component.
1113 * Defaults to zero (corresponding to vertex position)
1114 * @return pointer to the vertex component as a GrPoint
1115 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001116 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001117 int vertexIndex,
1118 int vertexSize,
1119 int offset = 0) {
1120 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001121 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001122 vertexIndex * vertexSize);
1123 }
1124 static const GrPoint* GetVertexPoint(const void* vertices,
1125 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001126 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001127 int offset = 0) {
1128 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001129 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001130 vertexIndex * vertexSize);
1131 }
1132
1133 /**
1134 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1135 * @param vertices the vetex array
1136 * @param vertexIndex the index of the vertex in the array
1137 * @param vertexSize the size of each vertex in the array
1138 * @param offset the offset in bytes of the vertex color
1139 * @return pointer to the vertex component as a GrColor
1140 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001141 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001142 int vertexIndex,
1143 int vertexSize,
1144 int offset) {
1145 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001146 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001147 vertexIndex * vertexSize);
1148 }
1149 static const GrColor* GetVertexColor(const void* vertices,
1150 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001151 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001152 int offset) {
1153 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001154 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001155 vertexIndex * vertexSize);
1156 }
1157
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001158 static void VertexLayoutUnitTest();
1159
reed@google.comac10a2d2010-12-22 21:39:39 +00001160protected:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001161
1162 enum GeometrySrcType {
1163 kNone_GeometrySrcType, //<! src has not been specified
1164 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1165 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1166 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1167 };
1168
1169 struct GeometrySrcState {
1170 GeometrySrcType fVertexSrc;
1171 union {
1172 // valid if src type is buffer
1173 const GrVertexBuffer* fVertexBuffer;
1174 // valid if src type is reserved or array
1175 int fVertexCount;
1176 };
1177
1178 GeometrySrcType fIndexSrc;
1179 union {
1180 // valid if src type is buffer
1181 const GrIndexBuffer* fIndexBuffer;
1182 // valid if src type is reserved or array
1183 int fIndexCount;
1184 };
1185
1186 GrVertexLayout fVertexLayout;
1187 };
1188
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001189 // given a vertex layout and a draw state, will a stage be used?
1190 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
1191 const DrState& state) {
1192 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1193 }
1194
1195 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001196 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1197 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001198 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001199
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 // Helpers for GrDrawTarget subclasses that won't have private access to
1201 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001202 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001204 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 { return sds.fState; }
1206
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001207 // implemented by subclass to allocate space for reserved geom
1208 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1209 int vertexCount,
1210 void** vertices) = 0;
1211 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1212 // implemented by subclass to handle release of reserved geom space
1213 virtual void releaseReservedVertexSpace() = 0;
1214 virtual void releaseReservedIndexSpace() = 0;
1215 // subclass must consume array contents when set
1216 virtual void onSetVertexSourceToArray(const void* vertexArray,
1217 int vertexCount) = 0;
1218 virtual void onSetIndexSourceToArray(const void* indexArray,
1219 int indexCount) = 0;
1220 // subclass is notified that geom source will be set away from an array
1221 virtual void releaseVertexArray() = 0;
1222 virtual void releaseIndexArray() = 0;
1223 // subclass overrides to be notified just before geo src state
1224 // is pushed/popped.
1225 virtual void geometrySourceWillPush() = 0;
1226 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1227 // subclass called to perform drawing
1228 virtual void onDrawIndexed(GrPrimitiveType type,
1229 int startVertex,
1230 int startIndex,
1231 int vertexCount,
1232 int indexCount) = 0;
1233 virtual void onDrawNonIndexed(GrPrimitiveType type,
1234 int startVertex,
1235 int vertexCount) = 0;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001236 // subclass overrides to be notified when clip is set.
1237 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001238
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001239
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001240 // Helpers for drawRect, protected so subclasses that override drawRect
1241 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001242 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001243 const GrRect* srcRects[]);
1244
1245 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001246 const GrMatrix* matrix,
1247 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001248 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001249 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001250 void* vertices);
1251
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001252 // accessor for derived classes
1253 const GeometrySrcState& getGeomSrc() const {
1254 return fGeoSrcStateStack.back();
1255 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001256
1257 GrClip fClip;
1258
reed@google.com8195f672011-01-12 18:14:28 +00001259 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001260
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001261private:
1262 // called when setting a new vert/idx source to unref prev vb/ib
1263 void releasePreviousVertexSource();
1264 void releasePreviousIndexSource();
1265
1266 enum {
1267 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001269 GrAlignedSTStorage<kPreallocGeoSrcStateStackCnt,
1270 GeometrySrcState>
1271 fGeoSrcStateStackStorage;
1272 GrTArray<GeometrySrcState, true> fGeoSrcStateStack;
1273
reed@google.comac10a2d2010-12-22 21:39:39 +00001274};
1275
1276#endif