blob: e0b66a43888afc0e738e2ab71014687dac916306 [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
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000057
58 /**
59 * The absolute maximum number of edges that may be specified for
60 * a single draw call when performing edge antialiasing. This is used for
61 * the size of several static buffers, so implementations of getMaxEdges()
62 * (below) should clamp to this value.
63 */
64 enum {
65 kMaxEdges = 32
66 };
67
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000068 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +000069 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +000070 */
bsalomon@google.comffca4002011-02-22 20:34:01 +000071 typedef int StageBitfield;
72 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +000073
74 /**
75 * Flags that affect rendering. Controlled using enable/disableState(). All
76 * default to disabled.
77 */
78 enum StateBits {
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000079 kDither_StateBit = 0x01, //<! Perform color dithering
80 kAntialias_StateBit = 0x02, //<! Perform anti-aliasing. The render-
reed@google.comac10a2d2010-12-22 21:39:39 +000081 // target must support some form of AA
82 // (msaa, coverage sampling, etc). For
83 // GrGpu-created rendertarget/textures
84 // this is controlled by parameters
85 // passed to createTexture.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000086 kClip_StateBit = 0x04, //<! Controls whether drawing is clipped
reed@google.comac10a2d2010-12-22 21:39:39 +000087 // against the region specified by
88 // setClip.
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000089 kNoColorWrites_StateBit = 0x08, //<! If set it disables writing colors.
90 // Useful while performing stencil
91 // ops.
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +000092 kEdgeAAConcave_StateBit = 0x10,//<! If set, edge AA will test edge
93 // pairs for convexity while
94 // rasterizing. Set this if the
95 // source polygon is non-convex.
bsalomon@google.comd302f142011-03-03 13:54:13 +000096
97 // subclass may use additional bits internally
98 kDummyStateBit,
99 kLastPublicStateBit = kDummyStateBit-1
100 };
101
102 enum DrawFace {
103 kBoth_DrawFace,
104 kCCW_DrawFace,
105 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 };
107
108 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000109 * The DrawTarget may reserve some of the high bits of the stencil. The draw
110 * target will automatically trim reference and mask values so that the
111 * client doesn't overwrite these bits.
112 * The number of bits available is relative to the currently set render
113 *target.
114 * @return the number of bits usable by the draw target client.
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000116 int getUsableStencilBits() const {
117 int bits = fCurrDrawState.fRenderTarget->stencilBits();
118 if (bits) {
119 return bits - 1;
120 } else {
121 return 0;
122 }
123 }
124
125 /**
126 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000127 * Changing the clip has the side-effect of possibly zeroing
128 * out the client settable stencil bits. So multipass algorithms
129 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130 * @param settings the stencil settings to use.
131 */
132 void setStencil(const GrStencilSettings& settings) {
133 fCurrDrawState.fStencilSettings = settings;
134 }
135
136 /**
137 * Shortcut to disable stencil testing and ops.
138 */
139 void disableStencil() {
140 fCurrDrawState.fStencilSettings.setDisabled();
141 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000143 class Edge {
144 public:
145 Edge() {}
146 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
147 GrPoint intersect(const Edge& other) {
148 return GrPoint::Make(
149 (fY * other.fZ - other.fY * fZ) /
150 (fX * other.fY - other.fX * fY),
151 (fX * other.fZ - other.fX * fZ) /
152 (other.fX * fY - fX * other.fY));
153 }
154 float fX, fY, fZ;
155 };
156
reed@google.comac10a2d2010-12-22 21:39:39 +0000157protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
reed@google.com8195f672011-01-12 18:14:28 +0000159 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000160 DrState() {
161 // make sure any pad is zero for memcmp
162 // all DrState members should default to something
163 // valid by the memset
164 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000165
166 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000167 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000168 fFirstCoverageStage = kNumStages;
169
170 // pedantic assertion that our ptrs will
171 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000172 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000173
174 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000175 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000176 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000177 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000179 GrBlendCoeff fSrcBlend;
180 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000181 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000182 GrTexture* fTextures[kNumStages];
183 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000184 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000185 GrRenderTarget* fRenderTarget;
186 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000187 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000188 GrColor fColorFilterColor;
189 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000190
191 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000192 GrMatrix fViewMatrix;
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000193 Edge fEdgeAAEdges[kMaxEdges];
194 int fEdgeAANumEdges;
reed@google.com8195f672011-01-12 18:14:28 +0000195 bool operator ==(const DrState& s) const {
196 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 }
reed@google.com8195f672011-01-12 18:14:28 +0000198 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 };
200
201public:
202 ///////////////////////////////////////////////////////////////////////////
203
204 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000205 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
207 /**
208 * Sets the current clip to the region specified by clip. All draws will be
209 * clipped against this clip if kClip_StateBit is enabled.
210 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000211 * Setting the clip may (or may not) zero out the client's stencil bits.
212 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 * @param description of the clipping region
214 */
215 void setClip(const GrClip& clip);
216
217 /**
218 * Gets the current clip.
219 *
220 * @return the clip.
221 */
222 const GrClip& getClip() const;
223
224 /**
225 * Sets the texture used at the next drawing call
226 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000227 * @param stage The texture stage for which the texture will be set
228 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000229 * @param texture The texture to set. Can be NULL though there is no advantage
230 * to settings a NULL texture if doing non-textured drawing
231 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000232 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
234 /**
235 * Retrieves the currently set texture.
236 *
237 * @return The currently set texture. The return value will be NULL if no
238 * texture has been set, NULL was most recently passed to
239 * setTexture, or the last setTexture was destroyed.
240 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000241 const GrTexture* getTexture(int stage) const;
242 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
244 /**
245 * Sets the rendertarget used at the next drawing call
246 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000247 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 */
249 void setRenderTarget(GrRenderTarget* target);
250
251 /**
252 * Retrieves the currently set rendertarget.
253 *
254 * @return The currently set render target.
255 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000256 const GrRenderTarget* getRenderTarget() const;
257 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000258
259 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000260 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000262 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000263 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000265 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000266 * @param samplerState Specifies the sampler state.
267 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000268 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
270 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000271 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000273 * @param stage the stage of the sampler to set
274 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000276 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
277 GrAssert(stage >= 0 && stage < kNumStages);
278 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000279 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
281 /**
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000282 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
283 * matrix
284 */
285 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
286 for (int i = 0; i < kNumStages; ++i) {
287 if ((1 << i) & stageMask) {
288 this->preConcatSamplerMatrix(i, matrix);
289 }
290 }
291 }
292
293 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000294 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000295 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000296 * @param stage the stage to of sampler to get
297 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000298 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000299 const GrMatrix& getSamplerMatrix(int stage) const {
300 return fCurrDrawState.fSamplerStates[stage].getMatrix();
301 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000302
303 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000304 * Sets the matrix of a stage's sampler
305 *
306 * @param stage the stage of sampler set
307 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000308 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000309 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000310 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
311 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000312
313 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 * Sets the matrix applied to veretx positions.
315 *
316 * In the post-view-matrix space the rectangle [0,w]x[0,h]
317 * fully covers the render target. (w and h are the width and height of the
318 * the rendertarget.)
319 *
320 * @param m the matrix used to transform the vertex positions.
321 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000322 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000323
324 /**
325 * Multiplies the current view matrix by a matrix
326 *
327 * After this call V' = V*m where V is the old view matrix,
328 * m is the parameter to this function, and V' is the new view matrix.
329 * (We consider positions to be column vectors so position vector p is
330 * transformed by matrix X as p' = X*p.)
331 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000332 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000334 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000335
336 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000337 * Multiplies the current view matrix by a matrix
338 *
339 * After this call V' = m*V where V is the old view matrix,
340 * m is the parameter to this function, and V' is the new view matrix.
341 * (We consider positions to be column vectors so position vector p is
342 * transformed by matrix X as p' = X*p.)
343 *
344 * @param m the matrix used to modify the view matrix.
345 */
346 void postConcatViewMatrix(const GrMatrix& m);
347
348 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000349 * Retrieves the current view matrix
350 * @return the current view matrix.
351 */
352 const GrMatrix& getViewMatrix() const;
353
354 /**
355 * Retrieves the inverse of the current view matrix.
356 *
357 * If the current view matrix is invertible, return true, and if matrix
358 * is non-null, copy the inverse into it. If the current view matrix is
359 * non-invertible, return false and ignore the matrix parameter.
360 *
361 * @param matrix if not null, will receive a copy of the current inverse.
362 */
363 bool getViewInverse(GrMatrix* matrix) const;
364
365 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 * Sets color for next draw to a premultiplied-alpha color.
367 *
368 * @param the color to set.
369 */
370 void setColor(GrColor);
371
372 /**
Scroggo97c88c22011-05-11 14:05:25 +0000373 * Add a color filter that can be represented by a color and a mode.
374 */
375 void setColorFilter(GrColor, SkXfermode::Mode);
376
377 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 * Sets the color to be used for the next draw to be
379 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
380 *
381 * @param alpha The alpha value to set as the color.
382 */
383 void setAlpha(uint8_t alpha);
384
385 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000386 * Controls whether clockwise, counterclockwise, or both faces are drawn.
387 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000389 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000390
391 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000392 * A common pattern is to compute a color with the initial stages and then
393 * modulate that color by a coverage value in later stage(s) (AA, mask-
394 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
395 * computed based on the pre-coverage-modulated color. The division of
396 * stages between color-computing and coverage-computing is specified by
397 * this method. Initially this is kNumStages (all stages are color-
398 * computing).
399 */
400 void setFirstCoverageStage(int firstCoverageStage) {
401 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
402 }
403
404 /**
405 * Gets the index of the first coverage-computing stage.
406 */
407 int getFirstCoverageStage() const {
408 return fCurrDrawState.fFirstCoverageStage;
409 }
410
411 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000412 * Gets whether the target is drawing clockwise, counterclockwise,
413 * or both faces.
414 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000415 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000416 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
418 /**
419 * Enable render state settings.
420 *
421 * @param flags bitfield of StateBits specifing the states to enable
422 */
423 void enableState(uint32_t stateBits);
424
425 /**
426 * Disable render state settings.
427 *
428 * @param flags bitfield of StateBits specifing the states to disable
429 */
430 void disableState(uint32_t stateBits);
431
432 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000433 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
434 }
435
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000436 bool isAntialiasState() const {
437 return 0 != (fCurrDrawState.fFlagBits & kAntialias_StateBit);
438 }
439
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000440 bool isClipState() const {
441 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 }
443
bsalomon@google.comd302f142011-03-03 13:54:13 +0000444 bool isColorWriteDisabled() const {
445 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
446 }
447
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 * Sets the blending function coeffecients.
450 *
451 * The blend function will be:
452 * D' = sat(S*srcCoef + D*dstCoef)
453 *
454 * where D is the existing destination color, S is the incoming source
455 * color, and D' is the new destination color that will be written. sat()
456 * is the saturation function.
457 *
458 * @param srcCoef coeffecient applied to the src color.
459 * @param dstCoef coeffecient applied to the dst color.
460 */
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000461 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
463 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000464 * Sets the blending function constant referenced by the following blending
465 * coeffecients:
466 * kConstC_BlendCoeff
467 * kIConstC_BlendCoeff
468 * kConstA_BlendCoeff
469 * kIConstA_BlendCoeff
470 *
471 * @param constant the constant to set
472 */
473 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
474
475 /**
476 * Retrieves the last value set by setBlendConstant()
477 * @return the blending constant value
478 */
479 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
480
481 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000482 * Used to save and restore the GrGpu's drawing state
483 */
484 struct SavedDrawState {
485 private:
reed@google.com8195f672011-01-12 18:14:28 +0000486 DrState fState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 friend class GrDrawTarget;
488 };
489
490 /**
491 * Saves the current draw state. The state can be restored at a later time
492 * with restoreDrawState.
493 *
494 * See also AutoStateRestore class.
495 *
496 * @param state will hold the state after the function returns.
497 */
498 void saveCurrentDrawState(SavedDrawState* state) const;
499
500 /**
501 * Restores previously saved draw state. The client guarantees that state
502 * was previously passed to saveCurrentDrawState and that the rendertarget
503 * and texture set at save are still valid.
504 *
505 * See also AutoStateRestore class.
506 *
507 * @param state the previously saved state to restore.
508 */
509 void restoreDrawState(const SavedDrawState& state);
510
511 /**
512 * Copies the draw state from another target to this target.
513 *
514 * @param srcTarget draw target used as src of the draw state.
515 */
516 void copyDrawState(const GrDrawTarget& srcTarget);
517
518 /**
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000519 * The format of vertices is represented as a bitfield of flags.
520 * Flags that indicate the layout of vertex data. Vertices always contain
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521 * positions and may also contain up to kMaxTexCoords sets of 2D texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000522 * coordinates and per-vertex colors. Each stage can use any of the texture
523 * coordinates as its input texture coordinates or it may use the positions.
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000525 * If no texture coordinates are specified for a stage then the stage is
526 * disabled.
reed@google.comac10a2d2010-12-22 21:39:39 +0000527 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000528 * Only one type of texture coord can be specified per stage. For
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 * example StageTexCoordVertexLayoutBit(0, 2) and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000530 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000532 * The order in memory is always (position, texture coord 0, ..., color)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000533 * with any unused fields omitted. Note that this means that if only texture
bsalomon@google.com5782d712011-01-21 21:03:59 +0000534 * coordinates 1 is referenced then there is no texture coordinates 0 and
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000535 * the order would be (position, texture coordinate 1[, color]).
536 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000538 /**
539 * Generates a bit indicating that a texture stage uses texture coordinates
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000541 * @param stage the stage that will use texture coordinates.
542 * @param texCoordIdx the index of the texture coordinates to use
543 *
544 * @return the bit to add to a GrVertexLayout bitfield.
545 */
546 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
547 GrAssert(stage < kNumStages);
548 GrAssert(texCoordIdx < kMaxTexCoords);
549 return 1 << (stage + (texCoordIdx * kNumStages));
550 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000551
552 /**
553 * Determines if blend is effectively disabled.
554 *
555 * @return true if blend can be disabled without changing the rendering
556 * result given the current state including the vertex layout specified
557 * with the vertex source.
558 */
559 bool canDisableBlend() const;
560
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000561 /**
562 * Sets the edge data required for edge antialiasing.
563 *
564 * @param edges 3 * 6 float values, representing the edge
565 * equations in Ax + By + C form
566 */
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000567 void setEdgeAAData(const Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000568
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000569private:
570 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
571public:
572 /**
573 * Generates a bit indicating that a texture stage uses the position
574 * as its texture coordinate.
575 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000576 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000577 * coordinates.
578 *
579 * @return the bit to add to a GrVertexLayout bitfield.
580 */
581 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
582 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000584 }
585private:
586 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000587
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000588public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000589
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000590 /**
591 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 */
593 enum VertexLayoutBits {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000594
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000595 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
596 //<! vertices have colors
597 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
598 //<! use text vertices. (Pos
599 // and tex coords may be
bsalomon@google.com5782d712011-01-21 21:03:59 +0000600 // a different type for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000601 // text [GrGpuTextVertex vs
602 // GrPoint].)
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000604 kDummyVertexLayoutBit,
605 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000607 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000608 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000609
610 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000611 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000612 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000613 * can use a different method. Once geometry is specified it can be used for
614 * multiple drawIndexed and drawNonIndexed calls.
615 *
616 * Sometimes it is necessary to perform a draw while upstack code has
617 * already specified geometry that it isn't finished with. There are push
618 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000619 *
620 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
621 * caller's client has already provided vertex data in a format
622 * the time compatible with a GrVertexLayout. The array must contain the
623 * data at set*SourceToArray is called. The source stays in effect for
624 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
625 * again or one of the other two paths is chosen.
626 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000627 * 2. Reserve. This is most useful when the caller has data it must
628 * transform before drawing and is not long-lived. The caller requests
629 * that the draw target make room for some amount of vertex and/or index
630 * data. The target provides ptrs to hold the vertex and/or index data.
631 *
632 * The data is writable up until the next drawIndexed, drawNonIndexed,
633 * or pushGeometrySource At this point the data is frozen and the ptrs
634 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000635 *
636 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
638 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
639 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000640 */
641
642 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643 * Reserves space for vertices. Draw target will use reserved vertices at
644 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 *
646 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000647 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 * of vertices to be filled by caller. The next draw will read
649 * these vertices.
650 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000651 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000652 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000653 * GrDrawTarget to decide whether to put data in buffers, to group vertex
654 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000655 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000656 * After the next draw or pushGeometrySource the vertices ptr is no longer
657 * valid and the geometry data cannot be further modified. The contents
658 * that were put in the reserved space can be drawn by multiple draws,
659 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000660 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000661 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000662 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 * @param vertices will point to reserved vertex space if vertexCount is
664 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000665 *
666 * @return true if succeeded in allocating space for the vertices and false
667 * if not.
668 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000669 bool reserveVertexSpace(GrVertexLayout vertexLayout,
670 int vertexCount,
671 void** vertices);
672 /**
673 * Reserves space for indices. Draw target will use the reserved indices at
674 * the next indexed draw.
675 *
676 * If succeeds:
677 * if indexCount > 0, *indices will be the array
678 * of indices to be filled by caller. The next draw will read
679 * these indices.
680 *
681 * If a client does not already have a index buffer then this is the
682 * preferred way to allocate index data. It allows the subclass of
683 * GrDrawTarget to decide whether to put data in buffers, to group index
684 * data that uses the same state (e.g. for deferred rendering), etc.
685 *
686 * After the next indexed draw or pushGeometrySource the indices ptr is no
687 * longer valid and the geometry data cannot be further modified. The
688 * contents that were put in the reserved space can be drawn by multiple
689 * draws, however.
690 *
691 * @param indexCount the number of indices to reserve space for. Can be 0.
692 * @param indices will point to reserved index space if indexCount is
693 * non-zero. Illegal to pass NULL if indexCount > 0.
694 */
695
696 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000697 /**
698 * Provides hints to caller about the number of vertices and indices
699 * that can be allocated cheaply. This can be useful if caller is reserving
700 * space but doesn't know exactly how much geometry is needed.
701 *
702 * Also may hint whether the draw target should be flushed first. This is
703 * useful for deferred targets.
704 *
705 * @param vertexLayout layout of vertices caller would like to reserve
706 * @param vertexCount in: hint about how many vertices the caller would
707 * like to allocate.
708 * out: a hint about the number of vertices that can be
709 * allocated cheaply. Negative means no hint.
710 * Ignored if NULL.
711 * @param indexCount in: hint about how many indices the caller would
712 * like to allocate.
713 * out: a hint about the number of indices that can be
714 * allocated cheaply. Negative means no hint.
715 * Ignored if NULL.
716 *
717 * @return true if target should be flushed based on the input values.
718 */
719 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000720 int* vertexCount,
721 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000722
723 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000724 * Sets source of vertex data for the next draw. Array must contain
725 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000726 *
727 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000728 * @param size size of the vertex data.
729 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000731 void setVertexSourceToArray(GrVertexLayout vertexLayout,
732 const void* vertexArray,
733 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000734
735 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000736 * Sets source of index data for the next indexed draw. Array must contain
737 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000738 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000739 * @param array cpu array containing index data.
740 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000741 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000742 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000743
744 /**
745 * Sets source of vertex data for the next draw. Data does not have to be
746 * in the buffer until drawIndexed or drawNonIndexed.
747 *
748 * @param buffer vertex buffer containing vertex data. Must be
749 * unlocked before draw call.
750 * @param vertexLayout layout of the vertex data in the buffer.
751 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000752 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
753 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000754
755 /**
756 * Sets source of index data for the next indexed draw. Data does not have
757 * to be in the buffer until drawIndexed or drawNonIndexed.
758 *
759 * @param buffer index buffer containing indices. Must be unlocked
760 * before indexed draw call.
761 */
762 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763
764 /**
765 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
766 * source to reserved, array, or buffer before next draw. May be able to free
767 * up temporary storage allocated by setVertexSourceToArray or
768 * reserveVertexSpace.
769 */
770 void resetVertexSource();
771
772 /**
773 * Resets index source. Indexed Drawing from reset indices is illegal. Set
774 * index source to reserved, array, or buffer before next indexed draw. May
775 * be able to free up temporary storage allocated by setIndexSourceToArray
776 * or reserveIndexSpace.
777 */
778 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000779
780 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000781 * Pushes and resets the vertex/index sources. Any reserved vertex / index
782 * data is finalized (i.e. cannot be updated after the matching pop but can
783 * be drawn from). Must be balanced by a pop.
784 */
785 void pushGeometrySource();
786
787 /**
788 * Pops the vertex / index sources from the matching push.
789 */
790 void popGeometrySource();
791
792 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000793 * Draws indexed geometry using the current state and current vertex / index
794 * sources.
795 *
796 * @param type The type of primitives to draw.
797 * @param startVertex the vertex in the vertex array/buffer corresponding
798 * to index 0
799 * @param startIndex first index to read from index src.
800 * @param vertexCount one greater than the max index.
801 * @param indexCount the number of index elements to read. The index count
802 * is effectively trimmed to the last completely
803 * specified primitive.
804 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000805 void drawIndexed(GrPrimitiveType type,
806 int startVertex,
807 int startIndex,
808 int vertexCount,
809 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000810
811 /**
812 * Draws non-indexed geometry using the current state and current vertex
813 * sources.
814 *
815 * @param type The type of primitives to draw.
816 * @param startVertex the vertex in the vertex array/buffer corresponding
817 * to index 0
818 * @param vertexCount one greater than the max index.
819 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820 void drawNonIndexed(GrPrimitiveType type,
821 int startVertex,
822 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000823
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000824 /**
825 * Helper function for drawing rects. This does not use the current index
826 * and vertex sources. After returning, the vertex and index sources may
827 * have changed. They should be reestablished before the next drawIndexed
828 * or drawNonIndexed. This cannot be called between reserving and releasing
829 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000830 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000831 * drawNonIndexed.
832 * @param rect the rect to draw
833 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000834 * @param stageEnableBitfield bitmask indicating which stages are enabled.
835 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000836 * @param srcRects specifies rects for stages enabled by stageEnableMask.
837 * if stageEnableMask bit i is 1, srcRects is not NULL,
838 * and srcRects[i] is not NULL, then srcRects[i] will be
839 * used as coordinates for stage i. Otherwise, if stage i
840 * is enabled then rect is used as the coordinates.
841 * @param srcMatrices optional matrices applied to srcRects. If
842 * srcRect[i] is non-NULL and srcMatrices[i] is
843 * non-NULL then srcRect[i] will be transformed by
844 * srcMatrix[i]. srcMatrices can be NULL when no
845 * srcMatrices are desired.
846 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000847 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000848 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000849 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000850 const GrRect* srcRects[],
851 const GrMatrix* srcMatrices[]);
852
853 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000854 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000855 * matrices.
856 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000857 void drawSimpleRect(const GrRect& rect,
858 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000859 StageBitfield stageEnableBitfield) {
860 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000861 }
862
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000863 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000864 * Clear the render target. Ignores the clip and all other draw state
865 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
866 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000867 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000868 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000869
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000870 /**
871 * Returns the maximum number of edges that may be specified in a single
872 * draw call when performing edge antialiasing. This is usually limited
873 * by the number of fragment uniforms which may be uploaded. Must be a
874 * minimum of six, since a triangle's vertices each belong to two boundary
875 * edges which may be distinct.
876 */
877 virtual int getMaxEdges() const { return 6; }
878
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000879 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000880
881 class AutoStateRestore : ::GrNoncopyable {
882 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000883 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000884 AutoStateRestore(GrDrawTarget* target);
885 ~AutoStateRestore();
886
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000887 /**
888 * if this object is already saving state for param target then
889 * this does nothing. Otherise, it restores previously saved state on
890 * previous target (if any) and saves current state on param target.
891 */
892 void set(GrDrawTarget* target);
893
reed@google.comac10a2d2010-12-22 21:39:39 +0000894 private:
895 GrDrawTarget* fDrawTarget;
896 SavedDrawState fDrawState;
897 };
898
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000899 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000900
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000901 class AutoViewMatrixRestore : ::GrNoncopyable {
902 public:
903 AutoViewMatrixRestore() {
904 fDrawTarget = NULL;
905 }
906
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000907 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000908 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
909 GrAssert(NULL != target);
910 }
911
912 void set(GrDrawTarget* target) {
913 GrAssert(NULL != target);
914 if (NULL != fDrawTarget) {
915 fDrawTarget->setViewMatrix(fMatrix);
916 }
917 fDrawTarget = target;
918 fMatrix = target->getViewMatrix();
919 }
920
921 ~AutoViewMatrixRestore() {
922 if (NULL != fDrawTarget) {
923 fDrawTarget->setViewMatrix(fMatrix);
924 }
925 }
926
927 private:
928 GrDrawTarget* fDrawTarget;
929 GrMatrix fMatrix;
930 };
931
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000932 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000933
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000934 /**
935 * Sets the view matrix to I and preconcats all stage matrices enabled in
936 * mask by the view inverse. Destructor undoes these changes.
937 */
938 class AutoDeviceCoordDraw : ::GrNoncopyable {
939 public:
940 AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask);
941 ~AutoDeviceCoordDraw();
942 private:
943 GrDrawTarget* fDrawTarget;
944 GrMatrix fViewMatrix;
945 GrMatrix fSamplerMatrices[kNumStages];
946 int fStageMask;
947 };
948
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000949 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000950
reed@google.comac10a2d2010-12-22 21:39:39 +0000951 class AutoReleaseGeometry : ::GrNoncopyable {
952 public:
953 AutoReleaseGeometry(GrDrawTarget* target,
954 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000955 int vertexCount,
956 int indexCount);
957 AutoReleaseGeometry();
958 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000959 bool set(GrDrawTarget* target,
960 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000961 int vertexCount,
962 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000963 bool succeeded() const { return NULL != fTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000964 void* vertices() const { return fVertices; }
965 void* indices() const { return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000966 GrPoint* positions() const {
967 return static_cast<GrPoint*>(fVertices);
968 }
969
970 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000971 void reset();
972
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000974 void* fVertices;
975 void* fIndices;
976 };
977
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000978 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000979
980 class AutoClipRestore : ::GrNoncopyable {
981 public:
982 AutoClipRestore(GrDrawTarget* target) {
983 fTarget = target;
984 fClip = fTarget->getClip();
985 }
986
987 ~AutoClipRestore() {
988 fTarget->setClip(fClip);
989 }
990 private:
991 GrDrawTarget* fTarget;
992 GrClip fClip;
993 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000994
995 ////////////////////////////////////////////////////////////////////////////
996
997 class AutoGeometryPush : ::GrNoncopyable {
998 public:
999 AutoGeometryPush(GrDrawTarget* target) {
1000 GrAssert(NULL != target);
1001 fTarget = target;
1002 target->pushGeometrySource();
1003 }
1004 ~AutoGeometryPush() {
1005 fTarget->popGeometrySource();
1006 }
1007 private:
1008 GrDrawTarget* fTarget;
1009 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
1011 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001012 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001013
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 /**
1015 * Helper function to compute the size of a vertex from a vertex layout
1016 * @return size of a single vertex.
1017 */
1018 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001019
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001020 /**
1021 * Helper function for determining the index of texture coordinates that
1022 * is input for a texture stage. Note that a stage may instead use positions
1023 * as texture coordinates, in which case the result of the function is
1024 * indistinguishable from the case when the stage is disabled.
1025 *
1026 * @param stage the stage to query
1027 * @param vertexLayout layout to query
1028 *
1029 * @return the texture coordinate index or -1 if the stage doesn't use
1030 * separate (non-position) texture coordinates.
1031 */
1032 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001033
1034 /**
1035 * Helper function to compute the offset of texture coordinates in a vertex
1036 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001037 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001038 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001040 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001041
1042 /**
1043 * Helper function to compute the offset of the color in a vertex
1044 * @return offset of color in vertex layout or -1 if the
1045 * layout has no color.
1046 */
1047 static int VertexColorOffset(GrVertexLayout vertexLayout);
1048
1049 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001050 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001051 * coordinates of some index.
1052 *
1053 * @param coordIndex the tex coord index to query
1054 * @param vertexLayout layout to query
1055 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001056 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001057 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001059 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001060 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001061
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 /**
1063 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001064 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001066 * @param stage the stage to query
1067 * @param vertexLayout layout to query
1068 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001069 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001070 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001072 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001073
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001074 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001075 * Helper function to compute the size of each vertex and the offsets of
1076 * texture coordinates and color. Determines tex coord offsets by tex coord
1077 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001078 * by StageTexCoordVertexLayoutBit.)
1079 *
1080 * @param vertexLayout the layout to query
1081 * @param texCoordOffsetsByIdx after return it is the offset of each
1082 * tex coord index in the vertex or -1 if
1083 * index isn't used.
1084 * @return size of a single vertex
1085 */
1086 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1087 int texCoordOffsetsByIdx[kMaxTexCoords],
1088 int *colorOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001089
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001090 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001091 * Helper function to compute the size of each vertex and the offsets of
1092 * texture coordinates and color. Determines tex coord offsets by stage
1093 * rather than by index. (Each stage can be mapped to any t.c. index
1094 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001095 * tex coords then that stage's offset will be 0 (positions are always at 0).
1096 *
1097 * @param vertexLayout the layout to query
1098 * @param texCoordOffsetsByStage after return it is the offset of each
1099 * tex coord index in the vertex or -1 if
1100 * index isn't used.
1101 * @return size of a single vertex
1102 */
1103 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1104 int texCoordOffsetsByStage[kNumStages],
1105 int *colorOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001106
1107 /**
1108 * Accessing positions, texture coords, or colors, of a vertex within an
1109 * array is a hassle involving casts and simple math. These helpers exist
1110 * to keep GrDrawTarget clients' code a bit nicer looking.
1111 */
1112
1113 /**
1114 * Gets a pointer to a GrPoint of a vertex's position or texture
1115 * coordinate.
1116 * @param vertices the vetex array
1117 * @param vertexIndex the index of the vertex in the array
1118 * @param vertexSize the size of each vertex in the array
1119 * @param offset the offset in bytes of the vertex component.
1120 * Defaults to zero (corresponding to vertex position)
1121 * @return pointer to the vertex component as a GrPoint
1122 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001123 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001124 int vertexIndex,
1125 int vertexSize,
1126 int offset = 0) {
1127 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001128 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001129 vertexIndex * vertexSize);
1130 }
1131 static const GrPoint* GetVertexPoint(const void* vertices,
1132 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001133 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001134 int offset = 0) {
1135 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001136 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001137 vertexIndex * vertexSize);
1138 }
1139
1140 /**
1141 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1142 * @param vertices the vetex array
1143 * @param vertexIndex the index of the vertex in the array
1144 * @param vertexSize the size of each vertex in the array
1145 * @param offset the offset in bytes of the vertex color
1146 * @return pointer to the vertex component as a GrColor
1147 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001148 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001149 int vertexIndex,
1150 int vertexSize,
1151 int offset) {
1152 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001153 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001154 vertexIndex * vertexSize);
1155 }
1156 static const GrColor* GetVertexColor(const void* vertices,
1157 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001158 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001159 int offset) {
1160 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001161 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001162 vertexIndex * vertexSize);
1163 }
1164
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001165 static void VertexLayoutUnitTest();
1166
reed@google.comac10a2d2010-12-22 21:39:39 +00001167protected:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001168
1169 enum GeometrySrcType {
1170 kNone_GeometrySrcType, //<! src has not been specified
1171 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1172 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1173 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1174 };
1175
1176 struct GeometrySrcState {
1177 GeometrySrcType fVertexSrc;
1178 union {
1179 // valid if src type is buffer
1180 const GrVertexBuffer* fVertexBuffer;
1181 // valid if src type is reserved or array
1182 int fVertexCount;
1183 };
1184
1185 GeometrySrcType fIndexSrc;
1186 union {
1187 // valid if src type is buffer
1188 const GrIndexBuffer* fIndexBuffer;
1189 // valid if src type is reserved or array
1190 int fIndexCount;
1191 };
1192
1193 GrVertexLayout fVertexLayout;
1194 };
1195
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001196 // given a vertex layout and a draw state, will a stage be used?
1197 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
1198 const DrState& state) {
1199 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1200 }
1201
1202 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001203 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1204 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001205 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001206
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 // Helpers for GrDrawTarget subclasses that won't have private access to
1208 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001209 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001210 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001211 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 { return sds.fState; }
1213
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001214 // implemented by subclass to allocate space for reserved geom
1215 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1216 int vertexCount,
1217 void** vertices) = 0;
1218 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1219 // implemented by subclass to handle release of reserved geom space
1220 virtual void releaseReservedVertexSpace() = 0;
1221 virtual void releaseReservedIndexSpace() = 0;
1222 // subclass must consume array contents when set
1223 virtual void onSetVertexSourceToArray(const void* vertexArray,
1224 int vertexCount) = 0;
1225 virtual void onSetIndexSourceToArray(const void* indexArray,
1226 int indexCount) = 0;
1227 // subclass is notified that geom source will be set away from an array
1228 virtual void releaseVertexArray() = 0;
1229 virtual void releaseIndexArray() = 0;
1230 // subclass overrides to be notified just before geo src state
1231 // is pushed/popped.
1232 virtual void geometrySourceWillPush() = 0;
1233 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1234 // subclass called to perform drawing
1235 virtual void onDrawIndexed(GrPrimitiveType type,
1236 int startVertex,
1237 int startIndex,
1238 int vertexCount,
1239 int indexCount) = 0;
1240 virtual void onDrawNonIndexed(GrPrimitiveType type,
1241 int startVertex,
1242 int vertexCount) = 0;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001243 // subclass overrides to be notified when clip is set.
1244 virtual void clipWillBeSet(const GrClip& clip) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +00001245
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001246
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001247 // Helpers for drawRect, protected so subclasses that override drawRect
1248 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001249 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001250 const GrRect* srcRects[]);
1251
1252 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001253 const GrMatrix* matrix,
1254 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001255 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001256 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001257 void* vertices);
1258
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001259 // accessor for derived classes
1260 const GeometrySrcState& getGeomSrc() const {
1261 return fGeoSrcStateStack.back();
1262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001263
1264 GrClip fClip;
1265
reed@google.com8195f672011-01-12 18:14:28 +00001266 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001267
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001268private:
1269 // called when setting a new vert/idx source to unref prev vb/ib
1270 void releasePreviousVertexSource();
1271 void releasePreviousIndexSource();
1272
1273 enum {
1274 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001276 GrAlignedSTStorage<kPreallocGeoSrcStateStackCnt,
1277 GeometrySrcState>
1278 fGeoSrcStateStackStorage;
1279 GrTArray<GeometrySrcState, true> fGeoSrcStateStack;
1280
reed@google.comac10a2d2010-12-22 21:39:39 +00001281};
1282
1283#endif