blob: c85fc35c25f755de932d19f39d060ef2aeaafdc4 [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 "GrClip.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrColor.h"
16#include "GrMatrix.h"
17#include "GrRefCnt.h"
18#include "GrRenderTarget.h"
19#include "GrSamplerState.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000020#include "GrStencil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
Scroggo97c88c22011-05-11 14:05:25 +000023#include "SkXfermode.h"
24
reed@google.comac10a2d2010-12-22 21:39:39 +000025class GrTexture;
reed@google.comac10a2d2010-12-22 21:39:39 +000026class GrClipIterator;
27class GrVertexBuffer;
28class GrIndexBuffer;
29
30class GrDrawTarget : public GrRefCnt {
31public:
32 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033 * Represents the draw target capabilities.
34 */
35 struct Caps {
36 Caps() { memset(this, 0, sizeof(Caps)); }
37 Caps(const Caps& c) { *this = c; }
38 Caps& operator= (const Caps& c) {
39 memcpy(this, &c, sizeof(Caps));
40 return *this;
41 }
42 void print() const;
43 bool f8BitPaletteSupport : 1;
44 bool fNPOTTextureSupport : 1;
45 bool fNPOTTextureTileSupport : 1;
46 bool fNPOTRenderTargetSupport : 1;
47 bool fTwoSidedStencilSupport : 1;
48 bool fStencilWrapOpsSupport : 1;
49 bool fHWAALineSupport : 1;
50 bool fShaderSupport : 1;
51 bool fShaderDerivativeSupport : 1;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000052 bool fGeometryShaderSupport : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000053 bool fFSAASupport : 1;
54 bool fDualSourceBlendingSupport : 1;
55 bool fBufferLockSupport : 1;
bsalomon@google.coma3108262011-10-10 14:08:47 +000056 bool fSupportPerVertexCoverage : 1;
bsalomon@google.com18c9c192011-09-22 21:01:31 +000057 int fMinRenderTargetWidth;
58 int fMinRenderTargetHeight;
59 int fMaxRenderTargetSize;
60 int fMaxTextureSize;
61 };
62
63 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +000064 * Number of texture stages. Each stage takes as input a color and
65 * 2D texture coordinates. The color input to the first enabled stage is the
66 * per-vertex color or the constant color (setColor/setAlpha) if there are
67 * no per-vertex colors. For subsequent stages the input color is the output
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000068 * color from the previous enabled stage. The output color of each stage is
bsalomon@google.com5782d712011-01-21 21:03:59 +000069 * the input color modulated with the result of a texture lookup. Texture
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000070 * lookups are specified by a texture a sampler (setSamplerState). Texture
71 * coordinates for each stage come from the vertices based on a
72 * GrVertexLayout bitfield. The output fragment color is the output color of
73 * the last enabled stage. The presence or absence of texture coordinates
74 * for each stage in the vertex layout indicates whether a stage is enabled
75 * or not.
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000076 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000077 enum {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000078 kNumStages = 3,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000079 kMaxTexCoords = kNumStages
80 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000082 /**
83 * The absolute maximum number of edges that may be specified for
84 * a single draw call when performing edge antialiasing. This is used for
85 * the size of several static buffers, so implementations of getMaxEdges()
86 * (below) should clamp to this value.
87 */
88 enum {
89 kMaxEdges = 32
90 };
91
bsalomon@google.comaeb21602011-08-30 18:13:44 +000092 /**
93 * When specifying edges as vertex data this enum specifies what type of
94 * edges are in use. The edges are always 4 GrScalars in memory, even when
95 * the edge type requires fewer than 4.
96 */
97 enum VertexEdgeType {
98 /* 1-pixel wide line
99 2D implicit line eq (a*x + b*y +c = 0). 4th component unused */
100 kHairLine_EdgeType,
101 /* 1-pixel wide quadratic
102 u^2-v canonical coords (only 2 components used) */
103 kHairQuad_EdgeType
104 };
105
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000106 /**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000107 * Bitfield used to indicate which stages are in use.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000109 typedef int StageBitfield;
110 GR_STATIC_ASSERT(sizeof(StageBitfield)*8 >= kNumStages);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 /**
113 * Flags that affect rendering. Controlled using enable/disableState(). All
114 * default to disabled.
115 */
116 enum StateBits {
bsalomon@google.com289533a2011-10-27 12:34:25 +0000117 /**
118 * Perform dithering. TODO: Re-evaluate whether we need this bit
119 */
120 kDither_StateBit = 0x01,
121 /**
122 * Perform HW anti-aliasing. This means either HW FSAA, if supported
123 * by the render target, or smooth-line rendering if a line primitive
124 * is drawn and line smoothing is supported by the 3D API.
125 */
126 kHWAntialias_StateBit = 0x02,
127 /**
128 * Draws will respect the clip, otherwise the clip is ignored.
129 */
130 kClip_StateBit = 0x04,
131 /**
132 * Disables writing to the color buffer. Useful when performing stencil
133 * operations.
134 */
135 kNoColorWrites_StateBit = 0x08,
136 /**
137 * Modifies the behavior of edge AA specified by setEdgeAA. If set,
138 * will test edge pairs for convexity when rasterizing. Set this if the
139 * source polygon is non-convex.
140 */
141 kEdgeAAConcave_StateBit = 0x10,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142 // subclass may use additional bits internally
143 kDummyStateBit,
144 kLastPublicStateBit = kDummyStateBit-1
145 };
146
147 enum DrawFace {
148 kBoth_DrawFace,
149 kCCW_DrawFace,
150 kCW_DrawFace,
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 };
152
153 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000154 * Sets the stencil settings to use for the next draw.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000155 * Changing the clip has the side-effect of possibly zeroing
156 * out the client settable stencil bits. So multipass algorithms
157 * using stencil should not change the clip between passes.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000158 * @param settings the stencil settings to use.
159 */
160 void setStencil(const GrStencilSettings& settings) {
161 fCurrDrawState.fStencilSettings = settings;
162 }
163
164 /**
165 * Shortcut to disable stencil testing and ops.
166 */
167 void disableStencil() {
168 fCurrDrawState.fStencilSettings.setDisabled();
169 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000170
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000171 class Edge {
172 public:
173 Edge() {}
174 Edge(float x, float y, float z) : fX(x), fY(y), fZ(z) {}
175 GrPoint intersect(const Edge& other) {
176 return GrPoint::Make(
177 (fY * other.fZ - other.fY * fZ) /
178 (fX * other.fY - other.fX * fY),
179 (fX * other.fZ - other.fX * fZ) /
180 (other.fX * fY - fX * other.fY));
181 }
182 float fX, fY, fZ;
183 };
184
reed@google.comac10a2d2010-12-22 21:39:39 +0000185protected:
reed@google.comac10a2d2010-12-22 21:39:39 +0000186
reed@google.com8195f672011-01-12 18:14:28 +0000187 struct DrState {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000188 DrState() {
189 // make sure any pad is zero for memcmp
190 // all DrState members should default to something
191 // valid by the memset
192 memset(this, 0, sizeof(DrState));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000193
194 // memset exceptions
Scroggo97c88c22011-05-11 14:05:25 +0000195 fColorFilterXfermode = SkXfermode::kDstIn_Mode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000196 fFirstCoverageStage = kNumStages;
197
198 // pedantic assertion that our ptrs will
199 // be NULL (0 ptr is mem addr 0)
bsalomon@google.comd302f142011-03-03 13:54:13 +0000200 GrAssert((intptr_t)(void*)NULL == 0LL);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000201
202 // default stencil setting should be disabled
bsalomon@google.comd302f142011-03-03 13:54:13 +0000203 GrAssert(fStencilSettings.isDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000204 fFirstCoverageStage = kNumStages;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000205 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 uint32_t fFlagBits;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000207 GrBlendCoeff fSrcBlend;
208 GrBlendCoeff fDstBlend;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000209 GrColor fBlendConstant;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000210 GrTexture* fTextures[kNumStages];
211 GrSamplerState fSamplerStates[kNumStages];
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000212 int fFirstCoverageStage;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000213 GrRenderTarget* fRenderTarget;
214 GrColor fColor;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000215 DrawFace fDrawFace;
Scroggo97c88c22011-05-11 14:05:25 +0000216 GrColor fColorFilterColor;
217 SkXfermode::Mode fColorFilterXfermode;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000218
219 GrStencilSettings fStencilSettings;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000220 GrMatrix fViewMatrix;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000221 VertexEdgeType fVertexEdgeType;
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000222 Edge fEdgeAAEdges[kMaxEdges];
223 int fEdgeAANumEdges;
reed@google.com8195f672011-01-12 18:14:28 +0000224 bool operator ==(const DrState& s) const {
225 return 0 == memcmp(this, &s, sizeof(DrState));
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 }
reed@google.com8195f672011-01-12 18:14:28 +0000227 bool operator !=(const DrState& s) const { return !(*this == s); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000228 };
229
230public:
231 ///////////////////////////////////////////////////////////////////////////
232
233 GrDrawTarget();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000234 virtual ~GrDrawTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
236 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 * Gets the capabilities of the draw target.
238 */
239 const Caps& getCaps() const { return fCaps; }
240
241 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 * Sets the current clip to the region specified by clip. All draws will be
243 * clipped against this clip if kClip_StateBit is enabled.
244 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000245 * Setting the clip may (or may not) zero out the client's stencil bits.
246 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 * @param description of the clipping region
248 */
249 void setClip(const GrClip& clip);
250
251 /**
252 * Gets the current clip.
253 *
254 * @return the clip.
255 */
256 const GrClip& getClip() const;
257
258 /**
259 * Sets the texture used at the next drawing call
260 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000261 * @param stage The texture stage for which the texture will be set
262 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 * @param texture The texture to set. Can be NULL though there is no advantage
264 * to settings a NULL texture if doing non-textured drawing
265 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000266 void setTexture(int stage, GrTexture* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
268 /**
269 * Retrieves the currently set texture.
270 *
271 * @return The currently set texture. The return value will be NULL if no
272 * texture has been set, NULL was most recently passed to
273 * setTexture, or the last setTexture was destroyed.
274 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000275 const GrTexture* getTexture(int stage) const;
276 GrTexture* getTexture(int stage);
reed@google.comac10a2d2010-12-22 21:39:39 +0000277
278 /**
279 * Sets the rendertarget used at the next drawing call
280 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000281 * @param target The render target to set.
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 */
283 void setRenderTarget(GrRenderTarget* target);
284
285 /**
286 * Retrieves the currently set rendertarget.
287 *
288 * @return The currently set render target.
289 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000290 const GrRenderTarget* getRenderTarget() const;
291 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
293 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000294 * Sets the sampler state for a stage used in subsequent draws.
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000296 * The sampler state determines how texture coordinates are
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000297 * intepretted and used to sample the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000299 * @param stage the stage of the sampler to set
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 * @param samplerState Specifies the sampler state.
301 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000302 void setSamplerState(int stage, const GrSamplerState& samplerState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
304 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000305 * Concats the matrix of a stage's sampler.
reed@google.comac10a2d2010-12-22 21:39:39 +0000306 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000307 * @param stage the stage of the sampler to set
308 * @param matrix the matrix to concat
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 */
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310 void preConcatSamplerMatrix(int stage, const GrMatrix& matrix) {
311 GrAssert(stage >= 0 && stage < kNumStages);
312 fCurrDrawState.fSamplerStates[stage].preConcatMatrix(matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000313 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
315 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000316 * Shortcut for preConcatSamplerMatrix on all stages in mask with same
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000317 * matrix
318 */
319 void preConcatSamplerMatrices(int stageMask, const GrMatrix& matrix) {
320 for (int i = 0; i < kNumStages; ++i) {
321 if ((1 << i) & stageMask) {
322 this->preConcatSamplerMatrix(i, matrix);
323 }
324 }
325 }
326
327 /**
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000328 * Shortcut for preConcatSamplerMatrix on all enabled stages in mask with
329 * same matrix
330 *
331 * @param stage the stage of the sampler to set
332 * @param matrix the matrix to concat
333 */
334 void preConcatEnabledSamplerMatrices(const GrMatrix& matrix) {
335 StageBitfield stageMask = this->enabledStages();
336 this->preConcatSamplerMatrices(stageMask, matrix);
337 }
338
339 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000340 * Gets the matrix of a stage's sampler
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000341 *
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000342 * @param stage the stage to of sampler to get
343 * @return the sampler state's matrix
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000344 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000345 const GrMatrix& getSamplerMatrix(int stage) const {
346 return fCurrDrawState.fSamplerStates[stage].getMatrix();
347 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000348
349 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000350 * Sets the matrix of a stage's sampler
351 *
352 * @param stage the stage of sampler set
353 * @param matrix the matrix to set
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000354 */
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000355 void setSamplerMatrix(int stage, const GrMatrix& matrix) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000356 fCurrDrawState.fSamplerStates[stage].setMatrix(matrix);
357 }
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000358
359 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000360 * Sets the matrix applied to veretx positions.
361 *
362 * In the post-view-matrix space the rectangle [0,w]x[0,h]
363 * fully covers the render target. (w and h are the width and height of the
364 * the rendertarget.)
365 *
366 * @param m the matrix used to transform the vertex positions.
367 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000368 void setViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369
370 /**
371 * Multiplies the current view matrix by a matrix
372 *
373 * After this call V' = V*m where V is the old view matrix,
374 * m is the parameter to this function, and V' is the new view matrix.
375 * (We consider positions to be column vectors so position vector p is
376 * transformed by matrix X as p' = X*p.)
377 *
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000378 * @param m the matrix used to modify the view matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000379 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000380 void preConcatViewMatrix(const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
382 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000383 * Multiplies the current view matrix by a matrix
384 *
385 * After this call V' = m*V where V is the old view matrix,
386 * m is the parameter to this function, and V' is the new view matrix.
387 * (We consider positions to be column vectors so position vector p is
388 * transformed by matrix X as p' = X*p.)
389 *
390 * @param m the matrix used to modify the view matrix.
391 */
392 void postConcatViewMatrix(const GrMatrix& m);
393
394 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000395 * Retrieves the current view matrix
396 * @return the current view matrix.
397 */
398 const GrMatrix& getViewMatrix() const;
399
400 /**
401 * Retrieves the inverse of the current view matrix.
402 *
403 * If the current view matrix is invertible, return true, and if matrix
404 * is non-null, copy the inverse into it. If the current view matrix is
405 * non-invertible, return false and ignore the matrix parameter.
406 *
407 * @param matrix if not null, will receive a copy of the current inverse.
408 */
409 bool getViewInverse(GrMatrix* matrix) const;
410
411 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 * Sets color for next draw to a premultiplied-alpha color.
413 *
414 * @param the color to set.
415 */
416 void setColor(GrColor);
417
418 /**
bsalomon@google.coma3108262011-10-10 14:08:47 +0000419 * Gets the currently set color.
420 * @return the current color.
421 */
422 GrColor getColor() const { return fCurrDrawState.fColor; }
423
424 /**
Scroggo97c88c22011-05-11 14:05:25 +0000425 * Add a color filter that can be represented by a color and a mode.
426 */
427 void setColorFilter(GrColor, SkXfermode::Mode);
428
429 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000430 * Sets the color to be used for the next draw to be
431 * (r,g,b,a) = (alpha, alpha, alpha, alpha).
432 *
433 * @param alpha The alpha value to set as the color.
434 */
435 void setAlpha(uint8_t alpha);
436
437 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000438 * Controls whether clockwise, counterclockwise, or both faces are drawn.
439 * @param face the face(s) to draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000441 void setDrawFace(DrawFace face) { fCurrDrawState.fDrawFace = face; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
443 /**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000444 * A common pattern is to compute a color with the initial stages and then
445 * modulate that color by a coverage value in later stage(s) (AA, mask-
446 * filters, glyph mask, etc). Color-filters, xfermodes, etc should be
447 * computed based on the pre-coverage-modulated color. The division of
448 * stages between color-computing and coverage-computing is specified by
449 * this method. Initially this is kNumStages (all stages are color-
450 * computing).
451 */
452 void setFirstCoverageStage(int firstCoverageStage) {
453 fCurrDrawState.fFirstCoverageStage = firstCoverageStage;
454 }
455
456 /**
457 * Gets the index of the first coverage-computing stage.
458 */
459 int getFirstCoverageStage() const {
460 return fCurrDrawState.fFirstCoverageStage;
461 }
462
463 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000464 * Gets whether the target is drawing clockwise, counterclockwise,
465 * or both faces.
466 * @return the current draw face(s).
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000468 DrawFace getDrawFace() const { return fCurrDrawState.fDrawFace; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
470 /**
471 * Enable render state settings.
472 *
473 * @param flags bitfield of StateBits specifing the states to enable
474 */
475 void enableState(uint32_t stateBits);
476
477 /**
478 * Disable render state settings.
479 *
480 * @param flags bitfield of StateBits specifing the states to disable
481 */
482 void disableState(uint32_t stateBits);
483
484 bool isDitherState() const {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000485 return 0 != (fCurrDrawState.fFlagBits & kDither_StateBit);
486 }
487
bsalomon@google.com289533a2011-10-27 12:34:25 +0000488 bool isHWAntialiasState() const {
489 return 0 != (fCurrDrawState.fFlagBits & kHWAntialias_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000490 }
491
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000492 bool isClipState() const {
493 return 0 != (fCurrDrawState.fFlagBits & kClip_StateBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 }
495
bsalomon@google.comd302f142011-03-03 13:54:13 +0000496 bool isColorWriteDisabled() const {
497 return 0 != (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit);
498 }
499
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 * Sets the blending function coeffecients.
502 *
503 * The blend function will be:
504 * D' = sat(S*srcCoef + D*dstCoef)
505 *
506 * where D is the existing destination color, S is the incoming source
507 * color, and D' is the new destination color that will be written. sat()
508 * is the saturation function.
509 *
510 * @param srcCoef coeffecient applied to the src color.
511 * @param dstCoef coeffecient applied to the dst color.
512 */
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000513 void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
reed@google.comac10a2d2010-12-22 21:39:39 +0000514
515 /**
bsalomon@google.com080773c2011-03-15 19:09:25 +0000516 * Sets the blending function constant referenced by the following blending
517 * coeffecients:
518 * kConstC_BlendCoeff
519 * kIConstC_BlendCoeff
520 * kConstA_BlendCoeff
521 * kIConstA_BlendCoeff
522 *
523 * @param constant the constant to set
524 */
525 void setBlendConstant(GrColor constant) { fCurrDrawState.fBlendConstant = constant; }
526
527 /**
528 * Retrieves the last value set by setBlendConstant()
529 * @return the blending constant value
530 */
531 GrColor getBlendConstant() const { return fCurrDrawState.fBlendConstant; }
532
533 /**
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000534 * Determines if blending will require a read of a dst given the current
535 * state set on the draw target
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000536 *
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000537 * @return true if the dst surface will be read at each pixel hit by the
538 * a draw operation.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000539 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000540 bool drawWillReadDst() const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000541
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000542 /**
543 * Color alpha and coverage are two inputs to the drawing pipeline. For some
544 * blend modes it is safe to fold the coverage into constant or per-vertex
545 * color alpha value. For other blend modes they must be handled separately.
546 * Depending on features available in the underlying 3D API this may or may
547 * not be possible.
548 *
549 * This function looks at the current blend on the draw target and the draw
550 * target's capabilities to determine whether coverage can be handled
551 * correctly.
552 */
553 bool canApplyCoverage() const;
554
555 /**
556 * Determines whether incorporating partial pixel coverage into the constant
557 * color specified by setColor or per-vertex colors will give the right
558 * blending result.
559 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000560 bool canTweakAlphaForCoverage() const;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000561
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000562 /**
563 * Determines the interpretation per-vertex edge data when the
564 * kEdge_VertexLayoutBit is set (see below). When per-vertex edges are not
565 * specified the value of this setting has no effect.
566 */
567 void setVertexEdgeType(VertexEdgeType type) {
568 fCurrDrawState.fVertexEdgeType = type;
569 }
570
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000571 /**
bsalomon@google.com471d4712011-08-23 15:45:25 +0000572 * Given the current draw state, vertex layout, and hw support, will HW AA
573 * lines be used (if line primitive type is drawn)? (Note that lines are
574 * always 1 pixel wide)
575 */
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000576 bool willUseHWAALines() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000577
578 /**
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000579 * Sets the edge data required for edge antialiasing.
580 *
581 * @param edges 3 * 6 float values, representing the edge
582 * equations in Ax + By + C form
583 */
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000584 void setEdgeAAData(const Edge* edges, int numEdges);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000585
bsalomon@google.coma3108262011-10-10 14:08:47 +0000586 /**
587 * Used to save and restore the GrGpu's drawing state
588 */
589 struct SavedDrawState {
590 private:
591 DrState fState;
592 friend class GrDrawTarget;
593 };
594
595 /**
596 * Saves the current draw state. The state can be restored at a later time
597 * with restoreDrawState.
598 *
599 * See also AutoStateRestore class.
600 *
601 * @param state will hold the state after the function returns.
602 */
603 void saveCurrentDrawState(SavedDrawState* state) const;
604
605 /**
606 * Restores previously saved draw state. The client guarantees that state
607 * was previously passed to saveCurrentDrawState and that the rendertarget
608 * and texture set at save are still valid.
609 *
610 * See also AutoStateRestore class.
611 *
612 * @param state the previously saved state to restore.
613 */
614 void restoreDrawState(const SavedDrawState& state);
615
616 /**
617 * Copies the draw state from another target to this target.
618 *
619 * @param srcTarget draw target used as src of the draw state.
620 */
621 void copyDrawState(const GrDrawTarget& srcTarget);
622
623 /**
624 * The format of vertices is represented as a bitfield of flags.
625 * Flags that indicate the layout of vertex data. Vertices always contain
626 * positions and may also contain up to kMaxTexCoords sets of 2D texture
627 * coordinates, per-vertex colors, and per-vertex coverage. Each stage can
628 * use any of the texture coordinates as its input texture coordinates or it
629 * may use the positions as texture coordinates.
630 *
631 * If no texture coordinates are specified for a stage then the stage is
632 * disabled.
633 *
634 * Only one type of texture coord can be specified per stage. For
635 * example StageTexCoordVertexLayoutBit(0, 2) and
636 * StagePosAsTexCoordVertexLayoutBit(0) cannot both be specified.
637 *
638 * The order in memory is always (position, texture coord 0, ..., color,
639 * coverage) with any unused fields omitted. Note that this means that if
640 * only texture coordinates 1 is referenced then there is no texture
641 * coordinates 0 and the order would be (position, texture coordinate 1
642 * [, color][, coverage]).
643 */
644
645 /**
646 * Generates a bit indicating that a texture stage uses texture coordinates
647 *
648 * @param stage the stage that will use texture coordinates.
649 * @param texCoordIdx the index of the texture coordinates to use
650 *
651 * @return the bit to add to a GrVertexLayout bitfield.
652 */
653 static int StageTexCoordVertexLayoutBit(int stage, int texCoordIdx) {
654 GrAssert(stage < kNumStages);
655 GrAssert(texCoordIdx < kMaxTexCoords);
656 return 1 << (stage + (texCoordIdx * kNumStages));
657 }
658
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000659private:
660 static const int TEX_COORD_BIT_CNT = kNumStages*kMaxTexCoords;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000661
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000662public:
663 /**
664 * Generates a bit indicating that a texture stage uses the position
665 * as its texture coordinate.
666 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000667 * @param stage the stage that will use position as texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000668 * coordinates.
669 *
670 * @return the bit to add to a GrVertexLayout bitfield.
671 */
672 static int StagePosAsTexCoordVertexLayoutBit(int stage) {
673 GrAssert(stage < kNumStages);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000674 return (1 << (TEX_COORD_BIT_CNT + stage));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000675 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000676
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000677private:
678 static const int STAGE_BIT_CNT = TEX_COORD_BIT_CNT + kNumStages;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000680public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000682 /**
683 * Additional Bits that can be specified in GrVertexLayout.
reed@google.comac10a2d2010-12-22 21:39:39 +0000684 */
685 enum VertexLayoutBits {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000686 /* vertices have colors (GrColor) */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000687 kColor_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 0),
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000688 /* vertices have coverage (GrColor where all channels should have the
689 * same value)
690 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 kCoverage_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 1),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000692 /* Use text vertices. (Pos and tex coords may be a different type for
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000693 * text [GrGpuTextVertex vs GrPoint].)
694 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 kTextFormat_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 2),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000696
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000697 /* Each vertex specificies an edge. Distance to the edge is used to
698 * compute a coverage. See setVertexEdgeType().
699 */
bsalomon@google.coma3108262011-10-10 14:08:47 +0000700 kEdge_VertexLayoutBit = 1 << (STAGE_BIT_CNT + 3),
reed@google.comac10a2d2010-12-22 21:39:39 +0000701 // for below assert
bsalomon@google.comd302f142011-03-03 13:54:13 +0000702 kDummyVertexLayoutBit,
703 kHighVertexLayoutBit = kDummyVertexLayoutBit - 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000704 };
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000705 // make sure we haven't exceeded the number of bits in GrVertexLayout.
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000706 GR_STATIC_ASSERT(kHighVertexLayoutBit < ((uint64_t)1 << 8*sizeof(GrVertexLayout)));
reed@google.comac10a2d2010-12-22 21:39:39 +0000707
708 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000709 * There are three methods for specifying geometry (vertices and optionally
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000710 * indices) to the draw target. When indexed drawing the indices and vertices
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000711 * can use a different method. Once geometry is specified it can be used for
712 * multiple drawIndexed and drawNonIndexed calls.
713 *
714 * Sometimes it is necessary to perform a draw while upstack code has
715 * already specified geometry that it isn't finished with. There are push
716 * pop methods
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000717 *
718 * 1. Provide a cpu array (set*SourceToArray). This is useful when the
719 * caller's client has already provided vertex data in a format
720 * the time compatible with a GrVertexLayout. The array must contain the
721 * data at set*SourceToArray is called. The source stays in effect for
722 * drawIndexed & drawNonIndexed calls until set*SourceToArray is called
723 * again or one of the other two paths is chosen.
724 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725 * 2. Reserve. This is most useful when the caller has data it must
726 * transform before drawing and is not long-lived. The caller requests
727 * that the draw target make room for some amount of vertex and/or index
728 * data. The target provides ptrs to hold the vertex and/or index data.
729 *
730 * The data is writable up until the next drawIndexed, drawNonIndexed,
731 * or pushGeometrySource At this point the data is frozen and the ptrs
732 * are no longer valid.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000733 *
734 * 3. Vertex and Index Buffers. This is most useful for geometry that will
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000735 * is long-lived. SetVertexSourceToBuffer and SetIndexSourceToBuffer are
736 * used to set the buffer and subsequent drawIndexed and drawNonIndexed
737 * calls use this source until another source is set.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000738 */
739
740 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000741 * Reserves space for vertices. Draw target will use reserved vertices at
742 * at the next draw.
reed@google.comac10a2d2010-12-22 21:39:39 +0000743 *
744 * If succeeds:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000745 * if vertexCount > 0, *vertices will be the array
reed@google.comac10a2d2010-12-22 21:39:39 +0000746 * of vertices to be filled by caller. The next draw will read
747 * these vertices.
748 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000749 * If a client does not already have a vertex buffer then this is the
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000750 * preferred way to allocate vertex data. It allows the subclass of
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000751 * GrDrawTarget to decide whether to put data in buffers, to group vertex
752 * data that uses the same state (e.g. for deferred rendering), etc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754 * After the next draw or pushGeometrySource the vertices ptr is no longer
755 * valid and the geometry data cannot be further modified. The contents
756 * that were put in the reserved space can be drawn by multiple draws,
757 * however.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000758 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000759 * @param vertexLayout the format of vertices (ignored if vertexCount == 0).
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000760 * @param vertexCount the number of vertices to reserve space for. Can be 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000761 * @param vertices will point to reserved vertex space if vertexCount is
762 * non-zero. Illegal to pass NULL if vertexCount > 0.
reed@google.comac10a2d2010-12-22 21:39:39 +0000763 *
764 * @return true if succeeded in allocating space for the vertices and false
765 * if not.
766 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000767 bool reserveVertexSpace(GrVertexLayout vertexLayout,
768 int vertexCount,
769 void** vertices);
770 /**
771 * Reserves space for indices. Draw target will use the reserved indices at
772 * the next indexed draw.
773 *
774 * If succeeds:
775 * if indexCount > 0, *indices will be the array
776 * of indices to be filled by caller. The next draw will read
777 * these indices.
778 *
779 * If a client does not already have a index buffer then this is the
780 * preferred way to allocate index data. It allows the subclass of
781 * GrDrawTarget to decide whether to put data in buffers, to group index
782 * data that uses the same state (e.g. for deferred rendering), etc.
783 *
784 * After the next indexed draw or pushGeometrySource the indices ptr is no
785 * longer valid and the geometry data cannot be further modified. The
786 * contents that were put in the reserved space can be drawn by multiple
787 * draws, however.
788 *
789 * @param indexCount the number of indices to reserve space for. Can be 0.
790 * @param indices will point to reserved index space if indexCount is
791 * non-zero. Illegal to pass NULL if indexCount > 0.
792 */
793
794 bool reserveIndexSpace(int indexCount, void** indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000795 /**
796 * Provides hints to caller about the number of vertices and indices
797 * that can be allocated cheaply. This can be useful if caller is reserving
798 * space but doesn't know exactly how much geometry is needed.
799 *
800 * Also may hint whether the draw target should be flushed first. This is
801 * useful for deferred targets.
802 *
803 * @param vertexLayout layout of vertices caller would like to reserve
804 * @param vertexCount in: hint about how many vertices the caller would
805 * like to allocate.
806 * out: a hint about the number of vertices that can be
807 * allocated cheaply. Negative means no hint.
808 * Ignored if NULL.
809 * @param indexCount in: hint about how many indices the caller would
810 * like to allocate.
811 * out: a hint about the number of indices that can be
812 * allocated cheaply. Negative means no hint.
813 * Ignored if NULL.
814 *
815 * @return true if target should be flushed based on the input values.
816 */
817 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000818 int* vertexCount,
819 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000820
821 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000822 * Sets source of vertex data for the next draw. Array must contain
823 * the vertex data when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000824 *
825 * @param array cpu array containing vertex data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000826 * @param size size of the vertex data.
827 * @param vertexCount the number of vertices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000828 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000829 void setVertexSourceToArray(GrVertexLayout vertexLayout,
830 const void* vertexArray,
831 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000832
833 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000834 * Sets source of index data for the next indexed draw. Array must contain
835 * the indices when this is called.
reed@google.comac10a2d2010-12-22 21:39:39 +0000836 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000837 * @param array cpu array containing index data.
838 * @param indexCount the number of indices in the array.
reed@google.comac10a2d2010-12-22 21:39:39 +0000839 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000840 void setIndexSourceToArray(const void* indexArray, int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000841
842 /**
843 * Sets source of vertex data for the next draw. Data does not have to be
844 * in the buffer until drawIndexed or drawNonIndexed.
845 *
846 * @param buffer vertex buffer containing vertex data. Must be
847 * unlocked before draw call.
848 * @param vertexLayout layout of the vertex data in the buffer.
849 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000850 void setVertexSourceToBuffer(GrVertexLayout vertexLayout,
851 const GrVertexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000852
853 /**
854 * Sets source of index data for the next indexed draw. Data does not have
855 * to be in the buffer until drawIndexed or drawNonIndexed.
856 *
857 * @param buffer index buffer containing indices. Must be unlocked
858 * before indexed draw call.
859 */
860 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000861
862 /**
863 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
864 * source to reserved, array, or buffer before next draw. May be able to free
865 * up temporary storage allocated by setVertexSourceToArray or
866 * reserveVertexSpace.
867 */
868 void resetVertexSource();
869
870 /**
871 * Resets index source. Indexed Drawing from reset indices is illegal. Set
872 * index source to reserved, array, or buffer before next indexed draw. May
873 * be able to free up temporary storage allocated by setIndexSourceToArray
874 * or reserveIndexSpace.
875 */
876 void resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000877
878 /**
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000879 * Pushes and resets the vertex/index sources. Any reserved vertex / index
880 * data is finalized (i.e. cannot be updated after the matching pop but can
881 * be drawn from). Must be balanced by a pop.
882 */
883 void pushGeometrySource();
884
885 /**
886 * Pops the vertex / index sources from the matching push.
887 */
888 void popGeometrySource();
889
890 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000891 * Draws indexed geometry using the current state and current vertex / index
892 * sources.
893 *
894 * @param type The type of primitives to draw.
895 * @param startVertex the vertex in the vertex array/buffer corresponding
896 * to index 0
897 * @param startIndex first index to read from index src.
898 * @param vertexCount one greater than the max index.
899 * @param indexCount the number of index elements to read. The index count
900 * is effectively trimmed to the last completely
901 * specified primitive.
902 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000903 void drawIndexed(GrPrimitiveType type,
904 int startVertex,
905 int startIndex,
906 int vertexCount,
907 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000908
909 /**
910 * Draws non-indexed geometry using the current state and current vertex
911 * sources.
912 *
913 * @param type The type of primitives to draw.
914 * @param startVertex the vertex in the vertex array/buffer corresponding
915 * to index 0
916 * @param vertexCount one greater than the max index.
917 */
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000918 void drawNonIndexed(GrPrimitiveType type,
919 int startVertex,
920 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000921
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000922 /**
923 * Helper function for drawing rects. This does not use the current index
924 * and vertex sources. After returning, the vertex and index sources may
925 * have changed. They should be reestablished before the next drawIndexed
926 * or drawNonIndexed. This cannot be called between reserving and releasing
927 * geometry. The GrDrawTarget subclass may be able to perform additional
bsalomon@google.comd302f142011-03-03 13:54:13 +0000928 * optimizations if drawRect is used rather than drawIndexed or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000929 * drawNonIndexed.
930 * @param rect the rect to draw
931 * @param matrix optional matrix applied to rect (before viewMatrix)
bsalomon@google.comffca4002011-02-22 20:34:01 +0000932 * @param stageEnableBitfield bitmask indicating which stages are enabled.
933 * Bit i indicates whether stage i is enabled.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000934 * @param srcRects specifies rects for stages enabled by stageEnableMask.
935 * if stageEnableMask bit i is 1, srcRects is not NULL,
936 * and srcRects[i] is not NULL, then srcRects[i] will be
937 * used as coordinates for stage i. Otherwise, if stage i
938 * is enabled then rect is used as the coordinates.
939 * @param srcMatrices optional matrices applied to srcRects. If
940 * srcRect[i] is non-NULL and srcMatrices[i] is
941 * non-NULL then srcRect[i] will be transformed by
942 * srcMatrix[i]. srcMatrices can be NULL when no
943 * srcMatrices are desired.
944 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000945 virtual void drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000946 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000947 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000948 const GrRect* srcRects[],
949 const GrMatrix* srcMatrices[]);
950
951 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000952 * Helper for drawRect when the caller doesn't need separate src rects or
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000953 * matrices.
954 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000955 void drawSimpleRect(const GrRect& rect,
956 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000957 StageBitfield stageEnableBitfield) {
958 drawRect(rect, matrix, stageEnableBitfield, NULL, NULL);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000959 }
960
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000961 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000962 * Clear the render target. Ignores the clip and all other draw state
963 * (blend mode, stages, etc). Clears the whole thing if rect is NULL,
964 * otherwise just the rect.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000965 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000966 virtual void clear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000967
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000968 /**
969 * Returns the maximum number of edges that may be specified in a single
970 * draw call when performing edge antialiasing. This is usually limited
971 * by the number of fragment uniforms which may be uploaded. Must be a
972 * minimum of six, since a triangle's vertices each belong to two boundary
973 * edges which may be distinct.
974 */
975 virtual int getMaxEdges() const { return 6; }
976
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000977 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000978
979 class AutoStateRestore : ::GrNoncopyable {
980 public:
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000981 AutoStateRestore();
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 AutoStateRestore(GrDrawTarget* target);
983 ~AutoStateRestore();
984
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000985 /**
986 * if this object is already saving state for param target then
987 * this does nothing. Otherise, it restores previously saved state on
988 * previous target (if any) and saves current state on param target.
989 */
990 void set(GrDrawTarget* target);
991
reed@google.comac10a2d2010-12-22 21:39:39 +0000992 private:
993 GrDrawTarget* fDrawTarget;
994 SavedDrawState fDrawState;
995 };
996
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000997 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000998
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000999 class AutoViewMatrixRestore : ::GrNoncopyable {
1000 public:
1001 AutoViewMatrixRestore() {
1002 fDrawTarget = NULL;
1003 }
1004
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001005 AutoViewMatrixRestore(GrDrawTarget* target)
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +00001006 : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) {
1007 GrAssert(NULL != target);
1008 }
1009
1010 void set(GrDrawTarget* target) {
1011 GrAssert(NULL != target);
1012 if (NULL != fDrawTarget) {
1013 fDrawTarget->setViewMatrix(fMatrix);
1014 }
1015 fDrawTarget = target;
1016 fMatrix = target->getViewMatrix();
1017 }
1018
1019 ~AutoViewMatrixRestore() {
1020 if (NULL != fDrawTarget) {
1021 fDrawTarget->setViewMatrix(fMatrix);
1022 }
1023 }
1024
1025 private:
1026 GrDrawTarget* fDrawTarget;
1027 GrMatrix fMatrix;
1028 };
1029
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001030 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001031
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001032 /**
1033 * Sets the view matrix to I and preconcats all stage matrices enabled in
1034 * mask by the view inverse. Destructor undoes these changes.
1035 */
1036 class AutoDeviceCoordDraw : ::GrNoncopyable {
1037 public:
1038 AutoDeviceCoordDraw(GrDrawTarget* target, int stageMask);
1039 ~AutoDeviceCoordDraw();
1040 private:
1041 GrDrawTarget* fDrawTarget;
1042 GrMatrix fViewMatrix;
1043 GrMatrix fSamplerMatrices[kNumStages];
1044 int fStageMask;
1045 };
1046
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001047 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +00001048
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 class AutoReleaseGeometry : ::GrNoncopyable {
1050 public:
1051 AutoReleaseGeometry(GrDrawTarget* target,
1052 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001053 int vertexCount,
1054 int indexCount);
1055 AutoReleaseGeometry();
1056 ~AutoReleaseGeometry();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001057 bool set(GrDrawTarget* target,
1058 GrVertexLayout vertexLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001059 int vertexCount,
1060 int indexCount);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001061 bool succeeded() const { return NULL != fTarget; }
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001062 void* vertices() const { GrAssert(this->succeeded()); return fVertices; }
1063 void* indices() const { GrAssert(this->succeeded()); return fIndices; }
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 GrPoint* positions() const {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001065 return static_cast<GrPoint*>(this->vertices());
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 }
1067
1068 private:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001069 void reset();
1070
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 GrDrawTarget* fTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 void* fVertices;
1073 void* fIndices;
1074 };
1075
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001076 ////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +00001077
1078 class AutoClipRestore : ::GrNoncopyable {
1079 public:
1080 AutoClipRestore(GrDrawTarget* target) {
1081 fTarget = target;
1082 fClip = fTarget->getClip();
1083 }
1084
1085 ~AutoClipRestore() {
1086 fTarget->setClip(fClip);
1087 }
1088 private:
1089 GrDrawTarget* fTarget;
1090 GrClip fClip;
1091 };
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001092
1093 ////////////////////////////////////////////////////////////////////////////
1094
1095 class AutoGeometryPush : ::GrNoncopyable {
1096 public:
1097 AutoGeometryPush(GrDrawTarget* target) {
1098 GrAssert(NULL != target);
1099 fTarget = target;
1100 target->pushGeometrySource();
1101 }
1102 ~AutoGeometryPush() {
1103 fTarget->popGeometrySource();
1104 }
1105 private:
1106 GrDrawTarget* fTarget;
1107 };
reed@google.comac10a2d2010-12-22 21:39:39 +00001108
1109 ////////////////////////////////////////////////////////////////////////////
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001110 // Helpers for picking apart vertex layouts
bsalomon@google.com5782d712011-01-21 21:03:59 +00001111
reed@google.comac10a2d2010-12-22 21:39:39 +00001112 /**
1113 * Helper function to compute the size of a vertex from a vertex layout
1114 * @return size of a single vertex.
1115 */
1116 static size_t VertexSize(GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001117
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001118 /**
1119 * Helper function for determining the index of texture coordinates that
1120 * is input for a texture stage. Note that a stage may instead use positions
1121 * as texture coordinates, in which case the result of the function is
1122 * indistinguishable from the case when the stage is disabled.
1123 *
1124 * @param stage the stage to query
1125 * @param vertexLayout layout to query
1126 *
1127 * @return the texture coordinate index or -1 if the stage doesn't use
1128 * separate (non-position) texture coordinates.
1129 */
1130 static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001131
1132 /**
1133 * Helper function to compute the offset of texture coordinates in a vertex
1134 * @return offset of texture coordinates in vertex layout or -1 if the
bsalomon@google.com5782d712011-01-21 21:03:59 +00001135 * layout has no texture coordinates. Will be 0 if positions are
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001136 * used as texture coordinates for the stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001137 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001138 static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001139
1140 /**
1141 * Helper function to compute the offset of the color in a vertex
1142 * @return offset of color in vertex layout or -1 if the
1143 * layout has no color.
1144 */
1145 static int VertexColorOffset(GrVertexLayout vertexLayout);
1146
bsalomon@google.coma3108262011-10-10 14:08:47 +00001147 /**
1148 * Helper function to compute the offset of the coverage in a vertex
1149 * @return offset of coverage in vertex layout or -1 if the
1150 * layout has no coverage.
1151 */
1152 static int VertexCoverageOffset(GrVertexLayout vertexLayout);
1153
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001154 /**
1155 * Helper function to compute the offset of the edge pts in a vertex
1156 * @return offset of edge in vertex layout or -1 if the
1157 * layout has no edge.
1158 */
1159 static int VertexEdgeOffset(GrVertexLayout vertexLayout);
1160
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001162 * Helper function to determine if vertex layout contains explicit texture
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001163 * coordinates of some index.
1164 *
1165 * @param coordIndex the tex coord index to query
1166 * @param vertexLayout layout to query
1167 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001168 * @return true if vertex specifies texture coordinates for the index,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001169 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001170 */
bsalomon@google.com5782d712011-01-21 21:03:59 +00001171 static bool VertexUsesTexCoordIdx(int coordIndex,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001172 GrVertexLayout vertexLayout);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001173
reed@google.comac10a2d2010-12-22 21:39:39 +00001174 /**
1175 * Helper function to determine if vertex layout contains either explicit or
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001176 * implicit texture coordinates for a stage.
reed@google.comac10a2d2010-12-22 21:39:39 +00001177 *
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001178 * @param stage the stage to query
1179 * @param vertexLayout layout to query
1180 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001181 * @return true if vertex specifies texture coordinates for the stage,
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001182 * false otherwise.
reed@google.comac10a2d2010-12-22 21:39:39 +00001183 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001184 static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout);
reed@google.comac10a2d2010-12-22 21:39:39 +00001185
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001186 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001187 * Helper function to compute the size of each vertex and the offsets of
1188 * texture coordinates and color. Determines tex coord offsets by tex coord
1189 * index rather than by stage. (Each stage can be mapped to any t.c. index
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001190 * by StageTexCoordVertexLayoutBit.)
1191 *
1192 * @param vertexLayout the layout to query
1193 * @param texCoordOffsetsByIdx after return it is the offset of each
1194 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001195 * index isn't used. (optional)
1196 * @param colorOffset after return it is the offset of the
1197 * color field in each vertex, or -1 if
1198 * there aren't per-vertex colors. (optional)
1199 * @param coverageOffset after return it is the offset of the
1200 * coverage field in each vertex, or -1 if
1201 * there aren't per-vertex coeverages.
1202 * (optional)
1203 * @param edgeOffset after return it is the offset of the
1204 * edge eq field in each vertex, or -1 if
1205 * there aren't per-vertex edge equations.
1206 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001207 * @return size of a single vertex
1208 */
1209 static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
1210 int texCoordOffsetsByIdx[kMaxTexCoords],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001211 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001212 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001213 int* edgeOffset);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001214
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001215 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +00001216 * Helper function to compute the size of each vertex and the offsets of
1217 * texture coordinates and color. Determines tex coord offsets by stage
1218 * rather than by index. (Each stage can be mapped to any t.c. index
1219 * by StageTexCoordVertexLayoutBit.) If a stage uses positions for
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001220 * tex coords then that stage's offset will be 0 (positions are always at 0).
1221 *
1222 * @param vertexLayout the layout to query
1223 * @param texCoordOffsetsByStage after return it is the offset of each
1224 * tex coord index in the vertex or -1 if
bsalomon@google.coma3108262011-10-10 14:08:47 +00001225 * index isn't used. (optional)
1226 * @param colorOffset after return it is the offset of the
1227 * color field in each vertex, or -1 if
1228 * there aren't per-vertex colors.
1229 * (optional)
1230 * @param coverageOffset after return it is the offset of the
1231 * coverage field in each vertex, or -1 if
1232 * there aren't per-vertex coeverages.
1233 * (optional)
1234 * @param edgeOffset after return it is the offset of the
1235 * edge eq field in each vertex, or -1 if
1236 * there aren't per-vertex edge equations.
1237 * (optional)
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001238 * @return size of a single vertex
1239 */
1240 static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
1241 int texCoordOffsetsByStage[kNumStages],
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001242 int *colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001243 int *coverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001244 int* edgeOffset);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001245
1246 /**
1247 * Accessing positions, texture coords, or colors, of a vertex within an
1248 * array is a hassle involving casts and simple math. These helpers exist
1249 * to keep GrDrawTarget clients' code a bit nicer looking.
1250 */
1251
1252 /**
1253 * Gets a pointer to a GrPoint of a vertex's position or texture
1254 * coordinate.
1255 * @param vertices the vetex array
1256 * @param vertexIndex the index of the vertex in the array
1257 * @param vertexSize the size of each vertex in the array
1258 * @param offset the offset in bytes of the vertex component.
1259 * Defaults to zero (corresponding to vertex position)
1260 * @return pointer to the vertex component as a GrPoint
1261 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001262 static GrPoint* GetVertexPoint(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001263 int vertexIndex,
1264 int vertexSize,
1265 int offset = 0) {
1266 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001267 return GrTCast<GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001268 vertexIndex * vertexSize);
1269 }
1270 static const GrPoint* GetVertexPoint(const void* vertices,
1271 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001272 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001273 int offset = 0) {
1274 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001275 return GrTCast<const GrPoint*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001276 vertexIndex * vertexSize);
1277 }
1278
1279 /**
1280 * Gets a pointer to a GrColor inside a vertex within a vertex array.
1281 * @param vertices the vetex array
1282 * @param vertexIndex the index of the vertex in the array
1283 * @param vertexSize the size of each vertex in the array
1284 * @param offset the offset in bytes of the vertex color
1285 * @return pointer to the vertex component as a GrColor
1286 */
bsalomon@google.comd302f142011-03-03 13:54:13 +00001287 static GrColor* GetVertexColor(void* vertices,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001288 int vertexIndex,
1289 int vertexSize,
1290 int offset) {
1291 intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001292 return GrTCast<GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001293 vertexIndex * vertexSize);
1294 }
1295 static const GrColor* GetVertexColor(const void* vertices,
1296 int vertexIndex,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001297 int vertexSize,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001298 int offset) {
1299 const intptr_t start = GrTCast<intptr_t>(vertices);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001300 return GrTCast<const GrColor*>(start + offset +
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001301 vertexIndex * vertexSize);
1302 }
1303
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001304 static void VertexLayoutUnitTest();
1305
reed@google.comac10a2d2010-12-22 21:39:39 +00001306protected:
bsalomon@google.com471d4712011-08-23 15:45:25 +00001307
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001308 /**
1309 * Optimizations for blending / coverage to be applied based on the current
1310 * state.
1311 * Subclasses that actually draw (as opposed to those that just buffer for
1312 * playback) must implement the flags that replace the output color.
1313 */
1314 enum BlendOptFlags {
1315 /**
1316 * No optimization
1317 */
1318 kNone_BlendOpt = 0,
1319 /**
1320 * Don't draw at all
1321 */
1322 kSkipDraw_BlendOptFlag = 0x2,
1323 /**
1324 * Emit the src color, disable HW blending (replace dst with src)
1325 */
1326 kDisableBlend_BlendOptFlag = 0x4,
1327 /**
1328 * The coverage value does not have to be computed separately from
1329 * alpha, the the output color can be the modulation of the two.
1330 */
1331 kCoverageAsAlpha_BlendOptFlag = 0x1,
1332 /**
1333 * Instead of emitting a src color, emit coverage in the alpha channel
1334 * and r,g,b are "don't cares".
1335 */
1336 kEmitCoverage_BlendOptFlag = 0x10,
1337 /**
1338 * Emit transparent black instead of the src color, no need to compute
1339 * coverage.
1340 */
1341 kEmitTransBlack_BlendOptFlag = 0x8,
1342 };
1343 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags);
bsalomon@google.com471d4712011-08-23 15:45:25 +00001344
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001345 // Determines what optimizations can be applied based on the blend.
1346 // The coeffecients may have to be tweaked in order for the optimization
1347 // to work. srcCoeff and dstCoeff are optional params that receive the
1348 // tweaked coeffecients.
1349 // Normally the function looks at the current state to see if coverage
1350 // is enabled. By setting forceCoverage the caller can speculatively
1351 // determine the blend optimizations that would be used if there was
1352 // partial pixel coverage
1353 BlendOptFlags getBlendOpts(bool forceCoverage = false,
1354 GrBlendCoeff* srcCoeff = NULL,
1355 GrBlendCoeff* dstCoeff = NULL) const;
1356
1357 // determine if src alpha is guaranteed to be one for all src pixels
1358 bool srcAlphaWillBeOne() const;
bsalomon@google.com471d4712011-08-23 15:45:25 +00001359
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001360 enum GeometrySrcType {
1361 kNone_GeometrySrcType, //<! src has not been specified
1362 kReserved_GeometrySrcType, //<! src was set using reserve*Space
1363 kArray_GeometrySrcType, //<! src was set using set*SourceToArray
1364 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer
1365 };
1366
1367 struct GeometrySrcState {
1368 GeometrySrcType fVertexSrc;
1369 union {
1370 // valid if src type is buffer
1371 const GrVertexBuffer* fVertexBuffer;
1372 // valid if src type is reserved or array
1373 int fVertexCount;
1374 };
1375
1376 GeometrySrcType fIndexSrc;
1377 union {
1378 // valid if src type is buffer
1379 const GrIndexBuffer* fIndexBuffer;
1380 // valid if src type is reserved or array
1381 int fIndexCount;
1382 };
1383
1384 GrVertexLayout fVertexLayout;
1385 };
1386
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001387 // given a vertex layout and a draw state, will a stage be used?
1388 static bool StageWillBeUsed(int stage, GrVertexLayout layout,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001389 const DrState& state) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001390 return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
1391 }
1392
1393 bool isStageEnabled(int stage) const {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001394 return StageWillBeUsed(stage, this->getGeomSrc().fVertexLayout,
1395 fCurrDrawState);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001396 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001397
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001398 StageBitfield enabledStages() const {
1399 StageBitfield mask = 0;
1400 for (int s = 0; s < kNumStages; ++s) {
1401 mask |= this->isStageEnabled(s) ? 1 : 0;
1402 }
1403 return mask;
1404 }
1405
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 // Helpers for GrDrawTarget subclasses that won't have private access to
1407 // SavedDrawState but need to peek at the state values.
reed@google.com8195f672011-01-12 18:14:28 +00001408 static DrState& accessSavedDrawState(SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001409 { return sds.fState; }
reed@google.com8195f672011-01-12 18:14:28 +00001410 static const DrState& accessSavedDrawState(const SavedDrawState& sds)
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 { return sds.fState; }
1412
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001413 // implemented by subclass to allocate space for reserved geom
1414 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
1415 int vertexCount,
1416 void** vertices) = 0;
1417 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0;
1418 // implemented by subclass to handle release of reserved geom space
1419 virtual void releaseReservedVertexSpace() = 0;
1420 virtual void releaseReservedIndexSpace() = 0;
1421 // subclass must consume array contents when set
1422 virtual void onSetVertexSourceToArray(const void* vertexArray,
1423 int vertexCount) = 0;
1424 virtual void onSetIndexSourceToArray(const void* indexArray,
1425 int indexCount) = 0;
1426 // subclass is notified that geom source will be set away from an array
1427 virtual void releaseVertexArray() = 0;
1428 virtual void releaseIndexArray() = 0;
1429 // subclass overrides to be notified just before geo src state
1430 // is pushed/popped.
1431 virtual void geometrySourceWillPush() = 0;
1432 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0;
1433 // subclass called to perform drawing
1434 virtual void onDrawIndexed(GrPrimitiveType type,
1435 int startVertex,
1436 int startIndex,
1437 int vertexCount,
1438 int indexCount) = 0;
1439 virtual void onDrawNonIndexed(GrPrimitiveType type,
1440 int startVertex,
1441 int vertexCount) = 0;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001442 // subclass overrides to be notified when clip is set. Must call
1443 // INHERITED::clipwillBeSet
1444 virtual void clipWillBeSet(const GrClip& clip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001445
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001446 // Helpers for drawRect, protected so subclasses that override drawRect
1447 // can use them.
bsalomon@google.comffca4002011-02-22 20:34:01 +00001448 static GrVertexLayout GetRectVertexLayout(StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001449 const GrRect* srcRects[]);
1450
1451 static void SetRectVertices(const GrRect& rect,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001452 const GrMatrix* matrix,
1453 const GrRect* srcRects[],
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001454 const GrMatrix* srcMatrices[],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001455 GrVertexLayout layout,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001456 void* vertices);
1457
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001458 // accessor for derived classes
1459 const GeometrySrcState& getGeomSrc() const {
1460 return fGeoSrcStateStack.back();
1461 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001462
1463 GrClip fClip;
1464
reed@google.com8195f672011-01-12 18:14:28 +00001465 DrState fCurrDrawState;
reed@google.comac10a2d2010-12-22 21:39:39 +00001466
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001467 Caps fCaps;
1468
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001469private:
1470 // called when setting a new vert/idx source to unref prev vb/ib
1471 void releasePreviousVertexSource();
1472 void releasePreviousIndexSource();
1473
1474 enum {
1475 kPreallocGeoSrcStateStackCnt = 4,
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 };
bsalomon@google.com92669012011-09-27 19:10:05 +00001477 SkSTArray<kPreallocGeoSrcStateStackCnt,
1478 GeometrySrcState, true> fGeoSrcStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001479
reed@google.comac10a2d2010-12-22 21:39:39 +00001480};
1481
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001482GR_MAKE_BITFIELD_OPS(GrDrawTarget::BlendOptFlags);
1483
reed@google.comac10a2d2010-12-22 21:39:39 +00001484#endif